1MODBUS_READ_REGISTER(3) Libmodbus Manual MODBUS_READ_REGISTER(3)
2
3
4
6 modbus_read_registers - read many registers
7
9 int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t
10 *dest);
11
13 The modbus_read_registers() function shall read the content of the nb
14 holding registers to the address addr of the remote device. The result
15 of reading is stored in dest array as word values (16 bits).
16
17 You must take care to allocate enough memory to store the results in
18 dest (at least nb * sizeof(uint16_t)).
19
20 The function uses the Modbus function code 0x03 (read holding
21 registers).
22
24 The modbus_read_registers() function shall return the number of read
25 registers if successful. Otherwise it shall return -1 and set errno.
26
28 EMBMDATA
29 Too many registers requested
30
32 modbus_t *ctx;
33 uint16_t tab_reg[64];
34 int rc;
35 int i;
36
37 ctx = modbus_new_tcp("127.0.0.1", 1502);
38 if (modbus_connect(ctx) == -1) {
39 fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
40 modbus_free(ctx);
41 return -1;
42 }
43
44 rc = modbus_read_registers(ctx, 0, 10, tab_reg);
45 if (rc == -1) {
46 fprintf(stderr, "%s\n", modbus_strerror(errno));
47 return -1;
48 }
49
50 for (i=0; i < rc; i++) {
51 printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
52 }
53
54 modbus_close(ctx);
55 modbus_free(ctx);
56
58 modbus_write_register(3) modbus_write_registers(3)
59
61 The libmodbus documentation was written by Stéphane Raimbault
62 <stephane.raimbault@gmail.com[1]>
63
65 1. stephane.raimbault@gmail.com
66 mailto:stephane.raimbault@gmail.com
67
68
69
70libmodbus 3.0.6 04/02/2014 MODBUS_READ_REGISTER(3)