Skip to content

Modbus Tcp Unit Id

DodaTech 1 min read

In this tutorial, you'll learn about Modbus TCP Unit ID Mismatch Causes No Response. We cover key concepts, practical examples, and best practices.

The Problem

Modbus TCP requests with wrong Unit ID are ignored by the server.

Quick Fix

Wrong

// Client sends request with Unit ID 0 (broadcast-like)
mb.readHoldingRegisters(0, 0, 1, data);  // Unit ID 0```

Server ignores request. No response sent.


### Right

```cpp
#include <ModbusIP.h>

ModbusIP mb;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);

  // Connect to Modbus TCP server
  mb.connect("192.168.1.100");

  // Send request with correct Unit ID
  // Parameter order: unitId, startAddr, count, destArray
  uint16_t data[10];

  int result = mb.readHoldingRegisters(1, 0, 5, data);
  if (result == 0) {
    Serial.println("Unit ID 1 responded");
    for (int i = 0; i < 5; i++) {
      Serial.println(data[i]);
    }
  }

  // Try multiple Unit IDs for gateway scenarios
  for (int id = 1; id <= 10; id++) {
    int res = mb.readHoldingRegisters(id, 0, 1, data);
    if (res == 0) {
      Serial.print("Found device at Unit ID ");
      Serial.println(id);
    }
  }
}

void loop() { mb.task(); }```

Unit ID 1 responded: 1234 5678 ... Found device at Unit ID 1 Found device at Unit ID 3


## Prevention

Modbus TCP embeds a Unit ID (slave address) in the MBAP header. This is used for gateway scenarios (Modbus TCP → Modbus RTU) where the Unit ID identifies the downstream RTU slave. For direct TCP devices, Unit ID is typically 1 or 0xFF (ignored). Unit ID 0 may cause no response as some servers treat it as broadcast (process only, no response).

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 Unit ID for?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>In Modbus TCP to RTU gateways, the Unit ID identifies which RTU slave on the downstream bus the request targets. The gateway strips the TCP header and sends the RTU frame with that slave address.</p>
<h3 id="what-if-the-server-ignores-unit-id">What if the server ignores Unit ID?</h3><p>Some TCP servers accept any Unit ID and treat it like 0xFF (ignored). Others validate. Check the server documentation.</p>
<h3 id="can-the-server-have-gt247-slaves">Can the server have &gt;247 slaves?</h3><p>Yes. Modbus TCP supports 65535 Unit IDs. An RTU gateway can bridge to multiple RTU buses, each with 1-247 slaves.</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro