Skip to content

Modbus Function Read Write

DodaTech 1 min read

In this tutorial, you'll learn about Modbus Read/Write Multiple Registers Not Working. We cover key concepts, practical examples, and best practices.

The Problem

Modbus FC 23 (Read/Write Multiple Registers) performs only read or only write.

Quick Fix

Wrong

// Separate FC 03 and FC 06 — not using combined FC 23
uint16_t data[1];
mb.readHoldingRegisters(1, 0, 1, data);
mb.writeRegister(1, 0, newValue);```

Read and write work but require two round trips. Slower.


### Right

```cpp
#include <ModbusRtu.h>

Modbus master(1, Serial, 2);

void setup() {
  master.begin(9600, SERIAL_8N1);
  Serial.begin(9600);
}

void loop() {
  // FC 23: Read/Write Multiple Registers
  // Read 5 registers starting at 0
  // Write 2 registers starting at 10
  uint16_t writeData[2] = {500, 600};
  uint16_t readData[5] = {0};

  int result = master.readWriteMultipleRegisters(
    1,             // Slave ID
    0, 5,          // Read: start addr, count
    10, 2,         // Write: start addr, count
    readData, writeData
  );

  if (result == 0) {
    Serial.println("FC 23 success:");
    for (int i = 0; i < 5; i++) {
      Serial.print("  Read[");
      Serial.print(i);
      Serial.print("]: ");
      Serial.println(readData[i]);
    }
  } else {
    Serial.print("FC 23 error: ");
    Serial.println(result);
  }
  delay(2000);
}```

FC 23 success: Read[0]: 1234 Read[1]: 5678 Read[2]: 500 (updated) Read[3]: 600 (updated) Read[4]: 900


## Prevention

Modbus FC 23 combines read and write in one request, reducing round trips. The write is applied BEFORE the read — so the read reflects the new values. Read and write address ranges must not overlap in the same request. Not all slaves support FC 23 (it's optional). Max registers: 125 read + 121 write (PDU limit).

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

## FAQ

<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">### When to use FC 23?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>When you need to atomically update configuration and verify it. The write-then-read sequence guarantees you read the new values.</p>
<h3 id="does-fc-23-work-on-all-devices">Does FC 23 work on all devices?</h3><p>No. FC 23 is optional and not implemented on all Modbus devices. Check device documentation. Fall back to FC 03 + FC 06/16 if FC 23 fails with Exception 01.</p>
<h3 id="can-read-and-write-ranges-overlap">Can read and write ranges overlap?</h3><p>No. The read and write address ranges must not overlap. The result is undefined if they do.</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro