I tested my ADAM-6350 module using QModMaster, and all read and write operations worked correctly, confirming that the module is functioning as expected. However, when I try to perform the same read and write operations using the provided C program with the libmodbus library, it does not work as intended. Specifically, while the program successfully connects to the module and modbus_write_bit returns no errors when writing to a coil, subsequent calls to modbus_read_bits always return a value of 0, regardless of the value written. Could this issue be related to incorrect coil or register addresses, specific initialization requirements for the ADAM-6350, or differences in how QModMaster handles Modbus TCP compared to libmodbus? Are there specific steps to verify the correct Modbus configuration or address mappings for the ADAM-6350 when using a custom program? Additionally, could there be library-specific nuances or settings I need to adjust to ensure proper communication?
#include <modbus.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define SERVER_IP "192.168.1.250"
#define SERVER_PORT 502
const uint16_t COIL_ADDRESS = 0x0001; // Address for a single coil
int main() {
int rc;
modbus_t *ctx;
uint8_t coil_value;
// Create Modbus context
ctx = modbus_new_tcp(SERVER_IP, SERVER_PORT);
if (ctx == NULL) {
fprintf(stderr, "Unable to create Modbus context: %s\n", modbus_strerror(errno));
return -1;
}
// Connect to Modbus server
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Unable to connect to Modbus server: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
printf("Connected to Modbus server %s:%d\n", SERVER_IP, SERVER_PORT);
// Write to a single coil
printf("Writing to coil at address 0x%04X...\n", COIL_ADDRESS);
rc = modbus_write_bit(ctx, COIL_ADDRESS, 1); // Turn the coil ON
if (rc == -1) {
fprintf(stderr, "Error writing to coil: %s\n", modbus_strerror(errno));
modbus_close(ctx);
modbus_free(ctx);
return -1;
}
printf("Successfully wrote to coil at address 0x%04X.\n", COIL_ADDRESS);
// Read the single coil
printf("Reading coil at address 0x%04X...\n", COIL_ADDRESS);
rc = modbus_read_bits(ctx, COIL_ADDRESS, 1, &coil_value);
if (rc == -1) {
fprintf(stderr, "Error reading coil: %s\n", modbus_strerror(errno));
modbus_close(ctx);
modbus_free(ctx);
return -1;
}
printf("Read coil value at address 0x%04X: %d\n", COIL_ADDRESS, coil_value);
// Close the connection
modbus_close(ctx);
modbus_free(ctx);
return 0;
}