Skip to main content

Unicore UNI protocol parser and generator

Reason this release was yanked:

obsolete

Project description

pyunigps

Current Status | Installation | Message Categories | Reading | Parsing | Generating | Serializing | Examples | Extensibility | Troubleshooting | Author & License

WORK IN PROGRESS - NOT FOR PRODUCTION USE

pyunigps is an original Python 3 parser for the UNI © protocol. UNI is our term for the proprietary binary protocol implemented on Unicore ™ GNSS receiver modules. pyunigps can also parse NMEA 0183 © and RTCM3 © protocols via the underlying pynmeagps and pyrtcm packages from the same author - hence it covers all the protocols that Unicore UNI GNSS receivers are capable of outputting.

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

This is an independent project and we have no affiliation whatsoever with Unicore.

Current Status

Status Release Build Codecov Release Date Last Commit Contributors Open Issues

The current pre-alpha release creates skeleton UNIReader and UNIMessage classes and basic tests using nominal data. It is NOT suitable for production use.

No Copilot


Installation

Python version PyPI version PyPI downloads

pyunigps is compatible with Python>=3.10. In the following, python3 & pip refer to the Python 3 executables. You may need to substitute python for python3, depending on your particular environment (on Windows it's generally python).

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

python3 -m pip install --upgrade pyunigps

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

python3 -m venv env
source env/bin/activate # (or env\Scripts\activate on Windows)
python3 -m pip install --upgrade pyunigps

For Conda users, pyunigps is also available from conda forge:

Anaconda-Server Badge Anaconda-Server Badge

conda install -c conda-forge pyunigps

UNI Message Categories - GET, SET, POLL

pyunigps divides UNI messages into three categories, signified by the mode or msgmode parameter.

mode description defined in
GET (0x00) output from the receiver (the default) unitypes_get.py
SET (0x01) command input to the receiver unitypes_set.py
POLL (0x02) query input to the receiver unitypes_poll.py

If you're simply streaming and/or parsing the output of a UNI receiver, the mode is implicitly GET. If you want to create or parse an input (command or query) message, you must set the mode parameter to SET or POLL. If the parser mode is set to 0x03 (SETPOLL), pyunigps will automatically determine the applicable input mode (SET or POLL) based on the message payload. See examples below for usage.


Reading (Streaming)

class pyunigps.UNIreader.UNIReader(stream, *args, **kwargs)

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

Individual UNI messages can then be read using the UNIReader.read() function, which returns both the raw binary data (as bytes) and the parsed data (as a UNIMessage object, via the parse() method). The function is thread-safe in so far as the incoming data stream object is thread-safe. UNIReader also implements an iterator.

The constructor accepts the following optional keyword arguments:

  • protfilter: NMEA_PROTOCOL (1), UNI_PROTOCOL (2), RTCM3_PROTOCOL (4). Can be OR'd; default is NMEA_PROTOCOL | UNI_PROTOCOL | RTCM3_PROTOCOL (7)
  • quitonerror: ERR_IGNORE (0) = ignore errors, ERR_LOG (1) = log errors and continue (default), ERR_RAISE (2) = (re)raise errors and terminate
  • validate: VALCKSUM (0x01) = validate checksum (default), VALNONE (0x00) = ignore invalid checksum or length
  • parsebitfield: 1 = parse bitfields ('X' type properties) as individual bit flags, where defined (default), 0 = leave bitfields as byte sequences
  • msgmode: GET (0) (default), SET (1), POLL (2), SETPOLL (3) = automatically determine SET or POLL input mode

Example A - Serial input. This example will output both UNI and NMEA messages but not RTCM3, and log any errors:

from serial import Serial

from pyunigps import ERR_LOG, NMEA_PROTOCOL, UNI_PROTOCOL, VALCKSUM, UNIReader

with Serial("/dev/ttyACM0", 115200, timeout=3) as stream:
    qgr = UNIReader(
        stream,
        protfilter=UNI_PROTOCOL | NMEA_PROTOCOL,
        quitonerror=ERR_LOG,
        validate=VALCKSUM,
        parsebitfield=1,
    )
    raw_data, parsed_data = qgr.read()
    if parsed_data is not None:
        print(parsed_data)
<UNI()>

Example B - File input (using iterator). This will only output UNI data, and fail on any error:

from pyunigps import ERR_RAISE, UNI_PROTOCOL, VALCKSUM, UNIReader

with open("pygpsdata_lg580p_UNI.log", "rb") as stream:
    qgr = UNIReader(
        stream, protfilter=UNI_PROTOCOL, validate=VALCKSUM, quitonerror=ERR_RAISE
    )
    for raw_data, parsed_data in qgr:
        print(parsed_data)
<UNI()>     

Example C - Socket input (using iterator). This will output UNI, NMEA and RTCM3 data, and ignore any errors:

import socket

from pyunigps import (
    ERR_IGNORE,
    NMEA_PROTOCOL,
    UNI_PROTOCOL,
    RTCM3_PROTOCOL,
    VALCKSUM,
    UNIReader,
)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as stream:
    stream.connect(("localhost", 50007))
    qgr = UNIReader(
        stream,
        protfilter=NMEA_PROTOCOL | UNI_PROTOCOL | RTCM3_PROTOCOL,
        validate=VALCKSUM,
        quitonerror=ERR_IGNORE,
    )
    for raw_data, parsed_data in qgr:
        print(parsed_data)
<UNI()>

Parsing

pyunigps.UNIreader.UNIReader.parse(message: bytes, **kwargs)

You can parse individual UNI messages using the static UNIReader.parse(data) function, which takes a bytes array containing a binary UNI message and returns a UNIMessage object.

NB: Once instantiated, a UNIMessage object is immutable.

The parse() method accepts the following optional keyword arguments:

  • msgmode: GET (0) (default), SET (1), POLL (2), SETPOLL (3) = automatically determine SET or POLL input mode
  • validate: VALCKSUM (0x01) = validate checksum (default), VALNONE (0x00) = ignore invalid checksum or length
  • parsebitfield: 1 = parse bitfields ('X' type properties) as individual bit flags, where defined (default), 0 = leave bitfields as byte sequences

Example A - parsing RAW-PPPB2B output message:

from pyunigps import GET, VALCKSUM, UNIReader

msg = UNIReader.parse(
    b"",
    msgmode=GET,  # this is the default so could be omitted here
    validate=VALCKSUM,
    parsebitfield=1,
)
print(msg)
<UNI()>

The UNIMessage object exposes different public attributes depending on its message type or 'identity', e.g. the RAW-PPPB2B message has the following attributes:

print(msg)
print(msg.identity)
print(msg.msgver)
print(msg.prn)
<UNI()>
RAW-PPPB2B
1
60

The payload attribute always contains the raw payload as bytes. Attributes within repeating groups are parsed with a two-digit suffix (svid_01, svid_02, etc.).


Generating

class pyunigps.UNImessage.UNIMessage(msggrp, msgid, **kwargs)

You can create a UNIMessage object by calling the constructor with the following parameters:

  1. message group (must be a valid group from pyunigps.UNI_MSGIDS)
  2. message id (must be a valid id from pyunigps.UNI_MSGIDS)
  3. (optional) a series of keyword parameters representing the message payload
  4. (optional) parsebitfield keyword - 1 = define bitfields as individual bits (default), 0 = define bitfields as byte sequences

The 'message group' and 'message id' parameters must be passed as bytes.

The message payload can be defined via keyword arguments in one of three ways:

  1. A single keyword argument of payload containing the full payload as a sequence of bytes (any other keyword arguments will be ignored). NB the payload keyword argument must be used for message types which have a 'variable by size' repeating group.
  2. One or more keyword arguments corresponding to individual message attributes. Any attributes not explicitly provided as keyword arguments will be set to a nominal value according to their type.
  3. If no keyword arguments are passed, the payload is assumed to be null.

Example A - generate a CFG-UART SET (command) message from individual keyword arguments:

from pyunigps import UNIMessage, SET
msg = UNIMessage()
print(msg)
<UNI()>

Example B - generate a INF-VER POLL (query) message from individual keyword arguments:

from pyunigps import UNIMessage, POLL
msg = UNIMessage()
print(msg)
<UNI(INF-VER)>

Example C - generate a RAW-PPPB2B GET (output) message from individual keyword arguments:

from pyunigps import UNIMessage, GET
msg = UNIMessage()
print(msg)
<UNI()>
        

Serializing

The UNIMessage class implements a serialize() method to convert a UNIMessage object to a bytes array suitable for writing to an output stream.

e.g. to create and send a RAW-PPPB2B message:

from serial import Serial
from pyunigps import UNIMessage
serialOut = Serial('COM7', 115200, timeout=5)
print(msg)
output = msg.serialize()
print(output)
serialOut.write(output)
<UNI()>

b''

Examples

The following command line examples can be found in the \examples folder:

  1. uniusage.py illustrates basic usage of the UNIMessage and UNIReader classes.

Extensibility

The UNI protocol is principally defined in the modules unitypes_*.py as a series of dictionaries. Message payload definitions must conform to the following rules:

1. attribute names must be unique within each message class
2. attribute types must be one of the valid types (S1, U2, X4, etc.)
3. if the attribute is scaled, attribute type is list of [attribute type as string (S1, U2, etc.), scaling factor as float] e.g. {"lat": [I4, 1e-7]}
4. repeating or bitfield groups must be defined as a tuple ('numr', {dict}), where:
   'numr' is either:
     a. an integer representing a fixed number of repeats e.g. 32
     b. a string representing the name of a preceding attribute containing the number of repeats e.g. 'numCh'
     c. an 'X' attribute type ('X1', 'X2', 'X4', etc) representing a group of individual bit flags
     d. 'None' for a 'variable by size' repeating group. Only one such group is permitted per payload and it must be at the end.
   {dict} is the nested dictionary of repeating items or bitfield group

Repeating attribute names are parsed with a two-digit suffix (svid_01, svid_02, etc.). Nested repeating groups are supported.


Troubleshooting

1. UnicodeDecode errors.

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

Author & License Information

semuadmin@semuconsulting.com

License

pyunigps is maintained entirely by unpaid volunteers. It receives no funding from advertising or corporate sponsorship. If you find the utility useful, please consider sponsoring the project with the price of a coffee...

Sponsor

Project details


Download files

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

Source Distribution

pyunigps-0.1.0.tar.gz (29.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyunigps-0.1.0-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

File details

Details for the file pyunigps-0.1.0.tar.gz.

File metadata

  • Download URL: pyunigps-0.1.0.tar.gz
  • Upload date:
  • Size: 29.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyunigps-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d57d7d1a496341526dae3d0647070aec3f40c5c3bf8005884457bf2cf709cd17
MD5 08975e0d8cfc596833fb515622dafe7e
BLAKE2b-256 c8a53277497b664c8e1e4316700e653858db0d37e1ccdeb548b39be5fdde3f68

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyunigps-0.1.0.tar.gz:

Publisher: deploy.yml on semuconsulting/pyunigps

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyunigps-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyunigps-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyunigps-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa5549a394faee96c14940f152bb661c370e38b388123326cb89b4327bb67ba1
MD5 023af964964feeb4be0f42984d1b2e70
BLAKE2b-256 cd90b0bc01f5a3cf766502143f0c29b8f790fbc6a70b0a352614355390058aa9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyunigps-0.1.0-py3-none-any.whl:

Publisher: deploy.yml on semuconsulting/pyunigps

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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