Documentation improvements to libmodbus page
[libmodbus.git] / src / modbus.h
blob82f5a220dd92d2d346f61e7fe60a285a5f520de0
1 /*
2 * Copyright © 2001-2011 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 #include <sys/time.h>
30 #else
31 #include "stdint.h"
32 #include <time.h>
33 #endif
35 #include "modbus-version.h"
37 #if defined(_WIN32)
38 # if defined(DLLBUILD)
39 /* define DLLBUILD when building the DLL */
40 # define EXPORT __declspec(dllexport)
41 # else
42 # define EXPORT __declspec(dllimport)
43 # endif
44 #else
45 # define EXPORT
46 #endif
48 #ifdef __cplusplus
49 # define MODBUS_BEGIN_DECLS extern "C" {
50 # define MODBUS_END_DECLS }
51 #else
52 # define MODBUS_BEGIN_DECLS
53 # define MODBUS_END_DECLS
54 #endif
56 MODBUS_BEGIN_DECLS
58 #ifndef FALSE
59 #define FALSE 0
60 #endif
62 #ifndef TRUE
63 #define TRUE 1
64 #endif
66 #ifndef OFF
67 #define OFF 0
68 #endif
70 #ifndef ON
71 #define ON 1
72 #endif
74 #define MODBUS_BROADCAST_ADDRESS 0
76 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
77 * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
78 * (chapter 6 section 11 page 29)
79 * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
81 #define MODBUS_MAX_READ_BITS 2000
82 #define MODBUS_MAX_WRITE_BITS 1968
84 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
85 * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
86 * (chapter 6 section 12 page 31)
87 * Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
88 * (chapter 6 section 17 page 38)
89 * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
91 #define MODBUS_MAX_READ_REGISTERS 125
92 #define MODBUS_MAX_WRITE_REGISTERS 123
93 #define MODBUS_MAX_RW_WRITE_REGISTERS 121
95 /* Random number to avoid errno conflicts */
96 #define MODBUS_ENOBASE 112345678
98 /* Protocol exceptions */
99 enum {
100 MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01,
101 MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
102 MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
103 MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
104 MODBUS_EXCEPTION_ACKNOWLEDGE,
105 MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
106 MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
107 MODBUS_EXCEPTION_MEMORY_PARITY,
108 MODBUS_EXCEPTION_NOT_DEFINED,
109 MODBUS_EXCEPTION_GATEWAY_PATH,
110 MODBUS_EXCEPTION_GATEWAY_TARGET,
111 MODBUS_EXCEPTION_MAX
114 #define EMBXILFUN (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION)
115 #define EMBXILADD (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS)
116 #define EMBXILVAL (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE)
117 #define EMBXSFAIL (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE)
118 #define EMBXACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_ACKNOWLEDGE)
119 #define EMBXSBUSY (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY)
120 #define EMBXNACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE)
121 #define EMBXMEMPAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_MEMORY_PARITY)
122 #define EMBXGPATH (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_PATH)
123 #define EMBXGTAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET)
125 /* Native libmodbus error codes */
126 #define EMBBADCRC (EMBXGTAR + 1)
127 #define EMBBADDATA (EMBXGTAR + 2)
128 #define EMBBADEXC (EMBXGTAR + 3)
129 #define EMBUNKEXC (EMBXGTAR + 4)
130 #define EMBMDATA (EMBXGTAR + 5)
131 #define EMBBADSLAVE (EMBXGTAR + 6)
133 extern const unsigned int libmodbus_version_major;
134 extern const unsigned int libmodbus_version_minor;
135 extern const unsigned int libmodbus_version_micro;
137 typedef struct _modbus modbus_t;
139 typedef struct {
140 int nb_bits;
141 int nb_input_bits;
142 int nb_input_registers;
143 int nb_registers;
144 uint8_t *tab_bits;
145 uint8_t *tab_input_bits;
146 uint16_t *tab_input_registers;
147 uint16_t *tab_registers;
148 } modbus_mapping_t;
150 typedef enum
152 MODBUS_ERROR_RECOVERY_NONE = 0,
153 MODBUS_ERROR_RECOVERY_LINK = (1<<1),
154 MODBUS_ERROR_RECOVERY_PROTOCOL = (1<<2),
155 } modbus_error_recovery_mode;
157 EXPORT int modbus_set_slave(modbus_t* ctx, int slave);
158 EXPORT int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery);
159 EXPORT void modbus_set_socket(modbus_t *ctx, int socket);
160 EXPORT int modbus_get_socket(modbus_t *ctx);
162 EXPORT void modbus_get_response_timeout(modbus_t *ctx, struct timeval *timeout);
163 EXPORT void modbus_set_response_timeout(modbus_t *ctx, const struct timeval *timeout);
165 EXPORT void modbus_get_byte_timeout(modbus_t *ctx, struct timeval *timeout);
166 EXPORT void modbus_set_byte_timeout(modbus_t *ctx, const struct timeval *timeout);
168 EXPORT int modbus_get_header_length(modbus_t *ctx);
170 EXPORT int modbus_connect(modbus_t *ctx);
171 EXPORT void modbus_close(modbus_t *ctx);
173 EXPORT void modbus_free(modbus_t *ctx);
175 EXPORT int modbus_flush(modbus_t *ctx);
176 EXPORT void modbus_set_debug(modbus_t *ctx, int boolean);
178 EXPORT const char *modbus_strerror(int errnum);
180 EXPORT int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
181 EXPORT int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
182 EXPORT int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
183 EXPORT int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
184 EXPORT int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
185 EXPORT int modbus_write_register(modbus_t *ctx, int reg_addr, int value);
186 EXPORT int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
187 EXPORT int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);
188 EXPORT int modbus_mask_write_register(modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);
189 EXPORT int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int write_nb,
190 const uint16_t *src, int read_addr, int read_nb,
191 uint16_t *dest);
192 EXPORT int modbus_report_slave_id(modbus_t *ctx, uint8_t *dest);
194 EXPORT modbus_mapping_t* modbus_mapping_new(int nb_bits, int nb_input_bits,
195 int nb_registers, int nb_input_registers);
196 EXPORT void modbus_mapping_free(modbus_mapping_t *mb_mapping);
198 EXPORT int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length);
200 EXPORT int modbus_receive(modbus_t *ctx, uint8_t *req);
201 EXPORT int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req);
203 EXPORT int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp);
205 EXPORT int modbus_reply(modbus_t *ctx, const uint8_t *req,
206 int req_length, modbus_mapping_t *mb_mapping);
207 EXPORT int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,
208 unsigned int exception_code);
211 * UTILS FUNCTIONS
214 #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
215 #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF)
216 #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1])
217 #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1])
218 #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
219 do { \
220 tab_int8[(index)] = (value) >> 8; \
221 tab_int8[(index) + 1] = (value) & 0xFF; \
222 } while (0)
224 EXPORT void modbus_set_bits_from_byte(uint8_t *dest, int index, const uint8_t value);
225 EXPORT void modbus_set_bits_from_bytes(uint8_t *dest, int index, unsigned int nb_bits,
226 const uint8_t *tab_byte);
227 EXPORT uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index, unsigned int nb_bits);
228 EXPORT float modbus_get_float(const uint16_t *src);
229 EXPORT float modbus_get_float_swapped(const uint16_t *src);
230 EXPORT void modbus_set_float(float f, uint16_t *dest);
231 EXPORT void modbus_set_float_swapped(float f, uint16_t *dest);
233 #include "modbus-tcp.h"
234 #include "modbus-rtu.h"
236 MODBUS_END_DECLS
238 #endif /* _MODBUS_H_ */