Skip to content

Commit 1baae6e

Browse files
committed
Multiple connection fix. ESP32 tested
1 parent a79e016 commit 1baae6e

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/ModbusIP_ESP8266.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,43 @@
1111

1212
void ModbusIP::begin() {
1313
WiFiServer::begin();
14+
for (uint8_t i = 0; i < TCP_MAX_CLIENTS; i++) {
15+
client[i] = WiFiClient();
16+
}
1417
}
1518

1619
void ModbusIP::task() {
1720
for (uint8_t n = 0; n < TCP_MAX_CLIENTS; n++) {
1821
if (!client[n] || !client[n].connected()) {
1922
client[n] = available();
23+
//if (client[n] && client[n].connected()) {
24+
// Serial.println(client[n].remoteIP());
25+
//}
2026
}
2127

2228
int raw_len = 0;
2329

24-
if (client[n]) {
25-
if (client[n].connected()) {
26-
for (int x = 0; x < 300; x++) { // Time to have data available
27-
if (client[n].available()) {
28-
while (client[n].available() > raw_len) { //Computes data length
30+
//if (client[n]) {
31+
if (client[n] && client[n].connected()) {
32+
// for (int x = 0; x < 300; x++) { // Time to have data available
33+
// if (client[n].available()) {
34+
// while (client[n].available() > raw_len) { //Computes data length
2935
raw_len = client[n].available();
30-
delay(1);
31-
}
32-
break;
33-
}
34-
delay(10);
35-
}
36+
// delay(1);
37+
// }
38+
// break;
39+
// }
40+
// delay(10);
41+
// }
3642
}
3743

3844
if (raw_len > 7) {
3945
for (int i=0; i<7; i++) _MBAP[i] = client[n].read(); //Get MBAP
4046

4147
_len = _MBAP[4] << 8 | _MBAP[5];
4248
_len--; // Do not count with last byte from MBAP
43-
if (_MBAP[2] !=0 || _MBAP[3] !=0) return; //Not a MODBUSIP packet
44-
if (_len > MODBUSIP_MAXFRAME) return; //Length is over MODBUSIP_MAXFRAME
49+
if (_MBAP[2] !=0 || _MBAP[3] !=0) continue; //Not a MODBUSIP packet
50+
if (_len > MODBUSIP_MAXFRAME) continue; //Length is over MODBUSIP_MAXFRAME
4551
_frame = (byte*) malloc(_len);
4652

4753
raw_len = raw_len - 7;
@@ -69,6 +75,6 @@ void ModbusIP::task() {
6975
free(_frame);
7076
_len = 0;
7177
}
72-
}
78+
//}
7379
}
7480
}

0 commit comments

Comments
 (0)