Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

System.Exception during Debug.Print

debug

Best Answer NooM, 12 March 2013 - 12:25 PM

i guess its: Debug.Print(new string(System.Text.Encoding.UTF8.GetChars(buffer)));

 

when you get the chars, it expects that the byte data is correct, now with serial the chances

are big that it isent (my experience so far)

 

if you send the data from a computer, print out there the byte data, and also on the netduino ( Debug.Print(buffer[0]); .. in a loop till buffer lenght) and compare them. it happened a lot for the data gets shifted and never syncronizes again

 

iam using cobs as packetframe, since that it never happened again

Go to the full post


  • Please log in to reply
5 replies to this topic

#1 JamesBrown

JamesBrown

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationNear San Diego

Posted 11 March 2013 - 05:49 PM

I am attempting to read a serial stream from a GPS shield attached to my Netduino plus 2.  The code is stright from a post in this forum.  I am getting an exception from the Debug.Print statement: 

 

  "A first chance exception of type 'System.Exception' occurred in mscorlib.dll"

 

When I turn on the serial input to this code. I don't know where to go with this.  How do I handle this exception.

What I would like to do is simple examine the buffer of data received.

using System;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;using System.IO.Ports;namespace GpsLogger{    public class Program    {        public static void Main()        {            // write your code here            SerialPort serialPort = new SerialPort("COM2", 4800, Parity.None, 8, StopBits.One);            serialPort.Open();            while (true)            {                int bytesToRead = serialPort.BytesToRead;                if (bytesToRead > 0)                {                    byte[] buffer = new byte[bytesToRead];                    serialPort.Read(buffer, 0, buffer.Length);                    Debug.Print(new string(System.Text.Encoding.UTF8.GetChars(buffer)));                }                Thread.Sleep(100);//wait a bit to get a few bytes at a time            }        }    }}

Thanks.. Jim

 



#2 NeverCast

NeverCast

    New Member

  • Members
  • Pip
  • 6 posts

Posted 11 March 2013 - 07:40 PM

What is the stack trace of the exception?



#3 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 12 March 2013 - 12:25 PM   Best Answer

i guess its: Debug.Print(new string(System.Text.Encoding.UTF8.GetChars(buffer)));

 

when you get the chars, it expects that the byte data is correct, now with serial the chances

are big that it isent (my experience so far)

 

if you send the data from a computer, print out there the byte data, and also on the netduino ( Debug.Print(buffer[0]); .. in a loop till buffer lenght) and compare them. it happened a lot for the data gets shifted and never syncronizes again

 

iam using cobs as packetframe, since that it never happened again



#4 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 12 March 2013 - 02:43 PM

Yes, surely what NooM said; I get that all the time too, when there is junk on the line (like when the serial device powers up or down), or if the port is configured incorrectly.

 

I would add this:

 

*  when faced with a problem like this, you can wrap the failing call in a try, and catch the exception.  the exception object has some useful info sometimes

 

catch (Exception e){   Debug.Print("exc in Blahblah: " + e);} 

 

this isn't real error handling, but you can get some info.  even setting a breakpoint there will allow you to look into 'e' and see it's members

*  when faced with a problem like this, I break the compound statement into distinct lines, so I can see which call failed.  the GetChars?  the new string?  The Debug.Print?  (its surely the GetChars in this case, but you see what I mean about breaking the compund statement up to find the particular statement that is throwing).

 

*  when debugging the serial protocol, as NooM also points out, it's useful to dump the raw binary.  Heres a little thing I find handy:

 

public static string HexStr(byte[] p, int nIdxStart, int nCount){  char[] c = new char[nCount * 2];  byte b;  for (int y = 0, x = 0; y < nCount; ++y, ++x)    {    int nIdx = nIdxStart + y;    b = ((byte)(p[nIdx] >> 4));    c[x] = (char)(b > 9 ? b + 0x37 : b + 0x30);    b = ((byte)(p[nIdx] & 0xF));    c[++x] = (char)(b > 9 ? b + 0x37 : b + 0x30);  }return new string(c);} 

 

Good Luck!



#5 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 12 March 2013 - 02:54 PM

btw, in my signature is a link to my playground, and there is the COBS code.

i really like it, cos it automatically resyncs. or aqt least makes it possible.

i removed the serial implementation for now since i had a bug with it on my board, so iam not

sure about netduino. but its very simple.

you encode your data, make a new byte array with 2 bytes bigger of the encoded data, add a 0 at pos 0 and the lenght of the encoded data on pos1, than in receive funktion look for the 0, when you receive one, the packet starts, than read the lenght, you know that how big the packet is and read the ammount of bytes, than decode it, violla



#6 JamesBrown

JamesBrown

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationNear San Diego

Posted 12 March 2013 - 06:04 PM

Yes - You guys were right.  I'm sending non-char data so that was causing the exception...







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.