maix.peripheral.uart

maix uart peripheral driver

You can use maix.peripheral.uart to access this module with MaixPy
This module is generated from MaixPy and MaixCDK

Module

No module

Enum

PARITY

uart parity enum

item describe
values PARITY_NONE: no parity
PARITY_ODD: odd parity
PARITY_EVEN: even parity
PARITY_MAX:

C++ defination code:

enum PARITY
    {
        PARITY_NONE = 0x00,  // no parity
        PARITY_ODD  = 0x01,  // odd parity
        PARITY_EVEN = 0x02,  // even parity
        PARITY_MAX
    }

STOP

uart stop bits

item describe
values STOP_1: 1 stop bit
STOP_2: 2 stop bits
STOP_1_5: 1.5 stop bits
STOP_MAX:

C++ defination code:

enum STOP
    {
        STOP_1   = 0x01,  // 1 stop bit
        STOP_2   = 0x02,  // 2 stop bits
        STOP_1_5 = 0x03,  // 1.5 stop bits
        STOP_MAX
    }

BITS

uart stop bits

item describe
values BITS_5: 5 data bits
BITS_6: 6 data bits
BITS_7: 7 data bits
BITS_8: 8 data bits
BITS_MAX:

C++ defination code:

enum BITS
    {
        BITS_5 = 5,  // 5 data bits
        BITS_6 = 6,  // 6 data bits
        BITS_7 = 7,  // 7 data bits
        BITS_8 = 8,  // 8 data bits
        BITS_MAX
    }

FLOW_CTRL

uart flow control

item describe
values FLOW_CTRL_NONE: no flow control
FLOW_CTRL_HW: hardware flow control
FLOW_CTRL_MAX:

C++ defination code:

enum FLOW_CTRL
    {
        FLOW_CTRL_NONE = 0,  // no flow control
        FLOW_CTRL_HW   = 1,  // hardware flow control
        FLOW_CTRL_MAX
    }

Variable

Function

list_devices

def list_devices() -> list[str]

Get supported uart ports.

item description
return uart ports list, string type.

C++ defination code:

std::vector<std::string> list_devices()

Class

UART

maix uart peripheral driver

C++ defination code:

class UART : public comm::CommBase

__init__

def __init__(self, port: str = '', baudrate: int = 115200, databits: BITS = ..., parity: PARITY = ..., stopbits: STOP = ..., flow_ctrl: FLOW_CTRL = ...) -> None

UART constructor. You need to call open() to open the device.

item description
type func
param port: uart port. string type, can get it by uart.list_devices().
If empty, will not open device in constructor, default empty.
if not empty, will auto open device in constructor, open fail will throw err.Exception.
baudrate: baudrate of uart. int type, default 115200.
databits: databits, values @see uart.DATA_BITS
parity: parity, values @see uart.PARITY
stopbits: stopbits, values @see uart.STOP_BITS
flow_control: flow_control, values @see uart.FLOW_CTRL
static False

C++ defination code:

UART(const std::string &port = "", int baudrate = 115200, uart::BITS databits = uart::BITS_8,
            uart::PARITY parity = uart::PARITY_NONE, uart::STOP stopbits = uart::STOP_1,
            uart::FLOW_CTRL flow_ctrl = uart::FLOW_CTRL_NONE)

set_port

def set_port(self, port: str) -> maix.err.Err

Set port

item description
type func
param port: uart port. string type, can get it by uart.list_devices().
return set port error code, err.Err type.
static False

C++ defination code:

err::Err set_port(const std::string &port)

get_port

def get_port(self) -> str

Get port

item description
type func
return uart port, string type.
static False

C++ defination code:

std::string get_port()

set_baudrate

def set_baudrate(self, baudrate: int) -> maix.err.Err

Set baud rate

item description
type func
param baudrate: baudrate of uart. int type, default 115200.
return set baud rate error code, err.Err type.
static False

C++ defination code:

err::Err set_baudrate(int baudrate)

get_baudrate

def get_baudrate(self) -> int

Get baud rate

item description
type func
return baud rate, int type.
static False

C++ defination code:

int get_baudrate()

open

def open(self) -> maix.err.Err

Open uart device, before open, port must be set in constructor or by set_port().\nIf already opened, do nothing and return err.ERR_NONE.

item description
type func
return open device error code, err.Err type.
static False

C++ defination code:

err::Err open()

is_open

def is_open(self) -> bool

Check if device is opened.

item description
type func
return true if opened, false if not opened.
static False

C++ defination code:

bool is_open()

close

def close(self) -> maix.err.Err

Close uart device, if already closed, do nothing and return err.ERR_NONE.

item description
type func
return close device error code, err.Err type.
static False

C++ defination code:

err::Err close()

set_received_callback

def set_received_callback(self, callback: typing.Callable[[UART, maix.Bytes(bytes)], None]) -> None

Set received callback function

item description
type func
param callback: function to call when received data
static False

C++ defination code:

void set_received_callback(std::function<void(uart::UART&, Bytes&)> callback)

write_str

def write_str(self, str: str) -> int

Send string data

item description
type func
param str: string data
return sent data length, < 0 means error, value is -err.Err.
static False

C++ defination code:

int write_str(const std::string &str)

write

def write(self, data: maix.Bytes(bytes)) -> int

Send data to uart

item description
type func
param data: direction [in], data to send, bytes type. If you want to send str type, use str.encode() to convert.
return sent length, int type, if < 0 means error, value is -err.Err.
static False

C++ defination code:

int write(Bytes &data)

available

def available(self, timeout: int = 0) -> int

Check if data available or wait data available.

item description
type func
param timeout: unit ms, timeout to wait data, default 0.
0 means check data available and return immediately,
> 0 means wait until data available or timeout.
- 1 means wait until data available.
return available data number, 0 if timeout or no data, <0 if error, value is -err.Err, can be err::ERR_IO, err::ERR_CANCEL, err::ERR_NOT_OPEN.
throw err.Exception if fatal error.
static False

C++ defination code:

int available(int timeout = 0)

read

def read(*args, **kwargs)

Recv data from uart

item description
type func
param len: max data length want to receive, default -1.
-1 means read data in uart receive buffer.
>0 means read len data want to receive.
other values is invalid.
timeout: unit ms, timeout to receive data, default 0.
0 means read data in uart receive buffer and return immediately,
-1 means block until read len data,
>0 means block until read len data or timeout.
return received data, bytes type.
Attention, you need to delete the returned object yourself in C++.
throw Read failed will raise err.Exception error.
static False

C++ defination code:

Bytes *read(int len = -1, int timeout = 0)

readline

def readline(*args, **kwargs)

Read line from uart, that is read until '\n' or '\r\n'.

item description
type func
param timeout: unit ms, timeout to receive data, default -1 means block until read '\n' or '\r\n'.
> 0 means block until read '\n' or '\r\n' or timeout.
return received data, bytes type. If timeout will return the current received data despite not read '\n' or '\r\n'.
e.g. If we want to read b'123\n', but when we only read b'12', timeout, then return b'12'.
static False

C++ defination code:

Bytes *readline(int timeout = -1)