modbus_new_rtu.txt 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. modbus_new_rtu(3)
  2. =================
  3. NAME
  4. ----
  5. modbus_new_rtu - create a libmodbus context for RTU
  6. SYNOPSIS
  7. --------
  8. *modbus_t *modbus_new_rtu(const char *'device', int 'baud', char 'parity', int 'data_bit', int 'stop_bit');*
  9. DESCRIPTION
  10. -----------
  11. The *modbus_new_rtu()* function shall allocate and initialize a _modbus_t_
  12. structure to communicate in RTU mode on a serial line.
  13. The _device_ argument specifies the name of the serial port handled by the OS,
  14. eg. "/dev/ttyS0" or "/dev/ttyUSB0". On Windows, it's necessary to prepend COM
  15. name with "\\.\" for COM number greater than 9, eg. "\\\\.\\COM10". See
  16. http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx for details
  17. The _baud_ argument specifies the baud rate of the communication, eg. 9600,
  18. 19200, 57600, 115200, etc.
  19. The _parity_ argument can have one of the following values:::
  20. * _N_ for none
  21. * _E_ for even
  22. * _O_ for odd
  23. The _data_bits_ argument specifies the number of bits of data, the allowed
  24. values are 5, 6, 7 and 8.
  25. The _stop_bits_ argument specifies the bits of stop, the allowed values are 1
  26. and 2.
  27. Once the _modbus_t_ structure is initialized, you must set the slave of your
  28. device with linkmb:modbus_set_slave[3] and connect to the serial bus with
  29. linkmb:modbus_connect[3].
  30. RETURN VALUE
  31. ------------
  32. The function shall return a pointer to a _modbus_t_ structure if
  33. successful. Otherwise it shall return NULL and set errno to one of the values
  34. defined below.
  35. ERRORS
  36. ------
  37. *EINVAL*::
  38. An invalid argument was given.
  39. EXAMPLE
  40. -------
  41. [source,c]
  42. -------------------
  43. modbus_t *ctx;
  44. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  45. if (ctx == NULL) {
  46. fprintf(stderr, "Unable to create the libmodbus context\n");
  47. return -1;
  48. }
  49. modbus_set_slave(ctx, YOUR_DEVICE_ID);
  50. if (modbus_connect(ctx) == -1) {
  51. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  52. modbus_free(ctx);
  53. return -1;
  54. }
  55. -------------------
  56. SEE ALSO
  57. --------
  58. linkmb:modbus_new_tcp[3]
  59. linkmb:modbus_free[3]
  60. AUTHORS
  61. -------
  62. The libmodbus documentation was written by Stéphane Raimbault
  63. <stephane.raimbault@gmail.com>