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, write_line_termination: bytes = b'', read_line_termination: bytes = b'\n', keep_alive: bool = True, delays: dict | None = None, timeout: float = 0.1, **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.
- Parameters:
address – The address of the serial socket, something like /dev/ttyUSB0.
baudrate – The baud rate for the serial communication.
read_line_termination – character that terminates a line when reading from the device.
write_line_termination – character that terminates a line when wrtiting to the device.
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_DEFAULTsets the default delays.**kwargs – Keyword arguments passed to
serial.serial_for_url
- address¶
- baudrate = 115200¶
- read_line_termination = b'\n'¶
- write_line_termination = b''¶
- 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_aliveis 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.
The self.write_line_termination is automatically appended to the message if it is not already present.
- 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.read_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.