Trouble parsing NMEA data from Serial Port
I'm retrieving NMEA sentences from a serial GPS. Then string are coming across like I would expect. The problem is that when parsing a sentence like this:
$GPRMC,040302.663,A,3939.7,N,10506.6,W,0.27,358.86,200804,,*1A
I use a simple bit of code to make sure I have the right sentect:
string[] Words = sBuffer.Split(',');
foreach (string item in Words)
{
if (item == "$GPRMC")
{
return "Correct Sentence";
}
else
{
return "Incorrect Sentence
}
}
I added the return in that location for the example. I have printed the split results to a text box and have seen that $GPRMC is indeed coming across in the item variable at some point. If the string is coming across why won't t开发者_如何学运维he if statement catch? Is is the $? How can I trouble shoot this?
It has been a while since I read an NMEA GPS...
Don't you need to compare the substring corresponding to the NMEA data type rather than the entire NMEA buffer elements? The .Split method splits sBuffer on all the commas in the NMEA sentence so that you have each individual element. But then you are testing the substring against the first element in a loop that implies that you want to look at every element. Confusing...
So wouldn't your test seem better as:
string[] Words=sBuffer.Split(',');
if(String.Compare(Words[0],"$GPRMC")==0)
{
return "Correct Sentence";
}
else
{
return "Incorrect Sentence
}
Is there a possibility that the NMEA stream is outputting sentences other than the Min Data, GPRMC sentence and you need to reread until you have the correct sentence? Also, are you sure that your GPS has the datatype as $GPRMC
rather than GPRMC
? I do not think there is supposed to be a $
in the datatype.
ie, in pseudo:
do {
buffer=read_NMEA(); //making sure the entire sentence is read...
array=split(buffer,",");
data_type=buffer[0];
}
while(data_type!="GPRMC" || readcount++<=MAX_NMEA_READS)
To debug your loop, try a console write of the elements:
string[] Words = sBuffer.Split(',');
foreach (string item in Words)
{
Console.WriteLine(item);
}
Are you calculating the checksum, I don't see it.
NMEA Wiki
EDIT: My answer underneath is no improvement, as commentator mtrw stated, the ==
is overloaded by the string class. I was wrong.
To my mind your if
-Statement is faulty. Using the ==
operator, you are checking if it is the same reference (which certainly will not be the case). To simply compare if the two strings contain the same value, use String.Equals()
.
精彩评论