pynmeagps

1.1.2 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse an NMEA message from a string using `NMEAReader.parse()` and how to construct a new NMEA message using the `NMEAMessage` class. It includes basic error handling and illustrates accessing parsed attributes and generating checksummed NMEA output. Note that NMEA messages should generally be byte strings (e.g., `b'$GPGGA...'`).

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}")

view raw JSON →