Modbus troubleshootingModbus 排错指南

Symptom, cause, fix.

现象、原因、解法。

Before anything else: confirm the four serial settings match the device exactly — baud rate, parity, data bits, stop bits. A wrong parity setting produces silence on some devices and garbage on others, and it looks exactly like a wiring fault.

先确认四件事:波特率、校验位、数据位、停止位与设备完全一致。校验位设错时,有的设备完全没反应,有的回一堆乱码,表现和接线故障一模一样。

1. Nothing comes back at all完全没有任何返回

Silence is not an exception — the device either never saw the frame or refused to answer it. Exceptions mean the device is alive, so if you get nothing, start at the wire.

"没有返回"和"回异常"是两回事。异常说明设备活着;完全没反应,先从物理层查。

  1. A and B swapped. RS-485 has no polarity protection. Swap the two data wires — it takes ten seconds and fixes a surprising share of cases.
  2. Unit ID mismatch. Try ID 1, then scan a range. A device configured for 247 will ignore everything addressed to 1, silently.
  3. Broadcast address. Unit 0 is broadcast: by specification every device acts on it and nobody answers. If you used 0, that is your answer.
  4. Half-duplex adapter direction. Cheap USB-RS485 dongles need RTS or automatic direction control. If the driver is wrong the adapter transmits but never listens.
  5. Wiring topology. RS-485 is a daisy chain, not a star. A star with long stubs reflects and garbles everything.
  6. Reference ground not connected. Two-wire RS-485 still needs a common ground reference on most transceivers; without it, long runs fail intermittently.
  7. Power. The device is not on, or is in a state where its serial stack is not running.
  1. A/B 接反。RS-485 没有极性保护,交换两根数据线试试——十秒钟的动作,能解决相当比例的现场问题。
  2. 从站地址不对。先试 1,再扫一段地址。地址是 247 的设备会静默忽略发往 1 的所有请求。
  3. 用了广播地址。地址 0 是广播:规范规定所有设备执行但谁都不许应答。填 0 就等于自问自答。
  4. 半双工方向控制。廉价 USB-RS485 靠 RTS 或自动方向控制,驱动不对时"发得出去、收不回来"。
  5. 拓扑。RS-485 必须手拉手串联,星形接法加长分支线会产生反射,把帧打烂。
  6. 参考地没接。两线制 RS-485 在多数收发器上仍需共地参考,不接时短距离正常、长距离间歇失败。
  7. 供电。设备没上电,或处于串口协议栈未运行的状态。

2. Bytes arrive but the CRC never matches有字节回来,但 CRC 永远不对

Garbage with a valid-looking length is electrical. Garbage with random lengths is usually a baud rate mismatch. Paste the bytes into the CRC calculator and look at the trace: if only the last two bytes are wrong, your link is fine and your checksum code is wrong.

长度正常但内容是乱码,通常是电气问题;长度都乱,多半是波特率不对。把字节粘进CRC 计算页看过程:如果只有末尾两字节不对,链路是好的,错在校验算法。

3. Exception 02 — illegal data address异常码 02——非法数据地址

The device is answering, so everything electrical is fine. It simply does not have a register there.

设备能应答,说明电气层没问题,只是那个地址它没有。

4. Exception 03 — illegal data value异常码 03——非法数据值

The address was accepted, the value was not. Either the quantity is too large for one request, or the device rejects that particular value.

地址认了,值不认。要么一次要得太多,要么这个具体数值设备不接受。

5. It answers, but the numbers are nonsense能应答,但数值离谱

Nine out of ten times this is byte or word order. Put the registers into the converter and read across the row: one of the four columns will be the value you expect.

十次里有九次是字节序 / 字序问题。把寄存器粘进转换页横向对比,四列里总有一列是你期望的值。

6. Works, then fails after a while一开始正常,跑一阵就出错

7. Modbus TCP connects but goes nowhereModbus TCP 连上了但没数据

A five-minute diagnostic order五分钟排查顺序

#CheckIf it fails
1Loopback: short TX to RX on the adapter, send a frame, see it echoDriver or adapter problem, not the device
2Baud / parity / data / stop bits match the manual exactlyFix and retry before touching wiring
3Unit ID 1, function 03, start 0, quantity 1Nothing? Try 2, 3, 247; swap A/B
4Raw bytes look like a valid frame; CRC passesElectrical: termination, ground, cable length, baud
5Exception code returnedProtocol-level: address map or value range
6Values plausible in one byte orderByte/word order or scaling factor

Steps 3–6 need no hardware other than the adapter: build the frame in the frame builder, send it, and paste whatever comes back into the decoder.

第 3–6 步除了转接器不需要别的硬件:在帧构建页生成报文,发送,把回来的字节粘进解析器即可。