Embedded C GPS Project

Andrew Bolster

Senior R&D Manager (Data Science) at Synopsys Software Integrity Group and Treasurer @ Bsides Belfast and NI OpenGovernment Network

Afternoon folks, I’m supposed to be studying but dont have the heart to, so I’m documenting a recent project from Uni.

The remit was to be able to parse RS232 data coming in from a GPS unit and reformat it for a LCD display. I dont have the part numbers handy but I was programming on a 18F series PIC that supported C.

Most of the ancillary code is more platform dependant, such as working with the PIC interrupts etc, so for the purposes of this code snippit, assumme that a NMEA sentence (I used RMC and some RMB, but never really finished that bit) stored as a character buffer, and a structure, as defined, to store relevent data in.

PLEASE read up about NMEA sentence structure before continuing

typedef    struct message  
       int day, month, year;  
       int hour, min, sec;  
       int lat_deg;  
       float lat_min;  
       char lat_ref;  
       int lng_deg;  
       float lng_min;  
       char lng_ref;  

Since NMEA sentences are comma separated values, I kinda cheated and iterated thru the string, replacing the commas with terminating characters and recording the next positions as character pointers.

 if(end)buffer[i]=0;        //wipe the rest of the sentence        
 if(buffer[i]==42&&check==1){        //asterix  
  if(checksum==chr2hex(&buffer[i+1])) valid=1;  
  else valid=0;  
  i+=2;          //get to the end of the checksum  
  end=1;          //this is the end of the current sentence  
 if(buffer[i]==36){        //dollarsign  
  AddToList(&(buffer[i+1]));     //ignore first character  
  buffer[i]=0;     //replace all commas with nulls  
  AddToList(&(buffer[i+1]));   //add next position to list of words  

The first character(dollar sign) is ignored because it is never needed beyond this point.

The NMEA sentence structure includes a asterix delimited checksum, i.e everything after the dollarsign and before the asterix is progressivly XOR’d and the hex value representation of this result is concatenated on the end of the sentence before transmission.

the chr2 hex function simply converts two ASCII characters to their Hex value equivalent.

AddToList, strangly enough, adds the pointer passed to it to a wordlist, which is an array of character pointers.

Now we have a list of pointers, because i used a character pointer array, and ended all the strings with nulls, we essentially now have individual strings for each part of the NMEA sentence, that can be addressed directly. eg:

{        //GPRMC  

In this instance, the get functions all take two arguments, what to read from, and where to put it.

For anyone whos interested the full code is here

I guess i better do some work then.

Please at least pretend to click my ads. I know they’re a joke, but still, it dont cost ya anything!