views:

372

answers:

3

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 the if statement catch? Is is the $? How can I trouble shoot this?

+2  A: 

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);
        }
drewk
A: 

Are you calculating the checksum, I don't see it.

NMEA Wiki

dbasnett
A: 

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

rdoubleui
The .NET string class overloads == to call `Equals`. See http://www.yoda.arachsys.com/csharp/strings.html
mtrw
Okay, I was assuming, didn't check it. Thanks mtrw! Then drewk's answer is just fine. Should be marked answered. Edited the post as well.
rdoubleui