Skip to main content

UBX Protocol Parser

Reason this release was yanked:

obsolete

Project description

pygnssutils

Current Status | Installation | GNSSReader | gnssdump CLI | gnssserver CLI | gnssntripclient CLI | Troubleshooting | Graphical Client | Author & License

pygnssutils is an original Python 3 utility built around three core GNSS protocol libraries from the same stable:

  1. pynmeagps (NMEA Protocol) - NMEA parsing and generation library
  2. pyubx2 (UBX Protocol) - UBX parsing and generation library
  3. pyrtcm (RTCM3 Protocol) - RTCM3 parsing library

The capabilities supported by this Beta release of pygnssutils include:

  1. GNSSReader class which reads and parses the NMEA, UBX or RTCM3 output of any GNSS device.
  2. GNSSStreamer class and its associated gnssdump CLI utility, which enhances the GNSSReader class with a range of configurable input and output media types (e.g. serial, file, socket and queue) and protocol/message filtering options.
  3. GNSSSocketServer class and its associated gnssserver CLI utility. This implements a TCP Socket Server for GNSS data streams which is also capable of being run as a simple NTRIP Server.
  4. GNSSNTRIPClient class and its associated gnssntripclient CLI utility. This implements a simple NTRIP Client which receives RTCM3 correction data from an NTRIP Server and (optionally) sends this to a designated output stream.

The pygnssutils homepage is located at https://github.com/semuconsulting/pygnssutils.

Current Status

Status Release Build Release Date Last Commit Contributors Open Issues

Sphinx API Documentation in HTML format is available at https://www.semuconsulting.com/pygnssutils.

Contributions welcome - please refer to CONTRIBUTING.MD.

Bug reports and Feature requests - please use the templates provided.


Installation

Python version PyPI version PyPI downloads

pygnssutils is compatible with Python >=3.7. See requirements for dependencies. It is recommended that the Python 3 scripts (bin) folder is in your PATH.

In the following, python3 & pip refer to the Python 3 executables. You may need to type python or pip3, depending on your particular environment.

The recommended way to install the latest version of pygnssutils is with pip:

python3 -m pip install --upgrade pygnssutils

If required, pygnssutils can also be installed into a virtual environment, e.g.:

python3 -m pip install --user --upgrade virtualenv
python3 -m virtualenv env
source env/bin/activate (or env\Scripts\activate on Windows)
(env) python3 -m pip install --upgrade pygnssutils
...
deactivate

GNSSReader

class pygnssutils.gnssreader.GNSSReader(stream, *args, **kwargs)

You can create a GNSSReader object by calling the constructor with an active stream object. The stream object can be any data stream which supports a read(n) -> bytes method (e.g. File or Serial, with or without a buffer wrapper). GNSSReader implements an internal SocketStream class to allow sockets to be read in the same way as other streams (see example below).

Individual input NMEA, UBX, or RTCM3 messages can then be read using the GNSSReader.read() function, which returns both the raw binary data (as bytes) and the parsed data (as a UBXMessage, NMEAMessage or RTCMMessage object). The function is thread-safe in so far as the incoming data stream object is thread-safe. GNSSReader also implements an iterator.

The constructor accepts the following optional keyword arguments:

  • protfilter: 1 = NMEA, 2 = UBX, 4 = RTCM3 (can be OR'd. default is 7 - NMEA & UBX & RTCM3)
  • quitonerror: 0 = ignore errors, 1 = log errors and continue (default), 2 = (re)raise errors and terminate
  • validate: VALCKSUM (0x01) = validate checksum (default), VALNONE (0x00) = ignore invalid checksum or length
  • parsebitfield: 1 = parse bitfields (UBX 'X' type properties) as individual bit flags, where defined (default), 0 = leave bitfields as byte sequences
  • msgmode: 0 = GET (default), 1 = SET, 2 = POLL

Usage:

Example - Serial input. This example will output both UBX and NMEA messages:

>>> from serial import Serial
>>> from pygnssutils import GNSSReader
>>> stream = Serial('/dev/tty.usbmodem14101', 9600, timeout=3)
>>> gnr = GNSSReader(stream)
>>> (raw_data, parsed_data) = gnr.read()
>>> print(parsed_data)

Example - File input (using iterator). This will only output UBX data:

>>> from pygnssutils import GNSSReader
>>> stream = open('ubxdata.bin', 'rb')
>>> gnr = GNSSReader(stream, protfilter=2)
>>> for (raw_data, parsed_data) in gnr: print(parsed_data)
...

Example - Socket input (using enhanced iterator). This will output UBX, NMEA and RTCM3 data:

>>> import socket
>>> from pygnssutils import GNSSReader
>>> stream = socket.socket(socket.AF_INET, socket.SOCK_STREAM):
>>> stream.connect(("localhost", 50007))
>>> gnr = GNSSReader(stream, protfilter=7)
>>> for (raw_data, parsed_data) in gnr.iterate(): print(parsed_data)
...

Refer to the Sphinx API documentation for further details.


GNSSStreamer and gnssdump CLI

class pygnssutils.gnssdump.GNSSStreamer(app=None, **kwargs)

GNSSStreamer is essentially a configurable input/output wrapper around the GNSSReader class. It supports a variety of input streams (including serial, file and socket) and outputs either to stdout (terminal), to an output file or to an custom output handler. The custom output handler can be a writeable output medium (serial, file, socket or queue) or an evaluable Python expression (e.g. lambda function).

The utility can output data in a variety of formats; parsed (1), raw binary (2), hexadecimal string (4), tabulated hexadecimal (8), parsed as string (16), JSON (32), or any combination thereof. You could, for example, output the parsed version of a UBX message alongside its tabular hexadecimal representation.

Any one of the following data stream specifiers must be provided:

  • port: serial port e.g. COM3 or /dev/ttyACM1
  • filename: fully qualified path to binary input file e.g. /logs/logfile.bin
  • socket: socket e.g. 192.168.0.72:50007 (port must be specified)
  • stream: any other instance of a stream class which implements a read(n) -> bytes method

For help and full list of optional arguments, type:

> gnssdump -h

Refer to the Sphinx API documentation for further details.

CLI Usage:

Assuming the Python 3 scripts (bin) directory is in your PATH, the CLI utility may be invoked from the shell thus:

Serial input example (with simple external output handler):

> gnssdump port=/dev/ttyACM1 baud=9600 timeout=5 quitonerror=1 protfilter=2 msgfilter=NAV-PVT outputhandler="lambda msg: print(f'lat: {msg.lat}, lon: {msg.lon}')"

2022-06-23 19:23:12.052109: Parsing GNSS data stream from serial: Serial<id=0x10fe8f100, open=True>(port='/dev/ttyACM1', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=5, xonxoff=False, rtscts=False, dsrdtr=False)...

lat: 51.352179, lon: -2.130762
lat: 51.352155, lon: -2.130751

File input example (in parsed and tabulated hexadecimal formats):

> gnssdump filename=pygpsdata.log quitonerror=2 format=9

2022-07-01 10:47:28.097706: Parsing GNSS data stream from file: <_io.BufferedReader name='pygpsdata.log'>...

<UBX(NAV-STATUS, iTOW=09:47:37, gpsFix=3, gpsFixOk=1, diffSoln=0, wknSet=1, towSet=1, diffCorr=0, carrSolnValid=1, mapMatching=0, psmState=0, spoofDetState=1, carrSoln=0, ttff=33377, msss=1912382)>
000: b562 0103 1000 f80c da1b 03dd 0208 6182  | b'\xb5b\x01\x03\x10\x00\xf8\x0c\xda\x1b\x03\xdd\x02\x08a\x82' |
016: 0000 3e2e 1d00 633d                      | b'\x00\x00>.\x1d\x00c=' |

<UBX(NAV-DOP, iTOW=09:47:37, gDOP=1.55, pDOP=1.32, tDOP=0.8, vDOP=1.11, hDOP=0.72, nDOP=0.59, eDOP=0.42)>
000: b562 0104 1200 f80c da1b 9b00 8400 5000  | b'\xb5b\x01\x04\x12\x00\xf8\x0c\xda\x1b\x9b\x00\x84\x00P\x00' |
016: 6f00 4800 3b00 2a00 9b75                 | b'o\x00H\x00;\x00*\x00\x9bu' |

<UBX(NAV-TIMEGPS, iTOW=09:47:37, fTOW=422082, week=2216, leapS=18, towValid=1, weekValid=1, leapSValid=1, tAcc=10)>
000: b562 0120 1000 f80c da1b c270 0600 a808  | b'\xb5b\x01 \x10\x00\xf8\x0c\xda\x1b\xc2p\x06\x00\xa8\x08' |
016: 1207 0a00 0000 3566                      | b'\x12\x07\n\x00\x00\x005f' |

Socket input example (in JSON format):

> gnssdump socket=192.168.0.20:50010 format=32 msgfilter=1087

2022-06-23 19:27:10.103332: Parsing GNSS data stream from: <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 57399), raddr=('127.0.0.1', 50010)>...

{"GNSS_Messages: [{"class": "<class 'pyrtcm.rtcmmessage.RTCMMessage'>", "identity": "1087", "payload": {"DF002": 1087, "DF003": 0, "GNSSEpoch": 738154640, "DF393": 1, "DF409": 0, "DF001_7": 0, "DF411": 0, "DF412": 0, "DF417": 0, "DF418": 0, "DF394": 1152921504606846976, "NSat": 1, "DF395": 1073741824, "NSig": 1, "DF396": 1, "DF405_01": 0.00050994, "DF406_01": 0.00194752, "DF407_01": 102, "DF420_01": 0, "DF408_01": 0, "DF404_01": 0.5118}},...]}

Output file example (this filters unwanted UBX config & debug messages from a u-center .ubx file):

> gnssdump filename=COM6__9600_220623_093412.ubx protfilter=1 format=2 verbosity=0 outfile=COM6__9600_220623_093412_filtered.ubx

GNSSSocketServer and gnssserver CLI

class pygnssutils.gnssserver.GNSSSocketServer(app=None, **kwargs)

GNSSSocketServer is essentially a wrapper around the GNSSStreamer and SocketServer classes (the latter based on the native Python ThreadingTCPServer framework) which uses queues to transport data between the two classes.

CLI Usage - Default Mode:

In its default configuration (ntripmode=0) gnssserver acts as an open, unauthenticated CLI TCP socket server, reading the binary data stream from a host-connected GNSS receiver and broadcasting the data to any local or remote TCP socket client capable of parsing binary GNSS data.

It supports most of gnssdump's formatting capabilities and could be configured to output a variety of non-binary formats (including, for example, JSON or hexadecimal), but the client software would need to be capable of parsing data in such formats.

Assuming the Python 3 scripts (bin) directory is in your PATH, the CLI utility may be invoked from the shell thus:

> gnssserver inport="/dev/tty.usbmodem14301" baudrate=115200 hostip=192.168.0.20 outport=6000
Starting server (type CTRL-C to stop)...
Starting input thread, reading from /dev/tty.usbmodem141301...

Parsing GNSS data stream from: Serial<id=0x1063647f0, open=True>(port='/dev/tty.usbmodem141301', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=3, xonxoff=False, rtscts=False, dsrdtr=False)...

Starting output thread, broadcasting on 192.168.0.20:6000...
Client ('192.168.0.56', 59565) has connected. Total clients: 1
Client ('192.168.0.34', 59566) has connected. Total clients: 2
Client ('192.168.0.41', 59567) has connected. Total clients: 3
Client ('192.168.0.56', 59565) has disconnected. Total clients: 2

gnssserver can be run as a daemon process (or even a service) but note that abrupt termination (i.e. without invoking the internal server.shutdown() method) may result in the designated TCP socket port being unavailable for a short period - this is operating system dependant.

For help and full list of optional arguments, type:

> gnssserver -h

Refer to the Sphinx API documentation for further details.

CLI Usage - NTRIP Mode:

gnssserver can also be configured to act as a single-mountpoint NTRIP Server (ntripmode=1), broadcasting RTCM3 RTK correction data to any authenticated NTRIP client on the standard 2101 port:

> gnssserver inport="/dev/tty.usbmodem14101" hostip=192.168.0.20 outport=2101 ntripmode=1 protfilter=4

NOTE THAT this configuration is predicated on the host-connected receiver being an RTK-capable device (e.g. the u-blox ZED-F9P) operating in 'Base Station' mode (either 'SURVEY_IN' or 'FIXED') and outputting the requisite RTCM3 RTK correction messages (1005, 1077, 1087, 1097, 1127, 1230). NTRIP server login credentials are set via environment variables PYGPSCLIENT_USER and PYGPSCLIENT_PASSWORD.

Clients

gnssserver will work with any client capable of parsing binary GNSS data from a TCP socket. Suitable clients include, but are not limited to:

  1. pygnssutils's gnssdump cli utility invoked thus:
> gnssdump socket=hostip:outport
  1. The PyGPSClient GUI application.

GNSSNTRIPClient and gnssntripclient CLI

class pygnssutils.gnssntripclient.GNSSNTRIPClient(app=None, **kwargs)

The GNSSNTRIPClient class provides a basic NTRIP Client capability and forms the basis of a gnssntripclient CLI utility. It receives RTCM3 correction data from an NTRIP server and (optionally) sends this to a designated output stream.

CLI Usage:

Assuming the Python 3 scripts (bin) directory is in your PATH, the CLI utility may be invoked from the shell thus:

To retrieve the sourcetable and determine the closest available mountpoint to the reference lat/lon, leave the mountpoint argument blank (the port defaults to 2101):

> gnssntripclient server=rtk2go.com reflat=37.23 reflon=-115.81 verbosity=2
2022-06-03 20:15:54.510294: Closest mountpoint to reference location 37.23,-115.81 = WW6RY, 351.51 km

Complete sourcetable follows...

['AGSSIAAP', 'Acheres', 'RTCM 3.0', '1004(1),1006(13),1012(1),1033(31)', '2', 'GPS+GLO', 'SNIP', 'FRA', '48.97', '2.17', '1', '0', 'sNTRIP', 'none', 'N', 'N', '2540', '']
...

To retrieve correction data from a designated mountpoint (this will send NMEA GGA position sentences to the server at intervals of 60 seconds, based on the supplied reference lat/lon):

> gnssntripclient server=rtk2go.com reflat=37.23 reflon=-115.81 mountpoint=UFOSRUS ggainterval=60 verbosity=2
2022-06-03 11:55:10.305870: <RTCM(1077, DF002=1077, DF003=0, GNSSEpoch=471328000, DF393=1, ...

For help and full list of optional arguments, type:

> gnssntripclient -h

Refer to the Sphinx API documentation for further details.


Troubleshooting

1. Unknown Protocol errors.

These are usually due to corruption of the serial data stream, either because the serial port configuration is incorrect (baud rate, parity, etc.) or because another process is attempting to use the same data stream.

  • Check that your UBX receiver UART1 or UART2 ports are configured for the desired baud rate - remember the factory default is 38400 (not 9600).
  • Check that no other process is attempting to use the same serial port, including daemon processes like gpsd.

2. Serial Permission errors.

These are usually caused by inadequate user privileges or contention with another process.

  • On Linux platforms, check that the user is a member of the tty and/or dialout groups.
  • Check that no other process is attempting to use the same serial port, including daemon processes like gpsd.

3. UnicodeDecode errors.

  • If reading UBX data from a log file, check that the file.open() procedure is using the rb (read binary) setting e.g. stream = open('ubxdatalog.log', 'rb').

4. Reading from NMEA log file returns no results.

  • If reading from a binary log file containing NMEA messages, ensure that the message terminator is CRLF (\r\n or x0d0a) rather than just LF (\n or 0x0a). Some standard text editors may replace a CRLF with LF - use a dedicated hex editor instead.

Graphical Client

A python/tkinter graphical GPS client which supports NMEA, UBX and RTCM3 protocols is available at:

https://github.com/semuconsulting/PyGPSClient


Author & License Information

semuadmin@semuconsulting.com

License

pygnssutils is maintained entirely by unpaid volunteers. If you find it useful, a small donation would be greatly appreciated!

Donations

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygnssutils-0.2.3.tar.gz (41.3 kB view hashes)

Uploaded Source

Built Distribution

pygnssutils-0.2.3-py3-none-any.whl (40.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page