Modbus function codes: reference with examples and exception codes

Catalin NegruPublished 6 min read

Part of the Modbus guide

Modbus function codes are the one-byte values at the start of every request that tell the server what to do: 0x03 reads holding registers, 0x06 writes one, 0x10 writes several. Valid codes run from 1 to 127. A server that refuses a request replies with the same code plus 0x80 and a one-byte exception code. Eight Modbus function codes cover nearly all field traffic.

This page is the reference section of the Modbus guide. The examples show the PDU only, which is the part shared by Modbus RTU and Modbus TCP; each transport adds its own address and framing around it.

The eight Modbus function codes in daily use

CodeNameTableQuantity per request
0x01 (1)Read CoilsCoils1 to 2,000
0x02 (2)Read Discrete InputsDiscrete inputs1 to 2,000
0x03 (3)Read Holding RegistersHolding registers1 to 125
0x04 (4)Read Input RegistersInput registers1 to 125
0x05 (5)Write Single CoilCoils1
0x06 (6)Write Single RegisterHolding registers1
0x0F (15)Write Multiple CoilsCoils1 to 1,968
0x10 (16)Write Multiple RegistersHolding registers1 to 123

The quantity limits follow from the 253-byte PDU. A read of 125 registers returns 250 data bytes plus a function code and a byte count. Many devices enforce lower limits than the specification; check the manual if large reads fail.

Read functions: 0x01 to 0x04

All four reads share one request layout: function code, a two-byte starting address and a two-byte quantity, both high byte first.

Read Holding Registers, 3 registers from address 0x006B Request PDU: 03 00 6B 00 03 Response PDU: 03 06 02 2B 00 00 00 64 03 function code 06 byte count (2 bytes per register) 02 2B first register = 555 00 00 second register = 0 00 64 third register = 100

0x04 has the same layout and reads the input register table instead. The two tables are separate: a value documented as 30009 is read with 0x04 at address 0x0008, and asking for it with 0x03 returns either an exception or an unrelated holding register. The addressing rules are in What is the Modbus protocol?.

Coil and discrete input reads pack the bits eight to a byte. The first coil requested is the least significant bit of the first data byte, and unused bits in the last byte are zero.

Read Coils, 19 coils from address 0x0013 Request PDU: 01 00 13 00 13 Response PDU: 01 03 CD 6B 05 03 byte count (19 bits need 3 bytes) CD 1100 1101: coils at addresses 0x13 to 0x1A, lowest address in bit 0 6B 0110 1011: addresses 0x1B to 0x22 05 0000 0101: addresses 0x23 to 0x25, upper five bits are padding

Write functions: 0x05, 0x06, 0x0F and 0x10

Write Single Coil sends 0xFF00 for on and 0x0000 for off. No other value is legal. Write Single Register sends the 16-bit value. For both, a successful response is an exact echo of the request.

Write Single Coil, address 0x00AC on Request and response PDU: 05 00 AC FF 00 Write Single Register, address 0x0001 = 3 Request and response PDU: 06 00 01 00 03

The multiple-write functions add a quantity and a byte count before the data. The response echoes the starting address and quantity without the data.

Write Multiple Registers, 2 registers from address 0x0001 Request PDU: 10 00 01 00 02 04 00 0A 01 02 Response PDU: 10 00 01 00 02 00 01 starting address 00 02 quantity: 2 registers 04 byte count 00 0A first register = 10 01 02 second register = 258

Two practical points. Some devices implement only 0x10 and reject 0x06, or the reverse, so a write that fails with exception 01 may succeed with the other function code. And a 32-bit setpoint that spans two registers should be written with one 0x10 request; two separate 0x06 writes leave the device holding half of the old value and half of the new one between them.

The other public Modbus function codes

The specification defines more codes than most devices implement. Several exist only on serial lines.

CodeNameNotes
0x07 (7)Read Exception StatusSerial line only
0x08 (8)DiagnosticsSerial line only; sub-functions for loopback and counters
0x0B (11)Get Comm Event CounterSerial line only
0x0C (12)Get Comm Event LogSerial line only
0x11 (17)Report Server IDSerial line only
0x14 (20)Read File Record
0x15 (21)Write File Record
0x16 (22)Mask Write RegisterSets or clears bits in one holding register with an AND mask and an OR mask
0x17 (23)Read/Write Multiple RegistersOne transaction; the write is performed before the read
0x18 (24)Read FIFO Queue
0x2B (43)Encapsulated Interface TransportSub-type 0x0E reads device identification: vendor name, product code, revision

Codes 65 to 72 and 100 to 110 are set aside for vendor-defined functions. A capture that shows one of those is a proprietary extension.

What does a Modbus exception response mean?

An exception response is two bytes of PDU: the request’s function code with the top bit set, then the exception code. A request with 0x03 that fails comes back as 0x83; 0x10 comes back as 0x90.

Request PDU: 03 00 6B 00 03 Exception PDU: 83 02
CodeNameWhat it usually means in the field
01Illegal FunctionThe device does not implement this function code
02Illegal Data AddressThe address, or address plus quantity, is outside what the device exposes
03Illegal Data ValueA field in the request is invalid: quantity out of range, wrong byte count, or a value the device will not accept
04Server Device FailureThe device hit an unrecoverable error while carrying out the request
05AcknowledgeRequest accepted, processing will take a while; used with programming commands
06Server Device BusyThe device is busy with a long command; retry later
08Memory Parity ErrorFile record read failed a consistency check
0AGateway Path UnavailableA gateway could not route the request to the unit identifier given
0BGateway Target Device Failed to RespondA gateway forwarded the request and the device behind it did not answer

An exception is good news in one respect: the device received the request intact and replied, so the wiring, the serial settings and the address are right. A device sends no reply at all when it is not addressed, when the request arrives with a bad CRC, or when the request was a broadcast. That case shows up as a timeout.

Diagnosing by exception code

Exception 02 causes the most confusion because the register often does exist. The common reasons:

  • The request is off by one. Register 40108 is address 107, and the device has nothing at address 108. See the addressing section of What is the Modbus protocol?.
  • The wrong table. The value is an input register and the request used 0x03.
  • The block read runs past the end of a contiguous range. Many devices reject a request if any register in it is undefined, so reading 20 registers across a gap in the map fails even though the first one is valid. Split the read at the gap.

Exception 01 on a write means the device does not support that write function, or the table is read-only by design. Exception 03 on a write usually means the value is outside the range the device accepts for that register. Exceptions 0A and 0B always come from a gateway, never from the end device.

How ControlCom uses Modbus function codes

On the ControlCom Edge Server, the on-site component of ControlCom Connect, you do not pick a function code per request. Each tag in a Modbus client has an Address Scope, and the four options name the read function they use: Coils (0x01), Discrete Inputs (0x02), Input Registers (0x04) and Holding Registers (0x03), the default. The Round size setting on the connection, 36 by default, is the maximum number of consecutive registers read in a single request, which is the setting to lower when a device rejects long block reads. Writes are off unless a tag is given a Write Command; the default is No Write Allowed.

The Modbus client documentation  describes each field. The platform overview shows how the values are used once collected, and the page for controls engineers covers verifying a register map before committing it.

Choose the table, the address and the data type for each tag, and the Edge Server issues the reads and publishes the values to ControlCom Connect.