modbus.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. */
  6. #ifndef MODBUS_H
  7. #define MODBUS_H
  8. // clang-format off
  9. /* Add this for macros that defined unix flavor */
  10. #if (defined(__unix__) || defined(unix)) && !defined(USG)
  11. # include <sys/param.h>
  12. #endif
  13. #ifndef _MSC_VER
  14. # include <stdint.h>
  15. #else
  16. # include "stdint.h"
  17. #endif
  18. #include "modbus-version.h"
  19. #if defined(_MSC_VER)
  20. # if defined(DLLBUILD)
  21. /* define DLLBUILD when building the DLL */
  22. # define MODBUS_API __declspec(dllexport)
  23. # else
  24. # define MODBUS_API __declspec(dllimport)
  25. # endif
  26. #else
  27. # define MODBUS_API
  28. #endif
  29. #ifdef __cplusplus
  30. # define MODBUS_BEGIN_DECLS extern "C" {
  31. # define MODBUS_END_DECLS }
  32. #else
  33. # define MODBUS_BEGIN_DECLS
  34. # define MODBUS_END_DECLS
  35. #endif
  36. // clang-format on
  37. MODBUS_BEGIN_DECLS
  38. #ifndef FALSE
  39. #define FALSE 0
  40. #endif
  41. #ifndef TRUE
  42. #define TRUE 1
  43. #endif
  44. #ifndef OFF
  45. #define OFF 0
  46. #endif
  47. #ifndef ON
  48. #define ON 1
  49. #endif
  50. /* Modbus function codes */
  51. #define MODBUS_FC_READ_COILS 0x01
  52. #define MODBUS_FC_READ_DISCRETE_INPUTS 0x02
  53. #define MODBUS_FC_READ_HOLDING_REGISTERS 0x03
  54. #define MODBUS_FC_READ_INPUT_REGISTERS 0x04
  55. #define MODBUS_FC_WRITE_SINGLE_COIL 0x05
  56. #define MODBUS_FC_WRITE_SINGLE_REGISTER 0x06
  57. #define MODBUS_FC_READ_EXCEPTION_STATUS 0x07
  58. #define MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F
  59. #define MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10
  60. #define MODBUS_FC_REPORT_SLAVE_ID 0x11
  61. #define MODBUS_FC_MASK_WRITE_REGISTER 0x16
  62. #define MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17
  63. #define MODBUS_BROADCAST_ADDRESS 0
  64. /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
  65. * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
  66. * (chapter 6 section 11 page 29)
  67. * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
  68. */
  69. #define MODBUS_MAX_READ_BITS 2000
  70. #define MODBUS_MAX_WRITE_BITS 1968
  71. /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
  72. * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
  73. * (chapter 6 section 12 page 31)
  74. * Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
  75. * (chapter 6 section 17 page 38)
  76. * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
  77. */
  78. #define MODBUS_MAX_READ_REGISTERS 125
  79. #define MODBUS_MAX_WRITE_REGISTERS 123
  80. #define MODBUS_MAX_WR_WRITE_REGISTERS 121
  81. #define MODBUS_MAX_WR_READ_REGISTERS 125
  82. /* The size of the MODBUS PDU is limited by the size constraint inherited from
  83. * the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256
  84. * bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server
  85. * address (1 byte) - CRC (2 bytes) = 253 bytes.
  86. */
  87. #define MODBUS_MAX_PDU_LENGTH 253
  88. /* Consequently:
  89. * - RTU MODBUS ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256
  90. * bytes.
  91. * - TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes.
  92. * so the maximum of both backend in 260 bytes. This size can used to allocate
  93. * an array of bytes to store responses and it will be compatible with the two
  94. * backends.
  95. */
  96. #define MODBUS_MAX_ADU_LENGTH 260
  97. /* Random number to avoid errno conflicts */
  98. #define MODBUS_ENOBASE 112345678
  99. /* Protocol exceptions */
  100. enum {
  101. MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01,
  102. MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
  103. MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
  104. MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
  105. MODBUS_EXCEPTION_ACKNOWLEDGE,
  106. MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
  107. MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
  108. MODBUS_EXCEPTION_MEMORY_PARITY,
  109. MODBUS_EXCEPTION_NOT_DEFINED,
  110. MODBUS_EXCEPTION_GATEWAY_PATH,
  111. MODBUS_EXCEPTION_GATEWAY_TARGET,
  112. MODBUS_EXCEPTION_MAX
  113. };
  114. #define EMBXILFUN (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION)
  115. #define EMBXILADD (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS)
  116. #define EMBXILVAL (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE)
  117. #define EMBXSFAIL (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE)
  118. #define EMBXACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_ACKNOWLEDGE)
  119. #define EMBXSBUSY (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY)
  120. #define EMBXNACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE)
  121. #define EMBXMEMPAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_MEMORY_PARITY)
  122. #define EMBXGPATH (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_PATH)
  123. #define EMBXGTAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET)
  124. /* Native libmodbus error codes */
  125. #define EMBBADCRC (EMBXGTAR + 1)
  126. #define EMBBADDATA (EMBXGTAR + 2)
  127. #define EMBBADEXC (EMBXGTAR + 3)
  128. #define EMBUNKEXC (EMBXGTAR + 4)
  129. #define EMBMDATA (EMBXGTAR + 5)
  130. #define EMBBADSLAVE (EMBXGTAR + 6)
  131. extern const unsigned int libmodbus_version_major;
  132. extern const unsigned int libmodbus_version_minor;
  133. extern const unsigned int libmodbus_version_micro;
  134. typedef struct _modbus modbus_t;
  135. typedef struct _modbus_mapping_t {
  136. int nb_bits;
  137. int start_bits;
  138. int nb_input_bits;
  139. int start_input_bits;
  140. int nb_input_registers;
  141. int start_input_registers;
  142. int nb_registers;
  143. int start_registers;
  144. uint8_t *tab_bits;
  145. uint8_t *tab_input_bits;
  146. uint16_t *tab_input_registers;
  147. uint16_t *tab_registers;
  148. } modbus_mapping_t;
  149. typedef enum {
  150. MODBUS_ERROR_RECOVERY_NONE = 0,
  151. MODBUS_ERROR_RECOVERY_LINK = (1 << 1),
  152. MODBUS_ERROR_RECOVERY_PROTOCOL = (1 << 2)
  153. } modbus_error_recovery_mode;
  154. typedef enum {
  155. MODBUS_QUIRK_NONE = 0,
  156. MODBUS_QUIRK_MAX_SLAVE = (1 << 1),
  157. MODBUS_QUIRK_REPLY_TO_BROADCAST = (1 << 2),
  158. MODBUS_QUIRK_ALL = 0xFF
  159. } modbus_quirks;
  160. MODBUS_API int modbus_set_slave(modbus_t *ctx, int slave);
  161. MODBUS_API int modbus_get_slave(modbus_t *ctx);
  162. MODBUS_API int modbus_set_error_recovery(modbus_t *ctx,
  163. modbus_error_recovery_mode error_recovery);
  164. MODBUS_API int modbus_set_socket(modbus_t *ctx, int s);
  165. MODBUS_API int modbus_get_socket(modbus_t *ctx);
  166. MODBUS_API int
  167. modbus_get_response_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
  168. MODBUS_API int
  169. modbus_set_response_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
  170. MODBUS_API int
  171. modbus_get_byte_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
  172. MODBUS_API int modbus_set_byte_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
  173. MODBUS_API int
  174. modbus_get_indication_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
  175. MODBUS_API int
  176. modbus_set_indication_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
  177. MODBUS_API int modbus_get_header_length(modbus_t *ctx);
  178. MODBUS_API int modbus_connect(modbus_t *ctx);
  179. MODBUS_API void modbus_close(modbus_t *ctx);
  180. MODBUS_API void modbus_free(modbus_t *ctx);
  181. MODBUS_API int modbus_flush(modbus_t *ctx);
  182. MODBUS_API int modbus_set_debug(modbus_t *ctx, int flag);
  183. MODBUS_API const char *modbus_strerror(int errnum);
  184. MODBUS_API int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
  185. MODBUS_API int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
  186. MODBUS_API int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
  187. MODBUS_API int
  188. modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
  189. MODBUS_API int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
  190. MODBUS_API int modbus_write_register(modbus_t *ctx, int reg_addr, const uint16_t value);
  191. MODBUS_API int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
  192. MODBUS_API int
  193. modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);
  194. MODBUS_API int
  195. modbus_mask_write_register(modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);
  196. MODBUS_API int modbus_write_and_read_registers(modbus_t *ctx,
  197. int write_addr,
  198. int write_nb,
  199. const uint16_t *src,
  200. int read_addr,
  201. int read_nb,
  202. uint16_t *dest);
  203. MODBUS_API int modbus_report_slave_id(modbus_t *ctx, int max_dest, uint8_t *dest);
  204. MODBUS_API modbus_mapping_t *
  205. modbus_mapping_new_start_address(unsigned int start_bits,
  206. unsigned int nb_bits,
  207. unsigned int start_input_bits,
  208. unsigned int nb_input_bits,
  209. unsigned int start_registers,
  210. unsigned int nb_registers,
  211. unsigned int start_input_registers,
  212. unsigned int nb_input_registers);
  213. MODBUS_API modbus_mapping_t *modbus_mapping_new(int nb_bits,
  214. int nb_input_bits,
  215. int nb_registers,
  216. int nb_input_registers);
  217. MODBUS_API void modbus_mapping_free(modbus_mapping_t *mb_mapping);
  218. MODBUS_API int
  219. modbus_send_raw_request(modbus_t *ctx, const uint8_t *raw_req, int raw_req_length);
  220. MODBUS_API int modbus_receive(modbus_t *ctx, uint8_t *req);
  221. MODBUS_API int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp);
  222. MODBUS_API int modbus_reply(modbus_t *ctx,
  223. const uint8_t *req,
  224. int req_length,
  225. modbus_mapping_t *mb_mapping);
  226. MODBUS_API int
  227. modbus_reply_exception(modbus_t *ctx, const uint8_t *req, unsigned int exception_code);
  228. MODBUS_API int modbus_enable_quirks(modbus_t *ctx, unsigned int quirks_mask);
  229. MODBUS_API int modbus_disable_quirks(modbus_t *ctx, unsigned int quirks_mask);
  230. /**
  231. * UTILS FUNCTIONS
  232. **/
  233. #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
  234. #define MODBUS_GET_LOW_BYTE(data) ((data) &0xFF)
  235. #define MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \
  236. (((int64_t) tab_int16[(index)] << 48) | ((int64_t) tab_int16[(index) + 1] << 32) | \
  237. ((int64_t) tab_int16[(index) + 2] << 16) | (int64_t) tab_int16[(index) + 3])
  238. #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) \
  239. (((int32_t) tab_int16[(index)] << 16) | (int32_t) tab_int16[(index) + 1])
  240. #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) \
  241. (((int16_t) tab_int8[(index)] << 8) | (int16_t) tab_int8[(index) + 1])
  242. #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
  243. do { \
  244. ((int8_t *) (tab_int8))[(index)] = (int8_t) ((value) >> 8); \
  245. ((int8_t *) (tab_int8))[(index) + 1] = (int8_t) (value); \
  246. } while (0)
  247. #define MODBUS_SET_INT32_TO_INT16(tab_int16, index, value) \
  248. do { \
  249. ((int16_t *) (tab_int16))[(index)] = (int16_t) ((value) >> 16); \
  250. ((int16_t *) (tab_int16))[(index) + 1] = (int16_t) (value); \
  251. } while (0)
  252. #define MODBUS_SET_INT64_TO_INT16(tab_int16, index, value) \
  253. do { \
  254. ((int16_t *) (tab_int16))[(index)] = (int16_t) ((value) >> 48); \
  255. ((int16_t *) (tab_int16))[(index) + 1] = (int16_t) ((value) >> 32); \
  256. ((int16_t *) (tab_int16))[(index) + 2] = (int16_t) ((value) >> 16); \
  257. ((int16_t *) (tab_int16))[(index) + 3] = (int16_t) (value); \
  258. } while (0)
  259. MODBUS_API void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value);
  260. MODBUS_API void modbus_set_bits_from_bytes(uint8_t *dest,
  261. int idx,
  262. unsigned int nb_bits,
  263. const uint8_t *tab_byte);
  264. MODBUS_API uint8_t modbus_get_byte_from_bits(const uint8_t *src,
  265. int idx,
  266. unsigned int nb_bits);
  267. MODBUS_API float modbus_get_float(const uint16_t *src);
  268. MODBUS_API float modbus_get_float_abcd(const uint16_t *src);
  269. MODBUS_API float modbus_get_float_dcba(const uint16_t *src);
  270. MODBUS_API float modbus_get_float_badc(const uint16_t *src);
  271. MODBUS_API float modbus_get_float_cdab(const uint16_t *src);
  272. MODBUS_API void modbus_set_float(float f, uint16_t *dest);
  273. MODBUS_API void modbus_set_float_abcd(float f, uint16_t *dest);
  274. MODBUS_API void modbus_set_float_dcba(float f, uint16_t *dest);
  275. MODBUS_API void modbus_set_float_badc(float f, uint16_t *dest);
  276. MODBUS_API void modbus_set_float_cdab(float f, uint16_t *dest);
  277. #include "modbus-rtu.h"
  278. #include "modbus-tcp.h"
  279. MODBUS_END_DECLS
  280. #endif /* MODBUS_H */