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:
- 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.
- Read Phase Second: Immediately following the successful write, the slave device executes the read operation, extracting registers beginning at the Read Starting Address.
- 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.
- 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:
- Write a new speed setpoint (
1500 RPM = 0x05DC) into holding register40101(wire address0x0064). - Simultaneously read the drive's current actual velocity and motor temperature from registers
40001–40002(wire address0x0000, quantity 2).
Master Request Telegram (Modbus RTU):
01 17 00 00 00 02 00 64 00 01 02 05 DC 47 98
01: Slave Address (Device 1)17: Function Code 23 (0x17)00 00: Read Starting Address (0x0000 = Register 40001)00 02: Quantity to Read (2 Registers)00 64: Write Starting Address (0x0064 = Register 40101)00 01: Quantity to Write (1 Register)02: Write Byte Count (1 register × 2 = 2 bytes)05 DC: Value to Write (1500 RPM)47 98: 16-bit CRC Checksum (Low byte first)
Slave Response Telegram (Modbus RTU):
01 17 04 05 D0 01 C2 B9 A4
01: Slave Address (Device 1)17: Function Code 23 Echo04: Read Byte Count (2 registers × 2 = 4 bytes)05 D0: Register 40001 value (Actual Velocity = 1488 RPM)01 C2: Register 40002 value (Motor Temperature = 45.0 °C)B9 A4: 16-bit CRC Checksum
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):
- Transaction Identifier (2 Bytes): Sequence tracking ID generated by the client.
- Protocol Identifier (2 Bytes): Always
0x0000for standard Modbus. - Length Field (2 Bytes): Total byte count of remaining Unit ID + PDU bytes.
- Unit Identifier (1 Byte): Target device address (or gateway routing ID).
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):
- Exception Code 01 (Illegal Function): The slave device does not support Function Code 23 in its firmware. (Fallback: use separate FC 16 and FC 03 calls).
- Exception Code 02 (Illegal Data Address): Either the read starting address, write starting address, or block end address falls outside the device's mapped holding register range.
- Exception Code 03 (Illegal Data Value): Read quantity exceeds 125, write quantity exceeds 121, or the Write Byte Count does not match
Quantity × 2. - Exception Code 04 (Server Device Failure): An unrecoverable internal hardware or memory fault occurred while writing or reading.
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:
- Emulate physical slave devices that natively respond to FC 23 requests.
- Verify memory isolation between read and write register zones.
- Inspect decoded protocol fields and raw hex telemetry in real time.
- Simulate edge-case exception responses (0x01, 0x02, 0x03) to test client recovery resilience.
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 →