libmodbus.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 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 establish 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. linkmb:modbus_rtu_get_rts[3]
  56. linkmb:modbus_rtu_set_rts[3]
  57. TCP (IPv4) Context
  58. ^^^^^^^^^^^^^^^^^^
  59. The TCP backend implements a Modbus variant used for communications over
  60. TCP/IPv4 networks. It does not require a checksum calculation as lower layer
  61. takes care of the same.
  62. Create a Modbus TCP context::
  63. linkmb:modbus_new_tcp[3]
  64. TCP PI (IPv4 and IPv6) Context
  65. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  66. The TCP PI (Protocol Indepedent) backend implements a Modbus variant used for
  67. communications over TCP IPv4 and IPv6 networks. It does not require a checksum
  68. calculation as lower layer takes care of the same.
  69. Contrary to the TCP IPv4 only backend, the TCP PI backend offers hostname
  70. resolution but it consumes about 1Kb of additional memory.
  71. Create a Modbus TCP context::
  72. linkmb:modbus_new_tcp_pi[3]
  73. Common
  74. ^^^^^^
  75. Before using any libmodbus functions, the caller must allocate and initialize a
  76. 'modbus_t' context with functions explained above, then the following functions
  77. are provided to modify and free a 'context':
  78. Free libmodbus context::
  79. linkmb:modbus_free[3]
  80. Set slave ID::
  81. linkmb:modbus_set_slave[3]
  82. Enable debug mode::
  83. linkmb:modbus_set_debug[3]
  84. Timeout settings::
  85. linkmb:modbus_get_byte_timeout[3]
  86. linkmb:modbus_set_byte_timeout[3]
  87. linkmb:modbus_get_response_timeout[3]
  88. linkmb:modbus_set_response_timeout[3]
  89. Error recovery mode::
  90. linkmb:modbus_set_error_recovery[3]
  91. Setter/getter of internal socket::
  92. linkmb:modbus_set_socket[3]
  93. linkmb:modbus_get_socket[3]
  94. Information about header::
  95. linkmb:modbus_get_header_length[3]
  96. A libmodbus 'context' is thread safe and may be shared among as many application
  97. threads as necessary, without any additional locking required on the part of the
  98. caller.
  99. Macros for data manipulation::
  100. - MODBUS_GET_HIGH_BYTE(data), extracts the high byte from a byte
  101. - MODBUS_GET_LOW_BYTE(data), extracts the low byte from a byte
  102. - MODBUS_GET_INT32_FROM_INT16(tab_int16, index), builds an int32 from the two
  103. first int16 starting at tab_int16[index]
  104. - MODBUS_GET_INT16_FROM_INT8(tab_int8, index), builds an int16 from the two
  105. first int8 starting at tab_int8[index]
  106. - MODBUS_SET_INT16_TO_INT8(tab_int8, index, value), set an int16 value into
  107. the two first bytes starting at tab_int8[index]
  108. Handling of bits and bytes::
  109. linkmb:modbus_set_bits_from_byte[3]
  110. linkmb:modbus_set_bits_from_bytes[3]
  111. linkmb:modbus_get_byte_from_bits[3]
  112. Set or get float numbers::
  113. linkmb:modbus_get_float[3]
  114. linkmb:modbus_set_float[3]
  115. linkmb:modbus_get_float_dcba[3]
  116. linkmb:modbus_set_float_dcba[3]
  117. Connection
  118. ~~~~~~~~~~
  119. The following functions are provided to establish and close a connection with
  120. Modbus devices:
  121. Establish a connection::
  122. linkmb:modbus_connect[3]
  123. Close a connection::
  124. linkmb:modbus_close[3]
  125. Flush a connection::
  126. linkmb:modbus_flush[3]
  127. Client
  128. ~~~~~~
  129. The Modbus protocol defines different data types and functions to read and write
  130. them from/to remote devices. The following functions are used by the clients to
  131. send Modbus requests:
  132. Read data::
  133. linkmb:modbus_read_bits[3]
  134. linkmb:modbus_read_input_bits[3]
  135. linkmb:modbus_read_registers[3]
  136. linkmb:modbus_read_input_registers[3]
  137. linkmb:modbus_report_slave_id[3]
  138. Write data::
  139. linkmb:modbus_write_bit[3]
  140. linkmb:modbus_write_register[3]
  141. linkmb:modbus_write_bits[3]
  142. linkmb:modbus_write_registers[3]
  143. Write and read data::
  144. linkmb:modbus_write_and_read_registers[3]
  145. Raw requests::
  146. linkmb:modbus_send_raw_request[3]
  147. linkmb:modbus_receive_confirmation[3]
  148. Reply an exception::
  149. linkmb:modbus_reply_exception[3]
  150. Server
  151. ~~~~~~
  152. The server is waiting for request from clients and must answer when it is
  153. concerned by the request. The libmodbus offers the following functions to
  154. handle requests:
  155. Data mapping:
  156. linkmb:modbus_mapping_new[3]
  157. linkmb:modbus_mapping_free[3]
  158. Receive::
  159. linkmb:modbus_receive[3]
  160. Reply::
  161. linkmb:modbus_reply[3]
  162. linkmb:modbus_reply_exception[3]
  163. ERROR HANDLING
  164. --------------
  165. The libmodbus functions handle errors using the standard conventions found on
  166. POSIX systems. Generally, this means that upon failure a libmodbus function
  167. shall return either a NULL value (if returning a pointer) or a negative value
  168. (if returning an integer), and the actual error code shall be stored in the
  169. 'errno' variable.
  170. The _modbus_strerror()_ function is provided to translate libmodbus-specific
  171. error codes into error message strings; for details refer to
  172. linkmb:modbus_strerror[3].
  173. MISCELLANEOUS
  174. -------------
  175. The _LIBMODBUS_VERSION_STRING_ constant indicates the libmodbus version the
  176. program has been compiled against. The variables 'libmodbus_version_major',
  177. 'libmodbus_version_minor', 'libmodbus_version_micro' give the version the
  178. program is linked against.
  179. AUTHORS
  180. -------
  181. The libmodbus documentation was written by Stéphane Raimbault
  182. <stephane.raimbault@gmail.com>
  183. RESOURCES
  184. ---------
  185. Main web site: <http://www.libmodbus.org/>
  186. Report bugs on the issue tracker at
  187. <http://github.com/stephane/libmodbus/issues>.
  188. COPYING
  189. -------
  190. Free use of this software is granted under the terms of the GNU Lesser General
  191. Public License (LGPL v2.1+). For details see the files `COPYING` and
  192. `COPYING.LESSER` included with the libmodbus distribution.