modbus_new_rtu.txt 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. *ENOMEM*::
  40. Out of memory. Possibly, the application hits its memory limit and/or whole
  41. system is running out of memory.
  42. EXAMPLE
  43. -------
  44. [source,c]
  45. -------------------
  46. modbus_t *ctx;
  47. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  48. if (ctx == NULL) {
  49. fprintf(stderr, "Unable to create the libmodbus context\n");
  50. return -1;
  51. }
  52. modbus_set_slave(ctx, YOUR_DEVICE_ID);
  53. if (modbus_connect(ctx) == -1) {
  54. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  55. modbus_free(ctx);
  56. return -1;
  57. }
  58. -------------------
  59. SEE ALSO
  60. --------
  61. linkmb:modbus_new_tcp[3]
  62. linkmb:modbus_free[3]
  63. AUTHORS
  64. -------
  65. The libmodbus documentation was written by Stéphane Raimbault
  66. <stephane.raimbault@gmail.com>