Modbus Function Code 15 (Write Multiple Coils): Protocol Specification & Hex Analysis
In industrial control, discrete outputs command physical on/off devices: relays, solenoid valves, contactors, pilot indicator lights, and digital alarm triggers. While Function Code 05 writes a single coil at a time, high-speed automated systems cannot afford the latency of polling individual commands across RS-485 serial buses. Modbus Function Code 15 (0x0F / Write Multiple Coils) allows a master controller to command up to 1,968 binary outputs in a single, atomic network frame.
Whether configuring motor starter banks over Modbus RTU or triggering remote I/O modules over Modbus TCP port 502, mastering the byte-packing algorithms, zero-based wire addressing, and echo verification of Function Code 15 is essential for every automation and embedded engineer.
The Modbus Register Model: Where Does FC15 Fit?
The Modbus protocol defines four distinct, fundamental data memory tables. Function Code 15 operates exclusively on read/write 1-bit Discrete Outputs (Coils):
| Table / Object Type | PLC Address Range | Wire Address (Hex) | Access | Read Function Code | Write Function Code |
|---|---|---|---|---|---|
| Discrete Outputs (Coils) | 00001 – 09999 | 0x0000 – 0xFFFF | Read / Write | FC01 (0x01) | FC05 (Single), FC15 (Multiple) |
| Discrete Inputs | 10001 – 19999 | 0x0000 – 0xFFFF | Read-Only | FC02 (0x02) | None (Physical Inputs) |
| Input Registers | 30001 – 39999 | 0x0000 – 0xFFFF | Read-Only | FC04 (0x04) | None (Analog Inputs) |
| Holding Registers | 40001 – 49999 | 0x0000 – 0xFFFF | Read / Write | FC03 (0x03) | FC06 (Single), FC16 (Multiple) |
Request and Response Frame Structure (PDU Breakdown)
Under the Modbus Application Protocol Specification V1.1b3, the Protocol Data Unit (PDU) for Function Code 15 is structured as follows:
1. Function Code 15 Request PDU
| Field | Size (Bytes) | Permitted Values | Description |
|---|---|---|---|
| Function Code | 1 Byte | 0x0F (15 decimal) |
Identifies Write Multiple Coils command. |
| Starting Address | 2 Bytes | 0x0000 to 0xFFFF |
Zero-based memory address of first coil. |
| Quantity of Outputs | 2 Bytes | 0x0001 to 0x07B0 (1 to 1968) |
Total number of contiguous coils to write. |
| Byte Count | 1 Byte | N = ceil(Quantity / 8) |
Number of data bytes following in the request. |
| Outputs Value | N Bytes | Bit-packed data | Coil states (1 = ON, 0 = OFF), LSB first. |
2. Function Code 15 Response PDU
| Field | Size (Bytes) | Value | Description |
|---|---|---|---|
| Function Code | 1 Byte | 0x0F |
Echoes the requested function code. |
| Starting Address | 2 Bytes | 0x0000 to 0xFFFF |
Echoes the starting address written. |
| Quantity of Outputs | 2 Bytes | 0x0001 to 0x07B0 |
Echoes the quantity of coils written. |
Bit-Packing Mathematics: How Coils Are Mapped
Unlike holding registers where each register occupies two full bytes (16 bits), coils are binary (1 bit per coil). To maximize transmission speed, Modbus packs 8 coils into each data byte:
Byte Count = ceil(Quantity of Coils / 8)
Example 1: 10 Coils -> ceil(10 / 8) = 2 Bytes
Example 2: 16 Coils -> ceil(16 / 8) = 2 Bytes
Example 3: 20 Coils -> ceil(20 / 8) = 3 Bytes
The first coil addressed maps to the Least Significant Bit (LSB / Bit 0) of the first data byte. Subsequent coils fill bits in ascending order:
| Byte Position | Bit 7 (MSB) | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 (LSB) |
|---|---|---|---|---|---|---|---|---|
| Data Byte 1 | Coil 7 | Coil 6 | Coil 5 | Coil 4 | Coil 3 | Coil 2 | Coil 1 | Coil 0 (Start) |
| Data Byte 2 | Padded (0) | Padded (0) | Padded (0) | Padded (0) | Padded (0) | Padded (0) | Coil 9 | Coil 8 |
Practical Hex Walkthrough: Writing 10 Coils
Let us trace a real-world engineering transaction: writing 10 coils starting at PLC address 00020 (zero-based wire offset 0x0013) on Slave ID 1 over Modbus RTU.
Suppose we wish to set the 10 coils to the following states:
[ON, OFF, ON, ON, OFF, OFF, ON, OFF, ON, ON].
- Byte 1 (Coils 0 to 7): Binary
0100 1101= Hex0x4D - Byte 2 (Coils 8 to 9 + 6 padded bits): Binary
0000 0011= Hex0x03
Modbus RTU Request Frame
01 0F 00 13 00 0A 02 4D 03 [CRC16_LO] [CRC16_HI]
Breakdown:
01 = Slave ID 1
0F = Function Code 15 (Write Multiple Coils)
00 13 = Starting Address (Wire 0x0013 = PLC Coil 00020)
00 0A = Quantity of Coils (10 decimal)
02 = Byte Count (2 Bytes)
4D 03 = Outputs Value (4D = Coils 0-7, 03 = Coils 8-9)
E4 4E = CRC-16 Checksum
Modbus RTU Normal Response Frame
01 0F 00 13 00 0A [CRC16_LO] [CRC16_HI]
Breakdown:
01 = Slave ID 1
0F = Function Code 15 Echo
00 13 = Starting Address Echo (0x0013)
00 0A = Quantity of Coils Echo (10 decimal)
94 0D = CRC-16 Checksum
Modbus TCP Request Frame (Port 502)
Over Modbus TCP, the frame is prepended with the 7-byte MBAP header and the CRC-16 is omitted:
00 01 00 00 00 09 01 0F 00 13 00 0A 02 4D 03
Breakdown:
00 01 = Transaction ID (0x0001)
00 00 = Protocol Identifier (0x0000 = Modbus TCP)
00 09 = Length (9 bytes remaining in message)
01 = Unit Identifier (Slave 1)
0F = Function Code 15
00 13 = Starting Address (0x0013)
00 0A = Quantity of Coils (10)
02 = Byte Count (2)
4D 03 = Outputs Value
Exception Handling & Error Responses
When an invalid request is received, the slave returns an Exception Response where the function code has its highest bit set (0x0F + 0x80 = 0x8F):
| Exception Code | Name | Common Cause with FC15 |
|---|---|---|
01 |
Illegal Function | The target device firmware does not support writing multiple coils or has write operations disabled. |
02 |
Illegal Data Address | The starting address or the block (Starting Address + Quantity) exceeds the maximum physical coil address of the device. |
03 |
Illegal Data Value | The Quantity of Outputs is 0 or exceeds 1,968 (0x07B0), or Byte Count != ceil(Quantity / 8). |
04 |
Slave Device Failure | An unrecoverable internal hardware error occurred while the device was committing the coil states to physical relays or non-volatile memory. |
Python Implementation: Sending FC15 with pymodbus
The standard Python library pymodbus provides built-in support for Function Code 15 via the write_coils() method:
from pymodbus.client import ModbusTcpClient
# Connect to Modbus TCP Server / Simulator
client = ModbusTcpClient('127.0.0.1', port=502)
client.connect()
# Desired coil states (Boolean list)
coil_states = [True, False, True, True, False, False, True, False, True, True]
# Write 10 coils starting at address 19 (PLC 00020)
# slave=1 specifies Unit ID
response = client.write_coils(address=19, values=coil_states, slave=1)
if response.isError():
print(f"Modbus Exception: {response}")
else:
print(f"Success! Starting Address: {response.address}, Quantity: {response.count}")
client.close()
Testing & Simulating Function Code 15 with ModbusSimulator
Physical relay boards and PLC hardware are easily damaged during initial commissioning if erroneous coil patterns trigger conflicting interlocks. Using ModbusSimulator allows engineers to thoroughly validate SCADA scripts and PLC drivers in software:
- Real-Time Coil Table: Monitor discrete coil values dynamically updated on screen with instant visual indicators (green for ON, red for OFF).
- Hex Transaction Sniffer: Inspect incoming ADU/PDU frames with byte-by-byte breakdowns of Function Code 15, address offsets, and bit-packed payloads.
- Configurable Exception Simulation: Inject simulated Exception 02 (Illegal Address) or Exception 04 (Device Failure) to verify client error recovery workflows.
- Multi-Protocol Support: Test seamlessly over local TCP loopback (port 502), virtual serial COM port pairs, or network RS-485 adapters.
Simulate and Debug Modbus Networks with Ease
Download ModbusSimulator today to test Function Code 15, holding registers, coils, and industrial SCADA telemetry safely on your PC.
Download ModbusSimulator Free →Frequently Asked Questions
What is Modbus Function Code 15?
Modbus Function Code 15 (0x0F / Write Multiple Coils) is the standard command used by a Modbus master to write a contiguous sequence of discrete outputs (coils, 0xxxx range) into a slave device in a single frame.
How many coils can be written in a single FC15 request?
A master can command between 1 and 1,968 coils in a single request. The byte count is calculated as ceil(Quantity of Coils / 8).
How are coil states packed into bytes?
Coils are packed 8 per byte, starting with the first coil at Bit 0 (LSB) and advancing to Bit 7 (MSB). Unused bits in the final byte are padded with zeros.
What is the response to Function Code 15?
The slave echoes the function code (0x0F), the starting address, and the quantity of coils written.
What is the difference between Function Code 05 and 15?
FC05 writes exactly one coil using a 16-bit constant (0xFF00/0x0000). FC15 writes up to 1,968 coils simultaneously using bit-packed bytes for maximum bus efficiency.