Modbus Function Code 21 (0x15): Write File Record Specification & Hex Guide

When configuring batch recipes, protection relay settings, or calibration tables across extended 6xxxx memory, standard Write Multiple Registers (FC 16) cannot address numbered archive files. The companion to Function Code 20 (Read File Record) in the Modbus Application Protocol Specification V1.1b3 is Function Code 21 (0x15) — Write File Record.

Function Code 21 writes one or more groups of consecutive 16-bit registers into numbered files (0x0001–0xFFFF) and confirms success by echoing the full request back to the master. Below is the byte-level PDU structure, TCP/RTU hex example, exception table, Python pymodbus snippet, and testing workflow in ModbusSimulator.

1. How Function Code 21 Works (V1.1b3 Specification)

Each Function Code 0x15 PDU contains a 1-byte Request Data Length (0x09–0xFB, i.e., 9 to 251 bytes) followed by one or more variable-length Sub-Request blocks:

  • Reference Type (1 Byte): Must always equal 0x06 for extended file record space.
  • File Number (2 Bytes): Unsigned 16-bit file index (0x0001 to 0xFFFF).
  • Record Number (2 Bytes): Starting 0-based record offset inside the file (0x0000 to 0x270F / 0–9,999).
  • Record Length (2 Bytes): Number of consecutive 16-bit registers (N) to write.
  • Record Data (2 × N Bytes): Big-Endian 16-bit register words to commit.

2. Request & Response Hex Frame Analysis

Suppose a SCADA master writes 3 registers (0x0601, 0x0300, 0x0384) into File Number 4 (0x0004) starting at Record Number 7 (0x0007) on Unit ID 1.

Modbus RTU Request Frame (18 Bytes)

[Slave] [FC] [ReqLen] [Ref] [FileNo] [RecNo] [RecLen] [Word0] [Word1] [Word2] [CRC-16]
  01     15     0D      06    00 04    00 07   00 03    06 01   03 00   03 84   [CRC]

Modbus TCP Request & Normal Echo Response ADU (21 Bytes)

[Trans ID] [Proto] [Len]  [Unit] [FC] [ReqLen] [Ref] [FileNo] [RecNo] [RecLen] [Data (6B)]
  00 01     00 00  00 0F   01     15     0D      06    00 04    00 07   00 03    06 01 03 00 03 84
Field Hex Value Size & Meaning
Function Code0x151 byte — Write File Record (decimal 21)
Request Data Length0x0D1 byte — Total sub-request bytes following (7 + 6 = 13)
Reference Type0x061 byte — Mandatory extended file record identifier
File & Record No.00 04 / 00 074 bytes — File 4, starting at Record 7
Record Length & Data00 03 + 6 bytesWrites 3 consecutive 16-bit Big-Endian registers; normal response echoes request

3. Standard Exception Codes for FC 21

If validation fails, the device responds with function code 0x95 (0x15 | 0x80) and a standard Modbus exception code:

Exception Code Name Root Cause in FC 21
0x01Illegal FunctionTarget PLC or RTU does not implement Function Code 21.
0x02Illegal Data AddressFile Number is read-only/missing or Record Number + Length exceeds file capacity.
0x03Illegal Data ValueReference Type != 0x06, mismatched Request Data Length, or PDU > 253 bytes.
0x04Server Device FailureNon-volatile flash/EEPROM write failure or write-protect interlock active.

4. Python Implementation Using Pymodbus

from pymodbus.client import ModbusTcpClient
from pymodbus.file_message import FileRecord

def write_plc_file_record(host="127.0.0.1", port=502, file_no=4, record_no=7, slave_id=1):
    client = ModbusTcpClient(host=host, port=port, timeout=2.0)
    if not client.connect():
        raise ConnectionError(f"Failed to connect to {host}:{port}")

    payload = [0x0601, 0x0300, 0x0384]
    record_req = FileRecord(
        file_number=file_no,
        record_number=record_no,
        record_length=len(payload),
        record_data=payload,
    )
    resp = client.write_file_record([record_req], slave=slave_id)
    if resp.isError():
        print(f"FC 21 Error: {resp}")
    else:
        print(f"Write confirmed for File {file_no} Record {record_no}")

    client.close()

if __name__ == "__main__":
    write_plc_file_record()

5. Verifying Modbus Frames With ModbusSimulator

Always validate register byte order, timeout thresholds, and exception responses in a safe virtual environment before touching live industrial controllers. Test your TCP and RTU endpoints with ModbusSimulator (Windows Modbus emulator, Modbus slave simulator, and Modbus TCP simulator), and review companion guides on FC 20 (Read File Record) and Modbus Data Types Explained.

Test Modbus TCP & RTU Devices Without Hardware

Download ModbusSimulator for Windows — unified Master + Slave simulator with live register grids, hex frame capture, and 30-day free trial.

Download Free 30-Day Trial

Frequently Asked Questions

What is Modbus Function Code 21 (0x15)?

Function Code 21 is Write File Record, used to write 16-bit register blocks into numbered 6xxxx extended memory files on PLCs, relays, and RTUs.

How do you confirm an FC 21 write succeeded?

A compliant server returns a normal response whose PDU echoes the exact sub-request bytes sent by the master.

What is the difference between FC 16 and FC 21?

FC 16 writes to flat 4xxxx holding registers, whereas FC 21 targets structured 6xxxx files indexed by both File Number and Record Number.

What triggers Exception Code 0x03 in FC 21?

An invalid Reference Type (anything other than 0x06), inconsistent byte length, or exceeding the 253-byte Modbus PDU limit returns 0x95 03.