modbus_send_raw_request.txt 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. modbus_send_raw_request(3)
  2. ==========================
  3. NAME
  4. ----
  5. modbus_send_raw_request - send a raw request
  6. SYNOPSIS
  7. --------
  8. *int modbus_send_raw_request(modbus_t *'ctx', const uint8_t *'raw_req', int 'raw_req_length');*
  9. DESCRIPTION
  10. -----------
  11. The *modbus_send_raw_request()* function shall send a request via the socket of
  12. the context _ctx_. This function must be used for debugging purposes because you
  13. have to take care to make a valid request by hand. The function only adds to the
  14. message, the header or CRC of the selected backend, so _raw_req_ must start and
  15. contain at least a slave/unit identifier and a function code. This function can
  16. be used to send request not handled by the library.
  17. The public header of libmodbus provides a list of supported Modbus functions
  18. codes, prefixed by `MODBUS_FC_` (eg. `MODBUS_FC_READ_HOLDING_REGISTERS`), to help
  19. build of raw requests.
  20. RETURN VALUE
  21. ------------
  22. The function shall return the full message length, counting the extra data
  23. relating to the backend, if successful. Otherwise it shall return -1 and set
  24. errno.
  25. EXAMPLE
  26. -------
  27. [source,c]
  28. -------------------
  29. modbus_t *ctx;
  30. /* Read 5 holding registers from address 1 */
  31. uint8_t raw_req[] = { 0xFF, MODBUS_FC_READ_HOLDING_REGISTERS, 0x00, 0x01, 0x0, 0x05 };
  32. int req_length;
  33. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  34. ctx = modbus_new_tcp("127.0.0.1", 1502);
  35. if (modbus_connect(ctx) == -1) {
  36. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  37. modbus_free(ctx);
  38. return -1;
  39. }
  40. req_length = modbus_send_raw_request(ctx, raw_req, 6 * sizeof(uint8_t));
  41. modbus_receive_confirmation(ctx, rsp);
  42. modbus_close(ctx);
  43. modbus_free(ctx);
  44. -------------------
  45. SEE ALSO
  46. --------
  47. linkmb:modbus_receive_confirmation[3]
  48. AUTHORS
  49. -------
  50. The libmodbus documentation was written by Stéphane Raimbault
  51. <stephane.raimbault@gmail.com>