• Stars
    star
    630
  • Rank 71,328 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 11 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python library for parsing the NMEA 0183 protocol (GPS)

pynmea2

pynmea2 is a python library for the NMEA 0183 protocol

pynmea2 is based on pynmea by Becky Lewis

The pynmea2 homepage is located at http://github.com/Knio/pynmea2

Compatibility

pynmea2 is compatable with Python 2.7 and Python 3.4+

Python version Build Status Coverage status

Installation

The recommended way to install pynmea2 is with pip:

pip install pynmea2

PyPI version PyPI downloads

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:

>>> 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
<GGA(timestamp=datetime.time(18, 43, 53), lat='1929.045', lat_dir='S', lon='02410.506', lon_dir='E', gps_qual='1', num_sats='04', horizontal_dil='2.6', altitude=100.0, altitude_units='M', geo_sep='-33.9', geo_sep_units='M', age_gps_data='', ref_station_id='0000')>

The NMEASentence object has different properties, depending on its sentence type. The GGA message has the following properties:

>>> 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, "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.

>>> 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:

>>> 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:

>>> 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

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

pySerial device example

See examples/read_serial.py

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

More Repositories

1

dominate

Dominate is a Python library for creating and manipulating HTML documents using an elegant DOM API. It allows you to write HTML pages in pure Python very concisely, which eliminate the need to learn another template language, and to take advantage of the more powerful features of Python.
Python
1,706
star
2

carhack

Automotive data aquisition, logging, diagnostics, and visualization project. Focusing on CAN-bus and the Nissan 370Z
JavaScript
72
star
3

myfitbit

Fitbit data export - Because your data belongs to you!
Python
34
star
4

sshsec

SSH server parameter tester
Python
21
star
5

scenerename

script to guess and batch rename media files using tvdb
Python
18
star
6

structpack

A Python library for serializing and deserializing object trees to JSON-compatable values (dicts, lists, strings, ints, floats, bools)
Python
11
star
7

komoot-export

Export all Komoot tours as GPX
Python
10
star
8

dominate.js

JavaScript library focused on quick and simple creation and manipulation of DOM elements
JavaScript
10
star
9

treecat

Displays a tree view of files and their contents
Python
9
star
10

httpproxy

A HTTP proxy server written in C++
C++
8
star
11

SmallptGPU

C
7
star
12

sync

a poor replacement of rsync, and a very efficient file copy utility
Python
6
star
13

everdb-native

C implementation of everdb - Fast and efficient on-disk data structures and embedded databases
C++
6
star
14

everdb

Fast and efficient on-disk data structures and embedded databases
5
star
15

prettytraceback

Pretty tracebacks for Python everywhere
Python
3
star
16

plok

A JavasSript library for rendering interactive and real-time graphs of time-series data
JavaScript
3
star
17

threespace

A python library for interfacing with the YEI 3-Space Sensor
Python
2
star
18

kniopass

A simple CLI password manager
Python
2
star
19

everdb-go

everdb is an embedded database that implements fast efficient on-disk data structures
Go
2
star
20

tumblr-export

Export tumblr posts and likes, including photos and videos
Python
2
star
21

nethealth

Python
2
star
22

Domino

Go
1
star
23

py35win64ext

An example C extension module for 64bit Python 3.5 on Windows
C
1
star
24

ultigpx

Automatically exported from code.google.com/p/ultigpx
Java
1
star
25

tag2itunes

writes ID3 tags to an iTunes database
Python
1
star
26

knio

linux config files
Python
1
star
27

tex2png

A python library to convert TeX strings to PNG images for embedding into webpages
Python
1
star
28

eRacer

3D racing game developed for games programming class at university of calgary
C++
1
star