Python Adapter
Python adapter for working with serial devices over:
- serial/UART (
pyserial) - local process via PTY (
PtyProcessTransport)
Two adapter classes are provided depending on the device type:
WshShellAdapter— for devices runningwsh-shell: text protocol with prompts, sync, command parsingRawSerialAdapter— for devices with binary protocols: raw byte read/write, no shell logic
Both share the same transport layer (SerialTransport) and port auto-discovery by VID/PID,
so all serial access goes through a single layer without importing pyserial directly.
Features
- connect by fixed
portor auto-discover byvid/pid - synchronize/authenticate with shell (
Press <Enter>->Login->Password-> prompt) - optionally verify sync by
ping_command_and_response - execute commands with timeout and retry logic
- parse command output into structured
dict(jsonorkey: value) - recover on failures with optional fallback ping
- raw binary read/write for non-shell devices (
RawSerialAdapter)
Quick Start
From repository root:
WshShellAdapter
For devices running wsh-shell — text protocol with prompt sync, command execution, and output parsing.
Minimal usage:
Config File (TOML)
Default path: wsh_shell_adapter/config.toml
[adapter] maps to AdapterConfig fields.
[local_shell] is used by run_local_shell.py.
Example:
If ping_command_and_response is set, sync() sends the command after prompt detection and
expects the configured response as the full cleaned output or as one full output line before marking
the adapter as synced.
If ping_command_and_response is missing or [], sync() sends a fixed probe command and treats
any non-empty command output as proof that the shell is alive.
auto_recover controls whether the adapter tries soft/hard recovery after command failures.
Load config directly:
Resolve port selection in application code without touching pyserial directly:
RawSerialAdapter
For devices that speak a binary protocol (no shell, no prompts), use RawSerialAdapter
instead of importing pyserial directly. Does not use a TOML config file — configuration
is passed directly as AdapterConfig.
Minimal usage:
AdapterConfig fields used by RawSerialAdapter: port, baudrate, vid, pid.
Available methods and properties:
connect()— open the serial port; auto-discovers by VID/PID ifportis not setdisconnect()— close the serial portwrite(data: bytes) -> int— write raw bytes; returns bytes writtenread(size: int) -> bytes— read up tosizebytesreset_input_buffer()— discard bytes waiting in the receive bufferin_waiting: int— number of bytes available to readis_open: bool— whether the port is currently openport: str | None— active port name, orNoneif not connectedstatus() -> dict—{"connected": bool, "port": str | None}
Helper Scripts
Local PTY helper
Before first run in a clean checkout, build example binary:
USB helper
It loads [adapter] settings from wsh_shell_adapter/config.toml and starts interactive REPL.
Tests
Install test dependencies:
Run all adapter tests: