Industrial bus

Modbus

The language existing industrial hosts already speak. Serial or TCP.

Industrial bus Modbus ESP32 family

Official path: examples/protocols/modbus

01 Overview

Modbus is one of the most common industrial protocols, and ESP-IDF provides official master/slave examples (path may shift slightly by IDF version).

It solves heterogeneous device interconnection: PLCs, meters, and VFDs often only speak Modbus, and ESP32 can bridge them to Wi-Fi or the cloud.

Use it when integrating with existing industrial hosts, sensor acquisition, or remote monitoring.

Skip it for consumer app control; BLE or HTTP is more appropriate.

Modbus has two physical layers: RTU over serial (usually RS485) and TCP over Ethernet or Wi-Fi.

This page focuses on ESP32 as a slave or master implementation points.

02 Hardware

  • MCU: ESP32 family with built-in UART and Wi-Fi, suitable for both Modbus RTU and TCP.
  • RS485 transceiver: RTU mode requires an external RS485 chip (e.g., MAX485), connected to ESP32 UART TX/RX and a GPIO for direction control.
  • Termination resistors: 120Ω at both ends of the RS485 bus; see schematic.
  • Isolation: Industrial sites often require isolated RS485 modules to prevent ground loops.
  • TCP mode: Use ESP32 Wi-Fi to connect to the LAN; ensure the field VLAN is reachable.
  • Power: Check transceiver supply voltage (typically 3.3V or 5V); see schematic.
  • Wiring: Ensure A/B lines are correct; reversed polarity causes communication failure.

03 Software flow

1. Choose RTU or TCP mode; configure UART or Wi-Fi accordingly.

2. Set slave address (1-247) and baud rate (e.g., 9600/115200).

3. Define the register map: holding registers, input registers, coils, etc.

4. Map real sensor data to register addresses.

5. Implement master or slave callbacks to handle read/write requests.

6. Add error handling: CRC failures, timeouts, exception codes.

7. Test with a Modbus debugging tool to verify communication.

8. Consider low power: sleep when idle, wake periodically for polling.

04 Core points

  • The register map is the product spec. Agree it before coding.
  • Custom work: point list, RS485, cloud in parallel.
  • Baud rate and parity must match the host, or communication fails.
  • RS485 direction control needs precise timing, or data collisions occur.
  • Don't copy Modbus implementations from other chips; ESP32 UART and interrupt handling differ.
  • For TCP mode, manage connections carefully to avoid multiple hosts connecting simultaneously.

中文

工业总线

Modbus

进现有工业主机的常用语言。串口或 TCP。

工业总线 Modbus ESP32 family

官方路径: examples/protocols/modbus

01 项目概述

Modbus 是工业现场最常见的通信协议之一,ESP-IDF 官方提供主从示例,路径随 IDF 版本略有调整。

它解决的是异构设备互联问题:PLC、电表、变频器等设备往往只支持 Modbus,通过 ESP32 可以桥接到 Wi-Fi 或云。

适用于需要对接现有工业主机、传感器采集、远程监控等场景。

不适用于消费级 App 控制,那种场景用 BLE 或 HTTP 更合适。

Modbus 有两种物理层:RTU 走串口(通常 RS485),TCP 走以太网或 Wi-Fi。

本页聚焦 ESP32 作为从站或主站的实现要点。

02 项目硬件描述

  • MCU: ESP32 系列,内置 UART 和 Wi-Fi,适合 Modbus RTU 和 TCP。
  • RS485 收发器: RTU 模式需要外部 RS485 芯片(如 MAX485),连接 ESP32 的 UART TX/RX 和 GPIO 控制方向。
  • 终端电阻: RS485 总线两端需 120Ω 终端电阻,具体见原理图。
  • 隔离: 工业现场建议使用隔离型 RS485 模块,防止地环路损坏设备。
  • TCP 模式: 使用 ESP32 的 Wi-Fi 连接局域网,确保现场 VLAN 能通。
  • 电源: 注意 RS485 收发器的供电电压(通常 3.3V 或 5V),见原理图。
  • 接线: 确保 A/B 线正确,极性反了会导致通信失败。

03 项目软件流程描述

1. 选择 RTU 或 TCP 模式,配置 UART 或 Wi-Fi。

2. 配置从站地址(1-247)和波特率(如 9600/115200)。

3. 定义寄存器表:保持寄存器、输入寄存器、线圈等。

4. 将真实传感器数据映射到寄存器地址。

5. 实现主站或从站回调,处理读写请求。

6. 添加错误处理:CRC 校验失败、超时、异常码。

7. 测试:使用 Modbus 调试工具验证通信。

8. 考虑低功耗:在空闲时进入睡眠,定时唤醒轮询。

04 项目核心点

  • 寄存器表就是产品说明书。 先和客户对表再写代码。
  • 可定制: 点表、RS485、和云并行。
  • 注意波特率和校验位 必须与主机一致,否则通信失败。
  • RS485 方向控制 需要精确时序,否则数据冲突。
  • 不要照搬其他芯片的 Modbus 实现,ESP32 的 UART 和中断处理不同。
  • TCP 模式注意连接管理,避免多个主机同时连接。