modbus.h 11 KB

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