Skip to main content

Drop-in 1-to-N multiplexer for pySerial hardware serial interfaces

Project description

pySerialMux

Drop-in 1-to-N multiplexer for pyserial hardware serial interfaces.

Quick usage

from pySerialMux import Serial

with Serial("/dev/ttyUSB0", baudrate=115200, timeout=1.0) as ser:
  ser.write(b"hello\n")
  print(ser.readline())

On-demand QoL and debug options

The proxy accepts optional flags through Serial(...):

  • debug=True
    Enables debug logging for proxy/broker lifecycle details.

  • logs=True
    Enables global binary message logging. The client will receive and buffer all serial and client-to-client traffic with timestamps and origin IDs.

  • ignore_baudrate_diff=True
    Allows a client to connect even when its requested baudrate differs from the broker's baudrate.

  • virtual_interface="<name>"
    Enables virtual routing mode for that interface name.

  • client_id="<id>"
    Optional, but required when using virtual_interface or target_id features.

  • target_id="<id>"
    Routes all writes directly to the client with the matching client_id instead of physical serial. This client will also be isolated from general serial broadcasts.

  • host_virtual_interface=True
    Marks the client as host for that virtual interface. Non-host clients writing to that interface are routed to the host client instead of physical serial.

Client-to-client targeting

Clients can communicate directly by using target_id:

# Setup two clients
alpha = Serial("COM1", client_id="Alpha", target_id="Beta")
beta  = Serial("COM1", client_id="Beta",  target_id="Alpha")

alpha.write(b"Hello Beta")
print(beta.read(10)) # b'Hello Beta'

Runtime targeting

You can change or clear the target at any time:

ser = Serial("COM1", client_id="Alpha")
ser.target_id = "Beta" # Now talking to Beta
ser.target_id = None   # Back to physical serial

Targeted writes

Use the target_id keyword argument for one-off targeted messages:

ser.write(b"One-off message", target_id="Beta")

Discovery and Shared State

Client Discovery

Access a live list of other connected clients:

print(ser.other_clients) # ['Beta', 'Gamma']

Shared Data Store

Share state information between all clients:

# Client Alpha
ser.set_shared("status", b"active")

# Client Beta
print(ser.shared.get("status")) # b'active'

Global Logging

When logs=True is enabled, the broker aggregates all traffic (serial and client-to-client) with high-precision timestamps and origin IDs.

Accessing logs

Retrieve all buffered log entries:

# Returns a list of dicts: 
# [{'timestamp': float, 'origin_type': OriginType, 'origin_id': str, 'data': bytes}, ...]
logs = ser.get_logs()

Real-time logging

Hook into every message as it arrives:

def my_logger(entry):
    print(f"[{entry['timestamp']}] {entry['origin_id']}: {entry['data']}")

ser.on_log = my_logger

Runtime control

You can enable or disable logging at any time:

ser.logs = True  # Start receiving logs
ser.logs = False # Stop receiving logs

Virtual interface example

# host side
host = Serial(
    "/dev/ttyUSB0",
    virtual_interface="lab-bus",
    client_id="host-1",
    host_virtual_interface=True,
)

# client side
client = Serial(
    "/dev/ttyUSB0",
    virtual_interface="lab-bus",
    client_id="client-1",
)

client.write(b"message to host")
print(host.read(15))

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

pyserialmux-0.2.0.tar.gz (30.5 kB view details)

Uploaded Source

Built Distribution

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

pyserialmux-0.2.0-py3-none-any.whl (26.2 kB view details)

Uploaded Python 3

File details

Details for the file pyserialmux-0.2.0.tar.gz.

File metadata

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

File hashes

Hashes for pyserialmux-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c8c3e4fbc24fd63515e16b668da5629f83e8fddf7879fb21a3847502f74a4fb9
MD5 32f2033ecedeed976575705d452ea5c6
BLAKE2b-256 2d78180dba0009323b02f9f1505dbdcfb4a285a17720356e5ab53f9cb11d7157

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyserialmux-0.2.0.tar.gz:

Publisher: publish-pypi.yml on KilianSen/pySerialMux

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

File details

Details for the file pyserialmux-0.2.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pyserialmux-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 180aecd199b3fdec5bdc1d0275cb076042b966d8ebfc75f6a52e6a09f65e28ef
MD5 2a5198a5eaad1833c2336a682a5abec8
BLAKE2b-256 cbaca65250b6a4cdd695e4b1547b384ecc7c0baf3c5631a69db02801f7042542

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyserialmux-0.2.0-py3-none-any.whl:

Publisher: publish-pypi.yml on KilianSen/pySerialMux

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