New API for set/get response and byte timeouts
[libmodbus.git] / src / modbus.h
blob460a567799e39c657f75f96aaa836efd7c3b43cb
1 /*
2 * Copyright © 2001-2013 Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef _MODBUS_H_
20 #define _MODBUS_H_
22 /* Add this for macros that defined unix flavor */
23 #if (defined(__unix__) || defined(unix)) && !defined(USG)
24 #include <sys/param.h>
25 #endif
27 #ifndef _MSC_VER
28 #include <stdint.h>
29 #else
30 #include "stdint.h"
31 #endif
33 #include "modbus-version.h"
35 #if defined(_MSC_VER)
36 # if defined(DLLBUILD)
37 /* define DLLBUILD when building the DLL */
38 # define MODBUS_API __declspec(dllexport)
39 # else
40 # define MODBUS_API __declspec(dllimport)
41 # endif
42 #else
43 # define MODBUS_API
44 #endif
46 #ifdef __cplusplus
47 # define MODBUS_BEGIN_DECLS extern "C" {
48 # define MODBUS_END_DECLS }
49 #else
50 # define MODBUS_BEGIN_DECLS
51 # define MODBUS_END_DECLS
52 #endif
54 MODBUS_BEGIN_DECLS
56 #ifndef FALSE
57 #define FALSE 0
58 #endif
60 #ifndef TRUE
61 #define TRUE 1
62 #endif
64 #ifndef OFF
65 #define OFF 0
66 #endif
68 #ifndef ON
69 #define ON 1
70 #endif
72 /* Modbus function codes */
73 #define MODBUS_FC_READ_COILS 0x01
74 #define MODBUS_FC_READ_DISCRETE_INPUTS 0x02
75 #define MODBUS_FC_READ_HOLDING_REGISTERS 0x03
76 #define MODBUS_FC_READ_INPUT_REGISTERS 0x04
77 #define MODBUS_FC_WRITE_SINGLE_COIL 0x05
78 #define MODBUS_FC_WRITE_SINGLE_REGISTER 0x06
79 #define MODBUS_FC_READ_EXCEPTION_STATUS 0x07
80 #define MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F
81 #define MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10
82 #define MODBUS_FC_REPORT_SLAVE_ID 0x11
83 #define MODBUS_FC_MASK_WRITE_REGISTER 0x16
84 #define MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17
86 #define MODBUS_BROADCAST_ADDRESS 0
88 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
89 * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
90 * (chapter 6 section 11 page 29)
91 * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
93 #define MODBUS_MAX_READ_BITS 2000
94 #define MODBUS_MAX_WRITE_BITS 1968
96 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
97 * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
98 * (chapter 6 section 12 page 31)
99 * Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
100 * (chapter 6 section 17 page 38)
101 * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
103 #define MODBUS_MAX_READ_REGISTERS 125
104 #define MODBUS_MAX_WRITE_REGISTERS 123
105 #define MODBUS_MAX_WR_WRITE_REGISTERS 121
106 #define MODBUS_MAX_WR_READ_REGISTERS 125
108 /* Random number to avoid errno conflicts */
109 #define MODBUS_ENOBASE 112345678
111 /* Protocol exceptions */
112 enum {
113 MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01,
114 MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
115 MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
116 MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
117 MODBUS_EXCEPTION_ACKNOWLEDGE,
118 MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
119 MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
120 MODBUS_EXCEPTION_MEMORY_PARITY,
121 MODBUS_EXCEPTION_NOT_DEFINED,
122 MODBUS_EXCEPTION_GATEWAY_PATH,
123 MODBUS_EXCEPTION_GATEWAY_TARGET,
124 MODBUS_EXCEPTION_MAX
127 #define EMBXILFUN (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION)
128 #define EMBXILADD (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS)
129 #define EMBXILVAL (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE)
130 #define EMBXSFAIL (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE)
131 #define EMBXACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_ACKNOWLEDGE)
132 #define EMBXSBUSY (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY)
133 #define EMBXNACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE)
134 #define EMBXMEMPAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_MEMORY_PARITY)
135 #define EMBXGPATH (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_PATH)
136 #define EMBXGTAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET)
138 /* Native libmodbus error codes */
139 #define EMBBADCRC (EMBXGTAR + 1)
140 #define EMBBADDATA (EMBXGTAR + 2)
141 #define EMBBADEXC (EMBXGTAR + 3)
142 #define EMBUNKEXC (EMBXGTAR + 4)
143 #define EMBMDATA (EMBXGTAR + 5)
144 #define EMBBADSLAVE (EMBXGTAR + 6)
146 extern const unsigned int libmodbus_version_major;
147 extern const unsigned int libmodbus_version_minor;
148 extern const unsigned int libmodbus_version_micro;
150 typedef struct _modbus modbus_t;
152 typedef struct {
153 int nb_bits;
154 int nb_input_bits;
155 int nb_input_registers;
156 int nb_registers;
157 uint8_t *tab_bits;
158 uint8_t *tab_input_bits;
159 uint16_t *tab_input_registers;
160 uint16_t *tab_registers;
161 } modbus_mapping_t;
163 typedef enum
165 MODBUS_ERROR_RECOVERY_NONE = 0,
166 MODBUS_ERROR_RECOVERY_LINK = (1<<1),
167 MODBUS_ERROR_RECOVERY_PROTOCOL = (1<<2),
168 } modbus_error_recovery_mode;
170 MODBUS_API int modbus_set_slave(modbus_t* ctx, int slave);
171 MODBUS_API int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery);
172 MODBUS_API int modbus_set_socket(modbus_t *ctx, int s);
173 MODBUS_API int modbus_get_socket(modbus_t *ctx);
175 MODBUS_API int modbus_get_response_timeout(modbus_t *ctx, long *to_sec, long *to_usec);
176 MODBUS_API int modbus_set_response_timeout(modbus_t *ctx, long to_sec, long to_usec);
178 MODBUS_API int modbus_get_byte_timeout(modbus_t *ctx, long *to_sec, long *to_usec);
179 MODBUS_API int modbus_set_byte_timeout(modbus_t *ctx, long to_sec, long to_usec);
181 MODBUS_API int modbus_get_header_length(modbus_t *ctx);
183 MODBUS_API int modbus_connect(modbus_t *ctx);
184 MODBUS_API void modbus_close(modbus_t *ctx);
186 MODBUS_API void modbus_free(modbus_t *ctx);
188 MODBUS_API int modbus_flush(modbus_t *ctx);
189 MODBUS_API int modbus_set_debug(modbus_t *ctx, int flag);
191 MODBUS_API const char *modbus_strerror(int errnum);
193 MODBUS_API int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
194 MODBUS_API int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
195 MODBUS_API int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
196 MODBUS_API int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
197 MODBUS_API int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
198 MODBUS_API int modbus_write_register(modbus_t *ctx, int reg_addr, int value);
199 MODBUS_API int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
200 MODBUS_API int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);
201 MODBUS_API int modbus_mask_write_register(modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);
202 MODBUS_API int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int write_nb,
203 const uint16_t *src, int read_addr, int read_nb,
204 uint16_t *dest);
205 MODBUS_API int modbus_report_slave_id(modbus_t *ctx, uint8_t *dest);
207 MODBUS_API modbus_mapping_t* modbus_mapping_new(int nb_bits, int nb_input_bits,
208 int nb_registers, int nb_input_registers);
209 MODBUS_API void modbus_mapping_free(modbus_mapping_t *mb_mapping);
211 MODBUS_API int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length);
213 MODBUS_API int modbus_receive(modbus_t *ctx, uint8_t *req);
214 MODBUS_API int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req);
216 MODBUS_API int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp);
218 MODBUS_API int modbus_reply(modbus_t *ctx, const uint8_t *req,
219 int req_length, modbus_mapping_t *mb_mapping);
220 MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,
221 unsigned int exception_code);
224 * UTILS FUNCTIONS
227 #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
228 #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF)
229 #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1])
230 #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1])
231 #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
232 do { \
233 tab_int8[(index)] = (value) >> 8; \
234 tab_int8[(index) + 1] = (value) & 0xFF; \
235 } while (0)
237 MODBUS_API void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value);
238 MODBUS_API void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,
239 const uint8_t *tab_byte);
240 MODBUS_API uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx, unsigned int nb_bits);
241 MODBUS_API float modbus_get_float(const uint16_t *src);
242 MODBUS_API float modbus_get_float_dcba(const uint16_t *src);
243 MODBUS_API void modbus_set_float(float f, uint16_t *dest);
244 MODBUS_API void modbus_set_float_dcba(float f, uint16_t *dest);
246 #include "modbus-tcp.h"
247 #include "modbus-rtu.h"
249 MODBUS_END_DECLS
251 #endif /* _MODBUS_H_ */