modbus.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright © 2001-2013 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef MODBUS_H
  19. #define MODBUS_H
  20. /* Add this for macros that defined unix flavor */
  21. #if (defined(__unix__) || defined(unix)) && !defined(USG)
  22. #include <sys/param.h>
  23. #endif
  24. #ifndef _MSC_VER
  25. #include <stdint.h>
  26. #else
  27. #include "stdint.h"
  28. #endif
  29. #include "modbus-version.h"
  30. #if defined(_MSC_VER)
  31. # if defined(DLLBUILD)
  32. /* define DLLBUILD when building the DLL */
  33. # define MODBUS_API __declspec(dllexport)
  34. # else
  35. # define MODBUS_API __declspec(dllimport)
  36. # endif
  37. #else
  38. # define MODBUS_API
  39. #endif
  40. #ifdef __cplusplus
  41. # define MODBUS_BEGIN_DECLS extern "C" {
  42. # define MODBUS_END_DECLS }
  43. #else
  44. # define MODBUS_BEGIN_DECLS
  45. # define MODBUS_END_DECLS
  46. #endif
  47. MODBUS_BEGIN_DECLS
  48. #ifndef FALSE
  49. #define FALSE 0
  50. #endif
  51. #ifndef TRUE
  52. #define TRUE 1
  53. #endif
  54. #ifndef OFF
  55. #define OFF 0
  56. #endif
  57. #ifndef ON
  58. #define ON 1
  59. #endif
  60. /* Modbus function codes */
  61. #define MODBUS_FC_READ_COILS 0x01
  62. #define MODBUS_FC_READ_DISCRETE_INPUTS 0x02
  63. #define MODBUS_FC_READ_HOLDING_REGISTERS 0x03
  64. #define MODBUS_FC_READ_INPUT_REGISTERS 0x04
  65. #define MODBUS_FC_WRITE_SINGLE_COIL 0x05
  66. #define MODBUS_FC_WRITE_SINGLE_REGISTER 0x06
  67. #define MODBUS_FC_READ_EXCEPTION_STATUS 0x07
  68. #define MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F
  69. #define MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10
  70. #define MODBUS_FC_REPORT_SLAVE_ID 0x11
  71. #define MODBUS_FC_MASK_WRITE_REGISTER 0x16
  72. #define MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17
  73. #define MODBUS_BROADCAST_ADDRESS 0
  74. /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
  75. * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
  76. * (chapter 6 section 11 page 29)
  77. * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
  78. */
  79. #define MODBUS_MAX_READ_BITS 2000
  80. #define MODBUS_MAX_WRITE_BITS 1968
  81. /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
  82. * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
  83. * (chapter 6 section 12 page 31)
  84. * Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
  85. * (chapter 6 section 17 page 38)
  86. * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
  87. */
  88. #define MODBUS_MAX_READ_REGISTERS 125
  89. #define MODBUS_MAX_WRITE_REGISTERS 123
  90. #define MODBUS_MAX_WR_WRITE_REGISTERS 121
  91. #define MODBUS_MAX_WR_READ_REGISTERS 125
  92. /* Random number to avoid errno conflicts */
  93. #define MODBUS_ENOBASE 112345678
  94. /* Protocol exceptions */
  95. enum {
  96. MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01,
  97. MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
  98. MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
  99. MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
  100. MODBUS_EXCEPTION_ACKNOWLEDGE,
  101. MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
  102. MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
  103. MODBUS_EXCEPTION_MEMORY_PARITY,
  104. MODBUS_EXCEPTION_NOT_DEFINED,
  105. MODBUS_EXCEPTION_GATEWAY_PATH,
  106. MODBUS_EXCEPTION_GATEWAY_TARGET,
  107. MODBUS_EXCEPTION_MAX
  108. };
  109. #define EMBXILFUN (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION)
  110. #define EMBXILADD (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS)
  111. #define EMBXILVAL (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE)
  112. #define EMBXSFAIL (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE)
  113. #define EMBXACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_ACKNOWLEDGE)
  114. #define EMBXSBUSY (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY)
  115. #define EMBXNACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE)
  116. #define EMBXMEMPAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_MEMORY_PARITY)
  117. #define EMBXGPATH (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_PATH)
  118. #define EMBXGTAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET)
  119. /* Native libmodbus error codes */
  120. #define EMBBADCRC (EMBXGTAR + 1)
  121. #define EMBBADDATA (EMBXGTAR + 2)
  122. #define EMBBADEXC (EMBXGTAR + 3)
  123. #define EMBUNKEXC (EMBXGTAR + 4)
  124. #define EMBMDATA (EMBXGTAR + 5)
  125. #define EMBBADSLAVE (EMBXGTAR + 6)
  126. extern const unsigned int libmodbus_version_major;
  127. extern const unsigned int libmodbus_version_minor;
  128. extern const unsigned int libmodbus_version_micro;
  129. typedef struct _modbus modbus_t;
  130. typedef struct {
  131. int nb_bits;
  132. int nb_input_bits;
  133. int nb_input_registers;
  134. int nb_registers;
  135. uint8_t *tab_bits;
  136. uint8_t *tab_input_bits;
  137. uint16_t *tab_input_registers;
  138. uint16_t *tab_registers;
  139. } modbus_mapping_t;
  140. typedef enum
  141. {
  142. MODBUS_ERROR_RECOVERY_NONE = 0,
  143. MODBUS_ERROR_RECOVERY_LINK = (1<<1),
  144. MODBUS_ERROR_RECOVERY_PROTOCOL = (1<<2),
  145. } modbus_error_recovery_mode;
  146. MODBUS_API int modbus_set_slave(modbus_t* ctx, int slave);
  147. MODBUS_API int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery);
  148. MODBUS_API int modbus_set_socket(modbus_t *ctx, int s);
  149. MODBUS_API int modbus_get_socket(modbus_t *ctx);
  150. MODBUS_API int modbus_get_response_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
  151. MODBUS_API int modbus_set_response_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
  152. MODBUS_API int modbus_get_byte_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
  153. MODBUS_API int modbus_set_byte_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
  154. MODBUS_API int modbus_get_header_length(modbus_t *ctx);
  155. MODBUS_API int modbus_connect(modbus_t *ctx);
  156. MODBUS_API void modbus_close(modbus_t *ctx);
  157. MODBUS_API void modbus_free(modbus_t *ctx);
  158. MODBUS_API int modbus_flush(modbus_t *ctx);
  159. MODBUS_API int modbus_set_debug(modbus_t *ctx, int flag);
  160. MODBUS_API const char *modbus_strerror(int errnum);
  161. MODBUS_API int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
  162. MODBUS_API int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
  163. MODBUS_API int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
  164. MODBUS_API int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
  165. MODBUS_API int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
  166. MODBUS_API int modbus_write_register(modbus_t *ctx, int reg_addr, int value);
  167. MODBUS_API int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
  168. MODBUS_API int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);
  169. MODBUS_API int modbus_mask_write_register(modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);
  170. MODBUS_API int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int write_nb,
  171. const uint16_t *src, int read_addr, int read_nb,
  172. uint16_t *dest);
  173. MODBUS_API int modbus_report_slave_id(modbus_t *ctx, int max_dest, uint8_t *dest);
  174. MODBUS_API modbus_mapping_t* modbus_mapping_new(int nb_bits, int nb_input_bits,
  175. int nb_registers, int nb_input_registers);
  176. MODBUS_API void modbus_mapping_free(modbus_mapping_t *mb_mapping);
  177. MODBUS_API int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length);
  178. MODBUS_API int modbus_receive(modbus_t *ctx, uint8_t *req);
  179. MODBUS_API int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req);
  180. MODBUS_API int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp);
  181. MODBUS_API int modbus_reply(modbus_t *ctx, const uint8_t *req,
  182. int req_length, modbus_mapping_t *mb_mapping);
  183. MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,
  184. unsigned int exception_code);
  185. /**
  186. * UTILS FUNCTIONS
  187. **/
  188. #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
  189. #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF)
  190. #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1])
  191. #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1])
  192. #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
  193. do { \
  194. tab_int8[(index)] = (value) >> 8; \
  195. tab_int8[(index) + 1] = (value) & 0xFF; \
  196. } while (0)
  197. MODBUS_API void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value);
  198. MODBUS_API void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,
  199. const uint8_t *tab_byte);
  200. MODBUS_API uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx, unsigned int nb_bits);
  201. MODBUS_API float modbus_get_float(const uint16_t *src);
  202. MODBUS_API float modbus_get_float_dcba(const uint16_t *src);
  203. MODBUS_API void modbus_set_float(float f, uint16_t *dest);
  204. MODBUS_API void modbus_set_float_dcba(float f, uint16_t *dest);
  205. #include "modbus-tcp.h"
  206. #include "modbus-rtu.h"
  207. MODBUS_END_DECLS
  208. #endif /* MODBUS_H */