herosdevices.core.bus.serial

Primitive functions and classes representing serial connections.

Module Contents

herosdevices.core.bus.serial.SERIAL_DELAYS_DEFAULT: dict
herosdevices.core.bus.serial.serial = None
class herosdevices.core.bus.serial.SerialConnection(address: str, baudrate: int = 115200, line_termination: bytes = b'\n', keep_alive: bool = True, delays: dict | None = None, **kwargs)[source]

A class to manage serial communication connections.

This class provides functionality to handle serial connections including opening/closing connections, reading data, and writing data. It inherits from the base class Connection.

Parameters:
  • address – The address of the serial socket, something like /dev/ttyUSB0.

  • baudrate – The baud rate for the serial communication.

  • line_termination – character that terminates a line in the communication.

  • keep_alive – Flag indicating whether to keep the connection open between operations.

  • delays

    Dictionary containing delay times for in between serial operations. Default serial delays for serial devices. Available keys are:

    • ”write”: Time to wait after writing a command to the device.

    • ”read_echo”: Time to wait before reading a response from the device.

    • ”flush”: Time to wait after flushing the device.

    herosdevices.core.bus.SERIAL_DELAYS_DEFAULT sets the default delays.

  • **kwargs – Keyword arguments passed to serial.serial_for_url

address
baudrate = 115200
line_termination = b'\n'
connection
keep_alive = True
delays
operation() collections.abc.Iterator[None][source]

Context manager for handling serial connection operations.

Ensures the serial connection is open before performing operations and closes it afterward if self.keep_alive is False.

Yields:

Yields control back to the caller for performing operations within the context.

wait(operation: str) None[source]

Introduce a (synchronous) delay based on the specified operation type.

Parameters:

operation – The operation type. For possible types see SERIAL_DELAYS_DEFAULT.

read() str | None[source]

Read all available data from the serial connection and decodes it into a string.

Returns:

The decoded data as string, or None if an error occurs.

read_line() str | None[source]

Read a single line from the serial connection.

Returns:

The decoded line as string, or None if an error occurs.

write(message: str, flush: bool = True, read_echo: bool = False, read_line: bool = False) None | str[source]

Write a message to the serial connection.

Parameters:
  • message – The message to be written to the serial connection.

  • flush – If True, flushes the written data immediately. Defaults to True.

  • read_echo – If True, reads back the echo after writing. Defaults to False.

  • read_line – If True, data is read until self.line_termination occurs in the data. Otherwise all available data is read.

Returns:

If read_echo is True, returns the echo read from the connection as string; otherwise returns None.