modbus.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #define MODBUS_TCP_DEFAULT_PORT 502
  22. #define HEADER_LENGTH_RTU 0
  23. #define PRESET_QUERY_SIZE_RTU 6
  24. #define PRESET_RESPONSE_SIZE_RTU 2
  25. #define HEADER_LENGTH_TCP 6
  26. #define PRESET_QUERY_SIZE_TCP 12
  27. #define PRESET_RESPONSE_SIZE_TCP 8
  28. #define CHECKSUM_SIZE_RTU 2
  29. #define CHECKSUM_SIZE_TCP 0
  30. /* 8 + HEADER_LENGTH_TCP */
  31. #define MIN_QUERY_SIZE 14
  32. /* MIN_RESPONSE_LENGTH + MAX(MAX*) */
  33. #define MAX_PACKET_SIZE 261
  34. #define MAX_READ_STATUS 800
  35. #define MAX_READ_HOLD_REGS 100
  36. #define MAX_READ_INPUT_REGS 100
  37. #define MAX_WRITE_COILS 800
  38. #define MAX_WRITE_REGS 100
  39. #define REPORT_SLAVE_ID_SIZE 75
  40. /* Time out between trames in microsecond */
  41. #define TIME_OUT_BEGIN_OF_TRAME 500000
  42. #define TIME_OUT_END_OF_TRAME 500000
  43. #ifndef FALSE
  44. #define FALSE 0
  45. #endif
  46. #ifndef TRUE
  47. #define TRUE 1
  48. #endif
  49. #ifndef OFF
  50. #define OFF 0
  51. #endif
  52. #ifndef ON
  53. #define ON 1
  54. #endif
  55. /* Function codes */
  56. #define FC_READ_COIL_STATUS 0x01 /* discretes inputs */
  57. #define FC_READ_INPUT_STATUS 0x02 /* discretes outputs */
  58. #define FC_READ_HOLDING_REGISTERS 0x03
  59. #define FC_READ_INPUT_REGISTERS 0x04
  60. #define FC_FORCE_SINGLE_COIL 0x05
  61. #define FC_PRESET_SINGLE_REGISTER 0x06
  62. #define FC_READ_EXCEPTION_STATUS 0x07
  63. #define FC_FORCE_MULTIPLE_COILS 0x0F
  64. #define FC_PRESET_MULTIPLE_REGISTERS 0x10
  65. #define FC_REPORT_SLAVE_ID 0x11
  66. /* Protocol exceptions */
  67. #define ILLEGAL_FUNCTION -0x01
  68. #define ILLEGAL_DATA_ADDRESS -0x02
  69. #define ILLEGAL_DATA_VALUE -0x03
  70. #define SLAVE_DEVICE_FAILURE -0x04
  71. #define SERVER_FAILURE -0x04
  72. #define ACKNOWLEDGE -0x05
  73. #define SLAVE_DEVICE_BUSY -0x06
  74. #define SERVER_BUSY -0x06
  75. #define NEGATIVE_ACKNOWLEDGE -0x07
  76. #define MEMORY_PARITY_ERROR -0x08
  77. #define GATEWAY_PROBLEM_PATH -0x0A
  78. #define GATEWAY_PROBLEM_TARGET -0x0B
  79. /* Local */
  80. #define COMM_TIME_OUT -0x0C
  81. #define PORT_SOCKET_FAILURE -0x0D
  82. #define SELECT_FAILURE -0x0E
  83. #define TOO_MANY_DATAS -0x0F
  84. #define INVALID_CRC -0x10
  85. #define INVALID_EXCEPTION_CODE -0x11
  86. #define CONNECTION_CLOSED -0x12
  87. /* Internal using */
  88. #define MSG_SIZE_UNDEFINED -1
  89. typedef enum { RTU, TCP } type_com_t;
  90. typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
  91. /* This structure is byte-aligned */
  92. typedef struct {
  93. /* Communication : RTU or TCP */
  94. type_com_t type_com;
  95. /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*"
  96. on Mac OS X for KeySpan USB<->Serial adapters this string
  97. had to be made bigger on OS X as the directory+file name
  98. was bigger than 19 bytes. Making it 67 bytes for now, but
  99. OS X does support 256 byte file names. May become a problem
  100. in the future. */
  101. #ifdef __APPLE_CC__
  102. char device[67];
  103. #else
  104. char device[19];
  105. #endif
  106. /* Parity: "even", "odd", "none" */
  107. char parity[5];
  108. /* Bauds: 19200 */
  109. int baud_i;
  110. /* Data bit */
  111. int data_bit;
  112. /* Stop bit */
  113. int stop_bit;
  114. /* Save old termios settings */
  115. struct termios old_tios;
  116. /* Descriptor (tty or socket) */
  117. int fd;
  118. /* Flag debug */
  119. int debug;
  120. /* IP address */
  121. char ip[16];
  122. /* TCP port */
  123. uint16_t port;
  124. /* Header length used for offset */
  125. int header_length;
  126. /* Checksum size RTU = 2 and TCP = 0 */
  127. int checksum_size;
  128. /* In error_treat with TCP, do a reconnect or just dump the error */
  129. error_handling_t error_handling;
  130. } modbus_param_t;
  131. typedef struct {
  132. int nb_coil_status;
  133. int nb_input_status;
  134. int nb_input_registers;
  135. int nb_holding_registers;
  136. uint8_t *tab_coil_status;
  137. uint8_t *tab_input_status;
  138. uint16_t *tab_input_registers;
  139. uint16_t *tab_holding_registers;
  140. } modbus_mapping_t;
  141. /* All functions used for sending or receiving data return :
  142. - the numbers of values (bits or word) if success (0 or more)
  143. - less than 0 for exceptions errors
  144. */
  145. /* Reads the boolean status of coils and sets the array elements in
  146. the destination to TRUE or FALSE */
  147. int read_coil_status(modbus_param_t *mb_param, int slave,
  148. int start_addr, int count, uint8_t *dest);
  149. /* Same as read_coil_status but reads the slaves input table */
  150. int read_input_status(modbus_param_t *mb_param, int slave,
  151. int start_addr, int count, uint8_t *dest);
  152. /* Reads the holding registers in a slave and put the data into an
  153. array */
  154. int read_holding_registers(modbus_param_t *mb_param, int slave,
  155. int start_addr, int count, uint16_t *dest);
  156. /* Reads the input registers in a slave and put the data into an
  157. array */
  158. int read_input_registers(modbus_param_t *mb_param, int slave,
  159. int start_addr, int count, uint16_t *dest);
  160. /* Turns on or off a single coil on the slave device */
  161. int force_single_coil(modbus_param_t *mb_param, int slave,
  162. int coil_addr, int state);
  163. /* Sets a value in one holding register in the slave device */
  164. int preset_single_register(modbus_param_t *mb_param, int slave,
  165. int reg_addr, int value);
  166. /* Takes an array of ints and sets or resets the coils on a slave
  167. appropriatly */
  168. int force_multiple_coils(modbus_param_t *mb_param, int slave,
  169. int start_addr, int coil_count, uint8_t *data);
  170. /* Copy the values in an array to an array on the slave */
  171. int preset_multiple_registers(modbus_param_t *mb_param, int slave,
  172. int start_addr, int reg_count, uint16_t *data);
  173. /* Returns some useful information about the modbus controller */
  174. int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest);
  175. /* Initialises a parameters structure
  176. - device : "/dev/ttyS0"
  177. - baud : 19200
  178. - parity : "even", "odd" or "none"
  179. - data_bits : 5, 6, 7, 8
  180. - stop_bits : 1, 2
  181. */
  182. void modbus_init_rtu(modbus_param_t *mb_param, char *device,
  183. int baud, char *parity, int data_bit,
  184. int stop_bit);
  185. /* Initialises a parameters structure for TCP
  186. - ip : "192.168.0.5"
  187. - port : 1099
  188. Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
  189. (502). It's convenient to use a port number greater than or equal
  190. to 1024 because it's not necessary to be root to use this port
  191. number.
  192. */
  193. void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, uint16_t port);
  194. /* By default, the error handling mode used is RECONNECT_ON_ERROR.
  195. With RECONNECT_ON_ERROR, the library will attempt an immediate
  196. reconnection which may hang for several seconds if the network to
  197. the remote target unit is down.
  198. With NOP_ON_ERROR, it is expected that the application will
  199. check for network error returns and deal with them as necessary.
  200. This function is only useful in TCP mode.
  201. */
  202. void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling);
  203. /* Sets up a serial port for RTU communications to modbus or a TCP
  204. connexion */
  205. int modbus_connect(modbus_param_t *mb_param);
  206. /* Closes the serial port and restores the previous port configuration
  207. or close the TCP connexion */
  208. void modbus_close(modbus_param_t *mb_param);
  209. /* Sets debug mode */
  210. void modbus_set_debug(modbus_param_t *mb_param, int boolean);
  211. /* Slave/client functions */
  212. int modbus_mapping_new(modbus_mapping_t *mb_mapping,
  213. int nb_coil_status, int nb_input_status,
  214. int nb_holding_registers, int nb_input_registers);
  215. void modbus_mapping_free(modbus_mapping_t *mb_mapping);
  216. int modbus_init_listen_tcp(modbus_param_t *mb_param);
  217. int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_size);
  218. void manage_query(modbus_param_t *mb_param, uint8_t *query,
  219. int query_size, modbus_mapping_t *mb_mapping);
  220. /* Not implemented :
  221. - read_exception_status()
  222. */
  223. /** Utils **/
  224. /* Set many inputs/coils form a single byte value (all 8 bits of the
  225. byte value are setted) */
  226. void set_bits_from_byte(uint8_t *dest, uint16_t address,
  227. const uint8_t value);
  228. /* Set many inputs/coils from a table of bytes (only the bits between
  229. address and address + nb_bits are setted) */
  230. void set_bits_from_bytes(uint8_t *dest, uint16_t address, uint16_t nb_bits,
  231. const uint8_t *tab_byte);
  232. /* Get the byte value from many inputs/coils.
  233. To obtain a full byte, set nb_bits to 8. */
  234. uint8_t get_byte_from_bits(const uint8_t *src, uint16_t address, int nb_bits);
  235. #endif /* _MODBUS_H_ */