Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Knio/pynmea2
Python library for parsing the NMEA 0183 protocol (GPS)
https://github.com/Knio/pynmea2
gps gps-data nmea-parser nmea-protocol nmea-sentences nmea0183 pynmea2 python
Last synced: 26 days ago
JSON representation
Python library for parsing the NMEA 0183 protocol (GPS)
- Host: GitHub
- URL: https://github.com/Knio/pynmea2
- Owner: Knio
- License: mit
- Created: 2013-07-04T04:49:36.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2024-04-28T15:02:43.000Z (8 months ago)
- Last Synced: 2024-11-20T18:26:45.716Z (about 1 month ago)
- Topics: gps, gps-data, nmea-parser, nmea-protocol, nmea-sentences, nmea0183, pynmea2, python
- Language: Python
- Homepage:
- Size: 268 KB
- Stars: 634
- Watchers: 50
- Forks: 223
- Open Issues: 35
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pynmea2
`pynmea2` is a python library for the [NMEA 0183](http://en.wikipedia.org/wiki/NMEA_0183) protocol
`pynmea2` is based on [`pynmea`](https://code.google.com/p/pynmea/) by Becky Lewis
The `pynmea2` homepage is located at
## Compatibility
`pynmea2` is compatable with Python 2.7 and Python 3.4+
![Python version](https://img.shields.io/pypi/pyversions/pynmea2.svg?style=flat)
[![Build status](https://github.com/Knio/pynmea2/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/Knio/pynmea2/actions/workflows/ci.yml?query=branch%3Amaster+)
[![Coverage status](https://img.shields.io/coveralls/github/Knio/pynmea2/master.svg?style=flat)](https://coveralls.io/r/Knio/pynmea2?branch=master)## Installation
The recommended way to install `pynmea2` is with
[pip](http://pypi.python.org/pypi/pip/):```bash
pip install pynmea2
```[![PyPI version](https://img.shields.io/pypi/v/pynmea2.svg?style=flat)](https://pypi.org/project/pynmea2/)
[![PyPI downloads](https://img.shields.io/pypi/dm/pynmea2.svg?style=flat)](https://pypi.org/project/pynmea2/)## Parsing
You can parse individual NMEA sentences using the `parse(data, check=False)` function, which takes a string containing a
NMEA 0183 sentence and returns a `NMEASentence` object. Note that the leading '$' is optional and trailing whitespace is ignored when parsing a sentence.With `check=False`, `parse` will accept NMEA messages that do not have checksums, however it will still raise `pynmea2.ChecksumError` if they are present. `check=True` will also raise `ChecksumError` if the checksum is missing.
Example:
```python
>>> 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")
>>> msg```
The `NMEASentence` object has different properties, depending on its sentence type.
The `GGA` message has the following properties:```python
>>> msg.timestamp
datetime.time(18, 43, 53)
>>> msg.lat
'1929.045'
>>> msg.lat_dir
'S'
>>> msg.lon
'02410.506'
>>> msg.lon_dir
'E'
>>> msg.gps_qual
'1'
>>> msg.num_sats
'04'
>>> msg.horizontal_dil
'2.6'
>>> msg.altitude
100.0
>>> msg.altitude_units
'M'
>>> msg.geo_sep
'-33.9'
>>> msg.geo_sep_units
'M'
>>> msg.age_gps_data
''
>>> msg.ref_station_id
'0000'
```Additional properties besides the ones explicitly in the message data may also exist.
For example, `latitude` and `longitude` properties exist as helpers to access the geographic coordinates as python floats ([DD](http://en.wikipedia.org/wiki/Decimal_degrees), "decimal degrees") instead of the DDDMM.MMMM ("Degrees, minutes, seconds") format used in the NMEA protocol. `latitude_minutes`, `latitude_seconds`, `longitude_minutes`, and `longitude_seconds` are also supported and allow easy creation of differently formatted location strings.
```python
>>> msg.latitude
-19.4840833333
>>> msg.longitude
24.1751
>>> '%02d°%07.4f′' % (msg.latitude, msg.latitude_minutes)
'-19°29.0450′'
>>> '%02d°%02d′%07.4f″' % (msg.latitude, msg.latitude_minutes, msg.latitude_seconds)
"-19°29′02.7000″"
```## Generating
You can create a `NMEASentence` object by calling the constructor with talker, message type, and data fields:
```python
>>> 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'))
```and generate a NMEA string from a `NMEASentence` object:
```python
>>> str(msg)
'$GPGGA,184353.07,1929.045,S,02410.506,E,1,04,2.6,100.00,M,-33.9,M,,0000*6D'
```## File reading example
See [examples/read_file.py](/examples/read_file.py)
```python
import pynmea2file = 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
```## `pySerial` device example
See [examples/read_serial.py](/examples/read_serial.py)
```python
import ioimport pynmea2
import serialser = 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
```