A Python framework for building NMEA 2000 (N2K) marine CAN-bus devices: codecs, fast-packet reassembly, ISO address claim, a device stack, and generated PGN classes.
Project description
nmea2k
A Python framework for building NMEA 2000 (N2K) marine CAN-bus devices — message codecs, fast-packet reassembly, ISO address claim, a device stack with RX dispatch, and generated PGN classes for the full canboat PGN set.
Heavily inspired by ttlappalainen/NMEA2000 (C++). PGN field codecs are generated from canboat (see pgn_gen.py); protocol PGNs with logic are class overrides with the nmea2k.protocol logic.
I did try to adapt the framework to Python's strength, this is not intended for embedded devices (maybe someone wants to check out if it runs in MicroPython).
I'm quite happy with the class generator using the canboat PGN definitions. It allows for class overrides. I first went the route of a single class that can mimic any PGN. I don't think that is as useful, or as performant.
This sat on my drive for a while, I never got around to getting it into a nice package to publish. With the help of a couple of AI friends, I got it cleaned up, a couple of bugs fixed and 2 examples added.
As far as I know it is a pretty complete implementation. But I do not have access to the specification or the test suites.
Enjoy
Not affiliated with or endorsed by the National Marine Electronics Association. "NMEA 2000" is a registered trademark of the NMEA; it is used here only descriptively, to name the protocol this library implements.
Install
pip install nmea2k[can] # normal: pulls python-can as the CAN backend
On constrained hosts where python-can can't be installed (e.g. a Raspberry Pi
under an externally-managed Python), install the base package and use the
bundled raw-SocketCAN shim instead:
pip install nmea2k
import sys, nmea2k.socketcan
sys.modules["can"] = nmea2k.socketcan # before importing the framework
Requires Python 3.10+.
What's here
| Module | Purpose |
|---|---|
nmea2k.message / nmea2k.fields / nmea2k.pgn |
CAN-ID + field-table codec engine |
nmea2k.fastpacket |
fast-packet (multi-frame) assembly/disassembly |
nmea2k.stack (N2K) |
the device stack: TX path, RX dispatch, timers |
nmea2k.device / nmea2k.device_list |
ISO address claim + network device list |
nmea2k.group / nmea2k.handler |
group-function handling + PGN handlers |
nmea2k.gen.* |
one generated class per PGN (from canboat) |
nmea2k.socketcan |
optional stdlib raw-SocketCAN can shim |
Examples
examples/windlass/— a complete anchor-windlass device (address claim, product info, heartbeat, windlass PGNs 128776/7/8): the reference "how to build a device" driver.examples/bridge/— a configurable two-bus N2K bridge that forwards frames between two CAN interfaces with per-direction PGN filtering.
Regenerating the PGN classes
The nmea2k.gen package is generated from CANboat's PGN database:
python tools/pgn_gen.py # reads data/canboat.json -> src/nmea2k/gen/ + lookups.py
License
Apache-2.0. PGN definitions are derived from the Apache-2.0-licensed
CANboat project — see NOTICE.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nmea2k-0.1.0.tar.gz.
File metadata
- Download URL: nmea2k-0.1.0.tar.gz
- Upload date:
- Size: 130.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b78d1f1fa05461cf33edc30b3dac83f040d2f33ccd452d7c2f50afd91b97100
|
|
| MD5 |
4b1ec542dd3c5c19b1fc55d91a434e17
|
|
| BLAKE2b-256 |
5cdaac7d1cde2725322e0f87ec3da3bd61f5c7d90b42e204811b86d3d45ee774
|
File details
Details for the file nmea2k-0.1.0-py3-none-any.whl.
File metadata
- Download URL: nmea2k-0.1.0-py3-none-any.whl
- Upload date:
- Size: 289.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
512b8597a1e9a1f5a3f2bc5f36e68cb75695c0ede6fc5ef2c4679e4ca1b9abd1
|
|
| MD5 |
df9f6be09e9d6db36fa7768f3828fbc4
|
|
| BLAKE2b-256 |
89942b3448918865d5ed15743d1a109f71e46b159d8f0a7ccae47cd8fed2c9d5
|