modbus.h 8.8 KB

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