[AVR] splitting a text string in AVR GCC for atmega 32 microcontroller.

Status
Not open for further replies.

ark5230

Advanced Member level 3
Joined
Jun 29, 2009
Messages
862
Helped
163
Reputation
324
Reaction score
140
Trophy points
1,323
Location
India
Visit site
Activity points
6,187
I am trying to implement a GPS module with AVR Atmega32.
I am finding difficulty in splitting the text read from GPS module, namely the $GPGGA line.
I need to split segments of this line that are separated by commas and use the latitude, longitude and altitude for further processing.

Any help in few line code to split this line will be very useful.

A Typical line is like this:

$GPGGA,124545.000,1954.3693,N,07520.6932,E,1,05,3.7,606.4,M,-64.3,M,,0000*7E

I am poor in C and C++ and also in AVR GCC.
 

Not tested code which I wrote.


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
char gsm_data[] = "$GPGGA,124545.000,1954.3693,N,07520.6932,E,1,05,3. 7,606.4,M,-
 
64.3,M,,0000*7E\r\n"
 
#define _LATITUDE  0
#define _LONGITUDE 1
 
char latitude[15], longitude[15], NS_EW[];
 
void Get_Latitude_Or_Longitude(char *s1, char *s2, char *s3, char option) {
 
    char i = 0;
 
    while(*s1) {
        if(*s1 == ',') {
            ++i;
        }
 
        if(option == 0) {
            if(i == 2) {
                *s++;
                break;
 
            }
        }
 
        if(option == 1) {
            if(i == 4) {
                *s++;
                break;
 
            }
        }
 
        *s++;
    }
 
    while(*s != ',') {
        *s2++ = *s1++;      
    }
 
    *s1++;
 
    *s3 = *s1;
}
 
 
 
//usage
 
 
while(1) {
 
    Get_Latitude_Or_Longitude(gps_data, latitude, NS_EW, _LATITUDE);
    //print latitude[] and NS_EW[]
    Get_Latitude_Or_Longitude(gps_data, longitude, NS_EW, _LONGITUDE);
    //print longitude[] and NS_EW[]
 
}

 
Thank you so much for complete and perfect solution.
I was struggling for days dur to lack of knowledge.
 

Fixed some bugs.


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
char gsm_data[] = "$GPGGA,124545.000,1954.3693,N,07520.6932,E,1,05,3. 7,606.4,M,-
 
64.3,M,,0000*7E\r\n"
 
#define _LATITUDE  0
#define _LONGITUDE 1
#define _ALTITUDE  2
 
char latitude[15], longitude[15], NS_EW[2];
 
void Get_Latitude_Or_Longitude(char *s1, char *s2, char *s3, char option) {
 
    char i = 0;
 
    while(*s1) {
        if(*s1 == ',') {
            ++i;
        }
 
        if(option == 0) {
            if(i == 2) {
                *s++;
                break;
 
            }
        }
 
        if(option == 1) {
            if(i == 4) {
                *s++;
                break;
 
            }
        }
 
        *s++;
    }
 
    while(*s != ',') {
        *s2++ = *s1++;      
    }
 
    *s1++;
    *s2 = '\0';
 
    *s3++ = *s1;
    *s3 = '\0';
 
}

 
Thanks agin,
I will try it on GPS, The microcontroller will send this info by SMS.
 

Altitude extraction is not written. Mention which field of the GPGGA sentence is altitude.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…