Modbus Function Code 23 (Read/Write Multiple Registers): Protocol Specification & Hex Analysis

In high-performance industrial automation, timing and bus efficiency are paramount. When controlling high-speed servo drives, variable frequency drives (VFDs), robotics, or closed-loop PID process controllers, transmitting a separate Function Code 16 (Write Multiple Registers) request followed by a Function Code 03 (Read Holding Registers) poll creates unacceptable latency. Half-duplex RS-485 transceiver turnaround delays and TCP acknowledgement round-trips consume precious milliseconds.

To solve this latency bottleneck, the official Modbus Application Protocol Specification defines Function Code 23 (0x17 Hex): Read/Write Multiple Registers. This advanced function code executes an atomic write and read operation in a single request-response exchange. In this comprehensive technical guide, we break down the exact byte-level packet formats, execution order rules, error handling, Python pymodbus scripts, and simulation techniques.

Test Modbus Function Code 23 Without Physical Hardware

Simulate combined read/write register operations, inspect raw hex telegrams, and test PLC communications in real time with ModbusSimulator for Windows 10/11.

Download Modbus Simulator Free →

How Function Code 23 Operates (Execution Order Rules)

Function Code 23 operates exclusively on 4xxxx Holding Registers. The standard specifies a strict sequence of internal execution inside the slave (server) device:

  1. Atomic Write Phase First: The slave device receives the PDU and executes the write operation first. The specified values are written into the holding registers beginning at the Write Starting Address.
  2. Read Phase Second: Immediately following the successful write, the slave device executes the read operation, extracting registers beginning at the Read Starting Address.
  3. Overlapping Memory Behavior: If the read register block overlaps the write register block, the read operation returns the newly written values, not the previous values.
  4. All-or-Nothing Transaction: If any error occurs during either the write or read validation (such as an address out of range or illegal quantity), no registers are modified, and an exception response is returned.

Function Code 23 Packet Structure (PDU Breakdown)

The Modbus Protocol Data Unit (PDU) for Function Code 23 contains parameters for both the read operation and write operation. Below is the precise byte-level specification:

1. FC 23 Request PDU Format

Field Length Hex / Value Description
Function Code 1 Byte 0x17 Function Code 23 (Read/Write Multiple Registers)
Read Starting Address 2 Bytes 0x0000 to 0xFFFF 0-based wire address of holding registers to read
Quantity to Read 2 Bytes 0x0001 to 0x007D Number of registers to read (1 to 125 registers)
Write Starting Address 2 Bytes 0x0000 to 0xFFFF 0-based wire address of holding registers to write
Quantity to Write 2 Bytes 0x0001 to 0x0079 Number of registers to write (1 to 121 registers)
Write Byte Count 1 Byte 2 × N Number of data bytes following (Quantity to Write × 2)
Write Registers Value N × 2 Bytes Variable 16-bit register values to write (Big-Endian high byte first)

2. FC 23 Normal Response PDU Format

Field Length Hex / Value Description
Function Code 1 Byte 0x17 Echo of Function Code 23
Read Byte Count 1 Byte 2 × M Number of data bytes in response (Quantity to Read × 2)
Read Registers Value M × 2 Bytes Variable 16-bit register values read from slave device

Real-World Example: Modbus RTU Hex Telegram

Consider a motion control scenario: A master PLC communicates with a multi-axis servo drive at Slave ID 1 over RS-485. The master needs to:

Master Request Telegram (Modbus RTU):

01 17 00 00 00 02 00 64 00 01 02 05 DC 47 98

Slave Response Telegram (Modbus RTU):

01 17 04 05 D0 01 C2 B9 A4

Modbus TCP Framing Differences

On Ethernet networks (Modbus TCP), the slave address and CRC-16 checksum are omitted. Instead, the PDU is prefixed by the 7-byte MBAP Header (Modbus Application Protocol Header):

Exception Handling for Function Code 23

If the slave device cannot complete the requested operation, it returns an exception response with the high bit set on the function code (0x17 | 0x80 = 0x97):

Python Implementation Using PyModbus

You can easily execute Function Code 23 requests in Python using the standard pymodbus library:

from pymodbus.client import ModbusTcpClient

# Connect to Modbus TCP Server or Simulator on port 502
client = ModbusTcpClient('127.0.0.1', port=502)
client.connect()

# Read 2 registers starting at 0x0000, write [1500] to register 0x0064
response = client.readwrite_registers(
    read_address=0x0000,
    read_count=2,
    write_address=0x0064,
    values=[1500],
    slave=1
)

if not response.isError():
    print(f"Read Registers: {response.registers}")
else:
    print(f"Modbus Exception: {response}")

client.close()

Simulating Function Code 23 with ModbusSimulator

Implementing and debugging Function Code 23 on production PLCs without testing carries significant operational risks. Inadvertently writing to wrong register blocks can trip safety interlocks or stall machinery.

With ModbusSimulator, you can:

Ready to Test Modbus Function Code 23?

Download ModbusSimulator for Windows 10/11 today. Full support for Modbus RTU, Modbus TCP, Function Codes 01–23, and dynamic analog register patterns.

Download Free Trial →