Serial interface core

serial_toolbox.interface_core

class serial_toolbox.interface_core.serial_interface(serial_port, terminal: bool = True, max_queue_size: int = 100, format: str = 'STR', logger: Logger = None)

Class for continuously reading from a serial port in a separate thread.

serial_port

Instance of the serial port to read from.

Type:

serial.Serial

thread

Thread used to continuously read from the serial port.

Type:

threading.Thread

stop_flag

Flag used to stop the thread.

Type:

bool

data_queue

Thread-safe queue to store incoming data.

Type:

queue.Queue

terminal

If True, print incoming data to console.

Type:

bool

data_index

A counter for received data.

Type:

int

max_queue_size

Maximum size for the data_queue.

Type:

int

print_queue(restore_queue: bool = False)
process_data(data)

Processes incoming data by adding the data to the data_queue.

Parameters:

data (str) – The data read from the serial port.

read_from_port()

Continuously reads data from the serial port until stop_flag is set to True.

Parameters:

serial_port (serial.Serial) – Instance of the serial port to read from.

write_to_port(data_str)

Writes data to the serial port.

Parameters:

data (str) – The data to write to the serial port.

serial_toolbox.interface_core.serial_monitor_cli(interactive: bool = True)

Command-line interface for serial port monitor.

Prompts the user to select a port, then starts the SerialReader on that port. Reading operation can be stopped by a KeyboardInterrupt (Ctrl+C).

The function creates and manages one thread for reading.

The reading thread continuously reads data from the serial port and prints to the console until KeyboardInterrupt is caught.

The thread is closed properly by setting its stop_flag to True and waiting for the thread to finish execution before returning from the function.