Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

GPS NMEA data processing using C function

Status
Not open for further replies.

Amalinda

Banned
Full Member level 1
Joined
Sep 25, 2012
Messages
104
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Activity points
0
Can someone please help me extract the GPS NMEA data from the raw data sent from the GPS device to a variable? I do not know the relationship between the sent raw data and the actual Latitudes and Longitudes. (the convertion process) thanks
 

Parsing the GPS string is easy. If you need to get only latitude and longitude, maybe you could parse the string "$GPRMC", then all the details are separated by commas. Jut find the commas, and get the lat/long.
 
thank you so much errakeshpillai. I found out that converting the raw NMEA GPS data is as difficult as extracting them. Do you have a C function to extract the NMEA data stings values to variables?

It would be so much appreciated.
 

thank you so much errakeshpillai. I found out that converting the raw NMEA GPS data is as difficult as extracting them. Do you have a C function to extract the NMEA data stings values to variables?

It would be so much appreciated.
Can you provide us with sample of raw NMEA GPS data?
 
here is a set of data I collected.


$GPGGA,025526.000,0653.409140,N,07954.150015,E,1,7,1.08,22.013,M,-96.608,M,,*4D
$GPGLL,0653.409140,N,07954.150015,E,025526.000,A,A*58
$GPGSA,A,3,21,29,18,24,22,25,15,,,,,,1.98,1.08,1.67*0F
$GPGSV,4,1,14,21,58,020,36,14,56,246,25,29,38,173,36,18,32,354,36*7E
$GPGSV,4,2,14,24,30,071,49,22,25,317,34,06,19,304,20,25,11,157,33*7E
$GPGSV,4,3,14,15,06,035,32,03,04,310,,31,03,205,,30,00,240,*78
$GPGSV,4,4,14,42,,,39,50,,,39*7F
$GPRMC,025526.000,A,0653.409140,N,07954.150015,E,0.000,115.0,260313,,,A*6F
$GPVTG,115.0,T,,M,0.000,N,0.000,K,A*08
$GPZDA,025526.000,26,03,2013,,*57

I hope its enough.I jusrt want a general C library or a general C function that can extract the data within these commas. Its hard to extract the data within as the number of digits is not constant for each line.
i hope im clear.
Thank you for all your responses.
 

You can also use the Function like
make a function for reading the GPS if you are using LCD as display you should print it on the LCD and if you are using Serial Port then print those in PC via Hyperterminal or "Tera Term"

void read_gps( void )
{
value= ugetchar();
if(value=='$')
{
value= ugetchar() ;
if(value=='G')
{
value= ugetchar() ;
if(value=='P')
{
value= ugetchar() ; //Value in UART Receiver
if(value=='R')
{
value= ugetchar();
if(value=='M')
{
value= ugetchar() ;
if(value=='C')
{
value= ugetchar() ;
if(value==',')
{
value= ugetchar() ;
while(value!=',')
{
value= ugetchar() ;
}
lati_value[0]= ugetchar() ;
value=lati_value[0];
for(i=1;value!='.';i++)
{
lati_value= ugetchar() ;
value=lati_value;
}
lati_dir= ugetchar() ;
value= ugetchar() ;
while(value!=',')
{
value= ugetchar() ;
}
longi_value[0]= ugetchar() ;
value=longi_value[0];
for(i=1;value!=',';i++)
{
longi_value= ugetchar() ;
value=longi_value;
}
longi_dir= ugetchar() ;
i=0;
while(lati_value!='\0')
{
USART_Transmit(lati_value[j]); //Prints to Serial
j++;
}
USART_Transmit(lati_dir);
//LCD_cmd(0xC0);
//_delay_ms(1000);
i=0;
while(longi_value!='\0')
{
USART_Transmit(longi_value);
i++;
}
USART_Transmit(longi_dir);
_delay_ms(100);

}
}
}
}
}
}
}
}
 

Well that is different Scenario in 908
i.e You can get a particular String in 908 using AT Command
but if You are using that GPS Modem then your First attention should be on Analysis of a Single NMEA Sentences like $GPGGA or $GPRMC etc
 

When data is received dump it into a char array. Now parse the array and check if char encountered is '$'. If yes, then read next five characters and see if they match to 'G', 'P' 'R', 'M', 'C'. If not, then search for next '$' and then repeat the process. Once 'G', 'P' 'R', M'M', 'C' is found then check for 4 ','. After 4 ',' are passed the data which is read till 5th ',' is received is one value you need. Then read the 6th ',' and then the value you read till 7th ',' is another value you need.

96139d1379098107-nmea.png


96141d1379101772-parse.png


96143d1379110833-nmea.png



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Program:    Parse String
Author:     Jayanth Devarayanadurga
Date:       14/09/2013
File Name:  nmea.c
*/
 
#pragma hdrstop
#pragma argsused
 
#include <stdio.h>
#include <tchar.h>
#include <string.h>
 
/*
 
$GPGGA,025526.000,0653.409140,N,07954.150015,E,1,7 ,1.08,22.013,M,-96.608,M,,*4D
$GPGLL,0653.409140,N,07954.150015,E,025526.000,A,A *58
$GPGSA,A,3,21,29,18,24,22,25,15,,,,,,1.98,1.08,1.6 7*0F
$GPGSV,4,1,14,21,58,020,36,14,56,246,25,29,38,173, 36,18,32,354,36*7E
$GPGSV,4,2,14,24,30,071,49,22,25,317,34,06,19,304, 20,25,11,157,33*7E
$GPGSV,4,3,14,15,06,035,32,03,04,310,,31,03,205,,3 0,00,240,*78
$GPGSV,4,4,14,42,,,39,50,,,39*7F
$GPRMC,025526.000,A,0653.409140,N,07954.150015,E,0 .000,115.0,260313,,,A*6F
$GPVTG,115.0,T,,M,0.000,N,0.000,K,A*08
$GPZDA,025526.000,26,03,2013,,*57
 
*/
 
unsigned char gpsData[] = {'$','G','P','G','G','A',',','0','2','5','5','2','6','.','0','0','0',',','0','6','5','3','.','4','0','9','1','4','0',',','N',',','0','7','9','5','4','.','1','5','0','0','1','5',',','E',',','1',',','7',' ',',','1','.','0','8',',','2','2','.','0','1','3',',','M',',','-','9','6','.','6','0','8',',','M',',',',','*','4','D',
'$','G','P','G','L','L',',','0','6','5','3','.','4','0','9','1','4','0',',','N',',','0','7','9','5','4','.','1','5','0','0','1','5',',','E',',','0','2','5','5','2','6','.','0','0','0',',','A',',','A',' ','*','5','8',
'$','G','P','G','S','A',',','A',',','3',',','2','1',',','2','9',',','1','8',',','2','4',',','2','2',',','2','5',',','1','5',',',',',',',',',',',',','1','.','9','8',',','1','.','0','8',',','1','.','6',' ','7','*','0','F',
'$','G','P','G','S','V',',','4',',','1',',','1','4',',','2','1',',','5','8',',','0','2','0',',','3','6',',','1','4',',','5','6',',','2','4','6',',','2','5',',','2','9',',','3','8',',','1','7','3',',',' ','3','6',',','1','8',',','3','2',',','3','5','4',',','3','6','*','7','E',
'$','G','P','G','S','V',',','4',',','2',',','1','4',',','2','4',',','3','0',',','0','7','1',',','4','9',',','2','2',',','2','5',',','3','1','7',',','3','4',',','0','6',',','1','9',',','3','0','4',',',' ','2','0',',','2','5',',','1','1',',','1','5','7',',','3','3','*','7','E',
'$','G','P','G','S','V',',','4',',','3',',','1','4',',','1','5',',','0','6',',','0','3','5',',','3','2',',','0','3',',','0','4',',','3','1','0',',',',','3','1',',','0','3',',','2','0','5',',',',','3',' ','0',',','0','0',',','2','4','0',',','*','7','8',
'$','G','P','G','S','V',',','4',',','4',',','1','4',',','4','2',',',',',',','3','9',',','5','0',',',',',',','3','9','*','7','F',
'$','G','P','R','M','C','0','2','5','5','2','6','.','0','0','0',',','A',',','0','6','5','3','.','4','0','9','1','4','0',',','N',',','0','7','9','5','4','.','1','5','0','0','1','5',',','E',',','0',' ','.','0','0','0',',','1','1','5','.','0',',','2','6','0','3','1','3',',',',',',','A','*','6','F',
'$','G','P','V','T','G',',','1','1','5','.','0',',','T',',',',','M',',','0','.','0','0','0',',','N',',','0','.','0','0','0',',','K',',','A','*','0','8',
'$','G','P','Z','D','A',',','0','2','5','5','2','6','.','0','0','0',',','2','6',',','0','3',',','2','0','1','3',',',',','*','5','7','\0'};
 
unsigned char latitude[15], longitude[15], input;
unsigned int i, j, k;
 
int _tmain(int argc, _TCHAR* argv[])
{
 
start:   printf("gpsDATA = %s\n\n", gpsData);
         input = '\0';
 
         printf("Parse String? Y/N: ");
         scanf("%s", &input);
         if((input == 'Y') || (input == 'y'))
                ;
         else
            goto exit;
 
         input = '\0';
 
         i = 0;
         j = 0;
         k = 0;
 
next:    while(gpsData[i] != '$'){
                 i++;
         }
 
         if((gpsData[i+1] == 'G') && (gpsData[i+2] == 'P') && (gpsData[i+3] == 'R') && (gpsData[i+4] == 'M') && (gpsData[i+5] == 'C')){
                        while(gpsData[i++] != 'A');
                        i++;
                        while(gpsData[i] != ',')
                                latitude[k++] = gpsData[i++];
                        i += 3;
                        latitude[k] = '\0';
                        printf("Latitude = %s\n", latitude);
 
                        k = 0;
 
                        while(gpsData[i] != ',')
                                longitude[k++] = gpsData[i++];
                        longitude[k] = '\0';
                        printf("Longitude = %s\n\n", longitude);
 
         }
         else{
              i++;
              goto next;
         }
 
         printf("Parse again? Y/N: ");
         scanf("%s", &input);
         if((input == 'Y') || (input == 'y')){
                goto start;
         }
         else
exit:        return 0;
 
}

 

Attachments

  • nmea.rar
    3.6 KB · Views: 193
  • nmea embedded.rar
    15.1 KB · Views: 209
  • nmea rev1.rar
    3.9 KB · Views: 177
  • nmea codeblocks.rar
    24.3 KB · Views: 194
  • nmea mikroC PRO PIC.rar
    41.8 KB · Views: 262
  • nmea.png
    nmea.png
    59.5 KB · Views: 361
  • nmea rev2.rar
    36 KB · Views: 190
  • parse.png
    parse.png
    62.5 KB · Views: 356
  • nmea rev a.rar
    16.4 KB · Views: 172
  • nmea.png
    nmea.png
    51.9 KB · Views: 290
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top