Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate CRC and modRS485 #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Read/Write Multiple Coil/Inputs Fixed! (8 bit bug)
Modbus Library for Arduino
==========================

Expand Down
12 changes: 6 additions & 6 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,19 +498,17 @@ 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;
}
#endif



178 changes: 150 additions & 28 deletions libraries/ModbusSerial/ModbusSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,44 @@ byte ModbusSerial::getSlaveId() {
return _slaveId;
}

bool ModbusSerial::config(HardwareSerial* port, long baud, u_int format, int txPin) {
bool ModbusSerial::config(HardwareSerial* port, long baud, u_int format, int txPin1) {
this->_port = port;
this->_txPin = txPin;
this->_txPin1 = txPin1;
(*port).begin(baud, format);

delay(2000);

if (txPin >= 0) {
pinMode(txPin, OUTPUT);
digitalWrite(txPin, LOW);
if (txPin1 >= 0) {
pinMode(txPin1, OUTPUT);
digitalWrite(txPin1, LOW);
}

if (baud > 19200) {
_t15 = 750;
_t35 = 1750;
} else {
_t15 = 15000000/baud; // 1T * 1.5 = T1.5
_t35 = 35000000/baud; // 1T * 3.5 = T3.5
}

return true;
}

bool ModbusSerial::config(HardwareSerial* port, long baud, u_int format, int txPin1, int txPin2) {
this->_port = port;
this->_txPin1 = txPin1;
this->_txPin2 = txPin2;
(*port).begin(baud, format);

delay(2000);

if (txPin1 >= 0) {
pinMode(txPin1, OUTPUT);
digitalWrite(txPin1, LOW);
}
if (txPin2 >= 0) {
pinMode(txPin2, OUTPUT);
digitalWrite(txPin2, LOW);
}

if (baud > 19200) {
Expand All @@ -41,16 +69,44 @@ bool ModbusSerial::config(HardwareSerial* port, long baud, u_int format, int txP
}

#ifdef USE_SOFTWARE_SERIAL
bool ModbusSerial::config(SoftwareSerial* port, long baud, int txPin) {
bool ModbusSerial::config(SoftwareSerial* port, long baud, int txPin1) {
this->_port = port;
this->_txPin = txPin;
this->_txPin1 = txPin1;
(*port).begin(baud);

delay(2000);

if (txPin >= 0) {
pinMode(txPin, OUTPUT);
digitalWrite(txPin, LOW);
if (txPin1 >= 0) {
pinMode(txPin1, OUTPUT);
digitalWrite(txPin1, LOW);
}

if (baud > 19200) {
_t15 = 750;
_t35 = 1750;
} else {
_t15 = 15000000/baud; // 1T * 1.5 = T1.5
_t35 = 35000000/baud; // 1T * 3.5 = T3.5
}

return true;
}

bool ModbusSerial::config(SoftwareSerial* port, long baud, int txPin1, int txPin2) {
this->_port = port;
this->_txPin1 = txPin1;
this->_txPin2 = txPin2;
(*port).begin(baud);

delay(2000);

if (txPin1 >= 0) {
pinMode(txPin1, OUTPUT);
digitalWrite(txPin1, LOW);
}
if (txPin2 >= 0) {
pinMode(txPin2, OUTPUT);
digitalWrite(txPin2, LOW);
}

if (baud > 19200) {
Expand All @@ -66,15 +122,42 @@ bool ModbusSerial::config(SoftwareSerial* port, long baud, int txPin) {
#endif

#ifdef __AVR_ATmega32U4__
bool ModbusSerial::config(Serial_* port, long baud, u_int format, int txPin) {
bool ModbusSerial::config(Serial_* port, long baud, u_int format, int txPin1){
this->_port = port;
this->_txPin = txPin;
this->_txPin1 = txPin1;
(*port).begin(baud, format);
while (!(*port));

if (txPin >= 0) {
pinMode(txPin, OUTPUT);
digitalWrite(txPin, LOW);
if (txPin1 >= 0) {
pinMode(txPin1, OUTPUT);
digitalWrite(txPin1, LOW);
}

if (baud > 19200) {
_t15 = 750;
_t35 = 1750;
} else {
_t15 = 15000000/baud; // 1T * 1.5 = T1.5
_t35 = 35000000/baud; // 1T * 3.5 = T3.5
}

return true;
}

bool ModbusSerial::config(Serial_* port, long baud, u_int format, int txPin1, int txPin2) {
this->_port = port;
this->_txPin1 = txPin1;
this->_txPin2 = txPin2;
(*port).begin(baud, format);
while (!(*port));

if (txPin1 >= 0) {
pinMode(txPin1, OUTPUT);
digitalWrite(txPin1, LOW);
}
if (txPin2 >= 0) {
pinMode(txPin2, OUTPUT);
digitalWrite(txPin2, LOW);
}

if (baud > 19200) {
Expand Down Expand Up @@ -116,8 +199,14 @@ bool ModbusSerial::receive(byte* frame) {
bool ModbusSerial::send(byte* frame) {
byte i;

if (this->_txPin >= 0) {
digitalWrite(this->_txPin, HIGH);
if (this->_txPin1 >= 0) {
digitalWrite(this->_txPin1, HIGH);
delay(1);
}

if (this->_txPin1 >= 0 && this->_txPin2 >= 0) {
digitalWrite(this->_txPin1, HIGH);
digitalWrite(this->_txPin2, HIGH);
delay(1);
}

Expand All @@ -128,14 +217,23 @@ bool ModbusSerial::send(byte* frame) {
(*_port).flush();
delayMicroseconds(_t35);

if (this->_txPin >= 0) {
digitalWrite(this->_txPin, LOW);
if (this->_txPin1 >= 0) {
digitalWrite(this->_txPin1, LOW);
}
if (this->_txPin1 >= 0 && this->_txPin2 >= 0) {
digitalWrite(this->_txPin1, LOW);
digitalWrite(this->_txPin2, LOW);
}
}

bool ModbusSerial::sendPDU(byte* pduframe) {
if (this->_txPin >= 0) {
digitalWrite(this->_txPin, HIGH);
if (this->_txPin1 >= 0 && this->_txPin2 >= 0) {
digitalWrite(this->_txPin1, HIGH);
digitalWrite(this->_txPin2, HIGH);
delay(1);
}
if (this->_txPin1 >= 0) {
digitalWrite(this->_txPin1, HIGH);
delay(1);
}

Expand All @@ -156,8 +254,12 @@ bool ModbusSerial::sendPDU(byte* pduframe) {
(*_port).flush();
delayMicroseconds(_t35);

if (this->_txPin >= 0) {
digitalWrite(this->_txPin, LOW);
if (this->_txPin1 >= 0) {
digitalWrite(this->_txPin1, LOW);
}
if (this->_txPin1 >= 0 && this->_txPin2 >= 0) {
digitalWrite(this->_txPin1, LOW);
digitalWrite(this->_txPin2, LOW);
}
}

Expand Down Expand Up @@ -187,7 +289,7 @@ void ModbusSerial::task() {
_len = 0;
}

word ModbusSerial::calcCrc(byte address, byte* pduFrame, byte pduLen) {
/*word ModbusSerial::calcCrc(byte address, byte* pduFrame, byte pduLen) {
byte CRCHi = 0xFF, CRCLo = 0x0FF, Index;

Index = CRCHi ^ address;
Expand All @@ -201,9 +303,29 @@ word ModbusSerial::calcCrc(byte address, byte* pduFrame, byte pduLen) {
}

return (CRCHi << 8) | CRCLo;
}



}*/

word ModbusSerial::calcCrc(byte address, byte* pduFrame, byte pduLen) {
uint8_t j;
uint16_t crc;

crc = 0xFFFF;
crc = crc ^ address;
for (j=0; j < 8 ; j++){
if (crc & 0x0001)
crc = (crc >> 1) ^ 0xA001;
else
crc = crc >> 1;
}
while (pduLen--) {
crc = crc ^*pduFrame++;
for (j=0; j < 8 ; j++){
if (crc & 0x0001)
crc = (crc >> 1) ^ 0xA001;
else
crc = crc >> 1;
}
}
return (crc << 8 | crc >> 8);

}
Loading