plesty.lib.traffic.serial

Serial traffic manager for Plesty device communication.

Classes

SerialTrafficManager

A TrafficManager implementation for managing serial communication.

Module Contents

class plesty.lib.traffic.serial.SerialTrafficManager(port, baudrate=9600, timeout=5, write_termination='\r', read_termination='\r\n', response_flag=None, parity: str | None = None, stopbits: str | None = None, bytesize: int | None = None)

Bases: plesty.lib.traffic.TrafficManager

A TrafficManager implementation for managing serial communication.

This class provides methods to open, close, and communicate with serial devices.

Initialize SerialTrafficManager with port, baud rate, and communication settings.

Parameters:
  • port – The serial port to connect to (e.g., ‘COM3’ or ‘/dev/ttyUSB0’).

  • baudrate – The baud rate for the serial communication. Defaults to 9600.

  • timeout – The timeout for serial operations in seconds. Defaults to 5.

  • write_termination – Termination suffix appended on write.

  • read_termination – Termination sequence used when reading responses.

  • response_flag – A string indicating a successful response from the device.

  • parity (str | None) – The parity setting; must be ‘none’, ‘even’, or ‘odd’.

  • stopbits (str | None) – The stop bits setting; must be ‘one’.

  • bytesize (int | None) – The byte size; must be 8.

PROTOCOL = 'serial'
port
baudrate = 9600
write_termination = '\r'
read_termination = Multiline-String
Show Value
"""
"""
response_flag = None
parity
stopbits
bytesize = 8
property is_open: bool

Return True if the serial connection is open.

Return type:

bool

preflight(address: str | None = None) → plesty.lib.traffic.preflight.Preflight

Report whether the port is present, and what is there instead.

No port is opened: the listing comes from the USB and tty descriptors. Opening a port to find out what it is means opening one another device server holds, and that has hung processes uninterruptibly.

A plain serial device has no protocol to filter on, so every port is a candidate and the address is matched by name first and by serial number second. The serial number is what survives a replug — a port name is assigned per USB socket, so a device that moves takes a new one.

Parameters:

address (str | None) – A port name ("COM3", "/dev/ttyUSB0") or a device serial number; None uses this manager’s own port.

Return type:

plesty.lib.traffic.preflight.Preflight

_open(baudrate: int | None = None, timeout: float | None = None)

Open the serial connection with the specified parameters.

Parameters:
  • baudrate (int | None) – The baud rate; defaults to the instance’s baudrate.

  • timeout (float | None) – The timeout in seconds; defaults to the instance’s timeout.

_explain_open_error(exc: BaseException) → str | None

Say whether the port was held or forbidden rather than absent.

Parameters:

exc (BaseException)

Return type:

str | None

_clear_buffer()

Clear the input and output buffers of the serial connection.

_waite_for_response(timeout=None)

Wait for a response until expected termination is received or timeout.

The same None test as _open(), and for the same reason: an explicit 0 must reach the window rather than be replaced by the instance’s. Fixing only _open would leave a caller’s 0 swallowed one level down, where it is harder to see because the obvious site now looks right.

A window of 0 does not read here — the loop below wants a positive one — so it takes the non-positive path and says so. That is this function’s existing contract for a window it cannot wait in; what changes is that a caller who asked for 0 now meets it, instead of quietly getting the instance’s timeout instead.

_send_command(command: str, timeout=None) → Any

Send a command to the device and wait for the response.

Parameters:
  • command (str) – The command to send to the device.

  • timeout – Timeout for waiting for the response; defaults to instance’s timeout.

Returns:

The response from the device, or False if the command failed or timed out.

Return type:

Any

orphan(command: str) → bool

Transmit command without waiting for or reading its answer.

The buffer is deliberately not cleared afterwards: the unread answer staying there is the whole point.

Parameters:

command (str) – A command the device will answer.

Returns:

True once transmitted.

Return type:

bool

_close()

Close the serial connection.