Hot!Decode GPS NMEA frame format.

Author
Akshayy
Super Member
  • Total Posts : 351
  • Reward points : 0
  • Joined: 2016/07/29 06:35:35
  • Location: 0
  • Status: offline
2018/04/13 04:34:56 (permalink)
0

Decode GPS NMEA frame format.

Hi I am using $GPRMC frame format in my application.I have configured GPS such as it gives packet every second.
I have to decode packet format
ex. $GPRMC,105353.000,A,1830.5581,N,07346.8776,E,0.66,146.50,130418,,,A*6D
 
I can use string operations as given below
 
int main()
{
 char str[] = "$GPRMC,105353.000,A,1830.5581,N,07346.8776,E,0.66,146.50,130418,,,A*6D";

 // Returns first token
 char *token = strtok(str, ",");
 
  printf("%s\n", token);

 // Keep printing tokens while one of the
 // delimiters present in str[].
 while (token != NULL)
 {
  printf("%s\n", token);
  token = strtok(NULL, ",");
 }

 return 0;
}

 
How can I decode it efficiently as string operations are not generally recommended.Any other way?
 
 

Regards,
Akshay.
#1

11 Replies Related Threads

    qɥb
    Monolothic Member
    • Total Posts : 2604
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: online
    Re: Decode GPS NMEA frame format. 2018/04/13 04:38:52 (permalink)
    0
    If it works, just leave it as is.
    Don't try to over optimise working code.
     
    So long as you don't add code doing lots of string copy operations, what you've posted so far isn't too bad.
     
     
    post edited by qɥb - 2018/04/13 04:40:11

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #2
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Decode GPS NMEA frame format. 2018/04/13 05:04:14 (permalink)
    0
    Anyways,I am likely to use strcpy instead of prinfsad: sad

    Regards,
    Akshay.
    #3
    rodims
    Super Member
    • Total Posts : 1292
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: Decode GPS NMEA frame format. 2018/04/13 06:02:50 (permalink)
    0
    qhbIf it works, just leave it as is.
    Don't try to over optimise working code.

     
    You can use string functions like strtok and strcpy, there usually is not much sense to code them yourself (which also might make things worse ...)
    printf, sprintf is rather different (depending on used system/CPU). But here you are using it only for quick debugging, so I assume it is not part of any final code.
    Otherwise I would not use sprintf, if e.g. strcpy, strcat can do the same for you.
    post edited by rodims - 2018/04/13 06:45:27
    #4
    Alpha Whisky
    Senior Member
    • Total Posts : 39
    • Reward points : 0
    • Joined: 2014/10/02 07:05:22
    • Location: 0
    • Status: offline
    Re: Decode GPS NMEA frame format. 2018/04/13 06:24:11 (permalink)
    0
    Another caveat. strtok is not re-entrant, thread safe, whatever you want to call it. You can't use it in multiple tasks at the same time if using an RTOS without some sort of mutex, nor can you use it in both an ISR and regular code without jumping through a lot of hoops.
     
    That being said, I would consider doing that level of processing in an ISR to be bad practice.
    #5
    Barbiani
    Super Member
    • Total Posts : 227
    • Reward points : 0
    • Status: offline
    Re: Decode GPS NMEA frame format. 2018/04/13 07:23:52 (permalink)
    0
    google for nmea single header library.
    #6
    maxruben
    Super Member
    • Total Posts : 3225
    • Reward points : 0
    • Joined: 2011/02/22 03:35:11
    • Location: Sweden
    • Status: offline
    Re: Decode GPS NMEA frame format. 2018/04/13 09:20:06 (permalink)
    4.5 (2)
    You can pretty easily build an array of pointers to char by changing the ',' to 0 and set the pointer for the next element to the following char. If that would help.
     
    /Ruben
    #7
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Decode GPS NMEA frame format. 2018/04/16 03:39:18 (permalink)
    0
    maxruben
    You can pretty easily build an array of pointers to char by changing the ',' to 0 and set the pointer for the next element to the following char. If that would help.
     
    /Ruben


    This seems working.
     
    I have posted above algorith that doest work if I have string like
     char str[]="$GPRMC,,,,N,07346.8776,E,0.66,146.50,130418,,,A*6D";
    if string has adjacent multiple commas


    Regards,
    Akshay.
    #8
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Decode GPS NMEA frame format. 2018/04/16 03:40:48 (permalink)
    0
    strtok doesnt work if it has adjacent tokens.

    Regards,
    Akshay.
    #9
    rodims
    Super Member
    • Total Posts : 1292
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: Decode GPS NMEA frame format. 2018/04/16 04:41:25 (permalink)
    0
    There are no adjacent tokens in your string. Instead you have adjacent delimiters in your NMEA string.
    You can not detect these "empty fields" in an NMEA string with the typical simple strtok loop. 
    #10
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Decode GPS NMEA frame format. 2018/04/16 04:50:04 (permalink)
    0
    rodims
    There are no adjacent tokens in your string. Instead you have adjacent delimiters in your NMEA string.
    You can not detect these "empty fields" in an NMEA string with the typical simple strtok loop. 


    Yes Agree.I have implemented as per #7. and it worked.

    Regards,
    Akshay.
    #11
    Akshayy
    Super Member
    • Total Posts : 351
    • Reward points : 0
    • Joined: 2016/07/29 06:35:35
    • Location: 0
    • Status: offline
    Re: Decode GPS NMEA frame format. 2018/04/16 23:43:45 (permalink)
    0
    Hi I have successfully parsed NMEA frames.
     
    I converted co-ordinates into minutes and speed into know.I have stored these string into structures.
     
     typedef struct
    {
    char Packetheader,framenumber[10,],latitude[10],longitude[10],speed[10],no of satellite;
     
    }
     
    I have to send data over TCP.For that I have to convert this structure into array each field separated by comma.

    Regards,
    Akshay.
    #12
    Jump to:
    © 2018 APG vNext Commercial Version 4.5