libmodbus.txt 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. libmodbus(7)
  2. ============
  3. NAME
  4. ----
  5. libmodbus - a 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 Ethernet 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 establish a connection with
  31. other 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
  47. before sending a message, you must set the slave (receiver) with
  48. linkmb:modbus_set_slave[3]. If you're running a slave, its slave number will be
  49. used to filter received messages.
  50. The libmodbus implementation of RTU isn't time based as stated in original
  51. Modbus specification, instead all bytes are sent as fast as possible and a
  52. response or an indication is considered complete when all expected characters
  53. have been received. This implementation offers very fast communication but you
  54. must take care to set a response timeout of slaves less than response timeout of
  55. master (ortherwise other slaves may ignore master requests when one of the slave
  56. is not responding).
  57. Create a Modbus RTU context::
  58. linkmb:modbus_new_rtu[3]
  59. Set the serial mode::
  60. linkmb:modbus_rtu_get_serial_mode[3]
  61. linkmb:modbus_rtu_set_serial_mode[3]
  62. linkmb:modbus_rtu_get_rts[3]
  63. linkmb:modbus_rtu_set_rts[3]
  64. linkmb:modbus_rtu_set_custom_rts[3]
  65. linkmb:modbus_rtu_get_rts_delay[3]
  66. linkmb:modbus_rtu_set_rts_delay[3]
  67. TCP (IPv4) Context
  68. ^^^^^^^^^^^^^^^^^^
  69. The TCP backend implements a Modbus variant used for communications over
  70. TCP/IPv4 networks. It does not require a checksum calculation as lower layer
  71. takes care of the same.
  72. Create a Modbus TCP context::
  73. linkmb:modbus_new_tcp[3]
  74. TCP PI (IPv4 and IPv6) Context
  75. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  76. The TCP PI (Protocol Independent) backend implements a Modbus variant used for
  77. communications over TCP IPv4 and IPv6 networks. It does not require a checksum
  78. calculation as lower layer takes care of the same.
  79. Contrary to the TCP IPv4 only backend, the TCP PI backend offers hostname
  80. resolution but it consumes about 1Kb of additional memory.
  81. Create a Modbus TCP context::
  82. linkmb:modbus_new_tcp_pi[3]
  83. Common
  84. ^^^^^^
  85. Before using any libmodbus functions, the caller must allocate and initialize a
  86. 'modbus_t' context with functions explained above, then the following functions
  87. are provided to modify and free a 'context':
  88. Free libmodbus context::
  89. linkmb:modbus_free[3]
  90. Set slave ID::
  91. linkmb:modbus_set_slave[3]
  92. Enable debug mode::
  93. linkmb:modbus_set_debug[3]
  94. Timeout settings::
  95. linkmb:modbus_get_byte_timeout[3]
  96. linkmb:modbus_set_byte_timeout[3]
  97. linkmb:modbus_get_response_timeout[3]
  98. linkmb:modbus_set_response_timeout[3]
  99. linkmb:modbus_get_indication_timeout[3]
  100. linkmb:modbus_set_indication_timeout[3]
  101. Error recovery mode::
  102. linkmb:modbus_set_error_recovery[3]
  103. Setter/getter of internal socket::
  104. linkmb:modbus_set_socket[3]
  105. linkmb:modbus_get_socket[3]
  106. Information about header::
  107. linkmb:modbus_get_header_length[3]
  108. Macros for data manipulation::
  109. - MODBUS_GET_HIGH_BYTE(data), extracts the high byte from a byte
  110. - MODBUS_GET_LOW_BYTE(data), extracts the low byte from a byte
  111. - MODBUS_GET_INT64_FROM_INT16(tab_int16, index), builds an int64 from the four
  112. first int16 starting at tab_int16[index]
  113. - MODBUS_GET_INT32_FROM_INT16(tab_int16, index), builds an int32 from the two
  114. first int16 starting at tab_int16[index]
  115. - MODBUS_GET_INT16_FROM_INT8(tab_int8, index), builds an int16 from the two
  116. first int8 starting at tab_int8[index]
  117. - MODBUS_SET_INT16_TO_INT8(tab_int8, index, value), set an int16 value into
  118. the two first bytes starting at tab_int8[index]
  119. - MODBUS_SET_INT32_TO_INT16(tab_int16, index, value), set an int32 value into
  120. the two first int16 starting at tab_int16[index]
  121. - MODBUS_SET_INT64_TO_INT16(tab_int16, index, value), set an int64 value into
  122. the four first int16 starting at tab_int16[index]
  123. Handling of bits and bytes::
  124. linkmb:modbus_set_bits_from_byte[3]
  125. linkmb:modbus_set_bits_from_bytes[3]
  126. linkmb:modbus_get_byte_from_bits[3]
  127. Set or get float numbers::
  128. linkmb:modbus_get_float_abcd[3]
  129. linkmb:modbus_set_float_abcd[3]
  130. linkmb:modbus_get_float_badc[3]
  131. linkmb:modbus_set_float_badc[3]
  132. linkmb:modbus_get_float_cdab[3]
  133. linkmb:modbus_set_float_cdab[3]
  134. linkmb:modbus_get_float_dcba[3]
  135. linkmb:modbus_set_float_dcba[3]
  136. linkmb:modbus_get_float[3] (deprecated)
  137. linkmb:modbus_set_float[3] (deprecated)
  138. Connection
  139. ~~~~~~~~~~
  140. The following functions are provided to establish and close a connection with
  141. Modbus devices:
  142. Establish a connection::
  143. linkmb:modbus_connect[3]
  144. Close a connection::
  145. linkmb:modbus_close[3]
  146. Flush a connection::
  147. linkmb:modbus_flush[3]
  148. Client
  149. ~~~~~~
  150. The Modbus protocol defines different data types and functions to read and write
  151. them from/to remote devices. The following functions are used by the clients to
  152. send Modbus requests:
  153. Read data::
  154. linkmb:modbus_read_bits[3]
  155. linkmb:modbus_read_input_bits[3]
  156. linkmb:modbus_read_registers[3]
  157. linkmb:modbus_read_input_registers[3]
  158. linkmb:modbus_report_slave_id[3]
  159. Write data::
  160. linkmb:modbus_write_bit[3]
  161. linkmb:modbus_write_register[3]
  162. linkmb:modbus_write_bits[3]
  163. linkmb:modbus_write_registers[3]
  164. Write and read data::
  165. linkmb:modbus_write_and_read_registers[3]
  166. Raw requests::
  167. linkmb:modbus_send_raw_request[3]
  168. linkmb:modbus_receive_confirmation[3]
  169. Reply an exception::
  170. linkmb:modbus_reply_exception[3]
  171. Server
  172. ~~~~~~
  173. The server is waiting for request from clients and must answer when it is
  174. concerned by the request.
  175. In TCP mode, you must not use the usual linkmb:modbus_connect[3] to establish the connection but a pair of accept/listen calls::
  176. linkmb:modbus_tcp_listen[3]
  177. linkmb:modbus_tcp_accept[3]
  178. linkmb:modbus_tcp_pi_listen[3]
  179. linkmb:modbus_tcp_pi_accept[3]
  180. then the data can be received with::
  181. linkmb:modbus_receive[3]
  182. and a response can be send with::
  183. linkmb:modbus_reply[3]
  184. linkmb:modbus_reply_exception[3]
  185. To handle the mapping of your Modbus data, you must use:
  186. linkmb:modbus_mapping_new[3]
  187. linkmb:modbus_mapping_free[3]
  188. ERROR HANDLING
  189. --------------
  190. The libmodbus functions handle errors using the standard conventions found on
  191. POSIX systems. Generally, this means that upon failure a libmodbus function
  192. shall return either a NULL value (if returning a pointer) or a negative value
  193. (if returning an integer), and the actual error code shall be stored in the
  194. 'errno' variable.
  195. The *modbus_strerror()* function is provided to translate libmodbus-specific
  196. error codes into error message strings; for details refer to
  197. linkmb:modbus_strerror[3].
  198. MISCELLANEOUS
  199. -------------
  200. The _LIBMODBUS_VERSION_STRING_ constant indicates the libmodbus version the
  201. program has been compiled against. The variables 'libmodbus_version_major',
  202. 'libmodbus_version_minor', 'libmodbus_version_micro' give the version the
  203. program is linked against.
  204. AUTHORS
  205. -------
  206. The libmodbus documentation was written by Stéphane Raimbault
  207. <stephane.raimbault@gmail.com>
  208. RESOURCES
  209. ---------
  210. Main web site: <http://www.libmodbus.org/>
  211. Report bugs on the issue tracker at
  212. <http://github.com/stephane/libmodbus/issues>.
  213. COPYING
  214. -------
  215. Free use of this software is granted under the terms of the GNU Lesser General
  216. Public License (LGPL v2.1+). For details see the file `COPYING.LESSER` included
  217. with the libmodbus distribution.