[plug] C help (only slightly Linux)
Tom Atkinson
tom at tyco.net.au
Mon Jul 13 20:26:42 WST 1998
Hi Kevin,
I have written a number of programs in the past that decode NMEA GPS data.
I am giving you a little function called "skip_comma()".
Here's how to use it. Assuming the char array "gps_str" contains the
following:
"$GPRMC,051809,A,2451.22,S,11343.69,E,001.6,284.7,030798,,*07"
Lets assume you want to store the 2nd field (051809) in the char array
called str1, the next field in the char c1, the 3rd field in the double
d1, and the 5th in the double d2. Here's the code to do it:
{
char str1[16];
char c1;
double d1, d2;
char *cp;
cp = gps_str;
cp = skip_comma(cp, 1); /* skip 1 comma; point to 2nd field */
strncpy(str1, cp, 6); /* copy 6 chars */
str1[6] = 0; /* delimit the string */
cp = skip_comma(cp, 1);
c1 = *cp;
cp = skip_comma(cp, 1);
d1 = atof(cp);
cp = skip_comma(cp, 2);
d2 = atof(cp);
}
There is no error or sanity checking in the above code; I will leave that
to you to put in! Good luck and don't give up too easily.
Tom Atkinson
On Mon, 13 Jul 1998, Shackleton, Kevin wrote:
> $GPXTE,A,A,,,N*3C
> $GPBWC,051808,,,,,,T,,M,,N,*12
> $GPRMC,051809,A,2451.22,S,11343.69,E,001.6,284.7,030798,,*07
> $GPRMB,A,,,,,,,,,,,,V*71
> $GPR00,,,,,,,,,,,,,,*45
> $GPGLL,2451.22,S,11343.69,E*7F
> $PGRMZ,-499,f,2*33
>
> the credit), or can someone recommend a C book that deals with string
> handling - the couple I've got practically ignore this area.
char *skip_comma(char *cp, int n)
{
while (n)
{
while (*cp != ',' && *cp)
cp++;
n--;
if (*cp == 0)
break;
cp++;
while (*cp && *cp == ' ')
cp++;
}
return cp;
}
More information about the plug
mailing list