openlifu.io.LIFUUart.LIFUUart

class openlifu.io.LIFUUart.LIFUUart(vid, pid, baudrate=921600, timeout=10, align=0, desc='VCP', demo_mode=False, async_mode=False)[source]

Bases: object

__init__(vid, pid, baudrate=921600, timeout=10, align=0, desc='VCP', demo_mode=False, async_mode=False)[source]

Initialize the UART instance.

Parameters:
  • vid (int) – Vendor ID of the USB device.

  • pid (int) – Product ID of the USB device.

  • baudrate (int) – Communication speed.

  • timeout (int) – Read timeout in seconds.

  • align (int) – Data alignment parameter.

  • desc (str) – Descriptor for the device (e.g. “TX” or “HV”).

  • demo_mode (bool) – If True, simulate the connection.

  • async_mode (bool) – If True, use asynchronous mode.

_read_data(timeout=20)[source]

Read data from the serial port in a separate thread.

_tx(data: bytes)[source]

Send data over UART.

add_demo_response(response: bytes)[source]

Add a predefined response for demo mode.

check_usb_status()[source]

Check if the USB device is connected or disconnected.

clear_buffer()[source]

Clear the read buffer.

connect()[source]

Open the serial port.

disconnect()[source]

Close the serial port.

is_connected() bool[source]

Check if the device is connected.

Returns:

True if connected, False otherwise.

Return type:

bool

list_vcp_with_vid_pid()[source]

Find the USB device by VID and PID.

async monitor_usb_status(interval=1)[source]

Periodically check for USB device connection.

print()[source]

Print the current UART configuration.

read_packet(timeout=20) UartPacket[source]

Read a packet from the UART interface.

Returns:

Parsed packet or an error packet if parsing fails.

Return type:

UartPacket

reopen_after_reset(retries=5, delay=1.0)[source]

Attempt to reopen the serial port after a device reset or disconnection. Returns True if reconnected, False otherwise.

run_coroutine(coro)[source]

Run a coroutine using the internal event loop.

send_packet(id=None, packetType=224, command=14, addr=0, reserved=0, data=None, timeout=20)[source]

Send a packet over UART and, if not running, return a response packet.

start_monitoring(interval=1)[source]

Start the periodic USB device connection check.

stop_monitoring()[source]

Stop the periodic USB device connection check.