Sensors and I/O

UART Echo

Starting point for meters, PLCs and modules. RS485 is only a level shift.

Sensors and I/O UART to a meter ESP32 family

Official path: examples/peripherals/uart/uart_echo

01 Overview

UART Echo is the starting point for serial debugging: read a byte, write it back. It doesn't implement any business protocol, only proves pins, baud, and wiring.

Use it when bringing up a second UART, connecting to meters, PLCs, or wireless modules. Don't rush to write protocol stacks before confirming the physical link.

Skip it when the protocol is already stable; add Modbus or custom frame parsing on top of echo, not permanent echo.

Hardware-wise, TTL to TTL is a direct cross connection; RS485 requires a transceiver and termination resistor. Common ground is mandatory.

Software-wise, ESP-IDF's UART driver provides event loops and buffer management for async RX/TX.

This is a public ESP-IDF example for verifying basic serial functionality, not product-specific logic.

02 Hardware

  • MCU: ESP32 family, any UART-capable pins.
  • Levels: TTL 3.3V, cross TX/RX. Never raw RS232 voltages.
  • RS485: Needs a transceiver (e.g., MAX485), A/B differential lines, 120Ω termination, DE/RE control.
  • Ground: Must share ground with the peer, or you get garbage.
  • Power: If powered by peer, check level matching and isolation.
  • Wiring: See schematic for pin assignments.
  • Layout: For long lines, consider shielding and termination.

03 Software flow

1. Configure UART port, baud, data bits, stop bits, parity, and buffer sizes.

2. Install driver and set pin mapping (TX, RX, optional RTS/CTS).

3. Create a read task using uart_read_bytes.

4. Write back the read data using uart_write_bytes.

5. Handle error events (e.g., buffer overflow, disconnection).

6. In products, replace with frame parsing, not permanent echo.

04 Core points

  • Garbage is usually ground or levels, then firmware.
  • RS485 requires DE/RE direction control to avoid contention.
  • Custom work: RS485 hardware, protocol, isolation.
  • Don't copy directly to other chips; pins and drivers differ.
  • Adjust buffers based on baud and line length.

中文

传感器与外设

UART Echo 串口

接电表、PLC、模组的起点。RS485 只是电平转换。

传感器与外设 串口接仪表 ESP32 family

官方路径: examples/peripherals/uart/uart_echo

01 项目概述

UART Echo 是串口调试的起点:从 UART 读什么写回什么。它不实现任何业务协议,只证明口线、波特率、接线是对的。

适用场景:调试第二串口、对接仪表、PLC 或无线模组。在确认物理链路之前,不要急着写协议栈。

不适用场景:已经稳定的协议栈。Echo 之上应加 Modbus 或自定义帧解析,而不是永久回显。

硬件上,TTL 对 TTL 直接交叉连接,RS485 需要收发器和终端电阻。共地是基本要求。

软件上,ESP-IDF 的 uart 驱动提供事件循环和缓冲管理,适合做异步收发。

本示例是公共 ESP-IDF 示例,用于验证串口基本功能,不涉及具体产品逻辑。

02 项目硬件描述

  • MCU: ESP32 系列,任意支持 UART 的引脚。
  • 电平: TTL 3.3V,交叉连接 TX/RX。不要直接接 RS232 的 ±12V。
  • RS485: 需外接收发器(如 MAX485),A/B 差分线,120Ω 终端电阻,DE/RE 控制脚。
  • 共地: 必须与对端设备共地,否则数据乱码。
  • 电源: 若对端设备供电,注意电平匹配和隔离。
  • 接线: 参考原理图,确认引脚分配。
  • 布局: 长线传输时考虑屏蔽和终端匹配。

03 项目软件流程描述

1. 配置 UART 端口、波特率、数据位、停止位、校验位和缓冲区大小。

2. 安装驱动并设置引脚映射(TX、RX、可选 RTS/CTS)。

3. 创建读任务,使用 uart_read_bytes 读取数据。

4. 将读取的数据通过 uart_write_bytes 写回。

5. 处理错误事件(如缓冲区溢出、断线)。

6. 产品中替换为帧解析逻辑,而不是永久 echo。

04 项目核心点

  • 乱码先查地和电平,再查固件。
  • RS485 需控制 DE/RE 方向,避免冲突。
  • 可定制: RS485 硬件、协议、隔离。
  • 不要直接复制到其他芯片,引脚和驱动不同。
  • 根据实际波特率和线长调整缓冲区。