modbus_set_slave.txt 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. modbus_set_slave(3)
  2. ===================
  3. NAME
  4. ----
  5. modbus_set_slave - set slave number in the context
  6. SYNOPSIS
  7. --------
  8. *int modbus_set_slave(modbus_t *'ctx', int 'slave');*
  9. DESCRIPTION
  10. -----------
  11. The *modbus_set_slave()* function shall set the slave number in the libmodbus
  12. context.
  13. The behavior depends of network and the role of the device:
  14. *RTU*::
  15. Define the slave ID of the remote device to talk in master mode or set the
  16. internal slave ID in slave mode. According to the protocol, a Modbus device must
  17. only accept message holding its slave number or the special broadcast number.
  18. *TCP*::
  19. The slave number is only required in TCP if the message must reach a device on a
  20. serial network. Some not compliant devices or software (such as modpoll) uses
  21. the slave ID as unit identifier, that's incorrect (cf page 23 of Modbus
  22. Messaging Implementation Guide v1.0b) but without the slave value, the faulty
  23. remote device or software drops the requests! The special value
  24. `MODBUS_TCP_SLAVE` (0xFF) can be used in TCP mode to restore the default value.
  25. The broadcast address is `MODBUS_BROADCAST_ADDRESS`. This special value must be
  26. use when you want all Modbus devices of the network receive the request.
  27. RETURN VALUE
  28. ------------
  29. The function shall return 0 if successful. Otherwise it shall return -1 and set
  30. errno to one of the values defined below.
  31. ERRORS
  32. ------
  33. *EINVAL*::
  34. The slave number is invalid.
  35. EXAMPLE
  36. -------
  37. [source,c]
  38. -------------------
  39. modbus_t *ctx;
  40. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  41. if (ctx == NULL) {
  42. fprintf(stderr, "Unable to create the libmodbus context\n");
  43. return -1;
  44. }
  45. rc = modbus_set_slave(ctx, YOUR_DEVICE_ID);
  46. if (rc == -1) {
  47. fprintf(stderr, "Invalid slave ID\n");
  48. modbus_free(ctx);
  49. return -1;
  50. }
  51. if (modbus_connect(ctx) == -1) {
  52. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  53. modbus_free(ctx);
  54. return -1;
  55. }
  56. -------------------
  57. SEE ALSO
  58. --------
  59. linkmb:modbus_get_slave[3]
  60. AUTHORS
  61. -------
  62. The libmodbus documentation was written by Stéphane Raimbault
  63. <stephane.raimbault@gmail.com>