modbus.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2001-2011 Stéphane Raimbault <stephane.raimbault@gmail.com>
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #ifndef _MODBUS_H_
20 #define _MODBUS_H_
21 
22 /* Add this for macros that defined unix flavor */
23 #if (defined(__unix__) || defined(unix)) && !defined(USG)
24 #include <sys/param.h>
25 #endif
26 
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
34 
36 
37 #ifdef __cplusplus
38 # define MODBUS_BEGIN_DECLS extern "C" {
39 # define MODBUS_END_DECLS }
40 #else
41 # define MODBUS_BEGIN_DECLS
42 # define MODBUS_END_DECLS
43 #endif
44 
46 
47 #ifndef FALSE
48 #define FALSE 0
49 #endif
50 
51 #ifndef TRUE
52 #define TRUE 1
53 #endif
54 
55 #ifndef OFF
56 #define OFF 0
57 #endif
58 
59 #ifndef ON
60 #define ON 1
61 #endif
62 
63 #define MODBUS_BROADCAST_ADDRESS 0
64 
65 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
66  * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
67  * (chapter 6 section 11 page 29)
68  * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
69  */
70 #define MODBUS_MAX_READ_BITS 2000
71 #define MODBUS_MAX_WRITE_BITS 1968
72 
73 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
74  * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
75  * (chapter 6 section 12 page 31)
76  * Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
77  * (chapter 6 section 17 page 38)
78  * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
79  */
80 #define MODBUS_MAX_READ_REGISTERS 125
81 #define MODBUS_MAX_WRITE_REGISTERS 123
82 #define MODBUS_MAX_RW_WRITE_REGISTERS 121
83 
84 /* Random number to avoid errno conflicts */
85 #define MODBUS_ENOBASE 112345678
86 
87 /* Protocol exceptions */
88 enum {
101 };
102 
103 #define EMBXILFUN (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION)
104 #define EMBXILADD (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS)
105 #define EMBXILVAL (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE)
106 #define EMBXSFAIL (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE)
107 #define EMBXACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_ACKNOWLEDGE)
108 #define EMBXSBUSY (MODBUS_ENOBASE + MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY)
109 #define EMBXNACK (MODBUS_ENOBASE + MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE)
110 #define EMBXMEMPAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_MEMORY_PARITY)
111 #define EMBXGPATH (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_PATH)
112 #define EMBXGTAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET)
113 
114 /* Native libmodbus error codes */
115 #define EMBBADCRC (EMBXGTAR + 1)
116 #define EMBBADDATA (EMBXGTAR + 2)
117 #define EMBBADEXC (EMBXGTAR + 3)
118 #define EMBUNKEXC (EMBXGTAR + 4)
119 #define EMBMDATA (EMBXGTAR + 5)
120 
121 extern const unsigned int libmodbus_version_major;
122 extern const unsigned int libmodbus_version_minor;
123 extern const unsigned int libmodbus_version_micro;
124 
125 typedef struct _modbus modbus_t;
126 
127 typedef struct {
128  int nb_bits;
132  uint8_t *tab_bits;
133  uint8_t *tab_input_bits;
135  uint16_t *tab_registers;
137 
138 typedef enum
139 {
144 
147 void modbus_set_socket(modbus_t *ctx, int socket);
149 
150 void modbus_get_response_timeout(modbus_t *ctx, struct timeval *timeout);
151 void modbus_set_response_timeout(modbus_t *ctx, const struct timeval *timeout);
152 
153 void modbus_get_byte_timeout(modbus_t *ctx, struct timeval *timeout);
154 void modbus_set_byte_timeout(modbus_t *ctx, const struct timeval *timeout);
155 
157 
159 void modbus_close(modbus_t *ctx);
160 
161 void modbus_free(modbus_t *ctx);
162 
164 void modbus_set_debug(modbus_t *ctx, int boolean);
165 
166 const char *modbus_strerror(int errnum);
167 
168 int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
169 int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
170 int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
171 int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
172 int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
173 int modbus_write_register(modbus_t *ctx, int reg_addr, int value);
174 int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
175 int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);
176 int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int write_nb,
177  const uint16_t *src, int read_addr, int read_nb,
178  uint16_t *dest);
179 int modbus_report_slave_id(modbus_t *ctx, uint8_t *dest);
180 
181 modbus_mapping_t* modbus_mapping_new(int nb_coil_status, int nb_input_status,
182  int nb_holding_registers, int nb_input_registers);
184 
185 int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length);
186 
187 int modbus_receive(modbus_t *ctx, uint8_t *req);
188 int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req);
189 
190 int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp);
191 
192 int modbus_reply(modbus_t *ctx, const uint8_t *req,
193  int req_length, modbus_mapping_t *mb_mapping);
194 int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,
195  unsigned int exception_code);
196 
201 #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
202 #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF)
203 #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1])
204 #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1])
205 #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
206  do { \
207  tab_int8[(index)] = (value) >> 8; \
208  tab_int8[(index) + 1] = (value) & 0xFF; \
209  } while (0)
210 
211 void modbus_set_bits_from_byte(uint8_t *dest, int index, const uint8_t value);
212 void modbus_set_bits_from_bytes(uint8_t *dest, int index, unsigned int nb_bits,
213  const uint8_t *tab_byte);
214 uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index, unsigned int nb_bits);
215 float modbus_get_float(const uint16_t *src);
216 void modbus_set_float(float f, uint16_t *dest);
217 
218 #include "modbus-tcp.h"
219 #include "modbus-rtu.h"
220 
222 
223 #endif /* _MODBUS_H_ */
void modbus_set_bits_from_byte(uint8_t *dest, int index, const uint8_t value)
Definition: modbus-data.c:30
const unsigned int libmodbus_version_major
Definition: modbus.c:39
int nb_registers
Definition: modbus.h:131
int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req)
modbus_mapping_t * modbus_mapping_new(int nb_coil_status, int nb_input_status, int nb_holding_registers, int nb_input_registers)
Definition: modbus.c:1547
float modbus_get_float(const uint16_t *src)
Definition: modbus-data.c:77
int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest)
Definition: modbus.c:1059
void modbus_set_byte_timeout(modbus_t *ctx, const struct timeval *timeout)
Definition: modbus.c:1505
int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, unsigned int exception_code)
Definition: modbus.c:982
int nb_input_bits
Definition: modbus.h:129
int modbus_receive(modbus_t *ctx, uint8_t *req)
Definition: modbus.c:460
uint8_t * tab_input_bits
Definition: modbus.h:133
void modbus_free(modbus_t *ctx)
Definition: modbus.c:1528
int modbus_flush(modbus_t *ctx)
Definition: modbus.c:118
int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery)
Definition: modbus.c:1470
const unsigned int libmodbus_version_micro
Definition: modbus.c:41
int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest)
Definition: modbus.c:1083
#define MODBUS_BEGIN_DECLS
Definition: modbus.h:41
void modbus_set_debug(modbus_t *ctx, int boolean)
Definition: modbus.c:1537
void modbus_close(modbus_t *ctx)
Definition: modbus.c:1520
int modbus_set_slave(modbus_t *ctx, int slave)
Definition: modbus.c:1465
int modbus_reply(modbus_t *ctx, const uint8_t *req, int req_length, modbus_mapping_t *mb_mapping)
Definition: modbus.c:641
int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest)
Definition: modbus.c:1173
int error_recovery
void modbus_get_byte_timeout(modbus_t *ctx, struct timeval *timeout)
Definition: modbus.c:1500
void modbus_get_response_timeout(modbus_t *ctx, struct timeval *timeout)
Definition: modbus.c:1489
int modbus_write_register(modbus_t *ctx, int reg_addr, int value)
Definition: modbus.c:1225
uint16_t * tab_registers
Definition: modbus.h:135
modbus_t * ctx
const char * modbus_strerror(int errnum)
Definition: modbus.c:53
void modbus_set_bits_from_bytes(uint8_t *dest, int index, unsigned int nb_bits, const uint8_t *tab_byte)
Definition: modbus-data.c:41
int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data)
Definition: modbus.c:1290
uint16_t * tab_input_registers
Definition: modbus.h:134
void modbus_set_float(float f, uint16_t *dest)
Definition: modbus-data.c:89
modbus_error_recovery_mode
Definition: modbus.h:138
int modbus_get_header_length(modbus_t *ctx)
Definition: modbus.c:1510
void modbus_mapping_free(modbus_mapping_t *mb_mapping)
Definition: modbus.c:1625
int nb_input_registers
Definition: modbus.h:130
int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data)
Definition: modbus.c:1231
int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp)
Definition: modbus.c:473
const unsigned int libmodbus_version_minor
Definition: modbus.c:40
int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest)
Definition: modbus.c:1153
uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index, unsigned int nb_bits)
Definition: modbus-data.c:57
int modbus_connect(modbus_t *ctx)
Definition: modbus.c:1515
modbus_mapping_t * mb_mapping
void modbus_set_socket(modbus_t *ctx, int socket)
Definition: modbus.c:1478
uint8_t * tab_bits
Definition: modbus.h:132
int modbus_report_slave_id(modbus_t *ctx, uint8_t *dest)
Definition: modbus.c:1410
void modbus_set_response_timeout(modbus_t *ctx, const struct timeval *timeout)
Definition: modbus.c:1494
int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length)
Definition: modbus.c:204
#define MODBUS_END_DECLS
Definition: modbus.h:42
int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int write_nb, const uint16_t *src, int read_addr, int read_nb, uint16_t *dest)
Definition: modbus.c:1336
int modbus_get_socket(modbus_t *ctx)
Definition: modbus.c:1483
int modbus_write_bit(modbus_t *ctx, int coil_addr, int status)
Definition: modbus.c:1218


libmodbus
Author(s):
autogenerated on Sat Nov 21 2020 03:17:32