modbus_read_registers.txt 1.6 KB

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