libmodbus.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. libmodbus(7)
  2. ============
  3. NAME
  4. ----
  5. libmodbus - fast and portable Modbus library
  6. SYNOPSIS
  7. --------
  8. *#include <modbus.h>*
  9. *cc* \`pkg-config --cflags --libs libmodbus` 'files'
  10. DESCRIPTION
  11. -----------
  12. libmodbus is a library to send/receive data with a device which respects the
  13. Modbus protocol. This library contains various backends to communicate over
  14. different networks (eg. serial in RTU mode or Ethernet in TCP/IPv6). The
  15. http://www.modbus.org site provides documentation about the protocol at
  16. http://www.modbus.org/specs.php.
  17. libmodbus provides an abstraction of the lower communication layers and offers
  18. the same API on all supported platforms.
  19. This documentation presents an overview of libmodbus concepts, describes how
  20. libmodbus abstracts Modbus communication with different hardware and platforms
  21. and provides a reference manual for the functions provided by the libmodbus
  22. library.
  23. Contexts
  24. ~~~~~~~~
  25. The Modbus protocol contains many variants (eg. serial RTU or Ehternet TCP), to
  26. ease the implementation of a variant, the library was designed to use a backend
  27. for each variant. The backends are also a convenient way to fulfill other
  28. requirements (eg. real-time operations). Each backend offers a specific function
  29. to create a new 'modbus_t' context. The 'modbus_t' context is an opaque
  30. structure containing all necessary information to etablish a connection with
  31. others Modbus devices according to the selected variant.
  32. You can choose the best context for your needs among:
  33. RTU Context
  34. ^^^^^^^^^^^
  35. The RTU backend (Remote Terminal Unit) is used in serial communication and makes
  36. use of a compact, binary representation of the data for protocol
  37. communication. The RTU format follows the commands/data with a cyclic redundancy
  38. check checksum as an error check mechanism to ensure the reliability of
  39. data. Modbus RTU is the most common implementation available for Modbus. A
  40. Modbus RTU message must be transmitted continuously without inter-character
  41. hesitations (extract from Wikipedia, Modbus, http://en.wikipedia.org/wiki/Modbus
  42. (as of Mar. 13, 2011, 20:51 GMT).
  43. The Modbus RTU framing calls a slave, a device/service which handle Modbus
  44. requests, and a master, a client which send requests. The communication is
  45. always initiated by the master.
  46. Many Modbus devices can be connected together on the same physical link so you
  47. need to define which slave is concerned by the message with
  48. linkmb:modbus_set_slave[3]. If you're running a slave, the slave number
  49. is used to filter messages.
  50. Create a Modbus RTU context::
  51. linkmb:modbus_new_rtu[3]
  52. Set the serial mode::
  53. linkmb:modbus_rtu_get_serial_mode[3]
  54. linkmb:modbus_rtu_set_serial_mode[3]
  55. TCP (IPv4) Context
  56. ^^^^^^^^^^^^^^^^^^
  57. The TCP backend implements a Modbus variant used for communications over
  58. TCP/IPv4 networks. It does not require a checksum calculation as lower layer
  59. takes care of the same.
  60. Create a Modbus TCP context::
  61. linkmb:modbus_new_tcp[3]
  62. TCP PI (IPv4 and IPv6) Context
  63. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  64. The TCP PI (Protocol Indepedent) backend implements a Modbus variant used for
  65. communications over TCP IPv4 and IPv6 networks. It does not require a checksum
  66. calculation as lower layer takes care of the same.
  67. Contrary to the TCP IPv4 only backend, the TCP PI backend offers hostname
  68. resolution but it consumes about 1Kb of additional memory.
  69. Create a Modbus TCP context::
  70. linkmb:modbus_new_tcp_pi[3]
  71. Common
  72. ^^^^^^
  73. Before using any libmodbus functions, the caller must allocate and initialize a
  74. 'modbus_t' context with functions explained above, then the following functions
  75. are provided to modify and free a 'context':
  76. Free libmodbus context::
  77. linkmb:modbus_free[3]
  78. Context setters and getters::
  79. linkmb:modbus_get_byte_timeout[3]
  80. linkmb:modbus_set_byte_timeout[3]
  81. linkmb:modbus_set_debug[3]
  82. linkmb:modbus_set_error_recovery[3]
  83. linkmb:modbus_get_header_length[3]
  84. linkmb:modbus_get_response_timeout[3]
  85. linkmb:modbus_set_response_timeout[3]
  86. linkmb:modbus_set_slave[3]
  87. linkmb:modbus_set_socket[3]
  88. linkmb:modbus_get_socket[3]
  89. A libmodbus 'context' is thread safe and may be shared among as many application
  90. threads as necessary, without any additional locking required on the part of the
  91. caller.
  92. Macros for data manipulation::
  93. MODBUS_GET_HIGH_BYTE(data), extracts the high byte from a byte
  94. MODBUS_GET_LOW_BYTE(data), extracts the low byte from a byte
  95. MODBUS_GET_INT32_FROM_INT16(tab_int16, index), builds an int32 from the two
  96. first int16 starting at tab_int16[index]
  97. MODBUS_GET_INT16_FROM_INT8(tab_int8, index), builds an int16 from the two
  98. first int8 starting at tab_int8[index]
  99. MODBUS_SET_INT16_TO_INT8(tab_int8, index, value), set an int16 value into
  100. the two first bytes starting at tab_int8[index]
  101. Functions for data manipulation::
  102. linkmb:modbus_set_bits_from_byte[3]
  103. linkmb:modbus_set_bits_from_bytes[3]
  104. linkmb:modbus_get_byte_from_bits[3]
  105. linkmb:modbus_get_float[3]
  106. linkmb:modbus_set_float[3]
  107. Connection
  108. ~~~~~~~~~~
  109. The following functions are provided to establish and close a connection with
  110. Modbus devices:
  111. Establish a connection::
  112. linkmb:modbus_connect[3]
  113. Close a connection::
  114. linkmb:modbus_close[3]
  115. Flush a connection::
  116. linkmb:modbus_flush[3]
  117. Client
  118. ~~~~~~
  119. The Modbus protocol defines different data types and functions to read and write
  120. them from/to remote devices. The following functions are used by the clients to
  121. send Modbus requests:
  122. Read data::
  123. linkmb:modbus_read_bits[3]
  124. linkmb:modbus_read_input_bits[3]
  125. linkmb:modbus_read_registers[3]
  126. linkmb:modbus_read_input_registers[3]
  127. libkmb:modbus_report_slave_id[3]
  128. Write data::
  129. linkmb:modbus_write_bit[3]
  130. linkmb:modbus_write_register[3]
  131. linkmb:modbus_write_bits[3]
  132. linkmb:modbus_write_registers[3]
  133. Write and read data::
  134. linkmb:modbus_write_and_read_registers[3]
  135. Raw requests::
  136. linkmb:modbus_send_raw_request[3]
  137. linkmb:modbus_receive_confirmation[3]
  138. Reply an exception::
  139. linkmb:modbus_reply_exception[3]
  140. Server
  141. ~~~~~~
  142. The server is waiting for request from clients and must answer when it is
  143. concerned by the request. The libmodbus offers the following functions to
  144. handle requests:
  145. Data mapping:
  146. linkmb:modbus_mapping_new[3]
  147. linkmb:modbus_mapping_free[3]
  148. Receive::
  149. linkmb:modbus_receive[3]
  150. Reply::
  151. linkmb:modbus_reply[3]
  152. linkmb:modbus_reply_exception[3]
  153. ERROR HANDLING
  154. --------------
  155. The libmodbus functions handle errors using the standard conventions found on
  156. POSIX systems. Generally, this means that upon failure a libmodbus function
  157. shall return either a NULL value (if returning a pointer) or a negative value
  158. (if returning an integer), and the actual error code shall be stored in the
  159. 'errno' variable.
  160. The _modbus_strerror()_ function is provided to translate libmodbus-specific
  161. error codes into error message strings; for details refer to
  162. linkmb:modbus_strerror[3].
  163. MISCELLANEOUS
  164. -------------
  165. The _LIBMODBUS_VERSION_STRING_ constant indicates the libmodbus version the
  166. program has been compiled against. The variables 'libmodbus_version_major',
  167. 'libmodbus_version_minor', 'libmodbus_version_micro' give the version the
  168. program is linked against.
  169. AUTHORS
  170. -------
  171. The libmodbus documentation was written by Stéphane Raimbault
  172. <stephane.raimbault@gmail.com>
  173. RESOURCES
  174. ---------
  175. Main web site: <http://www.libmodbus.org/>
  176. Report bugs on the issue tracker at
  177. <http://github.com/stephane/libmodbus/issues>.
  178. COPYING
  179. -------
  180. Free use of this software is granted under the terms of the GNU Lesser General
  181. Public License (LGPL v2.1+). For details see the files `COPYING` and
  182. `COPYING.LESSER` included with the libmodbus distribution.