UART相关的API可以在MaixPy官方文档里查询
一、发送数据
对于下载的模型可以采用如下方法添加串口:
1.在文件开头加上
from machine import UART
from fpioa_manager import fm
def UART_init(bound):
fm.register(10,fm.fpioa.UART1_TX,force=True)
fm.register(11,fm.fpioa.UART1_RX,force=True)#定于引脚10和11为串口收发引脚
uart_A=UART(UART.UART1,bound,8,0,0,timeout=1000,read_buf_len=4096)
2.在main里加入
UART_init(115200)
参数为串口的波特率
3.在img.draw_string的下一行加上
data=bytearray([0x2c,labels[obj.classid()],0x5b])
uart_A.write(data)
其中0x2c和0x5b为自定义的帧头和帧尾
STM32端的接收函数:
void K210_receive(u8 com_data)
{
static u8 receive_buff[12] = {0};
static u8 receive_num = 0;
receive_buff[receive_num++] = com_data;
if (receive_buff[0] != 0X2C)
receive_num = 0; //
if (Receiving_agreement(receive_buff, receive_num) && receive_num >= 4)
{
K210_data = receive_buff[1];
receive_num = 0;
}
}
二、K210接收数据
可以通过在主函数里加入以下代码实现:
read_data = uart.read(3)#参数为接受的数据长度
if read_data:#如果接收到数据
read_data.decode('utf-8')#解码
if read_data[0] == 0x2c and read_data[2] == 0x5b:#帧头帧尾
recFlag = read_data[1]