1 # modbus_read_registers
5 modbus_read_registers - read many registers
10 int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
15 The *modbus_read_registers()* function shall read the content of the `nb`
16 holding registers to the address `addr` of the remote device. The result of
17 reading is stored in `dest` array as word values (16 bits).
19 You must take care to allocate enough memory to store the results in `dest`
20 (at least `nb * sizeof(uint16_t)`).
22 The function uses the Modbus function code 0x03 (read holding registers).
26 The function shall return the number of read registers
27 if successful. Otherwise it shall return -1 and set errno.
31 - *EMBMDATA*, too many registers requested.
41 ctx = modbus_new_tcp("127.0.0.1", 502);
42 if (modbus_connect(ctx) == -1) {
43 fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
48 rc = modbus_read_registers(ctx, 0, 10, tab_reg);
50 fprintf(stderr, "%s\n", modbus_strerror(errno));
54 for (i=0; i < rc; i++) {
55 printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
64 - [modbus_write_register](modbus_write_register.md)
65 - [modbus_write_registers](modbus_write_registers.md)