@@ -12,39 +12,44 @@ Arduino library for communicating with Modbus server over Ethernet in TCP.
12
12
\mainpage Modbus over TCP/IP
13
13
ModbusTCP.h - Arduino library for communicating with Modbus server
14
14
over Ethernet (via TCP protocol).
15
-
15
+
16
16
This file is part of ModbusTCP.
17
17
18
18
@see **README** for details.
19
-
19
+
20
20
ModbusTCP is free software: you can redistribute it and/or modify
21
21
it under the terms of the GNU General Public License as published by
22
22
the Free Software Foundation, either version 3 of the License, or
23
23
(at your option) any later version.
24
-
24
+
25
25
ModbusTCP is distributed in the hope that it will be useful,
26
26
but WITHOUT ANY WARRANTY; without even the implied warranty of
27
27
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28
28
GNU General Public License for more details.
29
-
29
+
30
30
You should have received a copy of the GNU General Public License
31
31
along with ModbusTCP library. If not, see <http://www.gnu.org/licenses/>.
32
-
32
+
33
33
Adopted from ModbusMaster for RTU over RS-485 by Doc Walker
34
-
34
+
35
35
Modified by Narendra Dehury for TCP.
36
-
36
+
37
+ Modified by Florian K for ESP8266.
38
+
37
39
Copyright @ phoenixrobotix.com
38
-
40
+
39
41
*/
40
42
41
-
43
+
42
44
#ifndef Modbus_TCPIP_h
43
45
#define Modbus_TCPIP_h
44
46
47
+ #ifndef WIZNET_W5100
45
48
#define WIZNET_W5100 0 /* *< define 1 if WIZNET W5100 IC is used, otherwise 0 */
46
- #define ENC28J60 1 /* *< define 1 if ENC28J60 IC is used, otherwisw 0 */
47
-
49
+ #ifndef ENC28J60
50
+ #define ENC28J60 0 /* *< define 1 if ENC28J60 IC is used, otherwise 0 */
51
+ #ifndef ESP8266
52
+ #define ESP8266 1
48
53
49
54
50
55
/* _____STANDARD INCLUDES____________________________________________________ */
@@ -65,6 +70,10 @@ Arduino library for communicating with Modbus server over Ethernet in TCP.
65
70
#include < UIPEthernet.h>
66
71
#endif
67
72
73
+ #if ESP8266
74
+ #include < WiFiClient.h>
75
+ #endif
76
+
68
77
69
78
/* _____PROJECT INCLUDES_____________________________________________________ */
70
79
@@ -79,13 +88,15 @@ Arduino class library for communicating with Modbus server over TCP/IP.
79
88
class ModbusTCP
80
89
{
81
90
public:
82
-
91
+
83
92
IPAddress serverIP;
84
93
85
- #if WIZNET_W5100
94
+ #if WIZNET_W5100
86
95
EthernetClient ModbusClient;
87
96
#elif ENC28J60
88
97
UIPClient ModbusClient;
98
+ #elif ESP8266
99
+ WiFiClient ModbusClient;
89
100
#endif
90
101
91
102
char MBconnectionFlag = 0 ;
@@ -96,11 +107,11 @@ class ModbusTCP
96
107
void setTransactionID (uint16_t );
97
108
void setServerIPAddress (IPAddress);
98
109
void idle (void (*)());
99
-
110
+
100
111
// Modbus exception codes
101
112
/* *
102
113
Modbus protocol illegal function exception.
103
-
114
+
104
115
The function code received in the query is not an allowable action for
105
116
the server. This may be because the function code is only
106
117
applicable to newer devices, and was not implemented in the unit
@@ -114,46 +125,46 @@ class ModbusTCP
114
125
115
126
/* *
116
127
Modbus protocol illegal data address exception.
117
-
118
- The data address received in the query is not an allowable address for
119
- the server. More specifically, the combination of reference
120
- number and transfer length is invalid. For a controller with 100
121
- registers, the ADU addresses the first register as 0, and the last one
122
- as 99. If a request is submitted with a starting register address of 96
123
- and a quantity of registers of 4, then this request will successfully
124
- operate (address-wise at least) on registers 96, 97, 98, 99. If a
125
- request is submitted with a starting register address of 96 and a
126
- quantity of registers of 5, then this request will fail with Exception
127
- Code 0x02 "Illegal Data Address" since it attempts to operate on
128
- registers 96, 97, 98, 99 and 100, and there is no register with address
129
- 100.
128
+
129
+ The data address received in the query is not an allowable address for
130
+ the server. More specifically, the combination of reference
131
+ number and transfer length is invalid. For a controller with 100
132
+ registers, the ADU addresses the first register as 0, and the last one
133
+ as 99. If a request is submitted with a starting register address of 96
134
+ and a quantity of registers of 4, then this request will successfully
135
+ operate (address-wise at least) on registers 96, 97, 98, 99. If a
136
+ request is submitted with a starting register address of 96 and a
137
+ quantity of registers of 5, then this request will fail with Exception
138
+ Code 0x02 "Illegal Data Address" since it attempts to operate on
139
+ registers 96, 97, 98, 99 and 100, and there is no register with address
140
+ 100.
130
141
131
142
@ingroup constant
132
143
*/
133
144
static const uint8_t MBIllegalDataAddress = 0x02 ;
134
-
145
+
135
146
/* *
136
147
Modbus protocol illegal data value exception.
137
-
138
- A value contained in the query data field is not an allowable value for
139
- server. This indicates a fault in the structure of the
140
- remainder of a complex request, such as that the implied length is
141
- incorrect. It specifically does NOT mean that a data item submitted for
142
- storage in a register has a value outside the expectation of the
143
- application program, since the MODBUS protocol is unaware of the
148
+
149
+ A value contained in the query data field is not an allowable value for
150
+ server. This indicates a fault in the structure of the
151
+ remainder of a complex request, such as that the implied length is
152
+ incorrect. It specifically does NOT mean that a data item submitted for
153
+ storage in a register has a value outside the expectation of the
154
+ application program, since the MODBUS protocol is unaware of the
144
155
significance of any particular value of any particular register.
145
-
156
+
146
157
@ingroup constant
147
158
*/
148
159
static const uint8_t MBIllegalDataValue = 0x03 ;
149
160
// static const uint8_t MBIllegalResponseLength = 0x04;
150
-
161
+
151
162
/* *
152
163
Modbus TCP server connection failure exception.
153
-
164
+
154
165
An unrecoverable error occurred while the client is attempting to connect to
155
166
the server but fails, Usually a case with ENC82J60.
156
-
167
+
157
168
@ingroup constant
158
169
*/
159
170
@@ -162,62 +173,62 @@ class ModbusTCP
162
173
// Class-defined success/exception codes
163
174
/* *
164
175
ModbusTCP success.
165
-
176
+
166
177
Modbus transaction was successful; the following checks were valid:
167
178
- slave ID
168
179
- function code
169
180
- response code
170
181
- data
171
-
182
+
172
183
@ingroup constant
173
184
*/
174
185
static const uint8_t MBSuccess = 0x00 ;
175
-
186
+
176
187
/* *
177
188
ModbusTCP invalid response slave ID exception.
178
-
189
+
179
190
The slave ID in the response does not match that of the request.
180
-
191
+
181
192
@ingroup constant
182
193
*/
183
194
static const uint8_t MBInvalidTransactionID = 0xE0 ;
184
-
195
+
185
196
/* *
186
197
ModbusTCP invalid response function exception.
187
-
198
+
188
199
The function code in the response does not match that of the request.
189
-
200
+
190
201
@ingroup constant
191
202
*/
192
203
static const uint8_t MBInvalidFunction = 0xE1 ;
193
-
204
+
194
205
/* *
195
206
ModbusTCP response timed out exception.
196
-
197
- The entire response was not received within the timeout period,
198
- ModbusTCP::MBResponseTimeout.
199
-
207
+
208
+ The entire response was not received within the timeout period,
209
+ ModbusTCP::MBResponseTimeout.
210
+
200
211
@ingroup constant
201
212
*/
202
213
static const uint8_t MBResponseTimedOut = 0xE2 ;
203
-
214
+
204
215
/* *
205
216
ModbusTCP invalid response CRC exception.
206
-
217
+
207
218
The CRC in the response does not match the one calculated.
208
-
219
+
209
220
@ingroup constant
210
221
*/
211
222
static const uint8_t MBInvalidUnitID = 0xE3 ;
212
223
static const uint8_t MBInvalidProtocol = 0xE4 ;
213
224
214
- uint8_t getResponseBufferLength ();
225
+ uint8_t getResponseBufferLength ();
215
226
uint16_t getResponseBuffer (uint8_t );
216
227
void clearResponseBuffer ();
217
228
uint8_t setTransmitBuffer (uint8_t , uint16_t );
218
229
void clearTransmitBuffer ();
219
-
220
-
230
+
231
+
221
232
uint8_t readCoils (uint16_t , uint16_t );
222
233
uint8_t readDiscreteInputs (uint16_t , uint16_t );
223
234
uint8_t readHoldingRegisters (uint16_t , uint16_t );
@@ -228,10 +239,10 @@ class ModbusTCP
228
239
uint8_t writeMultipleRegisters (uint16_t , uint16_t );
229
240
uint8_t maskWriteRegister (uint16_t , uint16_t , uint16_t );
230
241
uint8_t readWriteMultipleRegisters (uint16_t , uint16_t , uint16_t , uint16_t );
231
-
242
+
232
243
private:
233
244
234
- uint8_t _u8MBUnitID; // /< Unit Identifier for individual unit-identification
245
+ uint8_t _u8MBUnitID; // /< Unit Identifier for individual unit-identification
235
246
uint16_t _u16MBTransactionID = 1 ; // /< Transaction id for each transaction
236
247
uint16_t _u16MBProtocolID = 0 ; // /< Constant
237
248
static const uint8_t MaxBufferSize = 64 ; // /< size of response/transmit buffers
@@ -241,7 +252,7 @@ class ModbusTCP
241
252
uint16_t _u16WriteAddress; // /< slave register to which to write
242
253
uint16_t _u16WriteQty; // /< quantity of words to write
243
254
uint8_t _u8ResponseBufferLength;
244
-
255
+
245
256
// Modbus function codes for bit access
246
257
static const uint8_t MBReadCoils = 0x01 ; // /< Modbus function 0x01 Read Coils
247
258
static const uint8_t MBReadDiscreteInputs = 0x02 ; // /< Modbus function 0x02 Read Discrete Inputs
@@ -255,13 +266,13 @@ class ModbusTCP
255
266
static const uint8_t MBWriteMultipleRegisters = 0x10 ; // /< Modbus function 0x10 Write Multiple Registers
256
267
static const uint8_t MBMaskWriteRegister = 0x16 ; // /< Modbus function 0x16 Mask Write Register
257
268
static const uint8_t MBReadWriteMultipleRegisters = 0x17 ; // /< Modbus function 0x17 Read Write Multiple Registers
258
-
269
+
259
270
260
271
static const uint16_t ku16MBResponseTimeout = 2000 ; // /< Modbus timeout [milliseconds]
261
-
272
+
262
273
// master function that conducts Modbus transactions
263
274
uint8_t ModbusMasterTransaction (uint8_t u8MBFunction);
264
-
275
+
265
276
// idle callback function; gets called during idle time between TX and RX
266
277
void (*_idle)();
267
278
};
0 commit comments