1 | /*
|
---|
2 | * Copyright © 2001-2008 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 |
|
---|
18 | #ifndef _MODBUS_H_
|
---|
19 | #define _MODBUS_H_
|
---|
20 |
|
---|
21 | #include <stdint.h>
|
---|
22 | #include <termios.h>
|
---|
23 | #include <arpa/inet.h>
|
---|
24 |
|
---|
25 | #ifdef __cplusplus
|
---|
26 | extern "C" {
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | #define MODBUS_TCP_DEFAULT_PORT 502
|
---|
30 |
|
---|
31 | #define HEADER_LENGTH_RTU 0
|
---|
32 | #define PRESET_QUERY_LENGTH_RTU 6
|
---|
33 | #define PRESET_RESPONSE_LENGTH_RTU 2
|
---|
34 |
|
---|
35 | #define HEADER_LENGTH_TCP 6
|
---|
36 | #define PRESET_QUERY_LENGTH_TCP 12
|
---|
37 | #define PRESET_RESPONSE_LENGTH_TCP 8
|
---|
38 |
|
---|
39 | #define CHECKSUM_LENGTH_RTU 2
|
---|
40 | #define CHECKSUM_LENGTH_TCP 0
|
---|
41 |
|
---|
42 | /* It's not really the minimal length (the real one is report slave ID
|
---|
43 | * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP
|
---|
44 | * communications to read many values or write a single one.
|
---|
45 | * Maximum between :
|
---|
46 | * - HEADER_LENGTH_TCP (6) + slave (1) + function (1) + address (2) +
|
---|
47 | * number (2)
|
---|
48 | * - HEADER_LENGTH_RTU (0) + slave (1) + function (1) + address (2) +
|
---|
49 | * number (2) + CRC (2)
|
---|
50 | */
|
---|
51 | #define MIN_QUERY_LENGTH 12
|
---|
52 |
|
---|
53 | /* Page 102, Application Notes of PIâMBUSâ300:
|
---|
54 | * The maximum length of the entire message must not exceed 256
|
---|
55 | * bytes.
|
---|
56 | */
|
---|
57 | #define MAX_MESSAGE_LENGTH 256
|
---|
58 |
|
---|
59 | #define MAX_STATUS 800
|
---|
60 | #define MAX_REGISTERS 100
|
---|
61 |
|
---|
62 | #define REPORT_SLAVE_ID_LENGTH 75
|
---|
63 |
|
---|
64 | /* Time out between trames in microsecond */
|
---|
65 | #define TIME_OUT_BEGIN_OF_TRAME 500000
|
---|
66 | #define TIME_OUT_END_OF_TRAME 500000
|
---|
67 |
|
---|
68 | #ifndef FALSE
|
---|
69 | #define FALSE 0
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #ifndef TRUE
|
---|
73 | #define TRUE 1
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | #ifndef OFF
|
---|
77 | #define OFF 0
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | #ifndef ON
|
---|
81 | #define ON 1
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | /* Function codes */
|
---|
85 | #define FC_READ_COIL_STATUS 0x01 /* discretes inputs */
|
---|
86 | #define FC_READ_INPUT_STATUS 0x02 /* discretes outputs */
|
---|
87 | #define FC_READ_HOLDING_REGISTERS 0x03
|
---|
88 | #define FC_READ_INPUT_REGISTERS 0x04
|
---|
89 | #define FC_FORCE_SINGLE_COIL 0x05
|
---|
90 | #define FC_PRESET_SINGLE_REGISTER 0x06
|
---|
91 | #define FC_READ_EXCEPTION_STATUS 0x07
|
---|
92 | #define FC_FORCE_MULTIPLE_COILS 0x0F
|
---|
93 | #define FC_PRESET_MULTIPLE_REGISTERS 0x10
|
---|
94 | #define FC_REPORT_SLAVE_ID 0x11
|
---|
95 |
|
---|
96 | /* Protocol exceptions */
|
---|
97 | #define ILLEGAL_FUNCTION -0x01
|
---|
98 | #define ILLEGAL_DATA_ADDRESS -0x02
|
---|
99 | #define ILLEGAL_DATA_VALUE -0x03
|
---|
100 | #define SLAVE_DEVICE_FAILURE -0x04
|
---|
101 | #define SERVER_FAILURE -0x04
|
---|
102 | #define ACKNOWLEDGE -0x05
|
---|
103 | #define SLAVE_DEVICE_BUSY -0x06
|
---|
104 | #define SERVER_BUSY -0x06
|
---|
105 | #define NEGATIVE_ACKNOWLEDGE -0x07
|
---|
106 | #define MEMORY_PARITY_ERROR -0x08
|
---|
107 | #define GATEWAY_PROBLEM_PATH -0x0A
|
---|
108 | #define GATEWAY_PROBLEM_TARGET -0x0B
|
---|
109 |
|
---|
110 | /* Local */
|
---|
111 | #define COMM_TIME_OUT -0x0C
|
---|
112 | #define PORT_SOCKET_FAILURE -0x0D
|
---|
113 | #define SELECT_FAILURE -0x0E
|
---|
114 | #define TOO_MANY_DATA -0x0F
|
---|
115 | #define INVALID_CRC -0x10
|
---|
116 | #define INVALID_EXCEPTION_CODE -0x11
|
---|
117 | #define CONNECTION_CLOSED -0x12
|
---|
118 |
|
---|
119 | /* Internal using */
|
---|
120 | #define MSG_LENGTH_UNDEFINED -1
|
---|
121 |
|
---|
122 | typedef enum { RTU, TCP } type_com_t;
|
---|
123 | typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
|
---|
124 |
|
---|
125 | /* This structure is byte-aligned */
|
---|
126 | typedef struct {
|
---|
127 | /* Descriptor (tty or socket) */
|
---|
128 | int fd;
|
---|
129 | /* Communication mode: RTU or TCP */
|
---|
130 | type_com_t type_com;
|
---|
131 | /* Flag debug */
|
---|
132 | int debug;
|
---|
133 | /* Header length used for offset */
|
---|
134 | int header_length;
|
---|
135 | /* Checksum length RTU = 2 and TCP = 0 */
|
---|
136 | int checksum_length;
|
---|
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 | // Whether to save raw i/o data or not
|
---|
165 | int response_length;
|
---|
166 | // Raw input/output data
|
---|
167 | int saveRawData;
|
---|
168 | int rawQueryLength;
|
---|
169 | int rawResponseLength;
|
---|
170 | uint8_t rawQuery[MIN_QUERY_LENGTH+4];
|
---|
171 | uint8_t rawResponse[MAX_MESSAGE_LENGTH];
|
---|
172 | } modbus_param_t;
|
---|
173 |
|
---|
174 | typedef struct {
|
---|
175 | int nb_coil_status;
|
---|
176 | int nb_input_status;
|
---|
177 | int nb_input_registers;
|
---|
178 | int nb_holding_registers;
|
---|
179 | uint8_t *tab_coil_status;
|
---|
180 | uint8_t *tab_input_status;
|
---|
181 | uint16_t *tab_input_registers;
|
---|
182 | uint16_t *tab_holding_registers;
|
---|
183 | } modbus_mapping_t;
|
---|
184 |
|
---|
185 |
|
---|
186 | /* All functions used for sending or receiving data return:
|
---|
187 | - the numbers of values (bits or word) if success (0 or more)
|
---|
188 | - less than 0 for exceptions errors
|
---|
189 | */
|
---|
190 |
|
---|
191 | /* Reads the boolean status of coils and sets the array elements in
|
---|
192 | the destination to TRUE or FALSE */
|
---|
193 | int read_coil_status(modbus_param_t *mb_param, int slave,
|
---|
194 | int start_addr, int nb, uint8_t *dest);
|
---|
195 |
|
---|
196 | /* Same as read_coil_status but reads the slaves input table */
|
---|
197 | int read_input_status(modbus_param_t *mb_param, int slave,
|
---|
198 | int start_addr, int nb, uint8_t *dest);
|
---|
199 |
|
---|
200 | /* Reads the holding registers in a slave and put the data into an
|
---|
201 | array */
|
---|
202 | int read_holding_registers(modbus_param_t *mb_param, int slave,
|
---|
203 | int start_addr, int nb, uint16_t *dest);
|
---|
204 |
|
---|
205 | /* Reads the input registers in a slave and put the data into an
|
---|
206 | array */
|
---|
207 | int read_input_registers(modbus_param_t *mb_param, int slave,
|
---|
208 | int start_addr, int nb, uint16_t *dest);
|
---|
209 |
|
---|
210 | /* Turns ON or OFF a single coil in the slave device */
|
---|
211 | int force_single_coil(modbus_param_t *mb_param, int slave,
|
---|
212 | int coil_addr, int state);
|
---|
213 |
|
---|
214 | /* Sets a value in one holding register in the slave device */
|
---|
215 | int preset_single_register(modbus_param_t *mb_param, int slave,
|
---|
216 | int reg_addr, int value);
|
---|
217 |
|
---|
218 | /* Sets/resets the coils in the slave from an array in argument */
|
---|
219 | int force_multiple_coils(modbus_param_t *mb_param, int slave,
|
---|
220 | int start_addr, int nb, const uint8_t *data);
|
---|
221 |
|
---|
222 | /* Copies the values in the slave from the array given in argument */
|
---|
223 | int preset_multiple_registers(modbus_param_t *mb_param, int slave,
|
---|
224 | int start_addr, int nb, const uint16_t *data);
|
---|
225 |
|
---|
226 | /* Returns the slave id! */
|
---|
227 | int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest);
|
---|
228 |
|
---|
229 | /* Initializes the modbus_param_t structure for RTU.
|
---|
230 | - device: "/dev/ttyS0"
|
---|
231 | - baud: 9600, 19200, 57600, 115200, etc
|
---|
232 | - parity: "even", "odd" or "none"
|
---|
233 | - data_bits: 5, 6, 7, 8
|
---|
234 | - stop_bits: 1, 2
|
---|
235 | */
|
---|
236 | void modbus_init_rtu(modbus_param_t *mb_param, const char *device,
|
---|
237 | int baud, const char *parity, int data_bit,
|
---|
238 | int stop_bit);
|
---|
239 |
|
---|
240 | /* Initializes the modbus_param_t structure for TCP.
|
---|
241 | - ip : "192.168.0.5"
|
---|
242 | - port : 1099
|
---|
243 |
|
---|
244 | Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
|
---|
245 | (502). It's convenient to use a port number greater than or equal
|
---|
246 | to 1024 because it's not necessary to be root to use this port
|
---|
247 | number.
|
---|
248 | */
|
---|
249 | void modbus_init_tcp(modbus_param_t *mb_param, const char *ip_address, int port);
|
---|
250 |
|
---|
251 | /* By default, the error handling mode used is RECONNECT_ON_ERROR.
|
---|
252 |
|
---|
253 | With RECONNECT_ON_ERROR, the library will attempt an immediate
|
---|
254 | reconnection which may hang for several seconds if the network to
|
---|
255 | the remote target unit is down.
|
---|
256 |
|
---|
257 | With NOP_ON_ERROR, it is expected that the application will
|
---|
258 | check for network error returns and deal with them as necessary.
|
---|
259 |
|
---|
260 | This function is only useful in TCP mode.
|
---|
261 | */
|
---|
262 | void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling);
|
---|
263 |
|
---|
264 | /* Establishes a modbus connexion.
|
---|
265 | Returns -1 if an error occured. */
|
---|
266 | int modbus_connect(modbus_param_t *mb_param);
|
---|
267 |
|
---|
268 | /* Closes a modbus connection */
|
---|
269 | void modbus_close(modbus_param_t *mb_param);
|
---|
270 |
|
---|
271 | /* Activates the debug messages */
|
---|
272 | void modbus_set_debug(modbus_param_t *mb_param, int boolean);
|
---|
273 |
|
---|
274 | // Activates saving raw query and responses
|
---|
275 | void modbus_set_raw_data_save(modbus_param_t *mb_param, int boolean);
|
---|
276 |
|
---|
277 | // Returns a raw query
|
---|
278 | int modbus_last_raw_query(modbus_param_t *mb_param, uint8_t *rawQuery);
|
---|
279 |
|
---|
280 | // Returns a raw response
|
---|
281 | int modbus_last_raw_response(modbus_param_t *mb_param, uint8_t *rawResponse);
|
---|
282 |
|
---|
283 | int modbus_last_raw_query_length(modbus_param_t *mb_param);
|
---|
284 |
|
---|
285 | int modbus_last_raw_response_length(modbus_param_t *mb_param);
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * SLAVE/CLIENT FUNCTIONS
|
---|
289 | **/
|
---|
290 |
|
---|
291 | /* Allocates 4 arrays to store coils, input status, input registers and
|
---|
292 | holding registers. The pointers are stored in modbus_mapping structure.
|
---|
293 |
|
---|
294 | Returns: TRUE if ok, FALSE on failure
|
---|
295 | */
|
---|
296 | int modbus_mapping_new(modbus_mapping_t *mb_mapping,
|
---|
297 | int nb_coil_status, int nb_input_status,
|
---|
298 | int nb_holding_registers, int nb_input_registers);
|
---|
299 |
|
---|
300 | /* Frees the 4 arrays */
|
---|
301 | void modbus_mapping_free(modbus_mapping_t *mb_mapping);
|
---|
302 |
|
---|
303 | /* Initializes the modbus_param_t structure for a TCP slave (server) */
|
---|
304 | int modbus_init_listen_tcp(modbus_param_t *mb_param);
|
---|
305 |
|
---|
306 | /* Listens for any query from a modbus master in TCP
|
---|
307 | Not tested in RTU communication. */
|
---|
308 | int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length);
|
---|
309 |
|
---|
310 | /* Manages the received query.
|
---|
311 | Analyses the query and constructs a response.
|
---|
312 |
|
---|
313 | If an error occurs, this function construct the response
|
---|
314 | accordingly.
|
---|
315 | */
|
---|
316 | void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
|
---|
317 | int query_length, modbus_mapping_t *mb_mapping);
|
---|
318 |
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * UTILS FUNCTIONS
|
---|
322 | **/
|
---|
323 |
|
---|
324 | /* Sets many input/coil status from a single byte value (all 8 bits of
|
---|
325 | the byte value are setted) */
|
---|
326 | void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
|
---|
327 |
|
---|
328 | /* Sets many input/coil status from a table of bytes (only the bits
|
---|
329 | between address and address + nb_bits are setted) */
|
---|
330 | void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits,
|
---|
331 | const uint8_t *tab_byte);
|
---|
332 |
|
---|
333 | /* Gets the byte value from many input/coil status.
|
---|
334 | To obtain a full byte, set nb_bits to 8. */
|
---|
335 | uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits);
|
---|
336 |
|
---|
337 | #ifdef __cplusplus
|
---|
338 | }
|
---|
339 | #endif
|
---|
340 |
|
---|
341 | #endif /* _MODBUS_H_ */
|
---|