modbus.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_LENGTH_RTU 6
  24. #define PRESET_RESPONSE_LENGTH_RTU 2
  25. #define HEADER_LENGTH_TCP 6
  26. #define PRESET_QUERY_LENGTH_TCP 12
  27. #define PRESET_RESPONSE_LENGTH_TCP 8
  28. #define CHECKSUM_LENGTH_RTU 2
  29. #define CHECKSUM_LENGTH_TCP 0
  30. /* 8 + HEADER_LENGTH_TCP */
  31. #define MIN_QUERY_LENGTH 14
  32. #define MAX_MESSAGE_LENGTH 256
  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_LENGTH 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. #define CONNECTION_CLOSED -0x12
  86. /* Internal using */
  87. #define MSG_LENGTH_UNDEFINED -1
  88. typedef enum { RTU, TCP } type_com_t;
  89. typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
  90. /* This structure is byte-aligned */
  91. typedef struct {
  92. /* Communication : RTU or TCP */
  93. type_com_t type_com;
  94. /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*"
  95. on Mac OS X for KeySpan USB<->Serial adapters this string
  96. had to be made bigger on OS X as the directory+file name
  97. was bigger than 19 bytes. Making it 67 bytes for now, but
  98. OS X does support 256 byte file names. May become a problem
  99. in the future. */
  100. #ifdef __APPLE_CC__
  101. char device[67];
  102. #else
  103. char device[19];
  104. #endif
  105. /* Parity: "even", "odd", "none" */
  106. char parity[5];
  107. /* Bauds: 19200 */
  108. int baud_i;
  109. /* Data bit */
  110. int data_bit;
  111. /* Stop bit */
  112. int stop_bit;
  113. /* Save old termios settings */
  114. struct termios old_tios;
  115. /* Descriptor (tty or socket) */
  116. int fd;
  117. /* Flag debug */
  118. int debug;
  119. /* IP address */
  120. char ip[16];
  121. /* TCP port */
  122. uint16_t port;
  123. /* Header length used for offset */
  124. int header_length;
  125. /* Checksum length RTU = 2 and TCP = 0 */
  126. int checksum_length;
  127. /* In error_treat with TCP, do a reconnect or just dump the error */
  128. error_handling_t error_handling;
  129. } modbus_param_t;
  130. typedef struct {
  131. int nb_coil_status;
  132. int nb_input_status;
  133. int nb_input_registers;
  134. int nb_holding_registers;
  135. uint8_t *tab_coil_status;
  136. uint8_t *tab_input_status;
  137. uint16_t *tab_input_registers;
  138. uint16_t *tab_holding_registers;
  139. } modbus_mapping_t;
  140. /* All functions used for sending or receiving data return:
  141. - the numbers of values (bits or word) if success (0 or more)
  142. - less than 0 for exceptions errors
  143. */
  144. /* Reads the boolean status of coils and sets the array elements in
  145. the destination to TRUE or FALSE */
  146. int read_coil_status(modbus_param_t *mb_param, int slave,
  147. int start_addr, int count, uint8_t *dest);
  148. /* Same as read_coil_status but reads the slaves input table */
  149. int read_input_status(modbus_param_t *mb_param, int slave,
  150. int start_addr, int count, uint8_t *dest);
  151. /* Reads the holding registers in a slave and put the data into an
  152. array */
  153. int read_holding_registers(modbus_param_t *mb_param, int slave,
  154. int start_addr, int count, uint16_t *dest);
  155. /* Reads the input registers in a slave and put the data into an
  156. array */
  157. int read_input_registers(modbus_param_t *mb_param, int slave,
  158. int start_addr, int count, uint16_t *dest);
  159. /* Turns ON or OFF a single coil in the slave device */
  160. int force_single_coil(modbus_param_t *mb_param, int slave,
  161. int coil_addr, int state);
  162. /* Sets a value in one holding register in the slave device */
  163. int preset_single_register(modbus_param_t *mb_param, int slave,
  164. int reg_addr, int value);
  165. /* Sets/resets the coils in the slave from an array in argument */
  166. int force_multiple_coils(modbus_param_t *mb_param, int slave,
  167. int start_addr, int nb_points, uint8_t *data);
  168. /* Copies the values in the slave from the array given in argument */
  169. int preset_multiple_registers(modbus_param_t *mb_param, int slave,
  170. int start_addr, int nb_points, uint16_t *data);
  171. /* Returns the slave id! */
  172. int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest);
  173. /* Initializes the modbus_param_t structure for RTU.
  174. - device: "/dev/ttyS0"
  175. - baud: 19200
  176. - parity: "even", "odd" or "none"
  177. - data_bits: 5, 6, 7, 8
  178. - stop_bits: 1, 2
  179. */
  180. void modbus_init_rtu(modbus_param_t *mb_param, char *device,
  181. int baud, char *parity, int data_bit,
  182. int stop_bit);
  183. /* Initializes the modbus_param_t structure for TCP.
  184. - ip : "192.168.0.5"
  185. - port : 1099
  186. Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
  187. (502). It's convenient to use a port number greater than or equal
  188. to 1024 because it's not necessary to be root to use this port
  189. number.
  190. */
  191. void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, int port);
  192. /* By default, the error handling mode used is RECONNECT_ON_ERROR.
  193. With RECONNECT_ON_ERROR, the library will attempt an immediate
  194. reconnection which may hang for several seconds if the network to
  195. the remote target unit is down.
  196. With NOP_ON_ERROR, it is expected that the application will
  197. check for network error returns and deal with them as necessary.
  198. This function is only useful in TCP mode.
  199. */
  200. void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling);
  201. /* Establishes a modbus connexion */
  202. int modbus_connect(modbus_param_t *mb_param);
  203. /* Closes a modbus connection */
  204. void modbus_close(modbus_param_t *mb_param);
  205. /* Activates the debug messages */
  206. void modbus_set_debug(modbus_param_t *mb_param, int boolean);
  207. /**
  208. * SLAVE/CLIENT FUNCTIONS
  209. **/
  210. /* Allocates 4 arrays to store coils, input status, input registers and
  211. holding registers. The pointers are stored in modbus_mapping structure.
  212. Returns: TRUE if ok, FALSE on failure
  213. */
  214. int modbus_mapping_new(modbus_mapping_t *mb_mapping,
  215. int nb_coil_status, int nb_input_status,
  216. int nb_holding_registers, int nb_input_registers);
  217. /* Frees the 4 arrays */
  218. void modbus_mapping_free(modbus_mapping_t *mb_mapping);
  219. /* Listens for any query from a modbus master in TCP */
  220. int modbus_init_listen_tcp(modbus_param_t *mb_param);
  221. /* FIXME */
  222. int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length);
  223. /* Manages the received query.
  224. Analyses the query and constructs a response.
  225. If an error occurs, this function construct the response
  226. accordingly.
  227. */
  228. void manage_query(modbus_param_t *mb_param, uint8_t *query,
  229. int query_length, modbus_mapping_t *mb_mapping);
  230. /* Not implemented :
  231. - read_exception_status()
  232. */
  233. /**
  234. * UTILS FUNCTIONS
  235. **/
  236. /* Sets many inputs/coils from a single byte value (all 8 bits of the
  237. byte value are setted) */
  238. void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
  239. /* Sets many inputs/coils from a table of bytes (only the bits between
  240. address and address + nb_bits are setted) */
  241. void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits,
  242. const uint8_t *tab_byte);
  243. /* Gets the byte value from many inputs/coils.
  244. To obtain a full byte, set nb_bits to 8. */
  245. uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits);
  246. #endif /* _MODBUS_H_ */