Example 12: UART serial communication
About 3 min
Example 12: UART serial communication
Synchronized Device Documentation
This page corresponds to the synchronized Chinese source. Commands, JSON examples, API paths, field names, and screenshots are kept aligned with the Chinese device-side source documentation.
What This Page Covers
- Overview
- Wiring
- JSON Configuration Example
- PeriphExec Linkage
- Notes
Source Reference
The detailed operational source is preserved below so implementation details stay exact while the English navigation, titles, and reading path remain available.
示例12:UART串口通信
实验概述
通过 UART(通用异步收发器)实现 ESP32 与外部设备的串口通信。ESP32 拥有 3 个 UART 端口,FastBee 支持配置串口用于调试输出、Modbus 通信或自定义数据交换。
硬件接线
| UART端口 | TX引脚 | RX引脚 | 用途 |
|---|---|---|---|
| UART0 | GPIO1 | GPIO3 | USB 调试输出 |
| UART1 | GPIO16 | GPIO17 | Modbus/外部设备 |
| UART2 | GPIO18 | GPIO19 | 扩展串口 |
UART0 默认用于 USB 调试输出,UART1/2 可用于连接外部设备。
JSON 配置示例
{
"peripherals": [
{
"id": "uart0",
"name": "串口0-调试",
"type": 1,
"enabled": false,
"pins": [1, 3],
"params": {
"baudRate": 115200,
"dataBits": 8,
"stopBits": 1,
"parity": 0
}
},
{
"id": "uart1",
"name": "串口1-Modbus",
"type": 1,
"enabled": false,
"pins": [16, 17],
"params": {
"baudRate": 9600,
"dataBits": 8,
"stopBits": 1,
"parity": 0
}
}
]
}参数说明
| 参数 | 说明 | 可选值 |
|---|---|---|
| baudRate | 波特率 | 9600, 19200, 38400, 57600, 115200 |
| dataBits | 数据位 | 7, 8 |
| stopBits | 停止位 | 1, 2 |
| parity | 校验位 | 0=无, 1=奇, 2=偶 |
外设执行联动
场景:串口数据发送(平台触发)
功能:通过云平台下发指令,通过UART1发送数据到外部设备
Web界面配置步骤
步骤1:创建规则
- 点击左侧菜单 外设配置 → 切换到 外设执行管理 标签
- 点击 新增规则 按钮
- 填写基础配置:
- 规则名称:
串口发送数据 - 上报数据:✅ 启用
- 启用:✅ 启用
- 规则名称:
步骤2:配置触发器
点击 添加触发 按钮
填写触发器配置:
字段 填写内容 说明 触发类型 选择 平台触发 接收云平台指令 操作符 选择 设置模式 接收数据作为动作参数
步骤3:配置动作
点击 添加动作 按钮
填写:
- 动作类型:选择 串口发送
- 目标外设:选择
uart1
点击 保存 按钮
测试方法:
通过云平台发送任意字符串,例如:Hello UART 数据将通过UART1的TX引脚(GPIO16)发送出去
UART参数说明
| 参数 | 说明 | 可选值 |
|---|---|---|
| 波特率 | 通信速率 | 9600, 19200, 38400, 57600, 115200 |
| 数据位 | 数据位数 | 7, 8 |
| 停止位 | 停止位数 | 1, 2 |
| 校验位 | 校验方式 | 无, 奇校验, 偶校验 |
注意事项
- UART0 占用:UART0(GPIO1/3)用于 USB 调试,不建议重新分配
- 引脚映射:ESP32 的 UART TX/RX 可映射到任意 GPIO(除 GPIO6-11)
- 电压匹配:ESP32 为 3.3V 电平,连接 5V 设备需电平转换
- Modbus 配置:用于 Modbus 时波特率通常为 9600,需与从站设备匹配
- 缓冲区:UART 接收缓冲区默认 256 字节,大数据量需及时读取
- ESP32-S3:S3 的 UART0 通过 USB CDC 输出,物理 UART 从 UART1 开始
