modbus.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. /* It's not really the minimal length (the real one is report slave ID
  35. * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP
  36. * communications to read many values or write a single one.
  37. * Maximum between :
  38. * - HEADER_LENGTH_TCP (6) + slave (1) + function (1) + address (2) +
  39. * number (2)
  40. * - HEADER_LENGTH_RTU (0) + slave (1) + function (1) + address (2) +
  41. * number (2) + CRC (2)
  42. */
  43. #define MIN_QUERY_LENGTH 12
  44. /* Page 102, Application Notes of PI–MBUS–300:
  45. * The maximum length of the entire message must not exceed 256
  46. * bytes.
  47. */
  48. #define MAX_MESSAGE_LENGTH 256
  49. #define MAX_STATUS 800
  50. #define MAX_REGISTERS 100
  51. #define REPORT_SLAVE_ID_LENGTH 75
  52. /* Time out between trames in microsecond */
  53. #define TIME_OUT_BEGIN_OF_TRAME 500000
  54. #define TIME_OUT_END_OF_TRAME 500000
  55. #ifndef FALSE
  56. #define FALSE 0
  57. #endif
  58. #ifndef TRUE
  59. #define TRUE 1
  60. #endif
  61. #ifndef OFF
  62. #define OFF 0
  63. #endif
  64. #ifndef ON
  65. #define ON 1
  66. #endif
  67. /* Function codes */
  68. #define FC_READ_COIL_STATUS 0x01 /* discretes inputs */
  69. #define FC_READ_INPUT_STATUS 0x02 /* discretes outputs */
  70. #define FC_READ_HOLDING_REGISTERS 0x03
  71. #define FC_READ_INPUT_REGISTERS 0x04
  72. #define FC_FORCE_SINGLE_COIL 0x05
  73. #define FC_PRESET_SINGLE_REGISTER 0x06
  74. #define FC_READ_EXCEPTION_STATUS 0x07
  75. #define FC_FORCE_MULTIPLE_COILS 0x0F
  76. #define FC_PRESET_MULTIPLE_REGISTERS 0x10
  77. #define FC_REPORT_SLAVE_ID 0x11
  78. /* Protocol exceptions */
  79. #define ILLEGAL_FUNCTION -0x01
  80. #define ILLEGAL_DATA_ADDRESS -0x02
  81. #define ILLEGAL_DATA_VALUE -0x03
  82. #define SLAVE_DEVICE_FAILURE -0x04
  83. #define SERVER_FAILURE -0x04
  84. #define ACKNOWLEDGE -0x05
  85. #define SLAVE_DEVICE_BUSY -0x06
  86. #define SERVER_BUSY -0x06
  87. #define NEGATIVE_ACKNOWLEDGE -0x07
  88. #define MEMORY_PARITY_ERROR -0x08
  89. #define GATEWAY_PROBLEM_PATH -0x0A
  90. #define GATEWAY_PROBLEM_TARGET -0x0B
  91. /* Local */
  92. #define COMM_TIME_OUT -0x0C
  93. #define PORT_SOCKET_FAILURE -0x0D
  94. #define SELECT_FAILURE -0x0E
  95. #define TOO_MANY_DATA -0x0F
  96. #define INVALID_CRC -0x10
  97. #define INVALID_EXCEPTION_CODE -0x11
  98. #define CONNECTION_CLOSED -0x12
  99. /* Internal using */
  100. #define MSG_LENGTH_UNDEFINED -1
  101. typedef enum { RTU, TCP } type_com_t;
  102. typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
  103. /* This structure is byte-aligned */
  104. typedef struct {
  105. /* Descriptor (tty or socket) */
  106. int fd;
  107. /* Communication mode: RTU or TCP */
  108. type_com_t type_com;
  109. /* Flag debug */
  110. int debug;
  111. /* Header length used for offset */
  112. int header_length;
  113. /* Checksum length RTU = 2 and TCP = 0 */
  114. int checksum_length;
  115. /* TCP port */
  116. int port;
  117. /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*"
  118. on Mac OS X for KeySpan USB<->Serial adapters this string
  119. had to be made bigger on OS X as the directory+file name
  120. was bigger than 19 bytes. Making it 67 bytes for now, but
  121. OS X does support 256 byte file names. May become a problem
  122. in the future. */
  123. #ifdef __APPLE_CC__
  124. char device[64];
  125. #else
  126. char device[16];
  127. #endif
  128. /* Bauds: 9600, 19200, 57600, 115200, etc */
  129. int baud;
  130. /* Data bit */
  131. uint8_t data_bit;
  132. /* Stop bit */
  133. uint8_t stop_bit;
  134. /* Parity: "even", "odd", "none" */
  135. char parity[5];
  136. /* In error_treat with TCP, do a reconnect or just dump the error */
  137. uint8_t error_handling;
  138. /* IP address */
  139. char ip[16];
  140. /* Save old termios settings */
  141. struct termios old_tios;
  142. } modbus_param_t;
  143. typedef struct {
  144. int nb_coil_status;
  145. int nb_input_status;
  146. int nb_input_registers;
  147. int nb_holding_registers;
  148. uint8_t *tab_coil_status;
  149. uint8_t *tab_input_status;
  150. uint16_t *tab_input_registers;
  151. uint16_t *tab_holding_registers;
  152. } modbus_mapping_t;
  153. /* All functions used for sending or receiving data return:
  154. - the numbers of values (bits or word) if success (0 or more)
  155. - less than 0 for exceptions errors
  156. */
  157. /* Reads the boolean status of coils and sets the array elements in
  158. the destination to TRUE or FALSE */
  159. int read_coil_status(modbus_param_t *mb_param, int slave,
  160. int start_addr, int nb, uint8_t *dest);
  161. /* Same as read_coil_status but reads the slaves input table */
  162. int read_input_status(modbus_param_t *mb_param, int slave,
  163. int start_addr, int nb, uint8_t *dest);
  164. /* Reads the holding registers in a slave and put the data into an
  165. array */
  166. int read_holding_registers(modbus_param_t *mb_param, int slave,
  167. int start_addr, int nb, uint16_t *dest);
  168. /* Reads the input registers in a slave and put the data into an
  169. array */
  170. int read_input_registers(modbus_param_t *mb_param, int slave,
  171. int start_addr, int nb, uint16_t *dest);
  172. /* Turns ON or OFF a single coil in the slave device */
  173. int force_single_coil(modbus_param_t *mb_param, int slave,
  174. int coil_addr, int state);
  175. /* Sets a value in one holding register in the slave device */
  176. int preset_single_register(modbus_param_t *mb_param, int slave,
  177. int reg_addr, int value);
  178. /* Sets/resets the coils in the slave from an array in argument */
  179. int force_multiple_coils(modbus_param_t *mb_param, int slave,
  180. int start_addr, int nb, const uint8_t *data);
  181. /* Copies the values in the slave from the array given in argument */
  182. int preset_multiple_registers(modbus_param_t *mb_param, int slave,
  183. int start_addr, int nb, const uint16_t *data);
  184. /* Returns the slave id! */
  185. int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest);
  186. /* Initializes the modbus_param_t structure for RTU.
  187. - device: "/dev/ttyS0"
  188. - baud: 9600, 19200, 57600, 115200, etc
  189. - parity: "even", "odd" or "none"
  190. - data_bits: 5, 6, 7, 8
  191. - stop_bits: 1, 2
  192. */
  193. void modbus_init_rtu(modbus_param_t *mb_param, const char *device,
  194. int baud, const char *parity, int data_bit,
  195. int stop_bit);
  196. /* Initializes the modbus_param_t structure for TCP.
  197. - ip : "192.168.0.5"
  198. - port : 1099
  199. Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
  200. (502). It's convenient to use a port number greater than or equal
  201. to 1024 because it's not necessary to be root to use this port
  202. number.
  203. */
  204. void modbus_init_tcp(modbus_param_t *mb_param, const char *ip_address, int port);
  205. /* By default, the error handling mode used is RECONNECT_ON_ERROR.
  206. With RECONNECT_ON_ERROR, the library will attempt an immediate
  207. reconnection which may hang for several seconds if the network to
  208. the remote target unit is down.
  209. With NOP_ON_ERROR, it is expected that the application will
  210. check for network error returns and deal with them as necessary.
  211. This function is only useful in TCP mode.
  212. */
  213. void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling);
  214. /* Establishes a modbus connexion.
  215. Returns -1 if an error occured. */
  216. int modbus_connect(modbus_param_t *mb_param);
  217. /* Closes a modbus connection */
  218. void modbus_close(modbus_param_t *mb_param);
  219. /* Activates the debug messages */
  220. void modbus_set_debug(modbus_param_t *mb_param, int boolean);
  221. /**
  222. * SLAVE/CLIENT FUNCTIONS
  223. **/
  224. /* Allocates 4 arrays to store coils, input status, input registers and
  225. holding registers. The pointers are stored in modbus_mapping structure.
  226. Returns: TRUE if ok, FALSE on failure
  227. */
  228. int modbus_mapping_new(modbus_mapping_t *mb_mapping,
  229. int nb_coil_status, int nb_input_status,
  230. int nb_holding_registers, int nb_input_registers);
  231. /* Frees the 4 arrays */
  232. void modbus_mapping_free(modbus_mapping_t *mb_mapping);
  233. /* Listens for any query from one or many modbus masters in TCP.
  234. Returns: socket
  235. */
  236. int modbus_slave_listen_tcp(modbus_param_t *mb_param, int nb_connection);
  237. /* Waits for a connection */
  238. int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket);
  239. /* Listens for any query from a modbus master in TCP, requires the socket file
  240. descriptor etablished with the master device in argument.
  241. Returns:
  242. - 0 if OK, or a negative error number if the request fails
  243. - query, message received
  244. - query_length, length in bytes of the message
  245. */
  246. int modbus_slave_receive(modbus_param_t *mb_param, int sockfd,
  247. uint8_t *query, int *query_length);
  248. /* Manages the received query.
  249. Analyses the query and constructs a response.
  250. If an error occurs, this function construct the response
  251. accordingly.
  252. */
  253. void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
  254. int query_length, modbus_mapping_t *mb_mapping);
  255. /**
  256. * UTILS FUNCTIONS
  257. **/
  258. /* Sets many input/coil status from a single byte value (all 8 bits of
  259. the byte value are setted) */
  260. void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
  261. /* Sets many input/coil status from a table of bytes (only the bits
  262. between address and address + nb_bits are setted) */
  263. void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits,
  264. const uint8_t *tab_byte);
  265. /* Gets the byte value from many input/coil status.
  266. To obtain a full byte, set nb_bits to 8. */
  267. uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits);
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* _MODBUS_H_ */