7 modbus_set_slave - set slave number in the context
12 *int modbus_set_slave(modbus_t *'ctx', int 'slave');*
17 The *modbus_set_slave()* function shall set the slave number in the libmodbus
20 The behavior depends of network and the role of the device:
23 Define the slave ID of the remote device to talk in master mode or set the
24 internal slave ID in slave mode. According to the protocol, a Modbus device must
25 only accept message holding its slave number or the special broadcast number.
28 The slave number is only required in TCP if the message must reach a device on a
29 serial network. Some not compliant devices or software (such as modpoll) uses
30 the slave ID as unit identifier, that's incorrect (cf page 23 of Modbus
31 Messaging Implementation Guide v1.0b) but without the slave value, the faulty
32 remote device or software drops the requests! The special value
33 `MODBUS_TCP_SLAVE` (0xFF) can be used in TCP mode to restore the default value.
35 The broadcast address is `MODBUS_BROADCAST_ADDRESS`. This special value must be
36 use when you want all Modbus devices of the network receive the request.
41 The function shall return 0 if successful. Otherwise it shall return -1 and set
42 errno to one of the values defined below.
48 The slave number is invalid.
57 ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
59 fprintf(stderr, "Unable to create the libmodbus context\n");
63 rc = modbus_set_slave(ctx, YOUR_DEVICE_ID);
65 fprintf(stderr, "Invalid slave ID\n");
70 if (modbus_connect(ctx) == -1) {
71 fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
79 The libmodbus documentation was written by Stéphane Raimbault
80 <stephane.raimbault@gmail.com>