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.
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));
.....
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
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
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
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.
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.
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
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
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
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.
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