SerialPort DataReceived event not processing bytes read
Hi, I'm working with a GlobalSat GPS device that will report GPS messages at 4800bps every second or so. I'm getting the message strings but I cannot get to process any of them.
I'm using http://www.codeproject.com/Articles/279647/NMEA-sentence-parser-builder for parsing the messages.
A Console.WriteLine() inside my DataReceived event will show the messages, but if I try to process anything I don't get any results. I tried firing a BackGroundWorker from within the SerialPort DataReceived event so that the BGW can take all the time it needs without getting further interruptions but that didn't work either.
If I interrupt at .ReadExisting() I get a reading and then it may or may not fire the BackGroundWorker. If I interrupt inside the BackGroundWorker after having interrupted the DataReceived then it will correctly process the message. And I'm not even worrying about losing messages when I receive something... I pretty much just check for a single message each time I get an interruption.
Code:
serialPortGPS.PortName = "COM1";
serialPortGPS.BaudRate = 4800;
serialPortGPS.ReceivedBytesThreshold = 1;
serialPortGPS.NewLine = "\r\n";
serialPortGPS.DataReceived += SerialPortGPS_DataReceived;
backgroundWorkerGPS.DoWork += BackgroundWorkerGPS_DoWork;
backgroundWorkerGPS.RunWorkerCompleted += BackgroundWorkerGPS_RunWorkerCompleted;
Code:
privatevoid SerialPortGPS_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
// Waypoint arival alarm sentence (AAM), talker - GP (GPS), for example
// string stringToParse = "$GPAAM,A,A,0.10,N,WPTNME*32\r\n";
string input = serialPortGPS.ReadExisting();
if (!backgroundWorkerGPS.IsBusy) backgroundWorkerGPS.RunWorkerAsync(input);
}
privatevoid BackgroundWorkerGPS_DoWork(object sender, DoWorkEventArgs e)
{
string input = (string)e.Argument;
int som = input.IndexOf('$');
int eom = input.IndexOf("\r\n");
if (som == -1) return; // no start of message
if (eom == -1) return; // no end of message
if (eom < som) return; // buffer scrambled
string message = input.Substring(som, 2 + eom - som);
e.Result = NMEAParser.Parse(message);
}
privatevoid BackgroundWorkerGPS_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Process the NMEA Message using e.Result
}
The application is not hanging or crashing. There's no error or anything actually. I don't see any kind of action. What can I do about this? Should I avoid using DataReceived and set a Timer to .ReadExisting() every now and then...? Sounds weird.
Thanks for any help!
Re: SerialPort DataReceived event not processing bytes read
I took a look at the example code in the article and wonder why you aren't using the already defined NMEASerialPort class?
Re: SerialPort DataReceived event not processing bytes read
I had not looked into that other project. Using the NMEAParser seemed simple enough, but that GNSS Receiver class makes it even simpler. That guy is a genius! Nevertheless I still find it strange that my regular SerialPort was having problems dealing with the messages. Anyway, solved. Thanks for taking the time to look into the example!
Re: SerialPort DataReceived event not processing bytes read
Quote:
Originally Posted by
Nikel
I had not looked into that other project. Using the NMEAParser seemed simple enough, but that GNSS Receiver class makes it even simpler. That guy is a genius! Nevertheless I still find it strange that my regular SerialPort was having problems dealing with the messages. Anyway, solved. Thanks for taking the time to look into the example!
Glad to help. If you are interested in figuring out why yours didn't work, try comparing his serial port implementation with yours.