From f2dbc4aa8a1bf780a29991fc29d18187e7812c13 Mon Sep 17 00:00:00 2001 From: Alessandro Ferrari Date: Sat, 19 Jun 2021 14:53:31 -0400 Subject: [PATCH 1/3] fixed multiples of 8 issues --- README.md | 4 ++++ libraries/Modbus/Modbus.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 822f437..b6ccde2 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,10 @@ I also thought it would be cool have a base library for Modbus and derive it for I appreciate the work of all the authors of the other libraries, of which I used several ideas to compose the modbus-arduino. At the end of this document is a list of libraries and their authors. +Why this fork? +============== +This fork fixes a few issues that I found where the coils and references which are multiples of 8 weren't working correctly. It was just a small (but significant) iterator issue. + Features ======== diff --git a/libraries/Modbus/Modbus.cpp b/libraries/Modbus/Modbus.cpp index 6e9c7da..7eef740 100644 --- a/libraries/Modbus/Modbus.cpp +++ b/libraries/Modbus/Modbus.cpp @@ -326,8 +326,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 +496,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++; From 4532d0b93da1f72fcf4930ed52238779adebd653 Mon Sep 17 00:00:00 2001 From: Alessandro Ferrari Date: Sat, 19 Jun 2021 16:56:23 -0400 Subject: [PATCH 2/3] changed README to beginning --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index b6ccde2..822f437 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,6 @@ I also thought it would be cool have a base library for Modbus and derive it for I appreciate the work of all the authors of the other libraries, of which I used several ideas to compose the modbus-arduino. At the end of this document is a list of libraries and their authors. -Why this fork? -============== -This fork fixes a few issues that I found where the coils and references which are multiples of 8 weren't working correctly. It was just a small (but significant) iterator issue. - Features ======== From 95952a90eaa7c4c20defe7b8cb8ce1d710134c44 Mon Sep 17 00:00:00 2001 From: Alessandro Ferrari Date: Sat, 19 Jun 2021 17:07:11 -0400 Subject: [PATCH 3/3] fixed more of the same bugs --- libraries/Modbus/Modbus.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libraries/Modbus/Modbus.cpp b/libraries/Modbus/Modbus.cpp index 7eef740..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;