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.

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

_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.

_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.