modbus.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. Copyright (C) 2001-2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the
  13. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA.
  15. */
  16. #ifndef _MODBUS_H_
  17. #define _MODBUS_H_
  18. #include <stdint.h>
  19. #include <termios.h>
  20. #include <arpa/inet.h>
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #define MODBUS_TCP_DEFAULT_PORT 502
  25. #define HEADER_LENGTH_RTU 0
  26. #define PRESET_QUERY_LENGTH_RTU 6
  27. #define PRESET_RESPONSE_LENGTH_RTU 2
  28. #define HEADER_LENGTH_TCP 6
  29. #define PRESET_QUERY_LENGTH_TCP 12
  30. #define PRESET_RESPONSE_LENGTH_TCP 8
  31. #define CHECKSUM_LENGTH_RTU 2
  32. #define CHECKSUM_LENGTH_TCP 0
  33. /* 8 + HEADER_LENGTH_TCP */
  34. #define MIN_QUERY_LENGTH 14
  35. #define MAX_MESSAGE_LENGTH 256
  36. #define MAX_READ_STATUS 800
  37. #define MAX_READ_HOLD_REGS 100
  38. #define MAX_READ_INPUT_REGS 100
  39. #define MAX_WRITE_COILS 800
  40. #define MAX_WRITE_REGS 100
  41. #define REPORT_SLAVE_ID_LENGTH 75
  42. /* Time out between trames in microsecond */
  43. #define TIME_OUT_BEGIN_OF_TRAME 500000
  44. #define TIME_OUT_END_OF_TRAME 500000
  45. #ifndef FALSE
  46. #define FALSE 0
  47. #endif
  48. #ifndef TRUE
  49. #define TRUE 1
  50. #endif
  51. #ifndef OFF
  52. #define OFF 0
  53. #endif
  54. #ifndef ON
  55. #define ON 1
  56. #endif
  57. /* Function codes */
  58. #define FC_READ_COIL_STATUS 0x01 /* discretes inputs */
  59. #define FC_READ_INPUT_STATUS 0x02 /* discretes outputs */
  60. #define FC_READ_HOLDING_REGISTERS 0x03
  61. #define FC_READ_INPUT_REGISTERS 0x04
  62. #define FC_FORCE_SINGLE_COIL 0x05
  63. #define FC_PRESET_SINGLE_REGISTER 0x06
  64. #define FC_READ_EXCEPTION_STATUS 0x07
  65. #define FC_FORCE_MULTIPLE_COILS 0x0F
  66. #define FC_PRESET_MULTIPLE_REGISTERS 0x10
  67. #define FC_REPORT_SLAVE_ID 0x11
  68. /* Protocol exceptions */
  69. #define ILLEGAL_FUNCTION -0x01
  70. #define ILLEGAL_DATA_ADDRESS -0x02
  71. #define ILLEGAL_DATA_VALUE -0x03
  72. #define SLAVE_DEVICE_FAILURE -0x04
  73. #define SERVER_FAILURE -0x04
  74. #define ACKNOWLEDGE -0x05
  75. #define SLAVE_DEVICE_BUSY -0x06
  76. #define SERVER_BUSY -0x06
  77. #define NEGATIVE_ACKNOWLEDGE -0x07
  78. #define MEMORY_PARITY_ERROR -0x08
  79. #define GATEWAY_PROBLEM_PATH -0x0A
  80. #define GATEWAY_PROBLEM_TARGET -0x0B
  81. /* Local */
  82. #define COMM_TIME_OUT -0x0C
  83. #define PORT_SOCKET_FAILURE -0x0D
  84. #define SELECT_FAILURE -0x0E
  85. #define TOO_MANY_DATAS -0x0F
  86. #define INVALID_CRC -0x10
  87. #define INVALID_EXCEPTION_CODE -0x11
  88. #define CONNECTION_CLOSED -0x12
  89. /* Internal using */
  90. #define MSG_LENGTH_UNDEFINED -1
  91. typedef enum { RTU, TCP } type_com_t;
  92. typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
  93. /* This structure is byte-aligned */
  94. typedef struct {
  95. /* Descriptor (tty or socket) */
  96. int fd;
  97. /* Communication mode: RTU or TCP */
  98. type_com_t type_com;
  99. /* Flag debug */
  100. int debug;
  101. /* Header length used for offset */
  102. int header_length;
  103. /* Checksum length RTU = 2 and TCP = 0 */
  104. int checksum_length;
  105. /* TCP port */
  106. int port;
  107. /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*"
  108. on Mac OS X for KeySpan USB<->Serial adapters this string
  109. had to be made bigger on OS X as the directory+file name
  110. was bigger than 19 bytes. Making it 67 bytes for now, but
  111. OS X does support 256 byte file names. May become a problem
  112. in the future. */
  113. #ifdef __APPLE_CC__
  114. char device[64];
  115. #else
  116. char device[16];
  117. #endif
  118. /* Bauds: 9600, 19200, 57600, 115200, etc */
  119. int baud;
  120. /* Data bit */
  121. uint8_t data_bit;
  122. /* Stop bit */
  123. uint8_t stop_bit;
  124. /* Parity: "even", "odd", "none" */
  125. char parity[5];
  126. /* In error_treat with TCP, do a reconnect or just dump the error */
  127. uint8_t error_handling;
  128. /* IP address */
  129. char ip[16];
  130. /* Save old termios settings */
  131. struct termios old_tios;
  132. } modbus_param_t;
  133. typedef struct {
  134. int nb_coil_status;
  135. int nb_input_status;
  136. int nb_input_registers;
  137. int nb_holding_registers;
  138. uint8_t *tab_coil_status;
  139. uint8_t *tab_input_status;
  140. uint16_t *tab_input_registers;
  141. uint16_t *tab_holding_registers;
  142. } modbus_mapping_t;
  143. /* All functions used for sending or receiving data return:
  144. - the numbers of values (bits or word) if success (0 or more)
  145. - less than 0 for exceptions errors
  146. */
  147. /* Reads the boolean status of coils and sets the array elements in
  148. the destination to TRUE or FALSE */
  149. int read_coil_status(modbus_param_t *mb_param, int slave,
  150. int start_addr, int count, uint8_t *dest);
  151. /* Same as read_coil_status but reads the slaves input table */
  152. int read_input_status(modbus_param_t *mb_param, int slave,
  153. int start_addr, int count, uint8_t *dest);
  154. /* Reads the holding registers in a slave and put the data into an
  155. array */
  156. int read_holding_registers(modbus_param_t *mb_param, int slave,
  157. int start_addr, int count, uint16_t *dest);
  158. /* Reads the input registers in a slave and put the data into an
  159. array */
  160. int read_input_registers(modbus_param_t *mb_param, int slave,
  161. int start_addr, int count, uint16_t *dest);
  162. /* Turns ON or OFF a single coil in the slave device */
  163. int force_single_coil(modbus_param_t *mb_param, int slave,
  164. int coil_addr, int state);
  165. /* Sets a value in one holding register in the slave device */
  166. int preset_single_register(modbus_param_t *mb_param, int slave,
  167. int reg_addr, int value);
  168. /* Sets/resets the coils in the slave from an array in argument */
  169. int force_multiple_coils(modbus_param_t *mb_param, int slave,
  170. int start_addr, int nb_points, uint8_t *data);
  171. /* Copies the values in the slave from the array given in argument */
  172. int preset_multiple_registers(modbus_param_t *mb_param, int slave,
  173. int start_addr, int nb_points, uint16_t *data);
  174. /* Returns the slave id! */
  175. int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest);
  176. /* Initializes the modbus_param_t structure for RTU.
  177. - device: "/dev/ttyS0"
  178. - baud: 9600, 19200, 57600, 115200, etc
  179. - parity: "even", "odd" or "none"
  180. - data_bits: 5, 6, 7, 8
  181. - stop_bits: 1, 2
  182. */
  183. void modbus_init_rtu(modbus_param_t *mb_param, char *device,
  184. int baud, char *parity, int data_bit,
  185. int stop_bit);
  186. /* Initializes the modbus_param_t structure for TCP.
  187. - ip : "192.168.0.5"
  188. - port : 1099
  189. Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
  190. (502). It's convenient to use a port number greater than or equal
  191. to 1024 because it's not necessary to be root to use this port
  192. number.
  193. */
  194. void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, int port);
  195. /* By default, the error handling mode used is RECONNECT_ON_ERROR.
  196. With RECONNECT_ON_ERROR, the library will attempt an immediate
  197. reconnection which may hang for several seconds if the network to
  198. the remote target unit is down.
  199. With NOP_ON_ERROR, it is expected that the application will
  200. check for network error returns and deal with them as necessary.
  201. This function is only useful in TCP mode.
  202. */
  203. void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling);
  204. /* Establishes a modbus connexion */
  205. int modbus_connect(modbus_param_t *mb_param);
  206. /* Closes a modbus connection */
  207. void modbus_close(modbus_param_t *mb_param);
  208. /* Activates the debug messages */
  209. void modbus_set_debug(modbus_param_t *mb_param, int boolean);
  210. /**
  211. * SLAVE/CLIENT FUNCTIONS
  212. **/
  213. /* Allocates 4 arrays to store coils, input status, input registers and
  214. holding registers. The pointers are stored in modbus_mapping structure.
  215. Returns: TRUE if ok, FALSE on failure
  216. */
  217. int modbus_mapping_new(modbus_mapping_t *mb_mapping,
  218. int nb_coil_status, int nb_input_status,
  219. int nb_holding_registers, int nb_input_registers);
  220. /* Frees the 4 arrays */
  221. void modbus_mapping_free(modbus_mapping_t *mb_mapping);
  222. /* Listens for any query from a modbus master in TCP */
  223. int modbus_init_listen_tcp(modbus_param_t *mb_param);
  224. /* FIXME */
  225. int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length);
  226. /* Manages the received query.
  227. Analyses the query and constructs a response.
  228. If an error occurs, this function construct the response
  229. accordingly.
  230. */
  231. void manage_query(modbus_param_t *mb_param, uint8_t *query,
  232. int query_length, modbus_mapping_t *mb_mapping);
  233. /* Not implemented :
  234. - read_exception_status()
  235. */
  236. /**
  237. * UTILS FUNCTIONS
  238. **/
  239. /* Sets many inputs/coils from a single byte value (all 8 bits of the
  240. byte value are setted) */
  241. void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
  242. /* Sets many inputs/coils from a table of bytes (only the bits between
  243. address and address + nb_bits are setted) */
  244. void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits,
  245. const uint8_t *tab_byte);
  246. /* Gets the byte value from many inputs/coils.
  247. To obtain a full byte, set nb_bits to 8. */
  248. uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits);
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #endif /* _MODBUS_H_ */