1 Star 0 Fork 12

豳草/upycraft_cn

forked from DFRobot/upycraft_cn 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
3.2.6 UART.md 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
gboldwang 提交于 2018-04-26 11:16 . first commit

   UART执行标准UART/USART双工串行通信协议。物理层上需要两根线: RX和TX。通信数据的数据格式一般为字符格式。

class machine.UART(id, baudrate, bits, parity, rx, tx, stop, timeout)

id:串口号
	1、2
baudrate:波特率
bits:每个字符的位数
parity:奇偶校验
	0 — 偶数
    1 — 奇数
rx,tx:UART读,写引脚
	Pin(0)、Pin(2)、Pin(4)、Pin(5)、Pin(9)、Pin(10)、Pin(12~19)、Pin(21~23)、Pin(25)、Pin(26)、Pin(34~36)、Pin(39)
    注:Pin(1)、Pin(3)被占用,一般不建议作tx,rx使用
stop:停止位数量
	1、2
timeout:超时时间(单位:毫秒)
	0 < timeout ≤ 0x7FFF FFFF (十进制:0 < timeout ≤ 2147483647)

定义UART

示例:

from machine import UART

uart = UART(2, baudrate=115200, bits=8, parity=0, rx=9, tx=27, stop=1, timeout=10)

类函数

1. UART.init(baudrate, bits, parity, stop, tx, rx, rts, cts))

函数说明:初始化串口。

2. UART.read(nbytes)

函数说明:读取nbytes个字节。

3. UART.read()

函数说明:读取数据。

4. UART.write(buf)

函数说明: 将字节缓冲区写入UART总线。

5. UART.readline()

函数说明:读一行数据,以换行符结束。

6. UART.readinto(buf)

函数说明:读取并写数据到缓冲区。

7. UART.readinto(buf, nbytes)

函数说明:读取并写入数据到缓冲区。

nbytes:读取的字节数

8. UART.any()

函数说明:判断串口是否有数据。有则返回字节数,否则返回0。

综合示例

发送端示例

from machine import UART
import time
u = UART(2, baudrate=115200, bits=8, parity=0, rx=9, tx=27, timeout=10) 
try:
  while True:
    u.write("ss")
    time.sleep(0.2)  
except:
  pass

接收端示例

from machine import UART
import time
u = UART(2, baudrate=115200, bits=8, parity=0, rx=9, tx=27, timeout = 10)
try:
  while True:
    if(u.any()):
      print(u.read())
except:
  pass

演示效果

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/meihaodianzi/upycraft_cn.git
git@gitee.com:meihaodianzi/upycraft_cn.git
meihaodianzi
upycraft_cn
upycraft_cn
master

搜索帮助

Cb406eda 1850385 E526c682 1850385