Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Read/Write Multiple Coil/Inputs Fixed! (8 bit bug)
Modbus Library for Arduino
==========================

Bugs in main repo are fixed regarding to read/write for more than 8 coils.

This library allows your Arduino to communicate via Modbus protocol. The Modbus is a master-slave protocol
used in industrial automation and can be used in other areas, such as home automation.

Expand Down
9 changes: 6 additions & 3 deletions libraries/Modbus/Modbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ void Modbus::readCoils(word startreg, word numregs) {
byte bitn = 0;
word totregs = numregs;
word i;
while (numregs--) {
while (numregs) {
i = (totregs - numregs) / 8;
if (this->Coil(startreg))
bitSet(_frame[2+i], bitn);
Expand All @@ -337,6 +337,7 @@ void Modbus::readCoils(word startreg, word numregs) {
if (bitn == 8) bitn = 0;
//increment the register
startreg++;
numregs--;
}

_reply = MB_REPLY_NORMAL;
Expand Down Expand Up @@ -377,7 +378,7 @@ void Modbus::readInputStatus(word startreg, word numregs) {
byte bitn = 0;
word totregs = numregs;
word i;
while (numregs--) {
while (numregs) {
i = (totregs - numregs) / 8;
if (this->Ists(startreg))
bitSet(_frame[2+i], bitn);
Expand All @@ -388,6 +389,7 @@ void Modbus::readInputStatus(word startreg, word numregs) {
if (bitn == 8) bitn = 0;
//increment the register
startreg++;
numregs--;
}

_reply = MB_REPLY_NORMAL;
Expand Down Expand Up @@ -496,14 +498,15 @@ void Modbus::writeMultipleCoils(byte* frame,word startreg, word numoutputs, byte
byte bitn = 0;
word totoutputs = numoutputs;
word i;
while (numoutputs--) {
while (numoutputs) {
i = (totoutputs - numoutputs) / 8;
this->Coil(startreg, bitRead(frame[6+i], bitn));
//increment the bit index
bitn++;
if (bitn == 8) bitn = 0;
//increment the register
startreg++;
numoutputs--;
}

_reply = MB_REPLY_NORMAL;
Expand Down
2 changes: 1 addition & 1 deletion libraries/ModbusIP/ModbusIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ void ModbusIP::task() {
_len = 0;
}
}
}
}
7 changes: 3 additions & 4 deletions libraries/ModbusIP/ModbusIP.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#include <Arduino.h>
#include <Modbus.h>
#include <SPI.h>
#include <Ethernet.h>
#include <UIPEthernet.h>

#ifndef MODBUSIP_H
#define MODBUSIP_H

#define MODBUSIP_PORT 502
#define MODBUSIP_MAXFRAME 200

//#define TCP_KEEP_ALIVE
#define TCP_KEEP_ALIVE

class ModbusIP : public Modbus {
private:
Expand All @@ -30,5 +30,4 @@ class ModbusIP : public Modbus {
void task();
};

#endif //MODBUSIP_H

#endif //MODBUSIP_H