import uart
The uart module exposes serial communication from firmware. Use it to talk to external controllers, sensors, modems, and protocol adapters over TX/RX pins.
Quick example
import uart
uart.begin(9600, 17, 16)
uart.write("AT
")
uart.on_data("handle_uart")
importuart
UART native Berry module.
The uart module exposes serial communication from firmware. Use it to talk to external controllers, sensors, modems, and protocol adapters over TX/RX pins.
API
5 available
uart.begin(baud: int, tx: any, rx: any) -> any
uart.begin(baud: int, tx: any, rx: any) -> anyInitialize UART pins and baud rate.
Parameters
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
baud |
int |
positional or keyword | Yes | Serial baud rate. |
tx |
any |
positional or keyword | Yes | GPIO used for transmit. |
rx |
any |
positional or keyword | Yes | GPIO used for receive. |
Returns
any Native initialization result.
uart.write(data: any) -> any
uart.write(data: any) -> anyWrite text or bytes to UART.
Parameters
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
data |
any |
positional or keyword | Yes | String or byte payload to send. |
Returns
any Native write result, if provided.
uart.on_data(callback: callback) -> str
uart.on_data(callback: callback) -> strRegister a callback for received UART data.
Parameters
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
callback |
callback |
positional or keyword | Yes | Function name or callable accepted by firmware. |
Returns
str Native registration result.
uart.off_data() -> any
uart.off_data() -> anyRemove the UART receive callback.
Returns
any Native removal result.
uart.stats() -> dict
uart.stats() -> dictReturn UART traffic counters.
Returns
dict Native map-like value with RX and TX counts.