DevTools Hub

Guides › What Is Modbus? RTU and TCP Frames, Function Codes, Registers

What Is Modbus? RTU and TCP Frames, Function Codes, Registers

Modbus is a communication protocol that lets industrial devices such as PLCs, drives, power meters, temperature controllers and sensors exchange data. Modicon (now Schneider Electric) created it in 1979 for its PLCs. Because the specification is open and the protocol is simple, it is still the most widely used industrial protocol. The Modbus Organization maintains the current specification.

Master and slave

Modbus is a request/response protocol. The side that asks is the master (client); the side that answers is the slave (server). A slave never sends data on its own; it only replies to requests. A typical setup is a PLC or HMI polling several temperature controllers one after another.

Modbus variants

VariantMediumEncodingError checkNotes
Modbus RTURS-485 / RS-232Binary bytesCRC-16Most common, efficient
Modbus ASCIIRS-485 / RS-232Hex characters (starts with a colon, ends with CR LF)LRCHuman-readable but twice as long; rarely used
Modbus TCPEthernet (TCP port 502)Binary + MBAP headerNone (TCP handles it)Growing in new installations

Data model: coils and registers

TableSizeAccessTraditional notationExample
Coil1 bitRead/write0xxxx (00001+)Relay output, run/stop command
Discrete Input1 bitRead only1xxxx (10001+)Limit switch, alarm contact
Input Register16 bitsRead only3xxxx (30001+)Measurements (temperature, current)
Holding Register16 bitsRead/write4xxxx (40001+)Setpoints, parameters

Watch the address offset: register "40001" in a manual is sent as 0x0000 in the frame. Protocol addresses start at 0. Mixing up 0-based and 1-based numbering, and reading every value one register off, is the most common field problem.

Modbus RTU frame

Slave addressFunction codeDataCRC
1 byte1 byte0 to 252 bytes2 bytes (low byte first)

RTU has no start or end characters. Frames are separated by a silent interval of at least 3.5 character times: about 4 ms at 9600 bps (one 11-bit character is about 1.15 ms). Above 19200 bps the interval is fixed at 1.75 ms. A gap of more than 1.5 character times inside a frame makes the receiver discard it.

Request and response example (function 03)

Read 10 holding registers starting at 40001 from slave 1:

Request:  01 03 00 00 00 0A C5 CD
          |  |  [start]  [count=10] [CRC 0xCDC5, low byte first]
          |  function 03
          slave address 1

Response: 01 03 14 [20 data bytes] [2 CRC bytes]
                (byte count 0x14 = 10 registers x 2 bytes)

Write the value 3 to register 40002 (protocol address 1) of slave 1 with function 06: 01 06 00 01 00 03 98 0B. On success the device echoes the same frame back.

Common function codes

CodeNameDescription
01 (0x01)Read CoilsUp to 2000 coils
02 (0x02)Read Discrete InputsUp to 2000 inputs
03 (0x03)Read Holding RegistersUp to 125 registers; the most used function
04 (0x04)Read Input RegistersUp to 125 registers
05 (0x05)Write Single CoilON = FF 00, OFF = 00 00
06 (0x06)Write Single RegisterOne register
15 (0x0F)Write Multiple CoilsSeveral coils
16 (0x10)Write Multiple RegistersUp to 123 registers
23 (0x17)Read/Write Multiple RegistersWrite and read in one transaction
43/14 (0x2B/0x0E)Read Device IdentificationVendor name, product code

Exception responses

If a slave cannot process a request, it returns the function code plus 0x80 and an exception code. Example: a failed function 03 request returns 01 83 02 ....

CodeNameTypical cause
01Illegal FunctionFunction code not supported by the device
02Illegal Data AddressAddress does not exist, 0/1 offset mix-up, count runs past the end
03Illegal Data ValueValue out of range, bad count field
04Server Device FailureInternal device error
05AcknowledgeLong-running command accepted
06Server Device BusyDevice busy; retry later
0AGateway Path UnavailableGateway misconfigured
0BGateway Target Failed to RespondDevice behind the gateway is not answering

No reply at all (a timeout) is not an exception. Check wiring, baud rate, parity, slave address and CRC byte order first.

Modbus TCP frame (MBAP header)

Transaction IDProtocol IDLengthUnit IDFunction + data
2 bytes2 bytes (always 0)2 bytes (bytes that follow)1 byteSame as RTU

The RTU example above becomes 00 01 00 00 00 06 01 03 00 00 00 0A over TCP. The MBAP header replaces the CRC, and the Unit ID addresses a serial device behind a TCP-to-RTU gateway.

32-bit values and byte order

Registers are 16 bits, so 32-bit integers and IEEE 754 floats span two registers. Each register is big-endian, but the order of the two registers varies by vendor. The float 25.5 is 41 CC 00 00; depending on the device it arrives as 41CC 0000 (ABCD) or 0000 41CC (CDAB, word-swapped). If a value looks absurdly large or small, try swapping the words.

Integer registers often carry scaled values. A temperature register reading 0x00FA = 250 with a scale of 0.1 from the manual means 25.0 °C.

RS-485 wiring basics

Troubleshooting checklist

  1. Baud rate, parity, stop bits and slave address match
  2. A/B polarity, termination, ground
  3. CRC value and byte order (check with the CRC calculator)
  4. Register offset (0-based vs 1-based) and function code (03 vs 04)
  5. Polling: never send the next request before the reply arrives

See the CRC guide for how the checksum works and the 4-20mA guide for analog signals.

Try it now: CRC Calculator (verify Modbus CRC) (runs in your browser, no data is sent anywhere)