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, 'direction': Direction, 'data': bytes}, ...]
# direction is Direction.TX (leaving the broker) or Direction.RX (entering the broker).
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['direction'].name}: {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.3.0.tar.gz (31.6 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.3.0-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyserialmux-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ea7b993029018b48ed34a5f35f2e9f10b9f1dbe529bd703b9ea0d76a96858628
MD5 729f9dbdd54f32f72840b5ba92d3cc85
BLAKE2b-256 60ec3818b84eaab2363e2b80af144e72e4b40e52e9e257a5b0ca8ce1049d3c0c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyserialmux-0.3.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.3.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pyserialmux-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6d922a8f5643f9a56fe66ea7e73a3acbb47f5dd712c87dea94646b7e439ae60
MD5 aabc3ff406ef3d0bdaa64f7856f38e05
BLAKE2b-256 0ab339f686943a88f64a4a2db9498b6eacbcb41e2a97ed7b8daa1bb1b840f07b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyserialmux-0.3.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