{"id":9228,"library":"pynmea2","title":"pynmea2 NMEA 0183 Protocol Parser","description":"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.","status":"active","version":"1.19.0","language":"en","source_language":"en","source_url":"https://github.com/Knio/pynmea2","tags":["nmea","gps","gnss","serial","protocol","parsing","location"],"install":[{"cmd":"pip install pynmea2","lang":"bash","label":"Install pynmea2"}],"dependencies":[],"imports":[{"note":"The primary function for parsing NMEA strings.","symbol":"parse","correct":"import pynmea2\nmsg = pynmea2.parse('$GPGGA,...')"},{"note":"While sentence types exist in `pynmea2.types.talker`, they are conveniently exposed directly under the `pynmea2` namespace for easier access, especially for creation.","wrong":"from pynmea2.types.talker import GGA","symbol":"GGA","correct":"from pynmea2 import GGA\nmsg = GGA('GP', 'GGA', (...))"},{"note":"Exception raised when an NMEA sentence has an invalid checksum.","symbol":"ChecksumError","correct":"from pynmea2 import ChecksumError"},{"note":"General exception for malformed NMEA sentences.","symbol":"ParseError","correct":"from pynmea2 import ParseError"}],"quickstart":{"code":"import pynmea2\nimport datetime\n\n# Example 1: Parsing an NMEA sentence\nraw_gga = '$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D'\ntry:\n    msg = pynmea2.parse(raw_gga)\n    print(f\"Parsed sentence type: {msg.sentence_type}\")\n    print(f\"Timestamp: {msg.timestamp}\")\n    print(f\"Latitude: {msg.latitude}, Longitude: {msg.longitude}\")\nexcept pynmea2.ChecksumError as e:\n    print(f\"Checksum error: {e}\")\nexcept pynmea2.ParseError as e:\n    print(f\"Parse error: {e}\")\n\n# Example 2: Creating an NMEA sentence\n# 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)\n# Note: lat/lon can be floats, others mostly strings/ints\ntimestamp_val = datetime.time(12, 0, 0)\nlat_val = '3404.70417'\nlat_dir_val = 'N'\nlon_val = '11808.66539'\nlon_dir_val = 'W'\n\ntry:\n    new_gga = pynmea2.GGA('GP', 'GGA', (\n        timestamp_val, lat_val, lat_dir_val, lon_val, lon_dir_val,\n        1, 8, 0.9, 500.0, 'M', 25.0, 'M', '', ''\n    ))\n    print(f\"\\nCreated NMEA sentence: {str(new_gga)}\")\nexcept ValueError as e:\n    print(f\"Error creating GGA sentence: {e}\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Wrap calls to `pynmea2.parse()` in a `try-except pynmea2.ChecksumError` block. If you absolutely must, you can disable checksum validation by passing `checksum=False` to `pynmea2.parse()`, but this is generally not recommended for reliable data.","message":"`pynmea2.parse()` performs checksum validation by default. If your NMEA sentences have invalid or missing checksums, it will raise a `pynmea2.ChecksumError`.","severity":"gotcha","affected_versions":"All 1.x.x versions"},{"fix":"Implement a buffer that accumulates data until a complete NMEA sentence (from `$` or `!` to `*XX` and newline) is received before passing it to `pynmea2.parse()`. Libraries like `pyserial` often handle line-by-line reading.","message":"When receiving NMEA data from a stream (e.g., serial port, socket), you often receive incomplete lines. `pynmea2.parse()` expects a complete NMEA sentence starting with `$` or `!` and ending with a newline, including the checksum.","severity":"gotcha","affected_versions":"All 1.x.x versions"},{"fix":"Always check the `msg.sentence_type` or `msg.talker_id` and refer to the NMEA 0183 standard or pynmea2 documentation for the available fields for that specific sentence type before accessing them.","message":"NMEA sentences have fixed fields, and attempting to access a non-existent field (e.g., `msg.speed` on a GGA sentence) will result in an `AttributeError`.","severity":"gotcha","affected_versions":"All 1.x.x versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Verify the integrity of your NMEA data source. If you're generating sentences, ensure the checksum is calculated correctly. For parsing, wrap `pynmea2.parse()` in a `try-except pynmea2.ChecksumError` block to handle bad data, or consider disabling checksum validation if data integrity is not paramount (e.g., `pynmea2.parse(data, checksum=False)`).","cause":"The NMEA sentence provided to `pynmea2.parse()` has an incorrect or missing checksum.","error":"pynmea2.ChecksumError: Checksum doesn't match"},{"fix":"Pre-process your input data to extract only the raw NMEA sentence, ensuring it starts with `$` or `!` and includes the full sentence up to the checksum and newline. This often involves buffering and line-splitting when reading from a stream.","cause":"The input string passed to `pynmea2.parse()` does not begin with the standard NMEA start character (`$` or `!`), indicating it's not a valid NMEA sentence or it's part of a larger string.","error":"pynmea2.ParseError: sentence did not start with '$' or '!'"},{"fix":"Consult the NMEA 0183 specification or the `pynmea2` documentation for the fields available for each sentence type. Use `msg.sentence_type` to dynamically determine the type and access fields conditionally.","cause":"You are trying to access a field that does not exist for the specific NMEA sentence type (e.g., trying to get 'altitude' from an RMC sentence, which doesn't have it).","error":"AttributeError: 'RMC' object has no attribute 'some_invalid_field'"}]}