Easy Modbus · plain-English Modbus reference

What are Modbus function codes?

Short answer

A function code is the verb of a Modbus request: it says which of the four data lists you want and whether you are reading or writing. You only need eight of them in practice. Function code 3 reads holding registers and is the one you will use most; 4 reads input registers; 1 and 2 read on/off values; 6 and 16 write registers; 5 and 15 write on/off values. Everything else in the specification is rarely seen in building equipment.

The eight that matter

CodeNameIn plain English
1Read CoilsRead on/off values you are allowed to change
2Read Discrete InputsRead on/off values you cannot change — statuses, alarms
3Read Holding RegistersRead numbers. The workhorse; most maps live here
4Read Input RegistersRead numbers you cannot change — usually live measurements
5Write Single CoilSwitch one on/off value
6Write Single RegisterSet one number
15Write Multiple CoilsSwitch several on/off values at once
16Write Multiple RegistersSet several numbers at once — needed for anything wider than 16 bits

Two notes that save time. Function code 3 versus 4 is a real distinction and not interchangeable: a value in the holding registers cannot be read with code 4, and plenty of devices put their measurements in holding registers anyway, so the map has to tell you which. Code 16 is required for 32-bit values, because writing the two halves with two separate code 6 requests leaves the device holding half of the old value and half of the new one for a moment, which on a setpoint can be a very large number indeed.

One more worth knowing about

Function code 43, sub-function 14 — Read Device Identification — asks the device who it is, and can return a vendor name, product code, model and firmware revision. It is the nearest thing Modbus has to self-description. It is also optional, and most equipment does not implement it, so a refusal here means nothing at all about whether the device is healthy.

When the device says no: exception codes

An exception reply means the device received the request, understood it, and declined. That is useful information: the device is alive and reachable.

CodeNameWhat to do
1Illegal functionThis device does not support that function code. Try 3 instead of 4, or vice versa.
2Illegal data addressThat register does not exist. Your address is wrong, or off by one. The device itself is fine.
3Illegal data valueThe value or the quantity is out of range. Often means you asked for too many registers at once, or wrote a value the device will not accept.
4Device failureSomething went wrong inside the device while handling the request.
5AcknowledgeAccepted, but it needs more time. Rare.
6Device busyTry again shortly.
10Gateway path unavailableThe gateway has no route to that unit ID.
11Gateway target device failed to respondThe gateway is fine; nothing answered at that unit ID on the serial side. Wrong unit ID, or a wiring problem.

How many registers in one request?

The specification allows 125 registers or 2000 bits per request. A lot of real equipment, especially cheap gateways, falls over well before that — truncating the reply, returning exception 3, or simply hanging. If reads work one at a time but fail in blocks, lower the block size to 32 or even 16 and try again.

It is still worth reading in blocks rather than one register at a time. On a serial chain each request costs tens of milliseconds of wire time, so forty separate reads is a visible wait, and forty chances to collide with whatever else is polling that chain.