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!