modbus.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright © 2001-2010 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program 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
  12. * GNU Lesser Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _MODBUS_H_
  18. #define _MODBUS_H_
  19. /* Add this for macros that defined unix flavor */
  20. #if (defined(__unix__) || defined(unix)) && !defined(USG)
  21. #include <sys/param.h>
  22. #endif
  23. #ifndef _MSC_VER
  24. #include <stdint.h>
  25. #include <sys/time.h>
  26. #else
  27. #include "stdint.h"
  28. #include <time.h>
  29. #endif
  30. #include "modbus-version.h"
  31. #ifdef __cplusplus
  32. # define MODBUS_BEGIN_DECLS extern "C" {
  33. # define MODBUS_END_DECLS }
  34. #else
  35. # define MODBUS_BEGIN_DECLS
  36. # define MODBUS_END_DECLS
  37. #endif
  38. MODBUS_BEGIN_DECLS
  39. #ifndef FALSE
  40. #define FALSE 0
  41. #endif
  42. #ifndef TRUE
  43. #define TRUE 1
  44. #endif
  45. #ifndef OFF
  46. #define OFF 0
  47. #endif
  48. #ifndef ON
  49. #define ON 1
  50. #endif
  51. #define MODBUS_BROADCAST_ADDRESS 0
  52. /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
  53. * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
  54. * (chapter 6 section 11 page 29)
  55. * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
  56. */
  57. #define MODBUS_MAX_READ_BITS 2000
  58. #define MODBUS_MAX_WRITE_BITS 1968
  59. /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
  60. * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
  61. * (chapter 6 section 12 page 31)
  62. * Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
  63. * (chapter 6 section 17 page 38)
  64. * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
  65. */
  66. #define MODBUS_MAX_READ_REGISTERS 125
  67. #define MODBUS_MAX_WRITE_REGISTERS 123
  68. #define MODBUS_MAX_RW_WRITE_REGISTERS 121
  69. /* Random number to avoid errno conflicts */
  70. #define MODBUS_ENOBASE 112345678
  71. /* Protocol exceptions */
  72. enum {
  73. MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01,
  74. MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
  75. MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
  76. MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
  77. MODBUS_EXCEPTION_ACKNOWLEDGE,
  78. MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
  79. MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
  80. MODBUS_EXCEPTION_MEMORY_PARITY,
  81. MODBUS_EXCEPTION_NOT_DEFINED,
  82. MODBUS_EXCEPTION_GATEWAY_PATH,
  83. MODBUS_EXCEPTION_GATEWAY_TARGET,
  84. MODBUS_EXCEPTION_MAX
  85. };
  86. #define EMBXILFUN (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION)
  87. #define EMBXILADD (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS)
  88. #define EMBXILVAL (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE)
  89. #define EMBXSFAIL (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE)
  90. #define EMBXACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_ACKNOWLEDGE)
  91. #define EMBXSBUSY (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY)
  92. #define EMBXNACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE)
  93. #define EMBXMEMPAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_MEMORY_PARITY)
  94. #define EMBXGPATH (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_PATH)
  95. #define EMBXGTAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET)
  96. /* Native libmodbus error codes */
  97. #define EMBBADCRC (EMBXGTAR + 1)
  98. #define EMBBADDATA (EMBXGTAR + 2)
  99. #define EMBBADEXC (EMBXGTAR + 3)
  100. #define EMBUNKEXC (EMBXGTAR + 4)
  101. #define EMBMDATA (EMBXGTAR + 5)
  102. extern const unsigned int libmodbus_version_major;
  103. extern const unsigned int libmodbus_version_minor;
  104. extern const unsigned int libmodbus_version_micro;
  105. typedef struct _modbus modbus_t;
  106. typedef struct {
  107. int nb_bits;
  108. int nb_input_bits;
  109. int nb_input_registers;
  110. int nb_registers;
  111. uint8_t *tab_bits;
  112. uint8_t *tab_input_bits;
  113. uint16_t *tab_input_registers;
  114. uint16_t *tab_registers;
  115. } modbus_mapping_t;
  116. int modbus_set_slave(modbus_t* ctx, int slave);
  117. int modbus_set_error_recovery(modbus_t *ctx, int enabled);
  118. void modbus_set_socket(modbus_t *ctx, int socket);
  119. int modbus_get_socket(modbus_t *ctx);
  120. void modbus_get_timeout_begin(modbus_t *ctx, struct timeval *timeout);
  121. void modbus_set_timeout_begin(modbus_t *ctx, const struct timeval *timeout);
  122. void modbus_get_timeout_end(modbus_t *ctx, struct timeval *timeout);
  123. void modbus_set_timeout_end(modbus_t *ctx, const struct timeval *timeout);
  124. int modbus_get_header_length(modbus_t *ctx);
  125. int modbus_connect(modbus_t *ctx);
  126. void modbus_close(modbus_t *ctx);
  127. void modbus_free(modbus_t *ctx);
  128. int modbus_flush(modbus_t *ctx);
  129. void modbus_set_debug(modbus_t *ctx, int boolean);
  130. const char *modbus_strerror(int errnum);
  131. int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
  132. int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
  133. int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
  134. int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
  135. int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
  136. int modbus_write_register(modbus_t *ctx, int reg_addr, int value);
  137. int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
  138. int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);
  139. int modbus_read_and_write_registers(modbus_t *ctx, int read_addr,
  140. int read_nb, uint16_t *dest, int write_addr,
  141. int write_nb, const uint16_t *data);
  142. int modbus_report_slave_id(modbus_t *ctx, uint8_t *dest);
  143. modbus_mapping_t* modbus_mapping_new(int nb_coil_status, int nb_input_status,
  144. int nb_holding_registers, int nb_input_registers);
  145. void modbus_mapping_free(modbus_mapping_t *mb_mapping);
  146. int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length);
  147. int modbus_receive(modbus_t *ctx, uint8_t *req);
  148. int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req);
  149. int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp);
  150. int modbus_reply(modbus_t *ctx, const uint8_t *req,
  151. int req_length, modbus_mapping_t *mb_mapping);
  152. int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,
  153. unsigned int exception_code);
  154. /**
  155. * UTILS FUNCTIONS
  156. **/
  157. #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
  158. #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF)
  159. #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1])
  160. #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1])
  161. #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
  162. do { \
  163. tab_int8[(index)] = (value) >> 8; \
  164. tab_int8[(index) + 1] = (value) & 0xFF; \
  165. } while (0)
  166. void modbus_set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
  167. void modbus_set_bits_from_bytes(uint8_t *dest, int address, unsigned int nb_bits,
  168. const uint8_t *tab_byte);
  169. uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address, unsigned int nb_bits);
  170. float modbus_get_float(const uint16_t *src);
  171. void modbus_set_float(float f, uint16_t *dest);
  172. #include "modbus-tcp.h"
  173. #include "modbus-rtu.h"
  174. MODBUS_END_DECLS
  175. #endif /* _MODBUS_H_ */