I'm writing a code to parse GPS data, and I've successfully written a checksum routine and now I'm looking at all $GPGGA sentences.
A standard GPGGA sentence looks like this:
$GPGGA,181600.000,4043.2087,N,07436.3990,W,1,04,4.3,225.7,M,-34.1,M,,0000*61
You can find more info about what each part is here: http://aprs.gids.nl/nmea/#gga
This is the code I have written to try to extract the relevant data:
if (!strncmp(sentence,"$GPGGA,", 7))
{
GPAflag = 1;
int index = 7;
dataextract(index, sentence, time);
dataextract(index, sentence, latitude);
dataextract(index, sentence, signlatitude);
dataextract(index, sentence, longitude);
// Serial.print("longitude has been recorded as: ");
// Serial.println(longitude);
dataextract(index, sentence, signlongitude);
// Serial.print("longitude has been recorded as: ");
// Serial.println(longitude);
dataextract(index, sentence, fix_validity);
// Serial.print("fix_validity has been recorded as: ");
// Serial.println(fix_validity);
dataextract(index, sentence, numberofsatellites);
Serial.print("numberofsatellites has been recorded as: ");
Serial.println(numberofsatellites);
dataextract(index, sentence, HDOP);
Serial.print("numberofsatellites has been recorded as: ");
Serial.println(numberofsatellites);
dataextract(index, sentence, altitude);
Serial.print("numberofsatellites has been recorded as: ");
Serial.println(numberofsatellites);
}
if (GPAflag)
{
GSAflag = 0;
GPAflag = 0;
Serial.print("$PRHAL,");
Serial.print(fix_validity);
Serial.print(",");
Serial.print(latitude);
Serial.print(",");
Serial.print(signlatitude);
Serial.print(",");
Serial.print(longitude);
Serial.print(",");
Serial.print(signlongitude);
Serial.print(",");
Serial.print(numberofsatellites);
Serial.print(",");
Serial.print(altitude);
Serial.println("*");
}
And the data_extract function:
char *dataextract(int &index, char * GPSdata, char data[])
{
int j = 0;
while (GPSdata[index] != ',')
{
data[j] = GPSdata[index];
index++;
j++;
}
index++;
return data;
}
So you'll notice I'm printing the char array numberofsatellites several times, here is the result of that:
numberofsatellites has been recorded as: 04
numberofsatellites has been recorded as: 044.0
numberofsatellites has been recorded as: 044.0314.5
$PRHAL,1,4043.1907,N,07436.3918W,W,044.0314.5,314.5*
$GPGGA,180511.000,4043.1907,N,07436.3918,W,1,04,4.0,314.5,M,-34.1,M,,0F
numberofsatellites has been recorded as: 044.0314.5
So basically, it gets recorded correctly, but then more data is recorded! What's even weirder, at least for a novice programmer like me, is that sometimes a value gets recorded properly and sometimes it doesn't. Here's the current status:
Gets recorded properly: time latitude signlatitude signlongitude fix_validity altitude
Get recorded, but wit extras: longitude numberofsatellites and probably HDOP as well
Why would data continue to be recorded in these arrays after the dataextract function has already returned the value? And how can I fix it?