11 #if (defined(__unix__) || defined(unix)) && !defined(USG) 12 #include <sys/param.h> 21 #include "modbus-version.h" 24 # if defined(DLLBUILD) 26 # define MODBUS_API __declspec(dllexport) 28 # define MODBUS_API __declspec(dllimport) 35 # define MODBUS_BEGIN_DECLS extern "C" { 36 # define MODBUS_END_DECLS } 38 # define MODBUS_BEGIN_DECLS 39 # define MODBUS_END_DECLS 61 #define MODBUS_FC_READ_COILS 0x01 62 #define MODBUS_FC_READ_DISCRETE_INPUTS 0x02 63 #define MODBUS_FC_READ_HOLDING_REGISTERS 0x03 64 #define MODBUS_FC_READ_INPUT_REGISTERS 0x04 65 #define MODBUS_FC_WRITE_SINGLE_COIL 0x05 66 #define MODBUS_FC_WRITE_SINGLE_REGISTER 0x06 67 #define MODBUS_FC_READ_EXCEPTION_STATUS 0x07 68 #define MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F 69 #define MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10 70 #define MODBUS_FC_REPORT_SLAVE_ID 0x11 71 #define MODBUS_FC_MASK_WRITE_REGISTER 0x16 72 #define MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17 74 #define MODBUS_BROADCAST_ADDRESS 0 81 #define MODBUS_MAX_READ_BITS 2000 82 #define MODBUS_MAX_WRITE_BITS 1968 91 #define MODBUS_MAX_READ_REGISTERS 125 92 #define MODBUS_MAX_WRITE_REGISTERS 123 93 #define MODBUS_MAX_WR_WRITE_REGISTERS 121 94 #define MODBUS_MAX_WR_READ_REGISTERS 125 101 #define MODBUS_MAX_PDU_LENGTH 253 111 #define MODBUS_MAX_ADU_LENGTH 260 114 #define MODBUS_ENOBASE 112345678 118 MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01,
119 MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
120 MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
121 MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
122 MODBUS_EXCEPTION_ACKNOWLEDGE,
123 MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
124 MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
125 MODBUS_EXCEPTION_MEMORY_PARITY,
126 MODBUS_EXCEPTION_NOT_DEFINED,
127 MODBUS_EXCEPTION_GATEWAY_PATH,
128 MODBUS_EXCEPTION_GATEWAY_TARGET,
132 #define EMBXILFUN (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION) 133 #define EMBXILADD (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS) 134 #define EMBXILVAL (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE) 135 #define EMBXSFAIL (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE) 136 #define EMBXACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_ACKNOWLEDGE) 137 #define EMBXSBUSY (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY) 138 #define EMBXNACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE) 139 #define EMBXMEMPAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_MEMORY_PARITY) 140 #define EMBXGPATH (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_PATH) 141 #define EMBXGTAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET) 144 #define EMBBADCRC (EMBXGTAR + 1) 145 #define EMBBADDATA (EMBXGTAR + 2) 146 #define EMBBADEXC (EMBXGTAR + 3) 147 #define EMBUNKEXC (EMBXGTAR + 4) 148 #define EMBMDATA (EMBXGTAR + 5) 149 #define EMBBADSLAVE (EMBXGTAR + 6) 151 extern const unsigned int libmodbus_version_major;
152 extern const unsigned int libmodbus_version_minor;
153 extern const unsigned int libmodbus_version_micro;
155 typedef struct _modbus modbus_t;
161 int start_input_bits;
162 int nb_input_registers;
163 int start_input_registers;
167 uint8_t *tab_input_bits;
168 uint16_t *tab_input_registers;
169 uint16_t *tab_registers;
174 MODBUS_ERROR_RECOVERY_NONE = 0,
175 MODBUS_ERROR_RECOVERY_LINK = (1<<1),
176 MODBUS_ERROR_RECOVERY_PROTOCOL = (1<<2)
177 } modbus_error_recovery_mode;
179 MODBUS_API
int modbus_set_slave(modbus_t* ctx,
int slave);
180 MODBUS_API
int modbus_get_slave(modbus_t* ctx);
181 MODBUS_API
int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery);
182 MODBUS_API
int modbus_set_socket(modbus_t *ctx,
int s);
183 MODBUS_API
int modbus_get_socket(modbus_t *ctx);
185 MODBUS_API
int modbus_get_response_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
186 MODBUS_API
int modbus_set_response_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
188 MODBUS_API
int modbus_get_byte_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
189 MODBUS_API
int modbus_set_byte_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
191 MODBUS_API
int modbus_get_indication_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
192 MODBUS_API
int modbus_set_indication_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
194 MODBUS_API
int modbus_get_header_length(modbus_t *ctx);
196 MODBUS_API
int modbus_connect(modbus_t *ctx);
197 MODBUS_API
void modbus_close(modbus_t *ctx);
199 MODBUS_API
void modbus_free(modbus_t *ctx);
201 MODBUS_API
int modbus_flush(modbus_t *ctx);
202 MODBUS_API
int modbus_set_debug(modbus_t *ctx,
int flag);
204 MODBUS_API
const char *modbus_strerror(
int errnum);
206 MODBUS_API
int modbus_read_bits(modbus_t *ctx,
int addr,
int nb, uint8_t *dest);
207 MODBUS_API
int modbus_read_input_bits(modbus_t *ctx,
int addr,
int nb, uint8_t *dest);
208 MODBUS_API
int modbus_read_registers(modbus_t *ctx,
int addr,
int nb, uint16_t *dest);
209 MODBUS_API
int modbus_read_input_registers(modbus_t *ctx,
int addr,
int nb, uint16_t *dest);
210 MODBUS_API
int modbus_write_bit(modbus_t *ctx,
int coil_addr,
int status);
211 MODBUS_API
int modbus_write_register(modbus_t *ctx,
int reg_addr,
const uint16_t value);
212 MODBUS_API
int modbus_write_bits(modbus_t *ctx,
int addr,
int nb,
const uint8_t *data);
213 MODBUS_API
int modbus_write_registers(modbus_t *ctx,
int addr,
int nb,
const uint16_t *data);
214 MODBUS_API
int modbus_mask_write_register(modbus_t *ctx,
int addr, uint16_t and_mask, uint16_t or_mask);
215 MODBUS_API
int modbus_write_and_read_registers(modbus_t *ctx,
int write_addr,
int write_nb,
216 const uint16_t *src,
int read_addr,
int read_nb,
218 MODBUS_API
int modbus_report_slave_id(modbus_t *ctx,
int max_dest, uint8_t *dest);
221 unsigned int start_bits,
unsigned int nb_bits,
222 unsigned int start_input_bits,
unsigned int nb_input_bits,
223 unsigned int start_registers,
unsigned int nb_registers,
224 unsigned int start_input_registers,
unsigned int nb_input_registers);
226 MODBUS_API
modbus_mapping_t* modbus_mapping_new(
int nb_bits,
int nb_input_bits,
227 int nb_registers,
int nb_input_registers);
230 MODBUS_API
int modbus_send_raw_request(modbus_t *ctx,
const uint8_t *raw_req,
int raw_req_length);
232 MODBUS_API
int modbus_receive(modbus_t *ctx, uint8_t *req);
234 MODBUS_API
int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp);
236 MODBUS_API
int modbus_reply(modbus_t *ctx,
const uint8_t *req,
238 MODBUS_API
int modbus_reply_exception(modbus_t *ctx,
const uint8_t *req,
239 unsigned int exception_code);
245 #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF) 246 #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF) 247 #define MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \ 248 (((int64_t)tab_int16[(index) ] << 48) + \ 249 ((int64_t)tab_int16[(index) + 1] << 32) + \ 250 ((int64_t)tab_int16[(index) + 2] << 16) + \ 251 (int64_t)tab_int16[(index) + 3]) 252 #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1]) 253 #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1]) 254 #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \ 256 tab_int8[(index)] = (value) >> 8; \ 257 tab_int8[(index) + 1] = (value) & 0xFF; \ 259 #define MODBUS_SET_INT32_TO_INT16(tab_int16, index, value) \ 261 tab_int16[(index) ] = (value) >> 16; \ 262 tab_int16[(index) + 1] = (value); \ 264 #define MODBUS_SET_INT64_TO_INT16(tab_int16, index, value) \ 266 tab_int16[(index) ] = (value) >> 48; \ 267 tab_int16[(index) + 1] = (value) >> 32; \ 268 tab_int16[(index) + 2] = (value) >> 16; \ 269 tab_int16[(index) + 3] = (value); \ 272 MODBUS_API
void modbus_set_bits_from_byte(uint8_t *dest,
int idx,
const uint8_t value);
273 MODBUS_API
void modbus_set_bits_from_bytes(uint8_t *dest,
int idx,
unsigned int nb_bits,
274 const uint8_t *tab_byte);
275 MODBUS_API uint8_t modbus_get_byte_from_bits(
const uint8_t *src,
int idx,
unsigned int nb_bits);
276 MODBUS_API
float modbus_get_float(
const uint16_t *src);
277 MODBUS_API
float modbus_get_float_abcd(
const uint16_t *src);
278 MODBUS_API
float modbus_get_float_dcba(
const uint16_t *src);
279 MODBUS_API
float modbus_get_float_badc(
const uint16_t *src);
280 MODBUS_API
float modbus_get_float_cdab(
const uint16_t *src);
282 MODBUS_API
void modbus_set_float(
float f, uint16_t *dest);
283 MODBUS_API
void modbus_set_float_abcd(
float f, uint16_t *dest);
284 MODBUS_API
void modbus_set_float_dcba(
float f, uint16_t *dest);
285 MODBUS_API
void modbus_set_float_badc(
float f, uint16_t *dest);
286 MODBUS_API
void modbus_set_float_cdab(
float f, uint16_t *dest);
288 #include "modbus-tcp.h" 289 #include "modbus-rtu.h"