pamoja-serial
SLIP and COBS byte stuffing with streaming decoders, so a UART byte stream carries discrete packets. One capability of pamoja, one memory-safe Rust core with bindings for TypeScript, Python, and C#.
Install
pip install pamoja-serial
from pamoja import serial
This pulls in pamoja-native, the compiled engine. pip install pamoja is the whole framework in one package.
Example
The script the test suite runs, spliced here as it ran.
From bindings/python/guides/serial.py:
from pamoja.hal import SerialPort, SerialSettings
from pamoja.serial import COBS_DELIMITER, CobsDecoder, cobs, slip
def reading(sequence: int, text: str) -> bytes:
"""A reading: a two-byte sequence number, most significant byte first, then its text."""
return sequence.to_bytes(2, "big") + text.encode()
# The line: 115200 baud, eight data bits, no parity, one stop bit. Ten bits a character.
settings = SerialSettings(115_200)
print(
f"line {settings}, {settings.bits_per_character} bits a character, "
f"{settings.character_nanos / 1_000:.2f} us each"
)
# The two ends of the cable with nothing plugged in. On a Raspberry Pi the gateway's end is
# SerialPort.open("/dev/serial0", settings) and nothing after this statement changes.
gateway, node = SerialPort.pair(settings)
# A UART carries bytes, and nothing marks where a message ends, so the node frames each reading
# with COBS: zero becomes the one byte that ends a frame and never appears inside one, which
# matters here, since the sequence number is full of zeros.
texts = ["wind=12.4", "wind=13.1", "wind=11.8"]
sent = 0
for sequence, text in enumerate(texts, start=1):
frame = cobs.encode(reading(sequence, text))
node.write(frame)
sent += len(frame)
print(
f"node {len(texts)} readings of {len(reading(1, texts[0]))} bytes, "
f"framed as {sent} bytes"
)
# A read returns whatever has arrived, which is rarely one frame: here it is all three. The
# decoder splits the stream back into payloads at each delimiter.
arrived = gateway.read(256, timeout=0.1)
print(f"gateway {len(arrived)} bytes in one read")
decoder = CobsDecoder()
payloads = decoder.feed(arrived)
for payload in payloads:
print(f"reading {int.from_bytes(payload[:2], 'big')} {payload[2:].decode()}")
# What one frame costs on the wire at this speed, start and stop bits included.
frame_length = sent // len(texts)
print(
f"on the wire {settings.transfer_micros(frame_length) / 1_000:.2f} ms "
f"for a {frame_length}-byte frame at {settings}"
)
# The node restarts partway through a frame. As it comes back up it sends a lone delimiter,
# which closes off the half frame, so the gateway drops it rather than gluing it to the next
# one, and then it sends the reading again.
again = cobs.encode(reading(4, "wind=12.9"))
node.write(again[: len(again) // 2])
node.write(bytes([COBS_DELIMITER]))
node.write(again)
dropped_before = decoder.discarded
resent = decoder.feed(gateway.read(256, timeout=0.1))
dropped = decoder.discarded - dropped_before
print(f"restart {dropped} frame cut short and dropped, then {resent[0][2:].decode()}")
# SLIP, the older framing, ends a frame with one reserved byte and escapes that byte and its
# own escape byte inside one. With no reserved bytes in a reading it costs a byte less than
# COBS; a payload full of them costs up to twice its length under SLIP, and never more than
# one byte in 254 over under COBS.
first = reading(1, texts[0])
slip_length = len(slip.encode(first))
cobs_length = len(cobs.encode(first))
print(
f"framing {len(first)} payload bytes: {slip_length} under SLIP, "
f"{cobs_length} under COBS"
)
# The node goes quiet. A read waits for the first byte up to its timeout; on a port with
# nothing plugged in it returns at once and counts the wait instead of sleeping through it, so
# a test of a silent node takes no time.
waited_before = gateway.waited_micros
quiet = gateway.read(256, timeout=0.5)
waited = (gateway.waited_micros - waited_before) // 1_000
print(f"silence {len(quiet)} bytes in {waited} ms, counted and not slept")
The same capability in every language
| Language | Package | Reference |
|---|---|---|
| Rust | pamoja-serial |
reference, docs.rs, install |
| TypeScript | @pamoja/serial |
reference, install |
| Python | pamoja-serial |
reference, install |
| C# | Pamoja.Serial |
reference, install |
Documentation
pamoja.serialreference, every class and function in this module.- The Serial framing guide, with the same example in Rust, TypeScript, and C#.
- Every capability, and the install page.
License
MIT
Metadata
Release files for pamoja-serial 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pamoja_serial-0.2.1.tar.gz | 5.5 kB | Without provenance | Details for pamoja_serial-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pamoja_serial-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for pamoja_serial-0.2.1-py3-none-any.whl |
Total release size: 11.6 kB
Release files / pamoja_serial-0.2.1.tar.gz
| Download URL | pamoja_serial-0.2.1.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
465d98fe440aa99e221c54551965c26cfd819712ee70061f4d97499376cb14bc
|
|
BLAKE2b-256 checksum How to use checksums |
b99e2803ab064291b89ffc2afaca1ae50894fc9bfdd9ab6c72b526750275f692
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.18
|
Release files / pamoja_serial-0.2.1-py3-none-any.whl
| Download URL | pamoja_serial-0.2.1-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d9c89b60592314f36628d4d6fe0d8807ee0782954b9282d510f7ed2c0af04050
|
|
BLAKE2b-256 checksum How to use checksums |
20e8f109e13c5cce64fb033ef100fbce8b8ff41005a0de847975591186a34ad3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.18
|
Security for pamoja-serial 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.