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 theflags 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 portsbaud – Baud rate
outfile (
TextIO
) – File-like object to write received serial data, use sys.stdout if omittedmode – 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() returnsfail_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
Data received by read() between the start_regex and stop_regex conditions
Data received after fail_regex was found
Return if the fail_regex condition has been found
Return if the serial connection is opened
Return if the start_regex condition has been found
Return if the stop_regex condition has been found
Methods
Close the serial COM port
Flush any received data
Retrun a list of COM ports
Open the a connection to the serial port
Read data for the given timeout or until stop_regex or fail_regex have been found in the received data.
List the COM ports and try to find the given port in the list
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 resolve_port(port)[source]¶
List the COM ports and try to find the given port in the list
- Return type:
str
- Parameters:
port (str) –
- 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 pointabort_event (Event) –
- Return type:
bool
- Returns:
True if the stop_regex or fail_regex were found in the received data. False on on timeout.