Skip to content

Commit a79e016

Browse files
committed
Multiple connection support
1 parent a236934 commit a79e016

File tree

6 files changed

+21
-76
lines changed

6 files changed

+21
-76
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ used in industrial automation and can be used in other areas, such as home autom
55

66
The Modbus generally uses serial RS-232 or RS-485 as physical layer (then called Modbus Serial) and TCP/IP via Ethernet or WiFi (Modbus IP).
77

8-
In the current version the library allows the ESP8266 operate as a slave, supporting Modbus IP via wireless network. For more information about Modbus see:
8+
In the current version the library allows the ESP8266/ESP32 operate as a slave, supporting Modbus IP via wireless network. For more information about Modbus see:
99

1010
http://pt.wikipedia.org/wiki/Modbus http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b.pdf
1111
http://www.modbus.org/docs/Modbus_Messaging_Implementation_Guide_V1_0b.pdf
@@ -62,8 +62,7 @@ This README is under development, for now, see the examples of the library.
6262

6363
## Contributions
6464

65-
https://github.com/emelinov/modbus-esp8266
66-
65+
https://github.com/emelinov/modbus-esp8266<br>
6766
6867

6968
Original version:

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name=modbus-esp8266
22
version=0.1
33
author=Andre Sarmento Barbosa
44
maintainer=Alexander Emelianov<[email protected]>
5-
sentence=Modbus Library for ESP8266
6-
paragraph=This library allows your ESP8266 to communicate via Modbus protocol. The Modbus is a master-slave protocol used in industrial automation and can be used in other areas, such as home automation.
5+
sentence=Modbus Library for ESP8266/ESP32
6+
paragraph=This library allows your ESP8266/ESP32 to communicate via Modbus protocol. The Modbus is a master-slave protocol used in industrial automation and can be used in other areas, such as home automation.
77
category=Communication
88
url=https://github.com/emelianov/modbus-esp8266
99
architectures=esp8266,esp32

src/Modbus.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*
22
Modbus.h - Header for Modbus Base Library
33
Copyright (C) 2014 André Sarmento Barbosa
4-
2017 Alexander Emelianov ([email protected])
54
*/
65
#include "Modbus.h"
76

src/Modbus.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*
22
Modbus.h - Header for Modbus Base Library
33
Copyright (C) 2014 André Sarmento Barbosa
4-
2017 Alexander Emelianov ([email protected])
54
*/
65
#include "Arduino.h"
76

src/ModbusIP_ESP8266.cpp

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,19 @@ void ModbusIP::begin() {
1414
}
1515

1616
void ModbusIP::task() {
17-
#ifdef TCP_KEEP_ALIVE
18-
if (client == NULL || !client.connected()) {
19-
// if (client)
20-
// delete client;
21-
//
22-
client = available();
23-
}
24-
#else
25-
WiFiClient client;
26-
#endif
17+
for (uint8_t n = 0; n < TCP_MAX_CLIENTS; n++) {
18+
if (!client[n] || !client[n].connected()) {
19+
client[n] = available();
20+
}
2721

2822
int raw_len = 0;
2923

30-
if (client) {
31-
if (client.connected()) {
24+
if (client[n]) {
25+
if (client[n].connected()) {
3226
for (int x = 0; x < 300; x++) { // Time to have data available
33-
if (client.available()) {
34-
while (client.available() > raw_len) { //Computes data length
35-
raw_len = client.available();
27+
if (client[n].available()) {
28+
while (client[n].available() > raw_len) { //Computes data length
29+
raw_len = client[n].available();
3630
delay(1);
3731
}
3832
break;
@@ -42,7 +36,7 @@ void ModbusIP::task() {
4236
}
4337

4438
if (raw_len > 7) {
45-
for (int i=0; i<7; i++) _MBAP[i] = client.read(); //Get MBAP
39+
for (int i=0; i<7; i++) _MBAP[i] = client[n].read(); //Get MBAP
4640

4741
_len = _MBAP[4] << 8 | _MBAP[5];
4842
_len--; // Do not count with last byte from MBAP
@@ -51,10 +45,10 @@ void ModbusIP::task() {
5145
_frame = (byte*) malloc(_len);
5246

5347
raw_len = raw_len - 7;
54-
for (int i=0; i< raw_len; i++) _frame[i] = client.read(); //Get Modbus PDU
48+
for (int i=0; i< raw_len; i++) _frame[i] = client[n].read(); //Get Modbus PDU
5549

5650
this->receivePDU(_frame);
57-
client.flush();
51+
client[n].flush();
5852

5953
if (_reply != MB_REPLY_OFF) {
6054
//MBAP
@@ -67,61 +61,14 @@ void ModbusIP::task() {
6761
for (int i=0; i<7; i++) sbuf[i] = _MBAP[i];
6862
for (int i=0; i<_len; i++) sbuf[i+7] = _frame[i];
6963

70-
client.write(sbuf, send_len);
64+
client[n].write(sbuf, send_len);
7165
}
7266
#ifndef TCP_KEEP_ALIVE
73-
client.stop();
67+
client[n].stop();
7468
#endif
7569
free(_frame);
7670
_len = 0;
7771
}
7872
}
7973
}
80-
/*
81-
uint8_t buffer[128] = {0};
82-
uint8_t mux_id;
83-
uint32_t len = _wifi->recv(&mux_id, buffer, sizeof(buffer), 100);
84-
85-
if (len > 0) {
86-
int i = 0;
87-
while (i < 7) {
88-
_MBAP[i] = buffer[i];
89-
i++;
90-
}
91-
92-
_len = _MBAP[4] << 8 | _MBAP[5];
93-
_len--; // Do not count with last byte from MBAP
94-
if (_MBAP[2] !=0 || _MBAP[3] !=0) return; //Not a MODBUSIP packet
95-
if (_len > MODBUSIP_MAXFRAME) return; //Length is over MODBUSIP_MAXFRAME
96-
97-
_frame = (byte*) malloc(_len);
98-
i = 0;
99-
while (i < _len){
100-
_frame[i] = buffer[7+i]; //Forget MBAP and take just modbus pdu
101-
i++;
102-
}
103-
104-
this->receivePDU(_frame);
105-
106-
if (_reply != MB_REPLY_OFF) {
107-
//MBAP
108-
_MBAP[4] = _len >> 8;
109-
_MBAP[5] = _len | 0x00FF;
110-
buffer[4] = _MBAP[4];
111-
buffer[5] = _MBAP[5];
112-
113-
i = 0;
114-
while (i < _len){
115-
buffer[i+7] = _frame[i];
116-
i++;
117-
}
118-
_wifi->send(mux_id, buffer, _len + 7);
119-
_wifi->releaseTCP(mux_id);
120-
}
121-
122-
free(_frame);
123-
_len = 0;
124-
}
125-
126-
}
127-
*/
74+
}

src/ModbusIP_ESP8266.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
#define MODBUSIP_TIMEOUT 10
1919

2020
#define TCP_KEEP_ALIVE
21+
#define TCP_MAX_CLIENTS 4
2122

2223
class ModbusIP : public Modbus, public WiFiServer {
2324
private:
2425
byte _MBAP[7];
2526
#ifdef TCP_KEEP_ALIVE
26-
WiFiClient client;
27+
WiFiClient client[TCP_MAX_CLIENTS];
2728
#endif
2829
public:
2930
ModbusIP() : WiFiServer(MODBUSIP_PORT) {

0 commit comments

Comments
 (0)