modbus.h 7.0 KB

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