1MODBUS_SET_SLAVE(3)            libmodbus Manual            MODBUS_SET_SLAVE(3)
2
3
4

NAME

6       modbus_set_slave - set slave number in the context
7

SYNOPSIS

9       int modbus_set_slave(modbus_t *ctx, int slave);
10

DESCRIPTION

12       The modbus_set_slave() function shall set the slave number in the
13       libmodbus context.
14
15       The behavior depends of network and the role of the device:
16
17       RTU
18           Define the slave ID of the remote device to talk in master mode or
19           set the internal slave ID in slave mode. According to the protocol,
20           a Modbus device must only accept message holding its slave number
21           or the special broadcast number.
22
23       TCP
24           The slave number is only required in TCP if the message must reach
25           a device on a serial network. Some not compliant devices or
26           software (such as modpoll) uses the slave ID as unit identifier,
27           that’s incorrect (cf page 23 of Modbus Messaging Implementation
28           Guide v1.0b) but without the slave value, the faulty remote device
29           or software drops the requests! The special value MODBUS_TCP_SLAVE
30           (0xFF) can be used in TCP mode to restore the default value.
31
32       The broadcast address is MODBUS_BROADCAST_ADDRESS. This special value
33       must be use when you want all Modbus devices of the network receive the
34       request.
35

RETURN VALUE

37       The function shall return 0 if successful. Otherwise it shall return -1
38       and set errno to one of the values defined below.
39

ERRORS

41       EINVAL
42           The slave number is invalid.
43

EXAMPLE

45           modbus_t *ctx;
46
47           ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
48           if (ctx == NULL) {
49               fprintf(stderr, "Unable to create the libmodbus context\n");
50               return -1;
51           }
52
53           rc = modbus_set_slave(ctx, YOUR_DEVICE_ID);
54           if (rc == -1) {
55               fprintf(stderr, "Invalid slave ID\n");
56               modbus_free(ctx);
57               return -1;
58           }
59
60           if (modbus_connect(ctx) == -1) {
61               fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
62               modbus_free(ctx);
63               return -1;
64           }
65

SEE ALSO

67       modbus_get_slave(3)
68

AUTHORS

70       The libmodbus documentation was written by Stéphane Raimbault
71       <stephane.raimbault@gmail.com>
72
73
74
75libmodbus v3.1.7                  07/20/2023               MODBUS_SET_SLAVE(3)
Impressum