Skip to content

Commit 6ad4f52

Browse files
authored
Merge pull request #2 from flouk19/master
Added ESP8266
2 parents 2809ff0 + 26c5241 commit 6ad4f52

File tree

4 files changed

+206
-69
lines changed

4 files changed

+206
-69
lines changed

ModbusTCP.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Arduino library for communicating with Modbus server over Ethernet in TCP.
2424
2525
Adopted from ModbusMaster for RTU over RS-485 by Doc Walker
2626
Modified by Narendra Dehury for TCP.
27+
Modified by Florian K for ESP8266.
2728
copyright @ phoenixrobotix.com
2829
2930
*/
@@ -583,6 +584,8 @@ uint8_t ModbusTCP::ModbusMasterTransaction(uint8_t u8MBFunction)
583584
if(!ModbusClient.connected()) { // fOR w5100
584585
#elif ENC28J60
585586
if(!MBconnectionFlag) { // For ENC28J60
587+
#elif ESP8266
588+
if (!ModbusClient.connected()) { // for esp8266
586589
#endif
587590
Serial.print(F("Trying to connect..."));
588591
MBconnectionFlag = 0;
@@ -602,9 +605,11 @@ uint8_t ModbusTCP::ModbusMasterTransaction(uint8_t u8MBFunction)
602605
else
603606
Serial.println(F("Already Connected to Server!!"));
604607

605-
608+
#ifdef ESP8266
609+
ModbusClient.write(&u8ModbusADU[0], u8ModbusADUSize);
610+
#else
606611
ModbusClient.write(u8ModbusADU, u8ModbusADUSize);
607-
612+
#endif
608613
u8ModbusADUSize = 0;
609614
// loop until we run out of time or bytes, or an error occurs
610615
u32StartTime = millis();
@@ -659,8 +664,11 @@ uint8_t ModbusTCP::ModbusMasterTransaction(uint8_t u8MBFunction)
659664
#if WIZNET_W5100
660665
ModbusClient.stop();
661666
Serial.println("WIZNET W5100 : Stopping");
662-
#else
667+
#elif ENC28J60
663668
Serial.println("ENC28J60 : Not Stopping");
669+
#elif ESP8266
670+
ModbusClient.stop();
671+
Serial.println("ESP8266 : Stopping");
664672
#endif
665673

666674

ModbusTCP.h

+76-65
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,44 @@ Arduino library for communicating with Modbus server over Ethernet in TCP.
1212
\mainpage Modbus over TCP/IP
1313
ModbusTCP.h - Arduino library for communicating with Modbus server
1414
over Ethernet (via TCP protocol).
15-
15+
1616
This file is part of ModbusTCP.
1717
1818
@see **README** for details.
19-
19+
2020
ModbusTCP is free software: you can redistribute it and/or modify
2121
it under the terms of the GNU General Public License as published by
2222
the Free Software Foundation, either version 3 of the License, or
2323
(at your option) any later version.
24-
24+
2525
ModbusTCP is distributed in the hope that it will be useful,
2626
but WITHOUT ANY WARRANTY; without even the implied warranty of
2727
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2828
GNU General Public License for more details.
29-
29+
3030
You should have received a copy of the GNU General Public License
3131
along with ModbusTCP library. If not, see <http://www.gnu.org/licenses/>.
32-
32+
3333
Adopted from ModbusMaster for RTU over RS-485 by Doc Walker
34-
34+
3535
Modified by Narendra Dehury for TCP.
36-
36+
37+
Modified by Florian K for ESP8266.
38+
3739
Copyright @ phoenixrobotix.com
38-
40+
3941
*/
4042

41-
43+
4244
#ifndef Modbus_TCPIP_h
4345
#define Modbus_TCPIP_h
4446

47+
#ifndef WIZNET_W5100
4548
#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
4853

4954

5055
/* _____STANDARD INCLUDES____________________________________________________ */
@@ -65,6 +70,10 @@ Arduino library for communicating with Modbus server over Ethernet in TCP.
6570
#include <UIPEthernet.h>
6671
#endif
6772

73+
#if ESP8266
74+
#include <WiFiClient.h>
75+
#endif
76+
6877

6978
/* _____PROJECT INCLUDES_____________________________________________________ */
7079

@@ -79,13 +88,15 @@ Arduino class library for communicating with Modbus server over TCP/IP.
7988
class ModbusTCP
8089
{
8190
public:
82-
91+
8392
IPAddress serverIP;
8493

85-
#if WIZNET_W5100
94+
#if WIZNET_W5100
8695
EthernetClient ModbusClient;
8796
#elif ENC28J60
8897
UIPClient ModbusClient;
98+
#elif ESP8266
99+
WiFiClient ModbusClient;
89100
#endif
90101

91102
char MBconnectionFlag = 0;
@@ -96,11 +107,11 @@ class ModbusTCP
96107
void setTransactionID(uint16_t);
97108
void setServerIPAddress(IPAddress);
98109
void idle(void (*)());
99-
110+
100111
// Modbus exception codes
101112
/**
102113
Modbus protocol illegal function exception.
103-
114+
104115
The function code received in the query is not an allowable action for
105116
the server. This may be because the function code is only
106117
applicable to newer devices, and was not implemented in the unit
@@ -114,46 +125,46 @@ class ModbusTCP
114125

115126
/**
116127
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.
130141
131142
@ingroup constant
132143
*/
133144
static const uint8_t MBIllegalDataAddress = 0x02;
134-
145+
135146
/**
136147
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
144155
significance of any particular value of any particular register.
145-
156+
146157
@ingroup constant
147158
*/
148159
static const uint8_t MBIllegalDataValue = 0x03;
149160
//static const uint8_t MBIllegalResponseLength = 0x04;
150-
161+
151162
/**
152163
Modbus TCP server connection failure exception.
153-
164+
154165
An unrecoverable error occurred while the client is attempting to connect to
155166
the server but fails, Usually a case with ENC82J60.
156-
167+
157168
@ingroup constant
158169
*/
159170

@@ -162,62 +173,62 @@ class ModbusTCP
162173
// Class-defined success/exception codes
163174
/**
164175
ModbusTCP success.
165-
176+
166177
Modbus transaction was successful; the following checks were valid:
167178
- slave ID
168179
- function code
169180
- response code
170181
- data
171-
182+
172183
@ingroup constant
173184
*/
174185
static const uint8_t MBSuccess = 0x00;
175-
186+
176187
/**
177188
ModbusTCP invalid response slave ID exception.
178-
189+
179190
The slave ID in the response does not match that of the request.
180-
191+
181192
@ingroup constant
182193
*/
183194
static const uint8_t MBInvalidTransactionID = 0xE0;
184-
195+
185196
/**
186197
ModbusTCP invalid response function exception.
187-
198+
188199
The function code in the response does not match that of the request.
189-
200+
190201
@ingroup constant
191202
*/
192203
static const uint8_t MBInvalidFunction = 0xE1;
193-
204+
194205
/**
195206
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+
200211
@ingroup constant
201212
*/
202213
static const uint8_t MBResponseTimedOut = 0xE2;
203-
214+
204215
/**
205216
ModbusTCP invalid response CRC exception.
206-
217+
207218
The CRC in the response does not match the one calculated.
208-
219+
209220
@ingroup constant
210221
*/
211222
static const uint8_t MBInvalidUnitID = 0xE3;
212223
static const uint8_t MBInvalidProtocol = 0xE4;
213224

214-
uint8_t getResponseBufferLength();
225+
uint8_t getResponseBufferLength();
215226
uint16_t getResponseBuffer(uint8_t);
216227
void clearResponseBuffer();
217228
uint8_t setTransmitBuffer(uint8_t, uint16_t);
218229
void clearTransmitBuffer();
219-
220-
230+
231+
221232
uint8_t readCoils(uint16_t, uint16_t);
222233
uint8_t readDiscreteInputs(uint16_t, uint16_t);
223234
uint8_t readHoldingRegisters(uint16_t, uint16_t);
@@ -228,10 +239,10 @@ class ModbusTCP
228239
uint8_t writeMultipleRegisters(uint16_t, uint16_t);
229240
uint8_t maskWriteRegister(uint16_t, uint16_t, uint16_t);
230241
uint8_t readWriteMultipleRegisters(uint16_t, uint16_t, uint16_t, uint16_t);
231-
242+
232243
private:
233244

234-
uint8_t _u8MBUnitID; ///< Unit Identifier for individual unit-identification
245+
uint8_t _u8MBUnitID; ///< Unit Identifier for individual unit-identification
235246
uint16_t _u16MBTransactionID = 1; ///< Transaction id for each transaction
236247
uint16_t _u16MBProtocolID = 0; ///< Constant
237248
static const uint8_t MaxBufferSize = 64; ///< size of response/transmit buffers
@@ -241,7 +252,7 @@ class ModbusTCP
241252
uint16_t _u16WriteAddress; ///< slave register to which to write
242253
uint16_t _u16WriteQty; ///< quantity of words to write
243254
uint8_t _u8ResponseBufferLength;
244-
255+
245256
// Modbus function codes for bit access
246257
static const uint8_t MBReadCoils = 0x01; ///< Modbus function 0x01 Read Coils
247258
static const uint8_t MBReadDiscreteInputs = 0x02; ///< Modbus function 0x02 Read Discrete Inputs
@@ -255,13 +266,13 @@ class ModbusTCP
255266
static const uint8_t MBWriteMultipleRegisters = 0x10; ///< Modbus function 0x10 Write Multiple Registers
256267
static const uint8_t MBMaskWriteRegister = 0x16; ///< Modbus function 0x16 Mask Write Register
257268
static const uint8_t MBReadWriteMultipleRegisters = 0x17; ///< Modbus function 0x17 Read Write Multiple Registers
258-
269+
259270

260271
static const uint16_t ku16MBResponseTimeout = 2000; ///< Modbus timeout [milliseconds]
261-
272+
262273
// master function that conducts Modbus transactions
263274
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction);
264-
275+
265276
// idle callback function; gets called during idle time between TX and RX
266277
void (*_idle)();
267278
};

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This library has been tested with an Arduino [Mega](https://www.arduino.cc/en/Ma
1111

1212
1. Wizent W5100 - [Ethernet library](https://www.arduino.cc/en/Reference/Ethernet).
1313
2. ENC28J60 - [UIPEthernet library](https://github.com/UIPEthernet/UIPEthernet).
14+
3. ESP8266 - [ESP8266 library](https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFi.h).
1415

1516
Note: It can be made compatible with Wiznet W5500 model, by adding new [Ethernet2 library](https://github.com/adafruit/Ethernet2) in the header file.
1617

@@ -19,7 +20,8 @@ Settings
1920
Depending on the ic used set the following Macros.
2021

2122
1. define WIZNET_W5100 = 0
22-
2. define ENC28J60 = 1
23+
2. define ENC28J60 = 0
24+
3. define ESP8266 = 1
2325

2426
Features
2527
--------

0 commit comments

Comments
 (0)