modbus.h 12 KB

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