mltk.utils.serial_reader.SerialReader

class SerialReader[source]

Utility for reading data from a serial COM port

NOTE: For the regex args below, if a string is provided then it will automatically be converted to an re.Pattern with the flags re.MULTILINE|re.DOTALL|re.IGNORECASE

Parameters:
  • port (str) – Name of serial COM port, if starts with “regex:” then try to find a matching port by listing all ports

  • baud – Baud rate

  • outfile (TextIO) – File-like object to write received serial data, use sys.stdout if omitted

  • mode – outfile mode, if mode=’r’ then write as ASCII (ignore char > 127 and r), if mode=’rb’ then write as binary

  • start_regex (Union[str, Pattern, List[Pattern]]) – Regex or list of Regex to use match against received serial data before writing to the captured_data buffer during read()

  • stop_regex (Union[str, Pattern, List[Pattern]]) – Regex or list of Regex to use match against received serial data to stop writing to the captured_data buffer and read() returns

  • fail_regex (Union[str, Pattern, List[Pattern]]) – Regex or list of Regex to use match against received serial data to abort read()

  • callback_regex (List[Tuple[Pattern, Callable[[Match], None]]]) – (Regex,Callback) or list of (Regex,Callback) which will invoke the given callback with the re.Match for each regex

See the source code on Github: mltk/utils/serial_reader.py

Properties

captured_data

Data received by read() between the start_regex and stop_regex conditions

error_message

Data received after fail_regex was found

failed

Return if the fail_regex condition has been found

is_open

Return if the serial connection is opened

started

Return if the start_regex condition has been found

stopped

Return if the stop_regex condition has been found

Methods

__init__

close

Close the serial COM port

flush

Flush any received data

list_ports

Retrun a list of COM ports

open

Open the a connection to the serial port

read

Read data for the given timeout or until stop_regex or fail_regex have been found in the received data.

resolve_port

List the COM ports and try to find the given port in the list

write

Write the given amount of data

__init__(port, baud=115200, outfile=None, mode='r', start_regex=None, stop_regex=None, fail_regex=None, callback_regex=None, ignore_chars=None)[source]
Parameters:
  • port (str) –

  • outfile (TextIO) –

  • start_regex (Union[str, Pattern, List[Pattern]]) –

  • stop_regex (Union[str, Pattern, List[Pattern]]) –

  • fail_regex (Union[str, Pattern, List[Pattern]]) –

  • callback_regex (List[Tuple[Pattern, Callable[[Match], None]]]) –

  • ignore_chars (List[int]) –

property is_open: bool

Return if the serial connection is opened

Return type:

bool

property started: bool

Return if the start_regex condition has been found

Return type:

bool

property stopped: bool

Return if the stop_regex condition has been found

Return type:

bool

property failed: bool

Return if the fail_regex condition has been found

Return type:

bool

property captured_data: str

Data received by read() between the start_regex and stop_regex conditions

Return type:

str

property error_message: str

Data received after fail_regex was found

Return type:

str

static list_ports()[source]

Retrun a list of COM ports

Return type:

List[Dict[str, str]]

static resolve_port(port)[source]

List the COM ports and try to find the given port in the list

Return type:

str

Parameters:

port (str) –

open()[source]

Open the a connection to the serial port

close()[source]

Close the serial COM port

flush(timeout=None)[source]

Flush any received data

Parameters:

timeout (float) –

read(timeout=None, activity_timeout=None, abort_event=None)[source]

Read data for the given timeout or until stop_regex or fail_regex have been found in the received data.

The captured_data property will contain the received data between the start_regex and stop_regex conditions. The error_message property will contain any data received after the fail_regex conditions.

NOTE: The outfile will contain ALL received data, regardless of the start_regex/stop_regex conditions

Parameters:
  • timeout (float) – Maximum time in seconds to receive serial data. If None then read until stop_regex/fail_regex found.

  • activity_timeout (float) – Maximum amount of time to wait without any new data received from the serial point

  • abort_event (Event) –

Return type:

bool

Returns:

True if the stop_regex or fail_regex were found in the received data. False on on timeout.

write(data, check_fail_condition=True, check_start_condition=True, check_stop_condition=True, delay_per_char=None)[source]

Write the given amount of data

Parameters:
  • data (str) –

  • delay_per_char (float) –