Modbus Slave Not Responding to Master
DodaTech
Updated 2026-06-26
1 min read
In this tutorial, you'll learn about Modbus Slave Not Responding to Master. We cover key concepts, practical examples, and best practices.
The Problem
Modbus slave (server) ignores or does not respond to master requests.
Quick Fix
Wrong
// Slave doesn't register any data or mapping tables
Master gets no response or exception 0x02 (Illegal Data Address).
Right
#include <modbus/modbus.h>
#include <stdio.h>
int main() {
int listening_socket;
modbus_t *ctx;
modbus_mapping_t *mb_mapping;
// TCP slave on port 502
ctx = modbus_new_tcp('0.0.0.0', 502);
// Create mapping: coils, discrete inputs, holding registers, input registers
mb_mapping = modbus_mapping_new_start_address(
0, 100, // Coils (0-99)
0, 100, // Discrete inputs (0-99)
0, 100, // Holding registers (0-99)
0, 100); // Input registers (0-99)
if (mb_mapping == NULL) {
fprintf(stderr, 'Failed to allocate mapping\n');
return -1;
}
// Set initial values
mb_mapping->tab_registers[0] = 1234; // Holding register 0 = 1234
listening_socket = modbus_tcp_listen(ctx, 1);
modbus_tcp_accept(ctx, &listening_socket);
for (;;) {
uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
int rc = modbus_receive(ctx, query);
if (rc > 0) {
modbus_reply(ctx, query, rc, mb_mapping);
}
}
}
Slave responds to master read/write requests with correct data.
Prevention
Create a modbus_mapping_t that maps the address space. Match the mapping addresses to what the master requests. For RTU: use modbus_new_rtu() and modbus_connect(). Check RS-485 wiring: A/A+/D+ to B/B-/D- crossover for multi-drop. Add termination resistors (120 Ohm) on long lines.
DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro