Modbus reference tablesModbus 速查表

Function codes, exception codes, limits, defaults.

功能码、异常码、限制、默认参数。

Function codes功能码

CodeNameUsed forMax per requestResponse PDU
0x01Read CoilsRead on/off outputs, read-write2000 bitsfc, count, packed bits
0x02Read Discrete InputsRead on/off inputs, read-only2000 bitsfc, count, packed bits
0x03Read Holding RegistersRead 16-bit read/write words125 regsfc, byte count, words
0x04Read Input RegistersRead 16-bit read-only words125 regsfc, byte count, words
0x05Write Single CoilForce one output ON or OFF1 bitecho of request
0x06Write Single RegisterWrite one 16-bit word1 regecho of request
0x07Read Exception StatusRead 8 device status bits—fc, status byte
0x08DiagnosticsLoopback test, counters, listen-only—fc, sub-function, data
0x0BGet Comm Event CounterEvent counter and status word—fc, status, count
0x0CGet Comm Event LogEvent log with run indicator—fc, byte count, events
0x0FWrite Multiple CoilsForce 1–1968 outputs at once1968 bitsfc, start, quantity
0x10Write Multiple RegistersWrite 1–123 words at once123 regsfc, start, quantity
0x11Report Server IDDevice type, run state, product string—fc, count, id bytes
0x14Read File RecordRead a record from a file—fc, count, sub-responses
0x15Write File RecordWrite a record to a file—echo of request
0x16Mask Write RegisterAND/OR mask on one register1 regecho of request
0x17Read/Write Multiple RegistersWrite then read in one transactionread 125 / write 121fc, byte count, words
0x18Read FIFO QueueRead a first-in-first-out register queue31 regsfc, byte count, fifo count

Codes 0x09–0x0A, 0x0D–0x0E are unassigned, 0x12–0x13 legacy, 0x19–0x2A reserved, 0x2B–0x40 for newer specifications. Everything above 0x80 in the first PDU byte means "exception".0x09–0x0A、0x0D–0x0E 未分配,0x12–0x13 为历史功能,0x19–0x2A 保留,0x2B–0x40 属于较新的规范。PDU 首字节高于 0x80 表示"异常应答"。

Exception codes异常码

CodeNameWhat it actually meansWhere to look first
0x01Illegal FunctionThe device does not implement this function codeManual's supported code list
0x02Illegal Data AddressThat address or range does not existBase address off by one? Wrong 30001/40001 prefix?
0x03Illegal Data ValueQuantity, value or byte count out of rangeToo many registers? FC05 value not 0xFF00/0x0000?
0x04Server Device FailureThe device failed while executingDevice state, power, internal fault log
0x05AcknowledgeAccepted, still processing — poll againLong operations; wait and repeat
0x06Server Device BusyBusy with another commandRetry after a longer delay
0x07Negative AcknowledgeProgram function cannot be performedDevice-specific programming state
0x08Memory Parity ErrorInternal memory check failedHardware fault, RMA
0x0AGateway Path UnavailableGateway has no route to that unitUnit ID mapping, gateway configuration
0x0BGateway Target Failed To RespondGateway reached nothing behind itDevice offline, wrong ID, wrong bus wiring

An exception response is the function code with the high bit set plus one byte: 01 83 02 C0 F1 means unit 1, function 03, exception 02.

异常应答 = 功能码置最高位 + 一个字节:01 83 02 C0 F1 表示从站 1、功能码 03、异常码 02。

Address conventions地址习惯

Data modelPrefixDocumented asProtocol address (what goes on the wire)
Coils (read/write bits)0xxxx000010
Discrete inputs1xxxx100010
Input registers3xxxx300010
Holding registers4xxxx400010
Holding register 114xxxx4001110

The prefix is documentation only — it never appears in the frame. The only number that matters is the protocol address, which is zero-based. Off-by-one errors here are the single most common cause of exception 02.

前缀只是文档写法,不会出现在报文里。报文里只有 0 基址的协议地址。这里差一位,是异常码 02 最常见的原因。

Data types in registers寄存器里的数据类型

TypeRegistersBytesRange / notes
coil / bit—1 bitPacked 8 per byte, least significant bit first
uint16120 … 65535
int1612−32768 … 32767, two's complement
uint32 / int3224Word order must be known: ABCD or CDAB mostly
float3224IEEE 754 single precision, ~7 significant digits
float6448IEEE 754 double, 4-register word order varies
scaled integer1–22–4Value ÷ 10, ÷ 100 or ÷ 1000 per the manual — e.g. 235 means 23.5 °C

Limits and defaults限制与默认值

Frame size limits帧长限制

  • PDU: 253 bytes maximum (1 function code + 252 data).
  • RTU frame: 256 bytes including address and CRC.
  • TCP ADU: 260 bytes including the 7-byte MBAP header.
  • Unit address: 1–247 on serial, 0 is broadcast (no reply), 255 is often "any".
  • PDU 最大 253 字节(1 字节功能码 + 252 字节数据)。
  • RTU 整帧最大 256 字节(含地址与 CRC)。
  • TCP ADU 最大 260 字节(含 7 字节 MBAP 头)。
  • 从站地址:串口 1–247,0 为广播(不应答),255 常表示"任意"。

Serial line defaults串口默认参数

  • Spec default: 9600 baud, 8 data bits, even parity, 1 stop bit (8E1).
  • 8N1 is at least as common in practice — the manual wins.
  • RS-485: 32 unit loads per segment without a repeater, 1200 m at low baud.
  • 120 Ω termination at both physical ends; bias resistors on one node.
  • 规范默认:9600、8 数据位、偶校验、1 停止位(8E1)。
  • 实际产品里 8N1 至少一样常见——以手册为准。
  • RS-485:不加中继一段最多 32 个单位负载,低波特率下最长 1200 m。
  • 物理两端各 120 Ω 终端电阻;偏置电阻只在一处加。

RTU inter-frame timing (3.5 characters)RTU 帧间隔(3.5 个字符时间)

BaudTime per character (8N1)3.5 chars — frame gap1.5 chars — intra-frame gap
96001.042 ms3.65 ms1.56 ms
192000.521 ms1.82 ms0.78 ms
384000.260 ms0.91 ms0.39 ms
576000.174 ms0.61 ms0.26 ms
1152000.087 ms0.30 ms0.13 ms

Above 19200 baud the specification allows a fixed 1.75 ms inter-frame delay instead of the calculated 3.5 characters, because character timing becomes imprecise.波特率高于 19200 时,规范允许用固定的 1.75 ms 帧间隔代替按 3.5 字符计算,因为此时字符时间已经不够精确。