modbus_read_registers.txt
1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
modbus_read_registers(3)
========================
NAME
----
modbus_read_registers - read many registers
SYNOPSIS
--------
*int modbus_read_registers(modbus_t *'ctx', int 'addr', int 'nb', uint16_t *'dest');*
DESCRIPTION
-----------
The *modbus_read_registers()* function shall read the content of the _nb_
holding registers to the address _addr_ of the remote device. The result of
reading is stored in _dest_ array as word values (16 bits).
You must take care to allocate enough memory to store the results in _dest_
(at least _nb_ * sizeof(uint16_t)).
The function uses the Modbus function code 0x03 (read holding registers).
RETURN VALUE
------------
The function shall return the number of read registers
if successful. Otherwise it shall return -1 and set errno.
ERRORS
------
*EMBMDATA*::
Too many registers requested
EXAMPLE
-------
[source,c]
-------------------
modbus_t *ctx;
uint16_t tab_reg[64];
int rc;
int i;
ctx = modbus_new_tcp("127.0.0.1", 1502);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
rc = modbus_read_registers(ctx, 0, 10, tab_reg);
if (rc == -1) {
fprintf(stderr, "%s\n", modbus_strerror(errno));
return -1;
}
for (i=0; i < rc; i++) {
printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
}
modbus_close(ctx);
modbus_free(ctx);
-------------------
SEE ALSO
--------
linkmb:modbus_write_register[3]
linkmb:modbus_write_registers[3]
AUTHORS
-------
The libmodbus documentation was written by Stéphane Raimbault
<stephane.raimbault@gmail.com>