modbus.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 struct {
  89. /* Communication : RTU or TCP */
  90. type_com_t type_com;
  91. /* Device: "/dev/ttyS0" or "/dev/ttyUSB0" */
  92. char device[19];
  93. /* Parity: "even", "odd", "none" */
  94. char parity[5];
  95. /* Bauds: 19200 */
  96. int baud_i;
  97. /* Data bit */
  98. int data_bit;
  99. /* Stop bit */
  100. int stop_bit;
  101. /* Save old termios settings */
  102. struct termios old_tios;
  103. /* Descriptor (tty or socket) */
  104. int fd;
  105. /* Flag debug */
  106. int debug;
  107. /* IP address */
  108. char ip[16];
  109. /* TCP port */
  110. uint16_t port;
  111. /* Header length used for offset */
  112. int header_length;
  113. /* Checksum size RTU = 2 and TCP = 0 */
  114. int checksum_size;
  115. } modbus_param_t;
  116. typedef struct {
  117. int nb_coil_status;
  118. int nb_input_status;
  119. int nb_input_registers;
  120. int nb_holding_registers;
  121. unsigned char *tab_coil_status;
  122. unsigned char *tab_input_status;
  123. unsigned short *tab_input_registers;
  124. unsigned short *tab_holding_registers;
  125. } modbus_mapping_t;
  126. /* All functions used for sending or receiving data return :
  127. - the numbers of values (bits or word) if success (0 or more)
  128. - less than 0 for exceptions errors
  129. */
  130. /* Reads the boolean status of coils and sets the array elements in
  131. the destination to TRUE or FALSE */
  132. int read_coil_status(modbus_param_t *mb_param, int slave,
  133. int start_addr, int count, int *dest);
  134. /* Same as read_coil_status but reads the slaves input table */
  135. int read_input_status(modbus_param_t *mb_param, int slave,
  136. int start_addr, int count, int *dest);
  137. /* Reads the holding registers in a slave and put the data into an
  138. array */
  139. int read_holding_registers(modbus_param_t *mb_param, int slave,
  140. int start_addr, int count, int *dest);
  141. /* Reads the input registers in a slave and put the data into an
  142. array */
  143. int read_input_registers(modbus_param_t *mb_param, int slave,
  144. int start_addr, int count, int *dest);
  145. /* Turns on or off a single coil on the slave device */
  146. int force_single_coil(modbus_param_t *mb_param, int slave,
  147. int coil_addr, int state);
  148. /* Sets a value in one holding register in the slave device */
  149. int preset_single_register(modbus_param_t *mb_param, int slave,
  150. int reg_addr, int value);
  151. /* Takes an array of ints and sets or resets the coils on a slave
  152. appropriatly */
  153. int force_multiple_coils(modbus_param_t *mb_param, int slave,
  154. int start_addr, int coil_count, int *data);
  155. /* Copy the values in an array to an array on the slave */
  156. int preset_multiple_registers(modbus_param_t *mb_param, int slave,
  157. int start_addr, int reg_count, int *data);
  158. /* Returns some useful information about the modbus controller */
  159. int report_slave_id(modbus_param_t *mb_param, int slave,
  160. unsigned char *dest);
  161. /* Initialises a parameters structure
  162. - device : "/dev/ttyS0"
  163. - baud : 19200
  164. - parity : "even", "odd" or "none"
  165. - data_bits : 5, 6, 7, 8
  166. - stop_bits : 1, 2
  167. */
  168. void modbus_init_rtu(modbus_param_t *mb_param, char *device,
  169. int baud, char *parity, int data_bit,
  170. int stop_bit);
  171. /* Initialises a parameters structure for TCP
  172. - ip : "192.168.0.5"
  173. - port : 1099
  174. Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
  175. (502). It's convenient to use a port number greater than or equal
  176. to 1024 because it's not necessary to be root to use this port
  177. number.
  178. */
  179. void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, uint16_t port);
  180. /* Sets up a serial port for RTU communications to modbus or a TCP
  181. connexion */
  182. int modbus_connect(modbus_param_t *mb_param);
  183. /* Closes the serial port and restores the previous port configuration
  184. or close the TCP connexion */
  185. void modbus_close(modbus_param_t *mb_param);
  186. /* Sets debug mode */
  187. void modbus_set_debug(modbus_param_t *mb_param, int boolean);
  188. /* Slave/client functions */
  189. int modbus_mapping_new(modbus_mapping_t *mb_mapping,
  190. int nb_coil_status, int nb_input_status,
  191. int nb_input_registers, int nb_holding_registers);
  192. void modbus_mapping_free(modbus_mapping_t *mb_mapping);
  193. int modbus_init_listen_tcp(modbus_param_t *mb_param);
  194. int modbus_listen(modbus_param_t *mb_param, unsigned char *query, int *query_size);
  195. void manage_query(modbus_param_t *mb_param, unsigned char *query,
  196. int query_size, modbus_mapping_t *mb_mapping);
  197. /* Not implemented :
  198. - read_exception_status()
  199. */
  200. #endif /* _MODBUS_H_ */