[uPyCraft IDE]ESP32 3.2.6 UART

2020-7-7 14:00:01 [显示全部楼层]
2246浏览
查看: 2246|回复: 1

[uPyCraft IDE] ESP32 3.2.6 UART

[复制链接]

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

演示效果

ESP32 3.2.6 UART图1

BG6IB  学徒

发表于 2020-11-13 20:07:26

这个函数好,可以随意使用任何一个引脚,板子打错最好的选择
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail