serial_io¶
Basic threaded serial command/response handling, avoids blocking the main thread with I/O operations during operation.
- class plateflo.serial_io.CmdExecThread(master)¶
Command queue monitoring thread.
Executes commands from queue and queues the response.
- Parameters:
master (SerialDevice) – The SerialDevice instance over which to send/recieve serial commands.
- execution_loop()¶
Sends commands to the Serial device and listens for a defined response.
Command queue objects are dicts containing the string command and EITHER a command length or command EOL character.
Examples
{cmd: “1H”, respLen: 1} # A single character response
{cmd: “1#”, respEOL: ‘n’} # Multiple chars, LF-terminated
- is_running()¶
execution_loop run state.
- Returns:
Running
- Return type:
bool
- run()¶
Start execution_loop thread
- stop()¶
Stop/join the execution_loop thread.
- class plateflo.serial_io.SerialDevice(port, baud=9600, timeout=0.2)¶
Handles writing of commands and reading back of responses.
- Parameters:
port (str) – Serial device port name. E.g. “COM4”.
baud (int, default=9600) – Serial device baud rate
timeout (float, default=0.2) – Time (seconds) to allow device to respond before commands timeout/fail.
- port¶
Serial device port name.
- Type:
str
- baud¶
Serial device baud rate.
- Type:
int
- timeout¶
Time (seconds) to allow device to respond before commands timeout/fail.
- Type:
float
- isOpen¶
Serial device connection open.
- Type:
bool
- close()¶
Close the device serial port
- open()¶
Open the device serial port
- write_cmd(cmd, rsp_len=None, EOL=None)¶
Send command to serial device, expect either a defined response length (rsp_len) –OR– a terminating character (EOL).
- Parameters:
cmd (str) – String to send to serial device.
rsp_len (int) – Number of characters to expect in response.
EOL (str) – Expected response terminating character (E.g. LF or CR)
- Returns:
Device response string. Empty string in case of response timeout.
- Return type:
str
- Raises:
ValueError – if both rsp_len and EOL parameters are provided, or if neither is provided
- plateflo.serial_io.list_ports()¶
List available system serial ports.
- Returns:
Serial port names
- Return type:
list