Simplified BCC Checksum calculation

This commit is contained in:
Petr Kracík 2019-06-12 11:23:03 +02:00
parent dd7e55d7a6
commit 14eb4d3308

View File

@ -84,8 +84,7 @@ void setup() {
// XOR this value with the second byte, and so forth up to and including the last byte (include 0x03 ETX !!), // XOR this value with the second byte, and so forth up to and including the last byte (include 0x03 ETX !!),
// and XOR the final value with 0xff. // and XOR the final value with 0xff.
bool firstBCCbyte = false; uint8_t localBCC;
uint8_t localBCC = 0;
void loop() { void loop() {
@ -97,8 +96,7 @@ void loop() {
if (r == 0x02) { if (r == 0x02) {
Serial.print("<STX>"); Serial.print("<STX>");
firstBCCbyte = true; localBCC = 0xFF; // Set checksum to 0xFF (first byte is XORed with 0xFF)
uint8_t localBCC = 0;
return; return;
} }
@ -108,8 +106,8 @@ void loop() {
Serial.print(Serial2.read()); Serial.print(Serial2.read());
Serial.println(); Serial.println();
Serial.print("Calculated <BCC>: "); Serial.print("Calculated <BCC>: ");
localBCC = localBCC ^ 0x03; localBCC = localBCC ^ r; // Include last byte (ETX) to checksum
localBCC = localBCC ^ 0xFF; localBCC = localBCC ^ 0xFF; // Final value XORed with 0xFF
Serial.print(localBCC); Serial.print(localBCC);
Serial.println(); Serial.println();
Serial.println(); Serial.println();
@ -119,12 +117,7 @@ void loop() {
} }
// Calculate BCC checksum // Calculate BCC checksum
if (firstBCCbyte) {
firstBCCbyte = false;
localBCC = r ^ 0xFF;
} else {
localBCC = localBCC ^ r; localBCC = localBCC ^ r;
}
Serial.write(r); Serial.write(r);
} }