modbus.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright © 2001-2011 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. #include <sys/time.h>
  27. #else
  28. #include "stdint.h"
  29. #include <time.h>
  30. #endif
  31. #include "modbus-version.h"
  32. #ifdef __cplusplus
  33. # define MODBUS_BEGIN_DECLS extern "C" {
  34. # define MODBUS_END_DECLS }
  35. #else
  36. # define MODBUS_BEGIN_DECLS
  37. # define MODBUS_END_DECLS
  38. #endif
  39. MODBUS_BEGIN_DECLS
  40. #ifndef FALSE
  41. #define FALSE 0
  42. #endif
  43. #ifndef TRUE
  44. #define TRUE 1
  45. #endif
  46. #ifndef OFF
  47. #define OFF 0
  48. #endif
  49. #ifndef ON
  50. #define ON 1
  51. #endif
  52. #define MODBUS_BROADCAST_ADDRESS 0
  53. /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
  54. * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
  55. * (chapter 6 section 11 page 29)
  56. * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
  57. */
  58. #define MODBUS_MAX_READ_BITS 2000
  59. #define MODBUS_MAX_WRITE_BITS 1968
  60. /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
  61. * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
  62. * (chapter 6 section 12 page 31)
  63. * Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
  64. * (chapter 6 section 17 page 38)
  65. * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
  66. */
  67. #define MODBUS_MAX_READ_REGISTERS 125
  68. #define MODBUS_MAX_WRITE_REGISTERS 123
  69. #define MODBUS_MAX_RW_WRITE_REGISTERS 121
  70. /* Random number to avoid errno conflicts */
  71. #define MODBUS_ENOBASE 112345678
  72. /* Protocol exceptions */
  73. enum {
  74. MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01,
  75. MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
  76. MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
  77. MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
  78. MODBUS_EXCEPTION_ACKNOWLEDGE,
  79. MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
  80. MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
  81. MODBUS_EXCEPTION_MEMORY_PARITY,
  82. MODBUS_EXCEPTION_NOT_DEFINED,
  83. MODBUS_EXCEPTION_GATEWAY_PATH,
  84. MODBUS_EXCEPTION_GATEWAY_TARGET,
  85. MODBUS_EXCEPTION_MAX
  86. };
  87. #define EMBXILFUN (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION)
  88. #define EMBXILADD (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS)
  89. #define EMBXILVAL (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE)
  90. #define EMBXSFAIL (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE)
  91. #define EMBXACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_ACKNOWLEDGE)
  92. #define EMBXSBUSY (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY)
  93. #define EMBXNACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE)
  94. #define EMBXMEMPAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_MEMORY_PARITY)
  95. #define EMBXGPATH (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_PATH)
  96. #define EMBXGTAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET)
  97. /* Native libmodbus error codes */
  98. #define EMBBADCRC (EMBXGTAR + 1)
  99. #define EMBBADDATA (EMBXGTAR + 2)
  100. #define EMBBADEXC (EMBXGTAR + 3)
  101. #define EMBUNKEXC (EMBXGTAR + 4)
  102. #define EMBMDATA (EMBXGTAR + 5)
  103. extern const unsigned int libmodbus_version_major;
  104. extern const unsigned int libmodbus_version_minor;
  105. extern const unsigned int libmodbus_version_micro;
  106. typedef struct _modbus modbus_t;
  107. typedef struct {
  108. int nb_bits;
  109. int nb_input_bits;
  110. int nb_input_registers;
  111. int nb_registers;
  112. uint8_t *tab_bits;
  113. uint8_t *tab_input_bits;
  114. uint16_t *tab_input_registers;
  115. uint16_t *tab_registers;
  116. } modbus_mapping_t;
  117. typedef enum
  118. {
  119. MODBUS_ERROR_RECOVERY_NONE = 0,
  120. MODBUS_ERROR_RECOVERY_LINK = (1<<1),
  121. MODBUS_ERROR_RECOVERY_PROTOCOL = (1<<2),
  122. } modbus_error_recovery_mode;
  123. int modbus_set_slave(modbus_t* ctx, int slave);
  124. int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery);
  125. void modbus_set_socket(modbus_t *ctx, int socket);
  126. int modbus_get_socket(modbus_t *ctx);
  127. void modbus_get_response_timeout(modbus_t *ctx, struct timeval *timeout);
  128. void modbus_set_response_timeout(modbus_t *ctx, const struct timeval *timeout);
  129. void modbus_get_byte_timeout(modbus_t *ctx, struct timeval *timeout);
  130. void modbus_set_byte_timeout(modbus_t *ctx, const struct timeval *timeout);
  131. int modbus_get_header_length(modbus_t *ctx);
  132. int modbus_connect(modbus_t *ctx);
  133. void modbus_close(modbus_t *ctx);
  134. void modbus_free(modbus_t *ctx);
  135. int modbus_flush(modbus_t *ctx);
  136. void modbus_set_debug(modbus_t *ctx, int boolean);
  137. const char *modbus_strerror(int errnum);
  138. int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
  139. int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
  140. int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
  141. int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
  142. int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
  143. int modbus_write_register(modbus_t *ctx, int reg_addr, int value);
  144. int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
  145. int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);
  146. int modbus_read_and_write_registers(modbus_t *ctx, int read_addr,
  147. int read_nb, uint16_t *dest, int write_addr,
  148. int write_nb, const uint16_t *data);
  149. int modbus_report_slave_id(modbus_t *ctx, uint8_t *dest);
  150. modbus_mapping_t* modbus_mapping_new(int nb_coil_status, int nb_input_status,
  151. int nb_holding_registers, int nb_input_registers);
  152. void modbus_mapping_free(modbus_mapping_t *mb_mapping);
  153. int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length);
  154. int modbus_receive(modbus_t *ctx, uint8_t *req);
  155. int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req);
  156. int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp);
  157. int modbus_reply(modbus_t *ctx, const uint8_t *req,
  158. int req_length, modbus_mapping_t *mb_mapping);
  159. int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,
  160. unsigned int exception_code);
  161. /**
  162. * UTILS FUNCTIONS
  163. **/
  164. #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
  165. #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF)
  166. #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1])
  167. #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1])
  168. #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
  169. do { \
  170. tab_int8[(index)] = (value) >> 8; \
  171. tab_int8[(index) + 1] = (value) & 0xFF; \
  172. } while (0)
  173. void modbus_set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
  174. void modbus_set_bits_from_bytes(uint8_t *dest, int address, unsigned int nb_bits,
  175. const uint8_t *tab_byte);
  176. uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address, unsigned int nb_bits);
  177. float modbus_get_float(const uint16_t *src);
  178. void modbus_set_float(float f, uint16_t *dest);
  179. #include "modbus-tcp.h"
  180. #include "modbus-rtu.h"
  181. MODBUS_END_DECLS
  182. #endif /* _MODBUS_H_ */