modbus.h 12 KB

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