UART Stream¶
This allows for streaming binary data between a Python script and embedded device via UART.
Features:
Asynchronous reception of binary data
Data flow control
C++ library (see mltk repo/cpp/shared/uart_stream)
Send/receive “commands”
NOTE: The embedded device must be running the uart_stream C++ library for this Python package to work.
Example Usage¶
The following is a snippet taken from alexa_demo.py
import io
from mltk.utils.uart_stream import UartStream
with UartStream() as uart:
data_buffer = io.BytesIO()
while True:
cmd = uart.read_command()
if cmd.code == 1:
print(f'Command received: {data_buffer.tell()} bytes')
uart.flush_input()
data_buffer.seek(0)
return data_buffer
data = uart.read()
if not data:
uart.wait(0.100)
continue
if data_buffer.getbuffer().nbytes == 0:
print('Receiving command ...')
data_buffer.write(data)
Also see UART Stream Data Test
API Reference¶
Allows for streaming binary data between a Python script and embedded device via UART |