pynmea2 Documentation

repository·master·Indexed 20 days ago

https://github.com/knio/pynmea2

A Python library for parsing and generating NMEA 0183 protocol sentences used in GPS and marine electronics. It provides functionality to convert NMEA strings into NMEASentence objects, validate checksums, and access geographic coordinates as decimal degrees.

Tokens
1.2K
Snippets
7
Records
7
Agent score
22%

What's inside pynmea2

  1. Access geographic coordinates as decimal degrees

    master

    While NMEA sentences use the DDDMM.MMMM format, pynmea2 provides helper properties on NMEASentence objects to access coordinates as Python floats (Decimal Degrees).

    Supported helpers:

    • latitude / longitude: Returns decimal degrees as floats.
    • latitude_minutes / longitude_minutes: Returns the minutes component.
    • latitude_seconds / longitude_seconds: Returns the seconds component.
    # Example of using coordinate helpers
    msg = pynmea2.parse("$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D")
    print(msg.latitude)   # -19.4840833333
    print(msg.longitude)  # 24.1751
  2. Read NMEA sentences from a pySerial device

    master

    When reading from a serial device, wrap the serial object in io.TextIOWrapper to handle line-based reading. Handle serial.SerialException for hardware errors and pynmea2.ParseError for malformed NMEA data.

    import io
    import pynmea2
    import serial
    
    ser = serial.Serial('/dev/ttyS1', 9600, timeout=5.0)
    sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
    
    while 1:
        try:
            line = sio.readline()
            msg = pynmea2.parse(line)
            print(repr(msg))
        except serial.SerialException as e:
            print('Device error: {}'.format(e))
            break
        except pynmea2.ParseError as e:
            print('Parse error: {}'.format(e))
            continue
  3. Read NMEA sentences from a file

    master

    Iterate through a file line by line and pass each line to pynmea2.parse(). Wrap the call in a try/except block to handle pynmea2.ParseError for corrupt lines.

    import pynmea2
    
    file = open('examples/data.log', encoding='utf-8')
    
    for line in file.readlines():
        try
            msg = pynmea2.parse(line)
            print(repr(msg))
        except pynmea2.ParseError as e:
            print('Parse error: {}'.format(e))
            continue
  4. Generate NMEA sentences

    master

    You can create a new NMEASentence object by calling the specific message class constructor (e.g., GGA) with the talker, message type, and a tuple of data fields. Use str(msg) to generate the formatted NMEA string.

    import pynmea2
    msg = pynmea2.GGA('GP', 'GGA', ('184353.07', '1929.045', 'S', '02410.506', 'E', '1', '04', '2.6', '100.00', 'M', '-33.9', 'M', '', '0000'))
    print(str(msg))
    # Output: $GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D
  5. Parse NMEA sentences with parse()

    master

    Use pynmea2.parse(data) to convert a NMEA 0183 sentence string into a NMEASentence object. The leading $ is optional and trailing whitespace is ignored.

    By default, the function validates checksums. You can configure checksum behavior using the checksums argument.

    import pynmea2
    msg = pynmea2.parse("$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D")
  6. Configure checksum validation behavior

    master

    The parse(data, checksums=...) function accepts three modes for handling checksums:

    checksums value'required''check' (default)'my_data_is_corrupt'
    sentence with valid checksumacceptedacceptedaccepted
    sentence without checksumChecksumErroracceptedaccepted
    sentence with invalid checksumChecksumErrorChecksumErroraccepted

    Note: Invalid checksums may also cause ParseError if the corruption prevents basic parsing.

    # Example of using the checksums parameter
    pynmea2.parse(data, checksums='required')