Skip to content

Commit d5512d9

Browse files
committed
Improve doc about slave ID in RTU
1 parent 59d34d2 commit d5512d9

File tree

7 files changed

+52
-21
lines changed

7 files changed

+52
-21
lines changed

docs/modbus_new_rtu.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,24 @@ values are 5, 6, 7 and 8.
3535
The `stop_bits` argument specifies the bits of stop, the allowed values are 1
3636
and 2.
3737
38-
Once the `modbus_t` structure is initialized, you must set the slave of your
39-
device with [modbus_set_slave](modbus_set_slave.md) and connect to the serial bus with
40-
[modbus_connect](modbus_connect.md).
38+
Once the `modbus_t` structure is initialized, you can connect to the serial bus
39+
with [modbus_connect](modbus_connect.md).
40+
41+
In RTU, your program can act as server or client:
42+
43+
- **server** is called *slave* in Modbus terminology, your program will expose
44+
data to the network by processing and answering the requests of one of several
45+
clients. It up to you to define the slave ID of your service with
46+
[modbus_set_slave](modbus_set_slave.md), this ID should be used by the client
47+
to communicate with your program.
48+
49+
- **client** is called *master* in Modbus terminology, your program will send
50+
requests to servers to read or write data from them. Before issuing the
51+
requests, you should define the slave ID of the remote device with
52+
[modbus_set_slave](modbus_set_slave.md). The slave ID is not an argument of
53+
the read/write functions because it's very frequent to talk with only one
54+
server so you can set it once and for all. The slave ID it not used in TCP
55+
communications so this way the API is common to both.
4156
4257
## Return value
4358
@@ -53,22 +68,33 @@ defined below.
5368
5469
## Example
5570
71+
In this example, the program will open a serial communication on USB. All
72+
subsequent calls such as read or write of registers will be sent on the wire and
73+
the request will be visible to all connected devices. According to the Modbus
74+
protocol, only the master associated to slave ID 10 will process and answer your
75+
requests.
76+
5677
```c
78+
const int REMOTE_ID = 10;
5779
modbus_t *ctx;
80+
uint16_t tab_reg[10];
5881
5982
ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
6083
if (ctx == NULL) {
6184
fprintf(stderr, "Unable to create the libmodbus context\n");
6285
return -1;
6386
}
6487
65-
modbus_set_slave(ctx, YOUR_DEVICE_ID);
66-
6788
if (modbus_connect(ctx) == -1) {
6889
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
6990
modbus_free(ctx);
7091
return -1;
7192
}
93+
94+
modbus_set_slave(ctx, REMOTE_ID);
95+
96+
// Read 2 registers from address 0 of server ID 10.
97+
modbus_read_registers(ctx, 0, 2, tab_reg);
7298
```
7399

74100
## See also

docs/modbus_read_bits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ to the address `addr` of the remote device. The result of reading is stored in
1717
`dest` array as unsigned bytes (8 bits) set to `TRUE` or `FALSE`.
1818
1919
You must take care to allocate enough memory to store the results in `dest`
20-
(at least `nb` * sizeof(uint8_t)).
20+
(at least `nb * sizeof(uint8_t)`).
2121
2222
The function uses the Modbus function code 0x01 (read coil status).
2323

docs/modbus_read_input_bits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ bits to the address `addr` of the remote device. The result of reading is store
1717
in `dest` array as unsigned bytes (8 bits) set to `TRUE` or `FALSE`.
1818
1919
You must take care to allocate enough memory to store the results in `dest`
20-
(at least `nb` * sizeof(uint8_t)).
20+
(at least `nb * sizeof(uint8_t)`).
2121
2222
The function uses the Modbus function code 0x02 (read input status).
2323

docs/modbus_read_input_registers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ input registers to address `addr` of the remote device. The result of the
1717
reading is stored in `dest` array as word values (16 bits).
1818
1919
You must take care to allocate enough memory to store the results in `dest` (at
20-
least `nb` * sizeof(uint16_t)).
20+
least `nb * sizeof(uint16_t)`).
2121
2222
The function uses the Modbus function code 0x04 (read input registers). The
2323
holding registers and input registers have different historical meaning, but

docs/modbus_read_registers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ holding registers to the address `addr` of the remote device. The result of
1717
reading is stored in `dest` array as word values (16 bits).
1818
1919
You must take care to allocate enough memory to store the results in `dest`
20-
(at least `nb` * sizeof(uint16_t)).
20+
(at least `nb * sizeof(uint16_t)`).
2121
2222
The function uses the Modbus function code 0x03 (read holding registers).
2323

docs/modbus_set_slave.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@ int modbus_set_slave(modbus_t *ctx, int slave);
1515
The *modbus_set_slave()* function shall set the slave number in the libmodbus
1616
context.
1717
18-
The behavior depends of network and the role of the device:
19-
20-
*RTU*::
21-
Define the slave ID of the remote device to talk in master mode or set the
22-
internal slave ID in slave mode. According to the protocol, a Modbus device must
23-
only accept message holding its slave number or the special broadcast number.
24-
25-
*TCP*::
26-
The slave number is only required in TCP if the message must reach a device on a
27-
serial network. Some not compliant devices or software (such as modpoll) uses
28-
the slave ID as unit identifier, that's incorrect (cf page 23 of Modbus
18+
It is usually only required to set the slave ID in **RTU**. The meaning of this
19+
ID will be different if your program acts as client (master) or server (slave).
20+
21+
As **RTU client**, *modbus_set_slave()* sets the ID of the remote device you
22+
want to communicate. Be sure to set the slave ID before issuing any Modbus
23+
requests on the serial bus. If you communicate with several servers (slaves),
24+
you can set the slave ID of the remote device before each request.
25+
26+
As **RTU server**, the slave ID allows the various clients to reach your
27+
service. You should use a free ID, once set, this ID should be known by the
28+
clients of the network. According to the protocol, a Modbus device must only
29+
accept message holding its slave number or the special broadcast number.
30+
31+
In **TCP**, the slave number is only required if the message must reach a device
32+
on a serial network. Some not compliant devices or software (such as modpoll)
33+
uses the slave ID as unit identifier, that's incorrect (cf page 23 of Modbus
2934
Messaging Implementation Guide v1.0b) but without the slave value, the faulty
3035
remote device or software drops the requests! The special value
3136
`MODBUS_TCP_SLAVE` (0xFF) can be used in TCP mode to restore the default value.

docs/modbus_write_and_read_registers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ to the address `read_addr` of the remote device. The result of reading is stored
2323
in `dest` array as word values (16 bits).
2424

2525
You must take care to allocate enough memory to store the results in `dest`
26-
(at least `nb` * sizeof(uint16_t)).
26+
(at least `nb * sizeof(uint16_t)`).
2727

2828
The function uses the Modbus function code 0x17 (write/read registers).
2929

0 commit comments

Comments
 (0)