Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions libraries/Modbus/Modbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,15 @@ void Modbus::readRegisters(word startreg, word numregs) {

word val;
word i = 0;
while(numregs--) {
while(numregs) {
//retrieve the value from the register bank for the current register
val = this->Hreg(startreg + i);
//write the high byte of the register value
_frame[2 + i * 2] = val >> 8;
//write the low byte of the register value
_frame[3 + i * 2] = val & 0xFF;
i++;
numregs--;
}

_reply = MB_REPLY_NORMAL;
Expand Down Expand Up @@ -278,10 +279,11 @@ void Modbus::writeMultipleRegisters(byte* frame,word startreg, word numoutputs,

word val;
word i = 0;
while(numoutputs--) {
while(numoutputs) {
val = (word)frame[6+i*2] << 8 | (word)frame[7+i*2];
this->Hreg(startreg + i, val);
i++;
numoutputs--;
}

_reply = MB_REPLY_NORMAL;
Expand Down Expand Up @@ -326,8 +328,8 @@ void Modbus::readCoils(word startreg, word numregs) {
byte bitn = 0;
word totregs = numregs;
word i;
while (numregs--) {
i = (totregs - numregs) / 8;
while (numregs) {
i = (totregs - numregs--) / 8;
if (this->Coil(startreg))
bitSet(_frame[2+i], bitn);
else
Expand Down Expand Up @@ -496,8 +498,8 @@ void Modbus::writeMultipleCoils(byte* frame,word startreg, word numoutputs, byte
byte bitn = 0;
word totoutputs = numoutputs;
word i;
while (numoutputs--) {
i = (totoutputs - numoutputs) / 8;
while (numoutputs) {
i = (totoutputs - numoutputs--) / 8;
this->Coil(startreg, bitRead(frame[6+i], bitn));
//increment the bit index
bitn++;
Expand Down