Simplified BCC Checksum calculation
This commit is contained in:
		@@ -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("<STX>");
 | 
			
		||||
      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 <BCC>: ");
 | 
			
		||||
      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;
 | 
			
		||||
    }
 | 
			
		||||
    Serial.write(r);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user