Skip to content

Modbus Master Cannot Communicate with Slave

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about Modbus Master Cannot Communicate with Slave. We cover key concepts, practical examples, and best practices.

The Problem

Modbus master (client) cannot establish communication with a Modbus slave (server).

Quick Fix

Wrong

modbus_new_rtu('/dev/ttyUSB0', 9600, 'N', 8, 1);  # Wrong serial params
Master gets no response or timeout on all requests.
#include <modbus/modbus.h>
#include <stdio.h>
#include <errno.h>

int main() {
  modbus_t *ctx;
  uint16_t tab_reg[64];
  
  // RTU mode — match slave settings
  ctx = modbus_new_rtu('/dev/ttyUSB0', 115200, 'N', 8, 1);
  if (ctx == NULL) {
    fprintf(stderr, 'Unable to create RTU context\n');
    return -1;
  }
  
  modbus_set_slave(ctx, 1);  // Slave ID
  modbus_set_timeout(ctx, 1, 0);  // 1 second timeout
  
  if (modbus_connect(ctx) == -1) {
    fprintf(stderr, 'Connection failed: %s\n', modbus_strerror(errno));
    modbus_free(ctx);
    return -1;
  }
  
  // Read holding registers starting at 0, count 10
  int rc = modbus_read_registers(ctx, 0, 10, tab_reg);
  if (rc == -1) {
    fprintf(stderr, 'Read failed: %s\n', modbus_strerror(errno));
  } else {
    printf('Read %d registers\n', rc);
  }
  
  modbus_close(ctx);
  modbus_free(ctx);
  return 0;
}
Read 10 registers (successful Modbus RTU communication).

Prevention

Match serial settings (baud, parity, data bits, stop bits) between master and slave. Set correct slave ID. Check serial device: /dev/ttyUSB0, /dev/ttyS0, /dev/ttyAMA0. Modbus RTU uses RS-485 half-duplex (DE/RE pin control). Modbus TCP uses Ethernet (port 502).

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

FAQ

### What is the difference between Modbus RTU and TCP?

RTU: serial (RS-232/485), binary encoding, CRC-16. TCP: Ethernet, TCP port 502, no CRC (TCP handles integrity).

What is the default slave ID?

Each slave has a unique ID (1-247). ID 0 is broadcast. ID 248-255 are reserved. Common default: 1.

What is the purpose of modbus_set_slave()?

Sets the slave ID for the request. The master addresses one slave per request. For broadcasts, use ID 0.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro