The NMEA protocol
Practically every GPS receiver speaks the same language: short lines of readable characters separated by commas, emitted several times a second. The standard comes from the marine industry of the early 1980s and has outlived every fashion since, because twenty lines of code are enough to parse it.
Structure of a sentence
- Total length is limited to 82 characters including the dollar sign and the line ending.
- Fields have a fixed position but no fixed length. Count commas, not characters.
- A field may be empty. Two consecutive commas are normal and mean "no value available".
- Commas are separators only, which is why NMEA writes decimals with a point.
Who is speaking: the identifier
The first two letters name the source, the last three the sentence type. While GPS was the only system the prefix was always GP, and many older programs still look for exactly that.
| Prefix | Source |
|---|---|
| GP | GPS |
| GL | GLONASS |
| GA | Galileo |
| GB or BD | BeiDou |
| GN | combined solution from several systems |
| P | proprietary, for example PGRM or PUBX |
A receiver using GPS and Galileo together sends its position as $GNGGA and the satellite lists separately as $GPGSV and $GAGSV. Filtering on GP alone then yields no position at all. This is by far the most common reason why an old program stops working with a new receiver.
The important sentence types
| Type | Content | Use |
|---|---|---|
| GGA | time, position, fix quality, satellites used, HDOP, altitude | the standard position sentence |
| RMC | time, date, position, speed, course, magnetic variation | the only one carrying the date |
| GSA | 2D/3D mode, satellites used, PDOP, HDOP, VDOP | which satellites entered the solution |
| GSV | satellites in view with elevation, azimuth, signal strength | basis of any sky plot |
| VTG | course and speed over ground | motion data separately |
| GNS | like GGA, with per system information | successor to GGA for multi constellation |
A sample GGA sentence, with the fields in order:
$GPGGA,141236.00,4808.3160,N,01134.3980,E,1,09,0.9,519.7,M,47.3,M,,*60
│ │ │ │ │ │ │ │ │ │ │ └ geoid separation
│ │ │ │ │ │ │ │ │ └ altitude above the geoid
│ │ │ │ │ │ │ └ HDOP
│ │ │ │ │ │ └ satellites used
│ │ │ │ │ └ fix quality: 1 = GPS, 2 = differential, 4 = RTK fixed
│ │ │ │ └ hemisphere
│ │ │ └ longitude, three digits of degrees
│ │ └ hemisphere
│ └ latitude, two digits of degrees
└ UTC time
The coordinate format, the usual trap
NMEA writes coordinates neither in decimal degrees nor in degrees, minutes and seconds, but in degrees and decimal minutes with no separator between them:
Read that value as decimal degrees and you end up in the Indian Ocean, because 4808.3160 is not a valid latitude. The subtler error is to take the digits before the point as degrees and the rest as a fraction of a degree, which puts the position up to 60 kilometres away and looks plausible at first glance.
The checksum
After the asterisk stands a two digit hexadecimal number: the bitwise XOR of every character between the dollar sign and the asterisk, both excluded. That is all, which is why it can be verified in three lines in any language. It catches single corrupted characters reliably but not transposed ones, since XOR ignores order.
Pitfalls when parsing
- The altitude in GGA is height above the geoid, not above the ellipsoid. The following field gives the geoid separation used; adding both yields ellipsoidal height. See map datum.
- GGA carries no date. For a complete timestamp, parse RMC or ZDA as well.
- The time is UTC, not local time and not GPS time; the receiver has already removed the leap seconds.
- An empty position field is not zero. Before the first fix a receiver already emits sentences with empty position fields. Reading them as numbers puts you at zero degrees by zero degrees, in the Gulf of Guinea.
- The satellite count in GGA is the number used, not the number visible. Visible satellites are listed in GSV and are almost always more.
- Proprietary sentences are common. A parser should skip unknown sentences rather than abort.
The quality field in GGA is the quickest indication of what kind of solution you have: 0 invalid, 1 ordinary GPS, 2 differential including SBAS, 4 RTK fixed, 5 RTK float, 6 dead reckoning with no satellite reception at all. Logging without watching for a 6 records computed rather than measured positions.