diff --git a/libraries/Modbus/Modbus.cpp b/libraries/Modbus/Modbus.cpp index 6e9c7da..3dc92fa 100644 --- a/libraries/Modbus/Modbus.cpp +++ b/libraries/Modbus/Modbus.cpp @@ -216,7 +216,7 @@ 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 @@ -224,6 +224,7 @@ void Modbus::readRegisters(word startreg, word numregs) { //write the low byte of the register value _frame[3 + i * 2] = val & 0xFF; i++; + numregs--; } _reply = MB_REPLY_NORMAL; @@ -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; @@ -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 @@ -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++;