A protocol from 1979 that still runs plants
Modbus is a simple protocol for reading and writing the values inside a PLC. It was made in 1979 for serial cables, and Modbus/TCP carries it over ordinary networks today.
It is everywhere in industry because it is simple. It is also a risk, because it never asks who is sending. All of this lesson happens in your own lab copy.
By the end of this lesson
Note
I can read a Modbus/TCP request byte by byte.
Key words
| Word | What it means | Example |
|---|---|---|
| Coil | a one-bit value a client may write | pump command |
| Register | a 16-bit number inside the PLC | a setpoint |
| Function code | the byte that says what a request does | 03 |
| MBAP header | the seven bytes in front of every Modbus/TCP request | |
| Exception | an error reply with a code saying what was wrong | |
| Big-endian | the high byte of a number comes first |
The cells students fill in are left blank in this preview.
The one rule
Warning
Send Modbus only to your own lab copy. A Modbus write to a real device can start a motor or open a valve. Sending one to a system that is not yours can be a crime under the Computer Misuse Act 1990.
Four tables hold everything a PLC shares
| Table | Size | Who may write | In the lab |
|---|---|---|---|
| Coils | 1 bit | Clients | 0 pump command, 1 manual mode |
| Discrete inputs | 1 bit | Nobody, read only | 0 pump running, 1 high switch, 2 safety trip |
| Holding registers | 16 bits | Clients | 0 low setpoint, 1 high setpoint |
| Input registers | 16 bits | Nobody, read only | 0 level, 1 scans, 3 level at last trip |
The cells students fill in are left blank in this preview.
A request is a header and a PDU
Every Modbus/TCP request starts with the seven-byte MBAP header. It holds a transaction id, a protocol id that is always 0, a length and a unit id.
Then comes the PDU: a function code and its data. For a read, the data is a start address and a count. For a write of one value, it is an address and the value. Every number is big-endian.
Build a request and read the reply
docker exec work python3 steps/frame.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: the request the lab sent
| Bytes | Field | Meaning |
|---|---|---|
| 00 01 | Transaction id | Request number 1 |
| 00 00 | Protocol id | Always 0 for Modbus |
| 00 06 | Length | Six bytes follow |
| 01 | Unit id | Device 1 |
| 03 | Function code | Read holding registers |
| 00 00 00 02 | Address and count | From 0, two registers |
The cells students fill in are left blank in this preview.
Try it: build a request
import struct
tid, unit, fc, address, count = 7, 1, 4, 0, 5
pdu = struct.pack(">BHH", fc, address, count)
frame = struct.pack(">HHHB", tid, 0, len(pdu) + 1, unit) + pdu
print(frame.hex(" "))
print("bytes:", len(frame))
# Change fc to 6, address to 1 and count to 75. What would that frame do?
Function codes read, write or refuse
Functions 1 to 4 read the four tables. Function 5 writes one coil and function 6 writes one register. Functions 15 and 16 write many at once.
When a request is wrong, the PLC sends an exception. It sets the top bit of the function code and adds a code. Code 1 is a function it lacks, 2 an address that does not exist, and 3 a bad value.
Ask for three things the PLC refuses
docker exec work python3 steps/errors.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Nothing in the frame says who is asking
Look at the table again. There is no user, no password and no signature. A PLC answers any request that reaches its port.
The next Run writes 81 into the high setpoint from the work box, then puts 80 back. The PLC accepts both. On a real plant, any machine that can reach port 502 could do the same.
Change a setpoint with no password
docker exec work python3 steps/noauth.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a PLC checks who sent a write. Plain Modbus has no login at all, so a write from any machine that can reach the port is obeyed. The defence is outside the protocol: who can reach the port, and a safety layer the network cannot change.