4-20ma / ModbusMaster

Enlighten your Arduino to be a Modbus master
http://4-20ma.io/ModbusMaster/
Apache License 2.0
603 stars 353 forks source link

fixed zero index problem where _u8ResponseBufferLength is set. #154

Open intelligentandgoodlooking opened 3 years ago

intelligentandgoodlooking commented 3 years ago

Description

when a 1 word (2 byte) response is received _u8ResponseBufferLength is erroneously set to 0 instead of 1 similarly, a 16 word (32 byte) response is received, _u8ResponseBufferLength is set to 15 instead of 16. This causes available() to return 1 less than it should.

Issues Resolved

It seems like not many people use the available() function.

Check List

General

The following have been modified to reflect new features, if warranted

The following have NOT been modified

PBudmark commented 3 years ago

Yes, the _u8ResponseBufferLength needs the stepup, but to avoid buffer overrun, the assignment should be moved inside brackets at 'if (i < ku8MaxBufferSize)' or else _u8ResponseBufferLength is increased even if no register is stored.

In the case with odd number of bytes, 'if (u8ModbusADU[2] % 2)', _u8ResponseBufferLength = i + 1; should be done inside the brackets, as i is already stepped from the for-statement.