/******************************************************************************
*GPS.C is a program to test the parsing of the GPS sentence $GPRMC
* and divide it into the required feilds:
*Time ,Date, Latitude,Longitude,speed
*******************************************************************************/
#include <string.h>
#define paws system("PAUSE"); // to pause the output window in DEV-C++ program
#define MAXSIZE 100
#define COMMA 0x2C
main(){
// char read from COM port
char charRead[]="$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A";
// Buffer collects chars read from GPS
char stringRead[MAXSIZE];
// Set an array for each attribute
unsigned char tempString[MAXSIZE];
unsigned char timeString[12];
unsigned char latitudeString[12];
unsigned char latitudeCardinalString[3];
unsigned char longitudeString[12];
unsigned char longitudeCardinalString[3];
unsigned char speedString[11];
unsigned char dateString[12];
// Declare pointer
unsigned char *pChar;
// Coordinated Universal Time(GMT) & Eastern Standard time
unsigned long utcTime, estTime;
unsigned long utcHour, estHour;
unsigned long utcMinutes, estMinutes;
unsigned long utcSeconds, estSeconds;
unsigned long date;
unsigned long day,month,year;
// Sets the last comma position
unsigned char lastCommaPosition;
// Sets the Latitude & Longitude
float latitude;
int latDegrees;
float latMinutes;
float longitude;
int longDegrees;
float longMinutes;
float speed;
// dummy variable
unsigned int j, k;
// Number of chars read per GPS message string
unsigned int i;
// initializing the serial ports SCI0 & SCI1
// Enter a loop for reading char from serial port(Data Reg.)
//do {
// checks if GPS messages start with $ char
if (charRead [0] == '$'){
i = 1;
while(i<MAXSIZE){
stringRead[i] = charRead[i]; // include each char read to the array
i++;
}
/* By this point, a complete GPS string has been read so save it*/
/* Append the null terminator to the string read */
stringRead[i+1] = '\0';
/* Analyze string that we collected */
j = 0;
pChar = stringRead; // put a pointer for the stringRead array
while(*(pChar+j) != COMMA) { /* COMMA 0x2C */
tempString[j] = *(pChar+j); //store the string before the comma
j++;
}
tempString[j] = '\0'; // append the null to the end
/* Check if string we collected is the $GPRMC message */
if (tempString[3] == 'R' && tempString[4] == 'M' && tempString[5] == 'C'){
/* Found GPRMC string. It has 11 commas total. Its NMEA sentence structure is:
$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A
Where:
RMC Recommended Minimum sentence C
123519 Fix taken at 12:35:19 UTC
A Status A=active or V=Void.
4807.038,N Latitude 48 deg 07.038' N
01131.000,E Longitude 11 deg 31.000' E
022.4 Speed over the ground in knots
084.4 Track angle in degrees True
230394 Date - 23rd of March 1994
003.1,W Magnetic Variation
*6A The checksum data, always begins with *
*/
pChar = stringRead; // pointer to the String read
// (1) Get UTC time
j = 7; /* start of time field */
k = 0;
while(*(pChar+j) != COMMA) { //loop until a comma is found
timeString[k] = *(pChar+j); //save in the time string
j++;
k++;
}
lastCommaPosition = j; // save the position of the last comma
timeString[k] = '\0'; // append the null to the end
sscanf(timeString, "%ld", &utcTime); //save the string to the variable
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 >= 0 && utcHour <= 20)
estHour = utcHour + 3;
else
estHour = utcHour - 21;
estMinutes = utcMinutes;
estSeconds = utcSeconds;
// (2) Empty Loop to avoid taking 'A' char
pChar = stringRead;
j = lastCommaPosition + 1; //start from the next comma
k = 0;
while(*(pChar+j) != COMMA) {
j++;
k++;
}
lastCommaPosition = j;
// (3) Get lattitude
pChar = stringRead;
j = lastCommaPosition +1; //start from the next comma
k = 0;
while(*(pChar+j) != COMMA) {
latitudeString[k] = *(pChar+j); //save in the latitude string
j++;
k++;
}
lastCommaPosition = j;
latitudeString[k] = '\0';
sscanf(latitudeString, "%f", &latitude);
latDegrees = (int)(latitude/100);
latMinutes = (float)(latitude - latDegrees*100);
// (4) 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';
// (5) Get longitude
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);
// (6) Get longitude Cardinal direction
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
longitudeCardinalString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
longitudeCardinalString[k] = '\0';
// (7) Get Speed in Knots
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
speedString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
speedString[k] = '\0';
sscanf(speedString, "%f", &speed);
speed = speed * 1.852; //convert the speed in Knots to Km/hour
// (8) Empty Loop to avoid taking the Track angle
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
j++;
k++;
}
lastCommaPosition = j;
// (9) Get date
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
dateString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
dateString[k] = '\0';
sscanf(dateString, "%ld", &date);
day = (date/10000); //extract day from long
month = (date - (day*10000))/100; // extract month from long
year = date - (day*10000) - (month*100); // extract year from long
//else not a GPRMC sentence
}
// otherwise not a $ character... so loop back until one arrives
}
// } while(1);
printf(" \t GPRMC Sentence Test\n");
printf(" **********************************************************\n" );
printf(" **********************************************************\n\n" );
printf(" Time : %02ld:%02ld:%02ld UTC = %02ld:%02ld:%02ld EST \n\n", utcHour, utcMinutes, utcSeconds, estHour, estMinutes, estSeconds);
printf(" Date : %02ld:%02ld:%02ld \n\n", day, month, year);
printf(" ----------------------------------------------------------\n" );
printf(" Latitude :\t %02d DEG %2.4f MIN ", latDegrees, latMinutes);
printf(" %s \n\n", latitudeCardinalString);
printf(" Longitude :\t %03d DEG %2.4f MIN ", longDegrees, longMinutes);
printf(" %s \n\n", longitudeCardinalString);
printf(" Speed : %f Km/hour \n\n", speed );
printf(" **********************************************************\n\n" );
paws //to end the pause function declared above
} /* end of main */