pynmea2 NMEA 0183 Protocol Parser

1.19.0 · active · verified Thu Apr 16

pynmea2 is a Python library for parsing and creating NMEA 0183 sentences, commonly used in GPS and other GNSS receivers. It provides a flexible API for working with various NMEA sentence types, handling checksums, and accessing data fields. The current version is 1.19.0, with a release cadence of several updates per year, primarily for bug fixes and new sentence type support.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse an existing NMEA 0183 GGA sentence and how to construct a new GGA sentence from scratch. It includes basic error handling for common parsing issues.

import pynmea2
import datetime

# Example 1: Parsing an NMEA sentence
raw_gga = '$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D'
try:
    msg = pynmea2.parse(raw_gga)
    print(f"Parsed sentence type: {msg.sentence_type}")
    print(f"Timestamp: {msg.timestamp}")
    print(f"Latitude: {msg.latitude}, Longitude: {msg.longitude}")
except pynmea2.ChecksumError as e:
    print(f"Checksum error: {e}")
except pynmea2.ParseError as e:
    print(f"Parse error: {e}")

# Example 2: Creating an NMEA sentence
# GGA fields: (timestamp, lat, lat_dir, lon, lon_dir, fix_quality, num_sats, hdop, alt, alt_units, geoid_sep, geoid_units, age_of_diff, diff_ref_id)
# Note: lat/lon can be floats, others mostly strings/ints
timestamp_val = datetime.time(12, 0, 0)
lat_val = '3404.70417'
lat_dir_val = 'N'
lon_val = '11808.66539'
lon_dir_val = 'W'

try:
    new_gga = pynmea2.GGA('GP', 'GGA', (
        timestamp_val, lat_val, lat_dir_val, lon_val, lon_dir_val,
        1, 8, 0.9, 500.0, 'M', 25.0, 'M', '', ''
    ))
    print(f"\nCreated NMEA sentence: {str(new_gga)}")
except ValueError as e:
    print(f"Error creating GGA sentence: {e}")

view raw JSON →