Skip to content

Commit faa63a2

Browse files
committed
Fixed a bug in readADU.
1 parent 4a3d49c commit faa63a2

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/ModbusRTUComm.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ ModbusRTUCommError ModbusRTUComm::readAdu(ModbusADU& adu) {
5454
while (!_serial.available()) {
5555
if (millis() - startMillis >= _readTimeout) return MODBUS_RTU_COMM_TIMEOUT;
5656
}
57+
uint16_t len = 0;
5758
unsigned long startMicros = micros();
5859
do {
5960
if (_serial.available()) {
6061
startMicros = micros();
61-
adu.rtu[adu.getRtuLen()] = _serial.read();
62-
adu.setRtuLen(adu.getRtuLen() + 1);
62+
adu.rtu[len] = _serial.read();
63+
len++;
6364
}
64-
} while (micros() - startMicros <= _charTimeout && adu.getRtuLen() < 256);
65-
65+
} while (micros() - startMicros <= _charTimeout && len < 256);
66+
adu.setRtuLen(len);
6667
while (micros() - startMicros < _frameTimeout);
6768
if (_serial.available()) {
6869
adu.setRtuLen(0);

0 commit comments

Comments
 (0)