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

Serial port exception


  • Please log in to reply
13 replies to this topic

#1 Maurice Spronkers

Maurice Spronkers

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationRozenburg (ZH), The Netherlands

Posted 10 November 2010 - 01:45 PM

I'am trying to readout a gps data stream.

It's working in hyper terminal
$GPRMC,131856,V,5154.2571,N,00415.4122,E,,,101110,001.0,W*7B
$GPGGA,131856,5154.2571,N,00415.4122,E,0,00,,,M,,M,,*54
$GPRMC,131857,V,5154.2571,N,00415.4122,E,,,101110,001.0,W*7A
$GPGGA,131857,5154.2571,N,00415.4122,E,0,00,,,M,,M,,*55

But when I read it in netduino i get an exception: An unhandled exception of type 'System.Exception' occurred in mscorlib.dll
In line System.Text.UTF8Encoding.UTF8.GetChars(buffer);
buffer contains (91,125,109,149,229,157,235,150,150,118,86,54,118,155,117,....)

Used the gps examples from this forum.
public static void Main()
        {
            //The serial port connection                          
            SerialPort serialPort = new SerialPort("COM1", 4800, Parity.None, 8, StopBits.One);
            serialPort.Open();

            if (serialPort.IsOpen)
            {
                serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPortDataReceived);
            }

            while (true)
            {
            }
         }

private static void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (e.EventType == SerialData.Chars)
            {
                SerialPort serialPort = (SerialPort)sender;
                int bytesToRead = serialPort.BytesToRead;

                //start reading the stream
                if (bytesToRead > 0)
                {
                    //get the waiting data
                    byte[] buffer = new byte[bytesToRead];
                    serialPort.Read(buffer, 0, buffer.Length);

                    //copy the byte array to a readable string
                    String str = new String(System.Text.UTF8Encoding.UTF8.GetChars(buffer));

                .....

Can't find what I'am doing wrong.


Kind regards
Maurice Spronkers
The Netherlands

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 10 November 2010 - 02:35 PM

Hi Maurice, Welcome to the Netduino community! UTF8 encoding should read all standard ASCII characters alright, but some sequences of "special characters" make it think that it needs to go into "international" mode--which is what is possibly happening here. Could you please post the actual byte array that it's having troubles with--so we can dig a big further? BTW, Chris Seto has been working on a really nice GPS parser if you'd like something you can pick up and run with... Chris

#3 Maurice Spronkers

Maurice Spronkers

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationRozenburg (ZH), The Netherlands

Posted 10 November 2010 - 07:06 PM

Thnx for the welcome, I have also tried the other project with the same problem. Byte array is 91,125,109,149,229,157,157,143,149,147,159,157,167,83,167,149,157,149,151,163,155,149,145,157,167,99,167,159,159,151,157,149,163,151,157,155,155,167,117,167,167,167,157,159,157 Should start with $GP = 36,71,80 I think there is a problem with the baudrate, but set it the same as on hyper terminal. Maurice

#4 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 10 November 2010 - 08:33 PM

Hi Maurice, NMEA parsing is actually quite tricky to do. Like CW said, I have my own tried and tested library that will work right off the bat. See: http://files.chrisseto.com/Aia

#5 Maurice Spronkers

Maurice Spronkers

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationRozenburg (ZH), The Netherlands

Posted 10 November 2010 - 10:17 PM

Hi Chris,

Same problem with your code. Same byte array in response array.

I have connected the Garmin GPS18LVC to 5V and GND. Output directly to pin RX.
Do I need an Max232 or something? Like a serial shield? http://www.cutedigi....roducts_id=4329

Connecting it directly to my computer comport works.


You have made an excellent class! Liked the fix part of it.

Maurice

#6 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 10 November 2010 - 11:14 PM

Hmm, well, it might be that the serial is RS232, but at TTL level. IF it works connected directly to your computers comport, it is probably RS232. So the answer is yes, you do need translation circuitry, like that Serial Shield.

#7 Maurice Spronkers

Maurice Spronkers

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationRozenburg (ZH), The Netherlands

Posted 14 November 2010 - 06:49 PM

Hi All, It works with the serial shield. Made my own. Thanks for helping me. Maurice Spronkers

#8 phantomtypist

phantomtypist

    Advanced Member

  • Members
  • PipPipPip
  • 142 posts
  • LocationNew York, NY

Posted 14 November 2010 - 08:59 PM

Did you guys look into just taking NMEA code out of the GPS.NET project on codeplex? There is a whole folder of classes dedicated to NMEA.


GPS.NET 3.0

#9 ikorsa

ikorsa

    New Member

  • Members
  • Pip
  • 4 posts

Posted 21 February 2011 - 10:53 PM

I use the same code to read from Serial port, but connection is not reliable. I use 10HZ update rate GPS with 115200 baud rate. serialPort.bytesToRead show less then 72 (but look like 24+8+.. =72) , the $GPRMC length = 72. some time 2-4 sentence is ok. It mix together normal $GPRMC sentence and sentence break on a parts. I have know idea why, when I connect GPS + serial to USB -> computer all ok!even on 230400 baudrate GPS send only $GPRMC each 100 ms.(nothing else) The code is the same as above ... Not enough speed for processor ? But in my project I have to hookup 20 HZ GPS ?! Thanks Igor

#10 ikorsa

ikorsa

    New Member

  • Members
  • Pip
  • 4 posts

Posted 21 February 2011 - 11:05 PM

sorry forgot to mention firmware 4.1.1 ALPHA7 This is the output data GPS update rate 10Hz (100ms) $GPRMC,223840.400,V,0000.0000,N,00000.0000,E,000.0,000.0,210211,,,N*7C $GPRMC,223840.500,V,0000.0000, N,00000.000 0,E,000.0, 000.0,210211 ,,,N*7D $GPRMC,223840.600,V,0000.0000,N,00000.0000,E,000.0,000.0,210211,,,N*7E $GPRMC,223840.700,V,0000 .0000,N,000 00.0000,E, 000.0,000.0 ,210211,,,N*7 F $GPRMC,223840.800,V,0000.0000,N,00000.0000,E,000.0,000.0,210211,,,N*70 $GPRMC,223840.900,V,0000.0000,N ,00000.0000 ,E,000.0,0 00.0,210211, ,,N*71 $GPRMC,223841.000,V,0000.0000,N,00000.0000,E,000.0,000.0,210211,,,N*79 $GPRMC,223841.100,V,0000 .0000,N,000 00.0000,E, 000.0,000.0, 210211,,,N*78 $GPRMC,223841.200,V,0000.0000,N,00000.0000,E,000.0,000.0,210211,,,N*7B $GPRMC,223841.300,V,0000.0000,N ,00000.0000 ,E,000.0,00 0.0,210211, ,,N*7A $GPRMC,223841.400,V,0000.0000,N,00000.0000,E,000.0,000.0,210211,,,N*7D $GPRMC,223841.500,V,0000 .0000,N,00 000.0000,E, 000.0,000.0, 210211,,,N*7C $GPRMC,223841.600,V,0000.0000,N,00000.0000,E,000.0,000.0,210211,,,N*7F $GPRMC,223841.700,V,0000.0000 ,N,00000.000 0,E,000.0, 000.0,210211 ,,,N*7E $GPRMC,223841.800,V,0000.0000,N,00000.0000,E,000.0,000.0,210211,,,N*71 $GPRMC,223841.900,V,0000. 0000,N,0000 0.0000,E,0 00.0,000.0,2 10211,,,N*70

#11 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 25 February 2011 - 12:47 AM

Hi ikorsa, I don't totally understand the problem you're seeing. Is there data being corrupted? Are you getting the data in chunks and trying to figure out how to wait until the whole stream comes in? If you can simplify the problem a bit we'll be happy to help... Chris

#12 ikorsa

ikorsa

    New Member

  • Members
  • Pip
  • 4 posts

Posted 25 February 2011 - 11:02 PM

Hi Chris, I probably solved the problen to put Thread.Sleep(90) in EventHandle procedure. But problem is 1. I read int bytesToRead = serialPort.BytesToRead; in my post it will NOT 72 for hole RMC line It look like - bytesToRead = 25 and I got this $GPRMC,223841.900,V,0000. bytesToRead = 11 and I got 0000,N,0000 bytesToRead = 10 and 0.0000,E,0 .... Some time bytesToRead = 72 and I got hole correct line $GPRMC...... When I'm using the Hyperterminal - I got correctly all lines I put Thread.Sleep(90), it help - it reduce "small" lines , but it's happend anyway. My plan to use the 20 HZ GPS (I'm waiting for hardware), but I'm not sure... I read some your post that .NET is not for real time programming, but this platform it really reduce a lot of time! My project is simple datalogger : 20 HZ GPS UART (230400) , 2 channel temperature SPI reading ( 2 cyllinders), Timer for RPM and 3 axis accelerometer analog or I2C ( I need to try both) micro-SD SPI and AT45DB361D SPI for Flash and Xbee (UART) for communication. I started on ARM7 (LPC2478) with Keil C... too much head ache for me it is not programming it look more like "know how". I switch to Arduino - more simple , but most Library that exist - very pure written - I bought Netduino now, and very happy with other my project with GPRS module and send and recieve SMS with .Net it so simple and "nicely" to program it! Great! I hope tha I can able to finish it before may with Netduino. Thanks Igor

#13 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 25 February 2011 - 11:40 PM

I probably solved the problen to put Thread.Sleep(90) in EventHandle procedure.
But problem is
1. I read int bytesToRead = serialPort.BytesToRead; in my post it will NOT 72 for hole RMC line
It look like
- bytesToRead = 25 and I got this $GPRMC,223841.900,V,0000.
bytesToRead = 11 and I got 0000,N,0000
bytesToRead = 10 and 0.0000,E,0
....
Some time bytesToRead = 72 and I got hole correct line $GPRMC......


Hi Igor,

I think what you are wanting is for the program to wait for all 72 bytes. Here's one way to do that in .NET:

int totalLength = 72;
byte[] buffer = new byte[total];

int currentIndex = 0;
while (currentIndex < total)
{
    // get data from the serial port and put it into the buffer at currentIndex; increment currentIndex by the number of bytes read.
    currentIndex += serialPort.Read(buffer, currentIndex, totalLength - currentIndex);
}

// now you'll have exactly "totalLength" bytes; in this case, 72 bytes

This is just meant as a starting point. Your final solution (reading in multiple sentence types) would be a bit more thorough. Also just FYI...there is a GPS library for .NET MF that takes care of all the parsing for you. Ask Chris Seto for more details if you're interested.

Chris

#14 ikorsa

ikorsa

    New Member

  • Members
  • Pip
  • 4 posts

Posted 26 February 2011 - 12:23 AM

Thanks Chris, 1. I'll try this way and let you know. 2. I'm still wondering why it's happend. May be somebody else has a 10 HZ GPS and Netduino to try ? 3. Parsing the NMEA is not big deal, but I do not need it in Netduino( may be I need to setup RTC ), I need just recieve it and put to the flash and SD. Detail parsing I 'll do on PC. Anyway to use 20 HZ GPS I'll switch to binary mode, but for debuging it easy to use NMEA... Igor




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.