modbus.h
9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
Copyright (C) 2001-2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#ifndef _MODBUS_H_
#define _MODBUS_H_
#include <stdint.h>
#include <termios.h>
#include <arpa/inet.h>
#define MODBUS_TCP_DEFAULT_PORT 502
#define HEADER_LENGTH_RTU 0
#define PRESET_QUERY_LENGTH_RTU 6
#define PRESET_RESPONSE_LENGTH_RTU 2
#define HEADER_LENGTH_TCP 6
#define PRESET_QUERY_LENGTH_TCP 12
#define PRESET_RESPONSE_LENGTH_TCP 8
#define CHECKSUM_LENGTH_RTU 2
#define CHECKSUM_LENGTH_TCP 0
/* 8 + HEADER_LENGTH_TCP */
#define MIN_QUERY_LENGTH 14
#define MAX_MESSAGE_LENGTH 256
#define MAX_READ_STATUS 800
#define MAX_READ_HOLD_REGS 100
#define MAX_READ_INPUT_REGS 100
#define MAX_WRITE_COILS 800
#define MAX_WRITE_REGS 100
#define REPORT_SLAVE_ID_LENGTH 75
/* Time out between trames in microsecond */
#define TIME_OUT_BEGIN_OF_TRAME 500000
#define TIME_OUT_END_OF_TRAME 500000
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef OFF
#define OFF 0
#endif
#ifndef ON
#define ON 1
#endif
/* Function codes */
#define FC_READ_COIL_STATUS 0x01 /* discretes inputs */
#define FC_READ_INPUT_STATUS 0x02 /* discretes outputs */
#define FC_READ_HOLDING_REGISTERS 0x03
#define FC_READ_INPUT_REGISTERS 0x04
#define FC_FORCE_SINGLE_COIL 0x05
#define FC_PRESET_SINGLE_REGISTER 0x06
#define FC_READ_EXCEPTION_STATUS 0x07
#define FC_FORCE_MULTIPLE_COILS 0x0F
#define FC_PRESET_MULTIPLE_REGISTERS 0x10
#define FC_REPORT_SLAVE_ID 0x11
/* Protocol exceptions */
#define ILLEGAL_FUNCTION -0x01
#define ILLEGAL_DATA_ADDRESS -0x02
#define ILLEGAL_DATA_VALUE -0x03
#define SLAVE_DEVICE_FAILURE -0x04
#define SERVER_FAILURE -0x04
#define ACKNOWLEDGE -0x05
#define SLAVE_DEVICE_BUSY -0x06
#define SERVER_BUSY -0x06
#define NEGATIVE_ACKNOWLEDGE -0x07
#define MEMORY_PARITY_ERROR -0x08
#define GATEWAY_PROBLEM_PATH -0x0A
#define GATEWAY_PROBLEM_TARGET -0x0B
/* Local */
#define COMM_TIME_OUT -0x0C
#define PORT_SOCKET_FAILURE -0x0D
#define SELECT_FAILURE -0x0E
#define TOO_MANY_DATAS -0x0F
#define INVALID_CRC -0x10
#define INVALID_EXCEPTION_CODE -0x11
#define CONNECTION_CLOSED -0x12
/* Internal using */
#define MSG_LENGTH_UNDEFINED -1
typedef enum { RTU, TCP } type_com_t;
typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
/* This structure is byte-aligned */
typedef struct {
/* Communication : RTU or TCP */
type_com_t type_com;
/* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*"
on Mac OS X for KeySpan USB<->Serial adapters this string
had to be made bigger on OS X as the directory+file name
was bigger than 19 bytes. Making it 67 bytes for now, but
OS X does support 256 byte file names. May become a problem
in the future. */
#ifdef __APPLE_CC__
char device[67];
#else
char device[19];
#endif
/* Parity: "even", "odd", "none" */
char parity[5];
/* Bauds: 9600, 19200, 57600, 115200, etc */
int baud;
/* Data bit */
int data_bit;
/* Stop bit */
int stop_bit;
/* Save old termios settings */
struct termios old_tios;
/* Descriptor (tty or socket) */
int fd;
/* Flag debug */
int debug;
/* IP address */
char ip[16];
/* TCP port */
uint16_t port;
/* Header length used for offset */
int header_length;
/* Checksum length RTU = 2 and TCP = 0 */
int checksum_length;
/* In error_treat with TCP, do a reconnect or just dump the error */
error_handling_t error_handling;
} modbus_param_t;
typedef struct {
int nb_coil_status;
int nb_input_status;
int nb_input_registers;
int nb_holding_registers;
uint8_t *tab_coil_status;
uint8_t *tab_input_status;
uint16_t *tab_input_registers;
uint16_t *tab_holding_registers;
} modbus_mapping_t;
/* All functions used for sending or receiving data return:
- the numbers of values (bits or word) if success (0 or more)
- less than 0 for exceptions errors
*/
/* Reads the boolean status of coils and sets the array elements in
the destination to TRUE or FALSE */
int read_coil_status(modbus_param_t *mb_param, int slave,
int start_addr, int count, uint8_t *dest);
/* Same as read_coil_status but reads the slaves input table */
int read_input_status(modbus_param_t *mb_param, int slave,
int start_addr, int count, uint8_t *dest);
/* Reads the holding registers in a slave and put the data into an
array */
int read_holding_registers(modbus_param_t *mb_param, int slave,
int start_addr, int count, uint16_t *dest);
/* Reads the input registers in a slave and put the data into an
array */
int read_input_registers(modbus_param_t *mb_param, int slave,
int start_addr, int count, uint16_t *dest);
/* Turns ON or OFF a single coil in the slave device */
int force_single_coil(modbus_param_t *mb_param, int slave,
int coil_addr, int state);
/* Sets a value in one holding register in the slave device */
int preset_single_register(modbus_param_t *mb_param, int slave,
int reg_addr, int value);
/* Sets/resets the coils in the slave from an array in argument */
int force_multiple_coils(modbus_param_t *mb_param, int slave,
int start_addr, int nb_points, uint8_t *data);
/* Copies the values in the slave from the array given in argument */
int preset_multiple_registers(modbus_param_t *mb_param, int slave,
int start_addr, int nb_points, uint16_t *data);
/* Returns the slave id! */
int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest);
/* Initializes the modbus_param_t structure for RTU.
- device: "/dev/ttyS0"
- baud: 9600, 19200, 57600, 115200, etc
- parity: "even", "odd" or "none"
- data_bits: 5, 6, 7, 8
- stop_bits: 1, 2
*/
void modbus_init_rtu(modbus_param_t *mb_param, char *device,
int baud, char *parity, int data_bit,
int stop_bit);
/* Initializes the modbus_param_t structure for TCP.
- ip : "192.168.0.5"
- port : 1099
Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
(502). It's convenient to use a port number greater than or equal
to 1024 because it's not necessary to be root to use this port
number.
*/
void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, int port);
/* By default, the error handling mode used is RECONNECT_ON_ERROR.
With RECONNECT_ON_ERROR, the library will attempt an immediate
reconnection which may hang for several seconds if the network to
the remote target unit is down.
With NOP_ON_ERROR, it is expected that the application will
check for network error returns and deal with them as necessary.
This function is only useful in TCP mode.
*/
void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling);
/* Establishes a modbus connexion */
int modbus_connect(modbus_param_t *mb_param);
/* Closes a modbus connection */
void modbus_close(modbus_param_t *mb_param);
/* Activates the debug messages */
void modbus_set_debug(modbus_param_t *mb_param, int boolean);
/**
* SLAVE/CLIENT FUNCTIONS
**/
/* Allocates 4 arrays to store coils, input status, input registers and
holding registers. The pointers are stored in modbus_mapping structure.
Returns: TRUE if ok, FALSE on failure
*/
int modbus_mapping_new(modbus_mapping_t *mb_mapping,
int nb_coil_status, int nb_input_status,
int nb_holding_registers, int nb_input_registers);
/* Frees the 4 arrays */
void modbus_mapping_free(modbus_mapping_t *mb_mapping);
/* Listens for any query from a modbus master in TCP */
int modbus_init_listen_tcp(modbus_param_t *mb_param);
/* FIXME */
int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length);
/* Manages the received query.
Analyses the query and constructs a response.
If an error occurs, this function construct the response
accordingly.
*/
void manage_query(modbus_param_t *mb_param, uint8_t *query,
int query_length, modbus_mapping_t *mb_mapping);
/* Not implemented :
- read_exception_status()
*/
/**
* UTILS FUNCTIONS
**/
/* Sets many inputs/coils from a single byte value (all 8 bits of the
byte value are setted) */
void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
/* Sets many inputs/coils from a table of bytes (only the bits between
address and address + nb_bits are setted) */
void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits,
const uint8_t *tab_byte);
/* Gets the byte value from many inputs/coils.
To obtain a full byte, set nb_bits to 8. */
uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits);
#endif /* _MODBUS_H_ */