From 14eb4d330810e6a94f5a1229bfcb8448ba979eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Krac=C3=ADk?= Date: Wed, 12 Jun 2019 11:23:03 +0200 Subject: [PATCH] Simplified BCC Checksum calculation --- ZPA_elektromer_UARTtest.ino | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/ZPA_elektromer_UARTtest.ino b/ZPA_elektromer_UARTtest.ino index f4fc101..3682722 100644 --- a/ZPA_elektromer_UARTtest.ino +++ b/ZPA_elektromer_UARTtest.ino @@ -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 !!), // and XOR the final value with 0xff. -bool firstBCCbyte = false; -uint8_t localBCC = 0; +uint8_t localBCC; void loop() { @@ -97,8 +96,7 @@ void loop() { if (r == 0x02) { Serial.print(""); - firstBCCbyte = true; - uint8_t localBCC = 0; + localBCC = 0xFF; // Set checksum to 0xFF (first byte is XORed with 0xFF) return; } @@ -108,8 +106,8 @@ void loop() { Serial.print(Serial2.read()); Serial.println(); Serial.print("Calculated : "); - localBCC = localBCC ^ 0x03; - localBCC = localBCC ^ 0xFF; + localBCC = localBCC ^ r; // Include last byte (ETX) to checksum + localBCC = localBCC ^ 0xFF; // Final value XORed with 0xFF Serial.print(localBCC); Serial.println(); Serial.println(); @@ -119,12 +117,7 @@ void loop() { } // Calculate BCC checksum - if (firstBCCbyte) { - firstBCCbyte = false; - localBCC = r ^ 0xFF; - } else { - localBCC = localBCC ^ r; - } + localBCC = localBCC ^ r; Serial.write(r); }