modbus.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright © 2001-2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _MODBUS_H_
  18. #define _MODBUS_H_
  19. #include <stdint.h>
  20. #include <termios.h>
  21. #include <arpa/inet.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define MODBUS_TCP_DEFAULT_PORT 502
  26. #define HEADER_LENGTH_RTU 0
  27. #define PRESET_QUERY_LENGTH_RTU 6
  28. #define PRESET_RESPONSE_LENGTH_RTU 2
  29. #define HEADER_LENGTH_TCP 6
  30. #define PRESET_QUERY_LENGTH_TCP 12
  31. #define PRESET_RESPONSE_LENGTH_TCP 8
  32. #define CHECKSUM_LENGTH_RTU 2
  33. #define CHECKSUM_LENGTH_TCP 0
  34. /* 8 + HEADER_LENGTH_TCP */
  35. #define MIN_QUERY_LENGTH 14
  36. #define MAX_MESSAGE_LENGTH 256
  37. #define MAX_STATUS 800
  38. #define MAX_REGISTERS 100
  39. #define REPORT_SLAVE_ID_LENGTH 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_LENGTH_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. /* Descriptor (tty or socket) */
  94. int fd;
  95. /* Communication mode: RTU or TCP */
  96. type_com_t type_com;
  97. /* Flag debug */
  98. int debug;
  99. /* Header length used for offset */
  100. int header_length;
  101. /* Checksum length RTU = 2 and TCP = 0 */
  102. int checksum_length;
  103. /* TCP port */
  104. int port;
  105. /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*"
  106. on Mac OS X for KeySpan USB<->Serial adapters this string
  107. had to be made bigger on OS X as the directory+file name
  108. was bigger than 19 bytes. Making it 67 bytes for now, but
  109. OS X does support 256 byte file names. May become a problem
  110. in the future. */
  111. #ifdef __APPLE_CC__
  112. char device[64];
  113. #else
  114. char device[16];
  115. #endif
  116. /* Bauds: 9600, 19200, 57600, 115200, etc */
  117. int baud;
  118. /* Data bit */
  119. uint8_t data_bit;
  120. /* Stop bit */
  121. uint8_t stop_bit;
  122. /* Parity: "even", "odd", "none" */
  123. char parity[5];
  124. /* In error_treat with TCP, do a reconnect or just dump the error */
  125. uint8_t error_handling;
  126. /* IP address */
  127. char ip[16];
  128. /* Save old termios settings */
  129. struct termios old_tios;
  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 in 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. /* Sets/resets the coils in the slave from an array in argument */
  167. int force_multiple_coils(modbus_param_t *mb_param, int slave,
  168. int start_addr, int nb_points, uint8_t *data);
  169. /* Copies the values in the slave from the array given in argument */
  170. int preset_multiple_registers(modbus_param_t *mb_param, int slave,
  171. int start_addr, int nb_points, uint16_t *data);
  172. /* Returns the slave id! */
  173. int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest);
  174. /* Initializes the modbus_param_t structure for RTU.
  175. - device: "/dev/ttyS0"
  176. - baud: 9600, 19200, 57600, 115200, etc
  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. /* Initializes the modbus_param_t 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, int 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. /* Establishes a modbus connexion */
  203. int modbus_connect(modbus_param_t *mb_param);
  204. /* Closes a modbus connection */
  205. void modbus_close(modbus_param_t *mb_param);
  206. /* Activates the debug messages */
  207. void modbus_set_debug(modbus_param_t *mb_param, int boolean);
  208. /**
  209. * SLAVE/CLIENT FUNCTIONS
  210. **/
  211. /* Allocates 4 arrays to store coils, input status, input registers and
  212. holding registers. The pointers are stored in modbus_mapping structure.
  213. Returns: TRUE if ok, FALSE on failure
  214. */
  215. int modbus_mapping_new(modbus_mapping_t *mb_mapping,
  216. int nb_coil_status, int nb_input_status,
  217. int nb_holding_registers, int nb_input_registers);
  218. /* Frees the 4 arrays */
  219. void modbus_mapping_free(modbus_mapping_t *mb_mapping);
  220. /* Listens for any query from a modbus master in TCP */
  221. int modbus_init_listen_tcp(modbus_param_t *mb_param);
  222. /* FIXME */
  223. int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length);
  224. /* Manages the received query.
  225. Analyses the query and constructs a response.
  226. If an error occurs, this function construct the response
  227. accordingly.
  228. */
  229. void manage_query(modbus_param_t *mb_param, uint8_t *query,
  230. int query_length, modbus_mapping_t *mb_mapping);
  231. /* Not implemented :
  232. - read_exception_status()
  233. */
  234. /**
  235. * UTILS FUNCTIONS
  236. **/
  237. /* Sets many inputs/coils from a single byte value (all 8 bits of the
  238. byte value are setted) */
  239. void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
  240. /* Sets many inputs/coils from a table of bytes (only the bits between
  241. address and address + nb_bits are setted) */
  242. void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits,
  243. const uint8_t *tab_byte);
  244. /* Gets the byte value from many inputs/coils.
  245. To obtain a full byte, set nb_bits to 8. */
  246. uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits);
  247. #ifdef __cplusplus
  248. }
  249. #endif
  250. #endif /* _MODBUS_H_ */