Skip to content

Commit 20a78c5

Browse files
committed
2 parents e909c88 + 56754f5 commit 20a78c5

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

src/Modbus.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ void Modbus::slavePDU(uint8_t* frame) {
8888
uint16_t field1 = (uint16_t)frame[1] << 8 | (uint16_t)frame[2];
8989
uint16_t field2 = (uint16_t)frame[3] << 8 | (uint16_t)frame[4];
9090
uint16_t bytecount_calc;
91+
uint16_t k;
9192
switch (fcode) {
9293
case FC_WRITE_REG:
9394
//field1 = reg, field2 = value
@@ -113,15 +114,17 @@ void Modbus::slavePDU(uint8_t* frame) {
113114
exceptionResponse(fcode, EX_ILLEGAL_VALUE);
114115
break;
115116
}
116-
for (int k = 0; k < field2; k++) { //Check Address (startreg...startreg + numregs)
117+
for (k = 0; k < field2; k++) { //Check Address (startreg...startreg + numregs)
117118
if (!searchRegister(HREG(field1) + k)) {
118119
exceptionResponse(fcode, EX_ILLEGAL_ADDRESS);
119120
break;
120121
}
121122
}
122-
setMultipleWords(frame + 6, HREG(field1), field2);
123-
successResponce(HREG(field1), field2, fcode);
124-
_reply = REPLY_NORMAL;
123+
if (k >= field2) {
124+
setMultipleWords(frame + 6, HREG(field1), field2);
125+
successResponce(HREG(field1), field2, fcode);
126+
_reply = REPLY_NORMAL;
127+
}
125128
break;
126129

127130
case FC_READ_COILS:
@@ -164,15 +167,17 @@ void Modbus::slavePDU(uint8_t* frame) {
164167
exceptionResponse(fcode, EX_ILLEGAL_VALUE);
165168
break;
166169
}
167-
for (int k = 0; k < field2; k++) { //Check Address (startreg...startreg + numregs)
170+
for (k = 0; k < field2; k++) { //Check Address (startreg...startreg + numregs)
168171
if (!searchRegister(COIL(field1) + k)) {
169172
exceptionResponse(fcode, EX_ILLEGAL_ADDRESS);
170173
break;
171174
}
172175
}
173-
setMultipleBits(frame + 6, COIL(field1), field2);
174-
successResponce(COIL(field1), field2, fcode);
175-
_reply = REPLY_NORMAL;
176+
if (k >= field2) {
177+
setMultipleBits(frame + 6, COIL(field1), field2);
178+
successResponce(COIL(field1), field2, fcode);
179+
_reply = REPLY_NORMAL;
180+
}
176181
break;
177182

178183
default:

src/Modbus.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ class Modbus {
204204
void exceptionResponse(FunctionCode fn, ResultCode excode);
205205
void successResponce(TAddress startreg, uint16_t numoutputs, FunctionCode fn);
206206
void slavePDU(uint8_t* frame); //For Slave
207-
void masterPDU(uint8_t* frame, uint8_t* sourceFrame, TAddress startreg, void* output= nullptr); //For Master
207+
void masterPDU(uint8_t* frame, uint8_t* sourceFrame, TAddress startreg, void* output = nullptr); //For Master
208208
// frame - data received form slave
209-
// sourceFrame - data sent fo slave
209+
// sourceFrame - data have sent fo slave
210210
// startreg - local register to start put data to
211211
// output - if not null put data to the buffer insted local registers. output assumed to by array of uint16_t or boolean
212212

src/ModbusIP_ESP8266.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "ModbusIP_ESP8266.h"
77

88
ModbusIP::ModbusIP() {
9-
_trans.reserve(MODBUSIP_MAX_TRANSACIONS);
9+
//_trans.reserve(MODBUSIP_MAX_TRANSACIONS);
1010
for (uint8_t i = 0; i < MODBUSIP_MAX_CLIENTS; i++)
1111
client[i] = nullptr;
1212
}
@@ -63,7 +63,7 @@ void ModbusIP::task() {
6363
}
6464
}
6565
// Close connection if callback returns false or MODBUSIP_MAX_CLIENTS reached
66-
currentClient->flush();
66+
//currentClient->flush();
6767
currentClient->stop();
6868
delete currentClient;
6969
}
@@ -78,22 +78,26 @@ void ModbusIP::task() {
7878
_len--; // Do not count with last byte from MBAP
7979

8080
if (__bswap_16(_MBAP.protocolId) != 0) { //Check if MODBUSIP packet. __bswap is usless there.
81-
client[n]->flush();
81+
client[n]->readBytes((uint8_t*)nullptr, client[n]->available());
82+
client[n]->flush();
8283
continue; // for (n)
8384
}
8485
if (_len > MODBUSIP_MAXFRAME) { //Length is over MODBUSIP_MAXFRAME
8586
exceptionResponse((FunctionCode)client[n]->read(), EX_SLAVE_FAILURE);
86-
client[n]->flush();
87+
client[n]->readBytes((uint8_t*)nullptr, client[n]->available());
88+
//client[n]->flush();
8789
} else {
8890
free(_frame);
8991
_frame = (uint8_t*) malloc(_len);
9092
if (!_frame) {
9193
exceptionResponse((FunctionCode)client[n]->read(), EX_SLAVE_FAILURE);
92-
client[n]->flush();
94+
client[n]->readBytes((uint8_t*)nullptr, client[n]->available());
95+
//client[n]->flush();
9396
} else {
9497
if (client[n]->readBytes(_frame, _len) < _len) { //Try to read MODBUS frame
9598
exceptionResponse((FunctionCode)_frame[0], EX_ILLEGAL_VALUE);
96-
client[n]->flush();
99+
client[n]->readBytes((uint8_t*)nullptr, client[n]->available());
100+
//client[n]->flush();
97101
} else {
98102
if (client[n]->localPort() == MODBUSIP_PORT) {
99103
// Process incoming frame as slave
@@ -119,7 +123,8 @@ void ModbusIP::task() {
119123
_trans.erase(it);
120124
}
121125
}
122-
client[n]->flush(); // Not sure if we need flush rest of data available
126+
//client[n]->readBytes((uint8_t*)nullptr, client[n]->available());
127+
//client[n]->flush(); // Not sure if we need flush rest of data available
123128
}
124129
}
125130
}
@@ -131,7 +136,9 @@ void ModbusIP::task() {
131136
memcpy(sbuf, _MBAP.raw, sizeof(_MBAP.raw));
132137
memcpy(sbuf + sizeof(_MBAP.raw), _frame, _len);
133138
client[n]->write(sbuf, send_len);
139+
//client[n]->flush();
134140
}
141+
client[n]->flush();
135142
free(_frame);
136143
_frame = nullptr;
137144
_len = 0;

src/ModbusIP_ESP8266.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ class ModbusIP : public Modbus {
6464
int8_t getSlave(IPAddress ip);
6565
int8_t getMaster(IPAddress ip);
6666
uint16_t send(IPAddress ip, TAddress startreg, cbTransaction cb, uint8_t unit = MODBUSIP_UNIT, void* data = nullptr, bool waitResponse = true);
67-
67+
// ip - slave ip address
68+
// startreg - first local register to save returned data to (miningless for write to slave operations)
69+
// cb - transaction callback function
70+
// unit - slave modbus unit id
71+
// data - if not null use buffer to save returned data instead of local registers
6872
public:
6973
ModbusIP();
7074
bool isTransaction(uint16_t id);

0 commit comments

Comments
 (0)