Skip to content

Modbus Read Holding Registers Returns Exception Code 02

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about Modbus Read Holding Registers Returns Exception Code 02. We cover key concepts, practical examples, and best practices.

The Problem

Modbus Function Code 03 (Read Holding Registers) returns exception 02 (Illegal Data Address).

Quick Fix

Wrong

// Reading registers outside the slave's address range
mb.readHoldingRegisters(1, 500, 10, data);  // Address 500-509```

Exception 02: Illegal Data Address. Slave has fewer registers.


### Right

```cpp
#include <ModbusRtu.h>

Modbus master(1, Serial, 2);

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

void loop() {
  // Read Holding Registers (FC 03)
  uint16_t regs[20];

  // Start at address 0, read 10 registers
  int result = master.readHoldingRegisters(1, 0, 10, regs);

  if (result == 0) {
    for (int i = 0; i < 10; i++) {
      Serial.print("HReg ");
      Serial.print(i);
      Serial.print(": 0x");
      Serial.println(regs[i], HEX);
    }
  } else if (result == 2) {
    Serial.println("Error: Illegal Data Address (registers don't exist)");
    Serial.println("Available register range: 0-99");
  } else {
    Serial.print("Error: ");
    Serial.println(result);
  }
  delay(2000);
}```

HReg 0: 0x04D2 (1234) HReg 1: 0x15B8 (5560) ...HReg 9: 0x0032 (50)


## Prevention

Modbus FC 03 reads holding registers. Exception 02 (Illegal Data Address) occurs when the starting address + count exceeds the slave's register range. Exception 03 (Illegal Data Value) occurs when the count is 0 or exceeds the max (typically 125 registers per request). Always verify the slave's register map documentation for valid address ranges.

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 read per request?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>125 holding registers per FC 03 request. For more, split into multiple requests. The Modbus specification limits PDU size to 253 bytes.</p>
<h3 id="what-address-numbering">What address numbering?</h3><p>Protocol uses 0-based addresses. Coil 1 = address 0. Register documentation often uses 1-based but the PDU is 0-based.</p>
<h3 id="can-i-read-across-multiple-slaves">Can I read across multiple slaves?</h3><p>No. Each request targets one slave ID. For multiple slaves, send separate requests.</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro