🐍 ProtocolDataUnits: A Python Package

ProtocolDataUnits: A Python Package for Encoding/Decoding Protocol Data Units

ProtocolDataUnits is a lightweight Python package designed to simplify encoding and decoding of binary messages defined by Protocol Data Units (PDUs) — especially in embedded, network, and robotics applications.

It was inspired by the robust features of the ProtocolDataUnits.jl Julia package written by Dr Mandar Chitre and has been reimagined for Python with additional flexibility and compatibility.


✨ Features

  • Define nested PDUs with complex field structures
  • Automatic handling of bit- and byte-aligned fields
  • Support for endian configuration and padding
  • Easy-to-define PDU schemas via dictionaries
  • Clean CLI tool and Python API
  • Integrated encoder and decoder with sanity checks

🔧 Installation

pip install ProtocolDataUnits

📦 Example Usage

Here is an example to define and use a PDU schema:

from protocoldataunits import encode_pdu, decode_pdu

schema = {
    'type': 'pdu',
    'fields': [
        {'name': 'version', 'type': 'uint', 'size': 4},
        {'name': 'msg_type', 'type': 'uint', 'size': 4},
        {'name': 'length', 'type': 'uint', 'size': 8},
        {'name': 'payload', 'type': 'bytes', 'size': 3},
    ]
}

data = {
    'version': 1,
    'msg_type': 2,
    'length': 3,
    'payload': b'abc'
}

binary = encode_pdu(schema, data)
decoded = decode_pdu(schema, binary)
print(decoded)

🧠 Use Cases

  • Building custom communication stacks (e.g., WHOI micromodem, Subnero modems)
  • Packing/unpacking binary telemetry and metadata
  • Generating interoperable messages between embedded systems
  • Simulating protocol behavior in test environments

If you’re interested in contributing or need support for your own PDU design, feel free to open an issue or feature request.


📌 Notes

  • Endianness and alignment are critical: make sure to define them explicitly in advanced use cases,
  • You can enable compress=True to minimize bit-level padding,
  • Nested fields are supported via pdu_fragment.
Jay Patel
Jay Patel
OFI Postdoctoral Fellow in Underwater Communication Systems

My research interests include electronics & communications, distributed underwater robotics, mobile computing and programmable matter.

Previous

Related