Code:
#include <stdio.h>
#include <stdlib.h>
gpsinfile = fopen("gpsData.txt", "r");
gpsoutfile = fopen("gpsParsedData.txt", "w");
numLinesRead = 0;
printf("Entering while loop...\n");
do {
charRead = gpsinfile(); /* read char from text file */
if(charRead == '$') { /* GPS messages start with $ char */
i = 0;
numLinesRead++;
stringRead[i] = charRead;
do {
charRead = com_rx();
if( (charRead != '\0') && (isalnum(charRead) || isspace(charRead) || ispunct(charRead)) ) {
i++;
stringRead[i] = charRead;
}
} while(charRead != CR);
/* By this point, a complete GPS string has been read so save it to file */
/* Append the null terminator to the string read */
stringRead[i+1] = '\0';
/* Analyze string that we collected */
j = 0;
pChar = stringRead;
while(*(pChar+j) != COMMA) {
tempString[j] = *(pChar+j);
j++;
}
tempString[j] = '\0';
/* Check if string we collected is the $GPGGA message */
if(tempString[3] == 'G' && tempString[4] == 'G' && tempString[5] == 'A') {
/*
Found GPGGA string. It has 14 commas total. Its NMEA sentence structure is:
$GPGAA,hhmmss.ss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,g.g,z,t.t,iii*CC
| | | | | | | | | | | | | | |
0 1 2 3 4 5 6 7
0123456789012345678901234567890123456789012345678901234567890123456789012
where:
GPGAA : GPS fixed data identifier
hhmmss.ss : Coordinated Universal Time (UTC), also known as GMT
ddmm.mmmm,n : Latitude in degrees, minutes and cardinal sign
dddmm.mmmm,e : Longitude in degrees, minutes and cardinal sign
q : Quality of fix. 1 = there is a fix
ss : Number of satellites being used
y.y : Horizontal dilution of precision
a.a,M : GPS antenna altitude in meters
g.g,M : geoidal separation in meters
t.t : Age of the defferential correction data
iiii : Deferential station's ID
*CC : checksum for the sentence
*/
pChar = stringRead;
/* Get UTC time */
j = 7; /* start of time field */
k = 0;
while(*(pChar+j) != COMMA) {
timeString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
timeString[k] = '\0';
sscanf(timeString, "%ld", &utcTime);
utcHour = (utcTime/10000); /* extract Hours from long */
utcMinutes = (utcTime - (utcHour*10000))/100; /* extract minutes from long */
utcSeconds = utcTime - (utcHour*10000) - (utcMinutes*100); /* extract seconds from long */
if(utcHour >= 4 && utcHour <= 23) estHour = utcHour - 4;
else estHour = utcHour + 20;
estMinutes = utcMinutes;
estSeconds = utcSeconds;
/* NB: %02ld formats long to print 2 chars wide, padding with 0 if necessary */
fprintf(gpsoutfile,"%02ld:%02ld:%02ld UTC = %02ld:%02ld:%02ld EST", utcHour, utcMinutes, utcSeconds, estHour, estMinutes, estSeconds);
/* Get lattitude: ddmm.mmmm */
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
latitudeString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
latitudeString[k] = '\0';
sscanf(latitudeString, "%f", &latitude);
latDegrees = (int)(latitude/100);
latMinutes = (float)(latitude - latDegrees*100);
fprintf(gpsoutfile,"\t%02d DEG\t%2.4f MIN", latDegrees, latMinutes);
/* Get lattitude Cardinal direction */
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
latitudeCardinalString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
latitudeCardinalString[k] = '\0';
fprintf(gpsoutfile," %s", latitudeCardinalString);
/* Get longitude: dddmm.mmmm */
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
longitudeString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
longitudeString[k] = '\0';
sscanf(longitudeString, "%f", &longitude);
longDegrees = (int)(longitude/100);
longMinutes = (float)(longitude - longDegrees*100);
fprintf(gpsoutfile,"\t%03d DEG\t%2.4f MIN", longDegrees, longMinutes);
fprintf(gpsoutfile,"\n");
} /* else not a GPGGA sentence */
printf("%d: (%d) %s\n", numLinesRead, i, stringRead);
} /* otherwise not a $ character... so loop back until one arrives */
} while(!kbhit());
printf("Exiting...");
fclose(gpsinfile);
fclose(gpsoutfile);
printf("done\n");
return (0);
} /* end of main */