07 — WRITING VALUES
The four write functions and when to pick each.
Estimated 8 minutes.
Four ways to write.
Modbus offers four write functions, splitting on single-vs-many and bit-vs-word. Pick the right one and the framing follows; pick the wrong one and you waste bandwidth or trip an exception.
The cheat sheet.
FC5 / FC6 for one. FC15 / FC16 for many. Bits on top row, words on bottom.
FC5 — one coil, two magic values.
FC5 writes a single coil. The "value" field is 16 bits, but only two values are legal: 0xFF00 (on) and 0x0000 (off). Anything else is rejected with an exception. Yes, it is weird; yes, it is historical.
FC5 on the wire.
Function + address + 0xFF00 for ON (or 0x0000 for OFF).
FC6 — one register, normal value.
FC6 writes one 16-bit register. The "value" is the actual 16-bit number you want to land in the slave's holding register. Big-endian, like reads.
Solve the output recipes.
Eight factory outputs — motors, heaters, lamps, valves — sit behind coils 0–7. Each named recipe is a pattern those bits should hold. Toggle individual coils to match each recipe; the device above each bit shows whether it matches. Solve all four recipes.
FC15 — many coils, same packing as FC1.
FC15 writes many coils in one shot. The data section uses the exact same LSB-first bit packing as an FC1 read response — same rules, same byte layout.
FC16 — many registers, same big-endian as FC3.
FC16 writes many registers. Each register is sent big-endian (high byte, low byte), same as an FC3 response. Maximum 123 registers per request — slightly less than the FC3 read limit, because the request itself takes more header bytes.
Write 5 motor setpoints at once.
A drive exposes 5 motor setpoints across 5 consecutive holding registers. You want all 5 updated in one transaction. Which function code?
- FC5
- FC6
- FC15
- FC16
Just turn one valve on.
A valve is wired to coil 12. You want to turn it on. Which function code?
- FC5
- FC6
- FC15
- FC16
Successful writes echo back.
A successful FC5/6 write returns the same address and value you sent. A successful FC15/16 returns the address and quantity. If the slave returns something different, treat it as an error even if there is no exception code.
You can write any value with the right function.
- FC5 single coil — 0xFF00 = on, 0x0000 = off.
- FC6 single register — plain 16-bit value, big-endian.
- FC15 many coils — same LSB-first packing as FC1.
- FC16 many registers — same big-endian as FC3, max 123.
Lesson 07 complete.
- You know all four write functions and when to use each.
- You remember the FC5 0xFF00 / 0x0000 trap.
- You can recognize a write echo from the slave.