modbus.h 12 KB

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