pyserial support for serial communication with your DUT.
Import
Methods
| Method | Description |
|---|---|
Serial() | Create serial connection |
readline() | Read a line |
read() | Read specified bytes |
read_until() | Read until delimiter |
write() | Write data |
open() | Open connection |
close() | Close connection |
is_open | Check connection state |
Method Reference
serial.Serial(port, baudrate, **kwargs)
Create a serial connection.
| Parameter | Type | Description |
|---|---|---|
port | str | Serial port path (e.g., /dev/ttyUSB1) |
baudrate | int | Baud rate in bits per second |
timeout | float | Read timeout in seconds |
bytesize | int | Data bits (5, 6, 7, or 8) |
parity | str | Parity (PARITY_NONE, PARITY_EVEN, PARITY_ODD) |
stopbits | int | Stop bits (1, 1.5, or 2) |
Serial object
readline()
Read a line from the serial port.
bytes - The received line
read(size)
Read a specified number of bytes.
| Parameter | Type | Description |
|---|---|---|
size | int | Number of bytes to read |
bytes - The received data
read_until(expected=b'\n', size=None)
Read until expected sequence is found.
| Parameter | Type | Description |
|---|---|---|
expected | bytes | Sequence to read until |
size | int | Maximum bytes to read |
bytes - Data up to expected sequence
write(data)
Write data to the serial port.
| Parameter | Type | Description |
|---|---|---|
data | bytes | Data to write |
int - Number of bytes written
close()
Close the serial connection.
is_open
Check if connection is open.
bool - True if connection is open
Examples
Basic Communication
Interactive Session
Data Logging
Command/Response Pattern
Hardware Integration
| Connection | Description |
|---|---|
| Raw UART | Direct TX/RX line connections |
| USB CDC | Virtual serial over USB |
| Flow Control | Hardware (RTS/CTS) and software (XON/XOFF) |
Notes
- Lager supports native
pyserialfor serial communication - Serial ports are designated during Lager setup configuration
- Supports both raw UART and USB CDC connections
- Default baud rate is 115200
- Always handle
SerialExceptionerrors appropriately - Use
decode('utf-8')for string conversion

