Skip to content

Modbus Rtu Slave

DodaTech 1 min read

In this tutorial, you'll learn about Modbus RTU Slave Not Responding to Master. We cover key concepts, practical examples, and best practices.

The Problem

Modbus RTU slave does not process or respond to master requests.

Quick Fix

Wrong

#include <ModbusRtu.h>

Modbus slave(1, Serial, 2);
// Slave not configured to respond to any function codes```

Slave receives request but does not respond.


### Right

```cpp
#include <ModbusRtu.h>

// Slave holding register array
uint16_t holdingRegs[10] = {0};

Modbus slave(1, Serial, 2);  // Slave ID 1

void setup() {
  holdingRegs[0] = 1234;   // Initial value
  holdingRegs[1] = 5678;

  slave.begin(9600, SERIAL_8N1);
  slave.setTimeOut(1000);

  pinMode(2, OUTPUT);

  // Set callbacks for request handling
  slave.onRequest([](int fc, int addr, int len) {
    Serial.print("FC: "); Serial.print(fc);
    Serial.print(" ADDR: "); Serial.print(addr);
    Serial.print(" LEN: "); Serial.println(len);
  });
}

void loop() {
  // Slave polls for incoming requests
  slave.poll(holdingRegs, 10);
}```

Master reads Holding Register 0 → gets 1234. Master writes Register 1 = 9999 → verified on next read.


## Prevention

Modbus RTU slave must be configured with a unique Slave ID (1-247). The slave.poll() function processes incoming requests and reads/writes the register array. The register array holds all accessible holding registers. Register count must match what the master expects. The slave handles function codes: 01 (Read Coils), 02 (Read Inputs), 03 (Read Holding Registers), 04 (Read Input Registers), 05 (Write Single Coil), 06 (Write Single Register), 15 (Write Multiple Coils), 16 (Write Multiple Registers).

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 slave address range?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>1-247 (valid). 0 = broadcast (slaves process but don't respond). 248-255 = reserved.</p>
<h3 id="how-many-registers-can-a-slave-have">How many registers can a slave have?</h3><p>Typically up to 65536 per type (holding, input, coil, discrete). Practical limit: ~1000 in most MCU implementations.</p>
<h3 id="does-slave-need-rede-control">Does slave need RE/DE control?</h3><p>Yes, if using RS485. The slave should only enable the driver when responding. Most Modbus libraries handle this automatically in poll().</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro