Search code examples
c++nmea

Parsing log files containing NMEA sentences C++


I have multiple log files of NMEA sentences that contain geographical positions captured by a camera. Example of one of the sentences: $GPRMC,100101.000,A,3723.1741,N,00559.5624,W,0.000,0.00,150914,,A*63

My question is, how do you reckon I can start on that? Just need someone to push me to the right direction, thanks.


Solution

  • I use this checksum function in my GPSReverse driver.

    string chk(const char* data)
        {
        // Assuming data contains a NMEA sentence (check it)
        // Variables for keeping track of data index and checksum 
        const char *datapointer = &data[1];
        char checksum = 0;
        // Loop through entire string, XORing each character to the next 
        while (*datapointer != '\0')
            {
            checksum ^= *datapointer;
            datapointer++;
            }
        // Print out the checksum in ASCII hex nybbles 
        char x[100] = {0};
        sprintf_s(x,100,"%02X",checksum);
        return x;
        }
    

    And after that, some append to the NMEA string (say, GGA) :

    string re = chk(gga.c_str());
    gga += "*";
    gga += re;
    gga += "\r\n";
    

    So you can read up to the *, calculate the checksum, and see if it matches the string after the *.

    Read more here.

    Each sentence begins with a '$' and ends with a carriage return/line feed sequence and can be no longer than 80 characters of visible text (plus the line terminators). The data is contained within this single line with data items separated by commas. The data itself is just ascii text and may extend over multiple sentences in certain specialized instances but is normally fully contained in one variable length sentence. The data may vary in the amount of precision contained in the message. For example time might be indicated to decimal parts of a second or location may be show with 3 or even 4 digits after the decimal point. Programs that read the data should only use the commas to determine the field boundaries and not depend on column positions. There is a provision for a checksum at the end of each sentence which may or may not be checked by the unit that reads the data. The checksum field consists of a '' and two hex digits representing an 8 bit exclusive OR of all characters between, but not including, the '$' and ''. A checksum is required on some sentences.