K210串口通信

一、串口配置

from machine import UART    #导入UART模块
fm.register(9, fm.fpioa.UART1_TX, force=True) #9为tx接rx
fm.register(10, fm.fpioa.UART1_RX, force=True) #10为rx接tx
uart_A = UART(UART.UART1, 9600, 8, 1, 0, timeout=1000, read_buf_len=4096)

基本是就是配置串口的引脚和串口参数的设置。

二、串口发送数据

串口发送数据调用uart_A.write()即可。

三、串口接收数据

串口接收数据调用uart_A.read()即可。注意在mxai py 中接收的字节流数据,需将字节流转为字符串并且还要去掉结束符才能使用接收的数据(如果利用数据判断需这样操作)

if uart_A.any():
    while uart_A.any():
        read_data = uart_A.read()
        read_data = read_data.decode('utf-8','ignore')  # 转为字符串
            if read_data[0] == 'b':                     # 切片
               .....
               read_data = ''

猜你喜欢

转载自blog.csdn.net/qq_53144843/article/details/124363237
今日推荐