Skip to content

Modbus Write Multiple Registers Causes Partial Write

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about Modbus Write Multiple Registers Causes Partial Write. We cover key concepts, practical examples, and best practices.

The Problem

Modbus FC 16 (Write Multiple Registers) succeeds but only some registers are written.

Quick Fix

Wrong

// Writing 150 registers in one request — exceeds buffer
uint16_t data[150];
mb.writeMultipleRegisters(1, 0, 150, data);```

Exception 03: Illegal Data Value (request too large) or partial write.


### Right

```cpp
#include <ModbusRtu.h>

Modbus master(1, Serial, 2);

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

void loop() {
  // Always verify the max registers per request (typically 123)
  const int MAX_PER_REQUEST = 120;  // Safe limit

  uint16_t data[120];
  for (int i = 0; i < 120; i++) {
    data[i] = i * 10;
  }

  // Write in chunks of MAX_PER_REQUEST
  int remaining = 200;  // Total registers to write
  int address = 0;

  while (remaining > 0) {
    int chunk = (remaining > MAX_PER_REQUEST) ? MAX_PER_REQUEST : remaining;

    int result = master.writeMultipleRegisters(
      1, address, chunk, &data[address]);

    if (result == 0) {
      Serial.print("Wrote ");
      Serial.print(chunk);
      Serial.print(" registers at ");
      Serial.println(address);
    } else {
      Serial.print("Error at address ");
      Serial.print(address);
      Serial.print(": ");
      Serial.println(result);
      break;
    }

    address += chunk;
    remaining -= chunk;
  }
  delay(5000);
}```

Wrote 120 registers at 0 Wrote 80 registers at 120 (Chunked write succeeds)


## Prevention

Modbus FC 16 writes multiple holding registers. Max registers per request: 123 (limited by 253-byte PDU). Writing more than the max causes Exception 03. Always chunk large writes. The response echoes the start address and count — verify by reading back. Some devices have a minimum response time between writes (5-10 ms for flash storage).

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">### What is the max registers per FC 16?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>123 holding registers. Calculated: (253 - 5) / 2 = 124, minus overhead = ~120. Formula: (PDU max - 5) / 2.</p>
<h3 id="does-fc-16-support-non-contiguous-addresses">Does FC 16 support non-contiguous addresses?</h3><p>No. All registers must be in a contiguous block. For scattered addresses, send multiple FC 06 requests.</p>
<h3 id="is-fc-16-atomic">Is FC 16 atomic?</h3><p>The Modbus spec does not guarantee atomic writes. If the request contains many registers, some may be written before an error on a later one. Verify the complete range after write.</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro