Writing Function Codes

Master the write function codes FC05/06/15/16 — write single coil, write single register, write multiple coils, write multiple registers — and the echo-back write confirmation

Chapter Objectives

Scenario: Ms. Wang Wants to "Take Action"

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.

Why It Matters

"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.

Core Concepts: The 4 Write Function Codes

CodeHexActionWhat it writesQuadrant
050x05Write Single CoilOne 1-bit switchwrite × single
060x06Write Single RegisterOne 16-bit valuewrite × single
150x0FWrite Multiple CoilsMultiple 1-bit switcheswrite × multiple
160x10Write Multiple RegistersMultiple 16-bit valueswrite × 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).

Structure of a Write Request and a Write Response

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.

Protocol Details and Byte-Level Examples

FC 06: Write Single Register (Request → Echo)

Request—write 100 (0x0064) into holding register 0x0000 of slave 1:

■ RTU Address ■ Function Code ■ Data ■ CRC
Slave 1 01
FC 06: Write Single Register 06
Register address 0x0000 00 00
Value written = 100 (0x0064) 00 64
CRC-16 88 21
ByteValueMeaning
001Slave 1
106Function code: Write Single Register
2–300 00Register address
4–500 64Value written
6–788 21CRC-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.

FC 05: Write Single Coil (ON = FF 00)

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.

FC 15: Write Multiple Coils (Bit Packing)

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.

FC 16: Write Multiple Registers

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.

Real-World Applications

Case 1: The Greenhouse "Control" Operation Table (Running Case)

What Ms. Wang wants to doFunction codeRequest (complete RTU frame)
Turn on the exhaust fanFC 0501 05 00 00 FF 00 8C 3A
Set the humidity target to 60FC 0601 06 00 01 00 3C D8 1B
Start 3 devices with one commandFC 1501 0F 00 00 00 03 01 0B CE 90
Write temperature/humidity limits in bulkFC 1601 10 00 00 00 02 04 00 64 00 C8 B3 E6

Case 2: The VFD Side Case—Writing the Speed

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.

Hands-On Lab Guide

The Lab wizard currently supports FC 05 / 06 (write single); practise them one at a time:

  1. FC 06: write 60 to 0x0001 → expected request 01 06 00 01 00 3C D8 1B, and the response should be a verbatim echo;
  2. FC 05: write coil 0x0000 to ON → expected request 01 05 00 00 FF 00 8C 3A; note that ON is FF 00;
  3. Compare the request and the response byte by byte—identical means the write succeeded.

(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.)

Common Misconceptions

Quiz Anchors

  1. How does the response to an FC06 write single register relate to the request? Why is it designed that way?
  2. To set coils 2 and 4 to ON (all others OFF), what should the FC15 data byte be?
  3. If you write to an input register address that only supports read-only FC04, what exception code will the slave most likely return?

Chapter Summary and What's Next

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.