Watching the temperature is not enough—when it gets hot, Ms. Wang wants to turn on the exhaust fan; when the humidity drops, she wants to set the target to 60%. These actions that "change device state" are handled in Modbus by a group of write function codes. Unlike reads, writes really do change data inside the slave, so the protocol is especially strict about them.
Analogy: write function codes are like "issuing an order"—the teacher says "turn on the fan", the student carries it out and replies "done (repeating your instruction verbatim)", and only when the teacher confirms the repetition is correct is the order considered delivered.
"Writing" is half of automatic control: setting setpoints, toggling switches, downloading recipes all rely on it. Write to the wrong address or the wrong area (writing to a read-only area) and the device will either ignore you or return an exception. Master the 4 write function codes and the buttons on your screen can truly "command" the equipment in the field.
| Code | Hex | Action | What it writes | Quadrant |
|---|---|---|---|---|
| 05 | 0x05 | Write Single Coil | One 1-bit switch | write × single |
| 06 | 0x06 | Write Single Register | One 16-bit value | write × single |
| 15 | 0x0F | Write Multiple Coils | Multiple 1-bit switches | write × multiple |
| 16 | 0x10 | Write Multiple Registers | Multiple 16-bit values | write × multiple |
Memory aid: 05/06 write "one", 15/16 write "a batch"; the ones ending in 5 write coils (bits), the ones ending in 6 write registers (16 bits).
Write single (05/06) request: function code + address (2B) + value (2B); the response echoes the entire request verbatim—that is the "write acknowledgement".
Write multiple (15/16) request: function code + starting address (2B) + quantity (2B) + byte count (1B) + data (N bytes); the response echoes only the "starting address + quantity", not the data.
Request—write 100 (0x0064) into holding register 0x0000 of slave 1:
01
06
00 00
00 64
88 21
| Byte | Value | Meaning |
|---|---|---|
| 0 | 01 | Slave 1 |
| 1 | 06 | Function code: Write Single Register |
| 2–3 | 00 00 | Register address |
| 4–5 | 00 64 | Value written |
| 6–7 | 88 21 | CRC-16 |
Response—after a successful write, the slave echoes the entire request verbatim: 01 06 00 00 00 64 88 21. When the echo matches the request byte for byte, the write succeeded; a mismatch means the write failed.
Request—write coil 0x0000 to ON: 01 05 00 00 FF 00 8C 3A. Note that the value for coil ON is FF 00 (not 00 01), and OFF is 00 00. The response likewise echoes the request verbatim.
Request—set coils 0, 1 and 3 to ON (binary 0b00001011, with the least significant bit being coil 0): 01 0F 00 00 00 03 01 0B CE 90. Here 0B=0b00001011: bits 0, 1 and 3 are 1. The response echoes the "starting address + quantity": 01 0F 00 00 00 03 15 CA.
Request—write 2 registers starting at 0x0000 with the values 100 and 200: 01 10 00 00 00 02 04 00 64 00 C8 B3 E6. The response echoes the address + quantity: 01 10 00 00 00 02 41 C8.
| What Ms. Wang wants to do | Function code | Request (complete RTU frame) |
|---|---|---|
| Turn on the exhaust fan | FC 05 | 01 05 00 00 FF 00 8C 3A |
| Set the humidity target to 60 | FC 06 | 01 06 00 01 00 3C D8 1B |
| Start 3 devices with one command | FC 15 | 01 0F 00 00 00 03 01 0B CE 90 |
| Write temperature/humidity limits in bulk | FC 16 | 01 10 00 00 00 02 04 00 64 00 C8 B3 E6 |
A factory host application sets a VFD's target speed to 1500 RPM: use FC 06 to write holding register 0x0000 with the value 1500 (0x05DC): 01 06 00 00 00 64 8B 03. Then use FC 03 to read back the present frequency and confirm the motor really is speeding up. Write the setpoint, read the actual value—this is the most typical pair of operations on the industrial floor.
The Lab wizard currently supports FC 05 / 06 (write single); practise them one at a time:
01 06 00 01 00 3C D8 1B, and the response should be a verbatim echo;01 05 00 00 FF 00 8C 3A; note that ON is FF 00;(FC 15 / 16 are not currently offered in the Lab wizard, and this course's exercises do not require them; if you are curious, use the Lab's "advanced mode" or the decoder to inspect the parsed result.)
FF 00, OFF is 00 00; anything else is treated as an error by the slave.In this chapter you mastered the 4 write function codes: 05/06 write a single item, 15/16 write a batch; you learned the structure of a write request and the "verbatim echo = write acknowledgement" rule, and you can read every command in the greenhouse control table.
In the next chapter we put these "verbs" into a real envelope—seeing how they are packed into an RTU frame and sent out byte by byte. That is the story of Chapter 6.