用UART应用实例说明MicroPython-ESP32串口通信方式

UART构造器

不同于其他MicroPython开发板,ESP32可以自定义管脚作为UART, 不过ESP32自身只有两个UART资源。

导入UART 模块

from machine import UART

UART对象的构造器函数:

UART(id, baudrate, bits, parity, rx, tx, stop, timeout)
id : 串口编号

ESP32的UART资源只有两个, id有效取值范围为1,2

bandrate: 波特率(时钟频率)

常用波特率为:
○ 9600 (默认)
○ 115200
信息接受双方的波特率必须一致,否则会乱码。

bits:单个字节的位数(比特数)

○ 8 (默认)
○ 7
○ 9

parity: 校验方式

○ None 不进行校验(默认)
○ 0 偶校验
○ 1 奇校验

rx:接收口的GPIO编号

在ESP32上面很多个管脚都可以自定义为UART管脚有效GPIO编号如下:

0,2,4,5,9,10, 12-19, 21-23,25, 2634-36, 39
tx:发送口的GPIO编号

有效GPIO管脚编号同rx

stop: 停止位个数

○ 1 (默认)
○ 2

timerout: 超时时间

取值范围: 0 < timeout ≤ 2147483647

from machine import UART
uart = UART(2, baudrate=115200, rx=13,tx=12,timeout=10)

uart.read(10)        # read 10 characters, returns a bytes object
                    # 读入10个字符, 返回一个比特对象
uart.read()            # read all available characters
                    # 读取所有的有效字符
uart.readline()        # read a line
                    # 读入一行
uart.readinto(buf)  # read and store into the given buffer
                    # 读入并且保存在缓存中
uart.write('abc')    # write the 3 characters
                    # 向串口写入3个字符abc
uart.readchar()     # read 1 character and returns it as an integer
                    # 读入一个字符
uart.writechar(42)  # write 1 character
                    # 写入ASCALL码为42的字符
uart.writechar(ord('*')) # 等同于uart.writechar(42)

#检测串口是否有数据
uart.any()          # returns the number of characters waiting

ESP32串口自发自收实验
接线 将开发板的 13号引脚与12号引脚用杜邦线相连接

'''
ESP32串口通信-字符串数据自发实验

接线 将开发板的 13号引脚与12号引脚用杜邦线相连接。

'''
from machine import UART,Pin
import utime

# 初始化一个UART对象
uart = UART(2, baudrate=115200, rx=13,tx=12,timeout=10)

count = 1

while True:
    print('\n\n===============CNT {}==============='.format(count))

    # 发送一条消息
    print('Send: {}'.format('hello {}\n'.format(count)))
    print('Send Byte :') # 发送字节数
    uart.write('hello {}\n'.format(count))
    # 等待1s钟
    utime.sleep_ms(1000)

    if uart.any():
        # 如果有数据 读入一行数据返回数据为字节类型
        # 例如  b'hello 1\n'
        bin_data = uart.readline()
        # 将手到的信息打印在终端
        print('Echo Byte: {}'.format(bin_data))

        # 将字节数据转换为字符串 字节默认为UTF-8编码
        print('Echo String: {}'.format(bin_data.decode()))
    # 计数器+1
    count += 1
    print('---------------------------------------')

输出

===============CNT 1===============
Send: hello 1

Send Byte :
8
Echo Byte: b'hello 1\n'
Echo String: hello 1

---------------------------------------


===============CNT 2===============
Send: hello 2

Send Byte :
8
Echo Byte: b'hello 2\n'
Echo String: hello 2

---------------------------------------


===============CNT 3===============
Send: hello 3

Send Byte :
8
Echo Byte: b'hello 3\n'
Echo String: hello 3

---------------------------------------

转自:https://www.cirmall.com/bbs/thread-102657-1-1.html

  • 7
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!我可以为您解答关于MicroPython ESP32和STM32之间串口通信的问题。 要实现ESP32和STM32的串口通信,您可以按照以下步骤进行操作: 1. 首先,确保您的ESP32和STM32设备上都已经安装了MicroPython固件。 2. 在ESP32上,您可以使用UART模块来设置串口通信。首先,导入UART模块: ```python from machine import UART ``` 3. 然后,通过实例UART类来创建一个串口对象。您需要指定串口的引脚和波特率等参数。例如,假设您使用的是ESP32UART2引脚和波特率为9600: ```python uart = UART(2, baudrate=9600) ``` 4. 接下来,您可以使用串口对象的`write()`方法向STM32发送数据: ```python uart.write('Hello STM32!') ``` 5. 在STM32上,您需要编写相应的代码来接收ESP32发送的数据。具体的代码取决于您使用的STM32开发板和编程环境。一般来说,您可以使用STM32的UART库来配置和接收数据。例如,使用STM32Cube HAL库: ```c #include "stm32f4xx_hal.h" UART_HandleTypeDef huart2; void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { // 处理接收到的数据 // ... } int main(void) { // 初始化STM32串口 HAL_UART_MspInit(&huart2); // 开启接收中断 HAL_UART_Receive_IT(&huart2, rx_data, 1); while (1) { // 主循环代码 // ... } } ``` 请注意,上述代码仅为示例,具体的实现细节可能因您的硬件和需求而异。 希望这些信息对您有所帮助!如果您还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值