pynmeagps
pynmeagps is an original Python 3 parser aimed primarily at the subset of the NMEA 0183 © v4 protocol relevant to GNSS/GPS receivers. It provides functionalities for both parsing incoming NMEA messages from various streams (e.g., serial, socket, file) and generating outbound NMEA messages. The library is actively maintained, currently at version 1.1.2, with regular updates and bug fixes.
Common errors
-
NMEAParseError: Unknown msgID GNACN, msgmode GET
cause Attempting to parse an NMEA message with an unknown or unsupported message ID while `VALMSGID` validation is active.fixEither disable `VALMSGID` validation (e.g., `validate=VALCKSUM`) to parse it to a nominal structure, or provide a `userdefined` payload definition dictionary if it's a proprietary message. -
AttributeError: 'NMEAReader' object has no attribute 'iterate'
cause Using the `iterate()` method on an `NMEAReader` instance, which was removed.fixReplace calls to `NMEAReader.iterate()` with direct iteration over the `NMEAReader` object itself (e.g., `for raw, parsed in nmr: ...`). -
Incorrect latitude values (e.g., negative) in parsed GLL sentences from Unicore UM9* firmware.
cause A known firmware error in certain Unicore UM9* devices generates malformed NMEA GLL sentences with incorrect negative latitude values.fixVersion 1.1.2 includes a workaround for this specific Unicore firmware error. Update to `pynmeagps` version 1.1.2 or later.
Warnings
- breaking The `NMEAReader.iterate()` method was removed in Release 1.0.26.
- deprecated The command-line utility `nmeadump` has been removed and replaced by `gnssdump`.
- gotcha Python 3.8 support was officially dropped as of Release 1.0.43.
- gotcha Parsing unknown or proprietary NMEA message types might raise `NMEAParseError` if strict validation (`VALMSGID`) is enabled.
Install
-
pip install pynmeagps -
conda install -c conda-forge pynmeagps
Imports
- NMEAReader
from pynmeagps import NMEAReader
- NMEAMessage
from pynmeagps import NMEAMessage
- NMEA_TALKERS
from pynmeagps.nmeatypes_core import NMEA_TALKERS
- VALCKSUM
from pynmeagps import VALCKSUM, VALMSGID, ERR_LOG
Quickstart
from pynmeagps import NMEAReader, NMEAMessage, VALCKSUM, VALMSGID
# Example 1: Parsing an NMEA message string
nmea_string = b'$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47\r\n'
try:
# Using NMEAReader.parse() for individual messages
parsed_message = NMEAReader.parse(nmea_string, validate=VALCKSUM | VALMSGID)
print(f"Parsed GGA message: {parsed_message}")
print(f" Talker: {parsed_message.talker}")
print(f" Message ID: {parsed_message.msgID}")
print(f" Latitude: {parsed_message.lat}° {parsed_message.lat_dir}")
print(f" Longitude: {parsed_message.lon}° {parsed_message.lon_dir}")
except Exception as e:
print(f"Error parsing message: {e}")
# Example 2: Creating an NMEA message (e.g., GLL)
# For a real application, ensure correct talker, msgID, and payload values
# This example uses nominal values for illustration
from datetime import datetime, time
try:
msg = NMEAMessage(
talker='GP',
msgID='GLL',
msgmode=0,
lat=48.12345,
lat_dir='N',
lon=11.67890,
lon_dir='E',
time=time(10, 30, 0),
status='A',
posMode='A'
)
# The to_nmea() method returns the checksummed NMEA message as bytes
generated_nmea = msg.to_nmea()
print(f"Generated GLL message: {generated_nmea.decode('ascii').strip()}")
except Exception as e:
print(f"Error generating message: {e}")