NMEA 2000 encoder and decoder
Project description
NMEA 2000 Python Library
A Python library for encoding and decoding NMEA 2000 frames. The encoding and decoding is based on the extensive canboat database. It also supports inexpensive CANBUS USB and TCP devices as gateways between your NMEA 2000 boat network and any Python code that wants to receive or send these messages.
This package is the backend for the Home Assistant NMEA 2000 Integration.
Features
- Decode NMEA 2000 frames: Parse and interpret raw NMEA 2000 data.
- Encode NMEA 2000 frames: Convert structured data back into the NMEA 2000 frame format.
- USB client: Send and receive NMEA 2000 data over CANBUS USB devices like Waveshare USB-CAN-A
- python-can client: Send and receive NMEA 2000 data over any generic USB or SocketCAN device supported by python-can
- TCP client: Send and receive NMEA 2000 data over CANBUS TCP devices like:
- PGN-specific parsing: Handle various PGNs with specific parsing rules based on canboat.
- Stateful decoder: The decoder supports NMEA 2000 fast messages, which are split across multiple CANBUS messages.
- CLI support: Built-in command-line interface for encoding and decoding frames.
Installation
You can install the library using pip:
pip install nmea2000
Alternatively, you can clone the repository and install it locally:
git clone https://github.com/tomer-w/nmea2000.git
cd nmea2000
pip install .
Usage
Decode NMEA 2000 Frame (CLI)
To decode a frame, use the decode command followed by the frame in Actisense hex format:
nmea2000-cli decode --frame "09FF7 0FF00 3F9FDCFFFFFFFFFF"
65280 Furuno: Heave: Manufacturer Code = Furuno (bytes = "3F 07"), Reserved = 3 (bytes = "03"), Industry Code = Marine (bytes = "04"), Heave = -0.036000000000000004 (bytes = "DC"), Reserved = 65535 (bytes = "FF FF 00")
Or in JSON format:
{"PGN":65280,"id":"furunoHeave","description":"Furuno: Heave","fields":[{"id":"manufacturer_code","name":"Manufacturer Code","description":"Furuno","unit_of_measurement":"","value":"Furuno","raw_value":1855},{"id":"reserved_11","name":"Reserved","description":"","unit_of_measurement":"","value":3,"raw_value":3},{"id":"industry_code","name":"Industry Code","description":"Marine Industry","unit_of_measurement":"","value":"Marine","raw_value":4},{"id":"heave","name":"Heave","description":"","unit_of_measurement":"m","value":-0.036000000000000004,"raw_value":-36},{"id":"reserved_48","name":"Reserved","description":"","unit_of_measurement":"","value":65535,"raw_value":65535}],"source":9,"destination":255,"priority":7}
Example Code
from nmea2000.decoder import NMEA2000Decoder
# Initialize decoder
decoder = NMEA2000Decoder()
# Decode a frame
frame_str = "09FF7 0FF00 3F9FDCFFFFFFFFFF"
decoded_frame = decoder.decode(frame_str)
# Print decoded frame
print(decoded_frame)
Example reading packets using python-can
import can
from nmea2000.decoder import NMEA2000Decoder
# Initialize decoder
decoder = NMEA2000Decoder()
# Connect to CAN bus (e.g. slcan device on /dev/ttyUSB0)
bus = can.interface.Bus(interface='slcan', channel="/dev/ttyUSB0", bitrate=250000)
# Decode frames
for msg in bus:
decoded_frame = decoder.decode(msg)
# Print decoded frame when ready (fast data intermediate frames return None)
if decoded_frame is not None:
print(decoded_frame)
TCP Client CLI
nmea2000-cli tcp_client --server 192.168.0.46 --port 8881 --type actisense
Use --json to output received messages as JSON (one object per line), useful for piping into other tools:
nmea2000-cli tcp_client --server 192.168.0.46 --port 8881 --type actisense --json
The --json flag is also available for usb_client and can_client.
TCP Client code
async def handle_received_data(message: NMEA2000Message):
"""User-defined callback function for received data."""
print(f"Callback: Received {message}")
client = EByteNmea2000Gateway(ip, port)
client.set_receive_callback(handle_received_data) # Register callback
Encode NMEA 2000 Frame (CLI)
You can also encode data into NMEA 2000 frames using the encode command:
nmea2000-cli encode --data "your_data_to_encode"
Example:
nmea2000-cli encode --data '{"PGN":65280,"id":"furunoHeave","description":"Furuno: Heave","fields":[{"id":"manufacturer_code","name":"Manufacturer Code","description":"Furuno","unit_of_measurement":"","value":"Furuno","raw_value":1855},{"id":"reserved_11","name":"Reserved","description":"","unit_of_measurement":"","value":3,"raw_value":3},{"id":"industry_code","name":"Industry Code","description":"Marine Industry","unit_of_measurement":"","value":"Marine","raw_value":4},{"id":"heave","name":"Heave","description":"","unit_of_measurement":"m","value":-0.036000000000000004,"raw_value":-36},{"id":"reserved_48","name":"Reserved","description":"","unit_of_measurement":"","value":65535,"raw_value":65535}],"source":9,"destination":255,"priority":7}'
Encoding frame: {"PGN":65280,"id":"furunoHeave","description":"Furuno: Heave","fields":[{"id":"manufacturer_code","name":"Manufacturer Code","description":"Furuno","unit_of_measurement":"","value":"Furuno","raw_value":1855},{"id":"reserved_11","name":"Reserved","description":"","unit_of_measurement":"","value":3,"raw_value":3},{"id":"industry_code","name":"Industry Code","description":"Marine Industry","unit_of_measurement":"","value":"Marine","raw_value":4},{"id":"heave","name":"Heave","description":"","unit_of_measurement":"m","value":-0.036000000000000004,"raw_value":-36},{"id":"reserved_48","name":"Reserved","description":"","unit_of_measurement":"","value":65535,"raw_value":65535}],"source":9,"destination":255,"priority":7}'
output:
09FF7 0FF00 3F9FDCFFFFFFFFFF
Example Code
from nmea2000.encoder import NMEA2000Encoder
from nmea2000.input_formats import N2KFormat
# Initialize encoder
encoder = NMEA2000Encoder(output_format=N2KFormat.TCP)
# Data to encode: vessel heading message (PGN 127250)
message = NMEA2000Message(
PGN=127250,
priority=2,
source=1,
destination=255,
fields=[
NMEA2000Field(
id="sid",
raw_value=0,
),
NMEA2000Field(
id="heading",
value=1, # 1 radian is 57 degrees
),
NMEA2000Field(
id="deviation",
raw_value=0,
),
NMEA2000Field(
id="variation",
raw_value=0,
),
NMEA2000Field(
id="reference",
raw_value=0,
),
NMEA2000Field(
id="reserved_58",
raw_value=0,
)
]
)
msg_bytes = encoder.encode(_generate_test_message())
print(msg_bytes)
Node-RED Integration
You can stream decoded NMEA 2000 data into Node-RED using the CLI with the --json flag and a Node-RED exec node.
Setup
-
Exec node — add an
execnode and configure:- Command:
nmea2000-cli tcp_client --server 192.168.1.100 --port 8881 --type EBYTE --json - Output: select "when stdout has data" so it emits a message for each line (not on process exit)
- Use spawn mode: enable
Use spawn() instead of exec() - Timeout: leave blank (this is a long-running process)
- Append msg.payload: uncheck
- Command:
-
JSON node — connect the exec node's first output (stdout) to a
jsonnode to parse each line into a JavaScript object. -
Process the data — use a
switchorfunctionnode to route by PGN:// Example function node: add topic by PGN msg.topic = "nmea2000/pgn/" + msg.payload.PGN; return msg;
Importable Flow
Copy and import this JSON into Node-RED (Menu → Import → Clipboard):
[
{
"id": "nmea2000_exec",
"type": "exec",
"name": "NMEA2000 Stream",
"command": "nmea2000-cli tcp_client --server 192.168.1.100 --port 8881 --type EBYTE --json",
"addpay": "",
"append": "",
"useSpawn": "true",
"oldrc": false,
"timer": "",
"wires": [["nmea2000_json"], [], []]
},
{
"id": "nmea2000_json",
"type": "json",
"name": "Parse JSON",
"property": "payload",
"wires": [["nmea2000_debug"]]
},
{
"id": "nmea2000_debug",
"type": "debug",
"name": "NMEA2000 Data",
"active": true,
"tosidebar": true,
"wires": []
}
]
Each received NMEA 2000 message arrives as a parsed JSON object:
{
"PGN": 127250,
"id": "vesselHeading",
"description": "Vessel Heading",
"source": 3,
"destination": 255,
"priority": 2,
"fields": [
{
"id": "heading",
"name": "Heading",
"value": 182.5,
"unit_of_measurement": "deg"
}
]
}
Tip: Replace
tcp_clientwithusb_clientorcan_clientdepending on your gateway hardware. All client commands support the--jsonflag.
Development
Contributions, feedback, and suggestions to improve this project are welcome. If you have ideas for new features, bug fixes, or improvements, feel free to open an issue or create a pull request. I’m always happy to collaborate and learn from the community!
Please don't hesitate to reach out with any questions, comments, or suggestions.
Setup for Development
To contribute to this library, clone the repository and install the required dependencies:
git clone https://github.com/tomer-w/nmea2000.git
cd nmea2000
pip install -e .[dev]
Running Tests
To run the tests, use:
pytest
Running the CLI Locally
To test the CLI locally, you can use the following command:
python -m nmea2000.cli decode --frame "your_hex_encoded_frame"
License
This project is licensed under the Apache 2.0 license - see the LICENSE file for details.
Acknowledgements
- This library leverages the canboat as the source for all PGN data.
- Special thanks to Rob from Smart Boat Innovations. His code was the initial inspiration for this project. Some of the code here may still be based on his latest open-source version.
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 nmea2000-2026.4.0.tar.gz.
File metadata
- Download URL: nmea2000-2026.4.0.tar.gz
- Upload date:
- Size: 392.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba8beb642bdb78510b609ba7a9f19eac32b3be881ea6b2f87b94b4d525b1e777
|
|
| MD5 |
fec2c8c8558e9575cd122e2db4fbf752
|
|
| BLAKE2b-256 |
ac59e8af0a2fc8c743c4b29b4e2b9d286077f73a0280554310779255a5ef2ffa
|
Provenance
The following attestation bundles were made for nmea2000-2026.4.0.tar.gz:
Publisher:
python-publish.yml on tomer-w/nmea2000
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nmea2000-2026.4.0.tar.gz -
Subject digest:
ba8beb642bdb78510b609ba7a9f19eac32b3be881ea6b2f87b94b4d525b1e777 - Sigstore transparency entry: 1217290606
- Sigstore integration time:
-
Permalink:
tomer-w/nmea2000@e74cbf827be3160040f91807765b085368512918 -
Branch / Tag:
refs/tags/v2026.4.0 - Owner: https://github.com/tomer-w
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@e74cbf827be3160040f91807765b085368512918 -
Trigger Event:
release
-
Statement type:
File details
Details for the file nmea2000-2026.4.0-py3-none-any.whl.
File metadata
- Download URL: nmea2000-2026.4.0-py3-none-any.whl
- Upload date:
- Size: 388.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cba83b097b9f68b20c0cb0c8070130803797f5db7292d450576a32f958bc3e58
|
|
| MD5 |
3719928e84c641838170c5b88f558166
|
|
| BLAKE2b-256 |
a9119c4e024efb6208561c5d3392a71c1b6034ea978ec7ae30d7f94202cbe09c
|
Provenance
The following attestation bundles were made for nmea2000-2026.4.0-py3-none-any.whl:
Publisher:
python-publish.yml on tomer-w/nmea2000
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nmea2000-2026.4.0-py3-none-any.whl -
Subject digest:
cba83b097b9f68b20c0cb0c8070130803797f5db7292d450576a32f958bc3e58 - Sigstore transparency entry: 1217290610
- Sigstore integration time:
-
Permalink:
tomer-w/nmea2000@e74cbf827be3160040f91807765b085368512918 -
Branch / Tag:
refs/tags/v2026.4.0 - Owner: https://github.com/tomer-w
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@e74cbf827be3160040f91807765b085368512918 -
Trigger Event:
release
-
Statement type: