Aventuras y Desventuras de un hobbyist

Aventuras y Desventuras de un hobbyist....

Parsing NMEA sentences in C


I am starting a new project involving a GPS with Bluetooth capabilities, so far I have been able to get the information from the GPS module but it comes as NMEA sentences containing the precious data.

So before doing something fancy on the computer it is needed to decode the information

There are 19 interpreted sentences:
   $GPBOD - Bearing, origin to destination
   $GPBWC - Bearing and distance to waypoint, great circle
   $GPGGA - Global Positioning System Fix Data
   $GPGLL - Geographic position, latitude / longitude
   $GPGSA - GPS DOP and active satellites 
   $GPGSV - GPS Satellites in view
   $GPHDT - Heading, True
   $GPR00 - List of waypoints in currently active route
   $GPRMA - Recommended minimum specific Loran-C data
   $GPRMB - Recommended minimum navigation info
   $GPRMC - Recommended minimum specific GPS/Transit data
   $GPRTE - Routes
   $GPTRF - Transit Fix Data
   $GPSTN - Multiple Data ID
   $GPVBW - Dual Ground / Water Speed
   $GPVTG - Track made good and ground speed
   $GPWPL - Waypoint location
   $GPXTE - Cross-track error, Measured
   $GPZDA - Date & Time

The data I am getting comes only in 5 sentences:
   
   $GPGGA - Global Positioning System Fix Data
   $GPGSA - GPS DOP and active satellites 
   $GPGSV - GPS Satellites in view
   $GPRMC - Recommended minimum specific GPS/Transit data
   $GPVTG - Track made good and ground speed
  
From these 5 I am only interested in $GPRMC The lines of data are
something similar to these two:

eg1. $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
eg2. $GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68


           225446       Time of fix 22:54:46 UTC
           A            Navigation receiver warning A = OK, V = warning
           4916.45,N    Latitude 49 deg. 16.45 min North
           12311.12,W   Longitude 123 deg. 11.12 min West
           000.5        Speed over ground, Knots
           054.7        Course Made Good, True
           191194       Date of fix  19 November 1994
           020.3,E      Magnetic variation 20.3 deg East
           *68          mandatory checksum
So to decode  we need to understand that where a numeric latitude or longitude is given, the two digits immediately to the left of the decimal point are whole minutes, to the right are decimals of minutes, and the remaining digits to the left of the whole minutes are whole degrees.
      eg. 4533.35 is 45 degrees and 33.35 minutes. ".35" of a minute is exactly 21 seconds.
      eg. 16708.033 is 167 degrees and 8.033 minutes. ".033" of a minute is about 2 seconds

So I wrote a small program that outputs a json file, although I have it divided into 3 files I am posting it as a whole unit:


Once we have this file we can use it in many webapps to display the location and some information about the points, the file will look like:
There are many smart formats and visualizers to display this info such as geoJson, tileJson,
 GPX (a standard format used with many devices and programs, including Garmin's eTrex, GPSMAP, Oregon, Dakota, Colorado, & Nüvi series), Google Earth (.kml/.kmz), Google Maps routes (URLs), Geocaching.com (.loc), FAI/IGC glider logs, Microsoft Excel, Google Spreadsheets,XML feeds, Garmin Forerunner (.xml/.hst/.tcx), Timex Trainer, OziExplorer, Cetus GPS, PathAway, cotoGPS, CompeGPS, TomTom (.pgl), IGN Rando (.rdn), Emtac Trine, Suunto X9/X9i (.sdf), Fugawi, NetStumbler, and of course tab-delimited or comma-separated text.


Finally  you end up with a nice map similar the one below: