MODBUS_SET_SLAVE(3) | libmodbus Manual | MODBUS_SET_SLAVE(3) |
modbus_set_slave - set slave number in the context
int modbus_set_slave(modbus_t *ctx, int slave);
The modbus_set_slave() function shall set the slave number in the libmodbus context.
The behavior depends of network and the role of the device:
RTU
TCP
The broadcast address is MODBUS_BROADCAST_ADDRESS. This special value must be use when you want all Modbus devices of the network receive the request.
The function shall return 0 if successful. Otherwise it shall return -1 and set errno to one of the values defined below.
EINVAL
modbus_t *ctx; ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1); if (ctx == NULL) {
fprintf(stderr, "Unable to create the libmodbus context\n");
return -1; } rc = modbus_set_slave(ctx, YOUR_DEVICE_ID); if (rc == -1) {
fprintf(stderr, "Invalid slave ID\n");
modbus_free(ctx);
return -1; } if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1; }
The libmodbus documentation was written by Stéphane Raimbault <stephane.raimbault@gmail.com>
11/20/2021 | libmodbus v3.1.4 |