BCC Checksum calculation
This commit is contained in:
		@@ -78,8 +78,17 @@ void setup() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// BCC is calculated as follows (source https://github.com/lvzon/dsmr-p1-parser/blob/master/doc/IEC-62056-21-notes.md):
 | 
				
			||||||
 | 
					// The BCC is calculated over the bytes after STX up to and including the ETX byte
 | 
				
			||||||
 | 
					// To calculate this BCC, take the first byte XOR 0xff
 | 
				
			||||||
 | 
					// 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;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void loop() {
 | 
					void loop() {
 | 
				
			||||||
  // put your main code here, to run repeatedly:
 | 
					
 | 
				
			||||||
  ArduinoOTA.handle();
 | 
					  ArduinoOTA.handle();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -88,19 +97,34 @@ void loop() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if (r == 0x02) {
 | 
					    if (r == 0x02) {
 | 
				
			||||||
      Serial.print("<STX>");
 | 
					      Serial.print("<STX>");
 | 
				
			||||||
 | 
					      firstBCCbyte = true;
 | 
				
			||||||
 | 
					      uint8_t localBCC = 0;
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (r == 0x03) {
 | 
					    if (r == 0x03) {
 | 
				
			||||||
      Serial.println("<ETX>");
 | 
					      Serial.println("<ETX>");
 | 
				
			||||||
      Serial.print("Checksum <BCC>: ");
 | 
					      Serial.print("Checksum <BCC>: ");
 | 
				
			||||||
      Serial.println(Serial2.read());
 | 
					      Serial.print(Serial2.read());
 | 
				
			||||||
 | 
					      Serial.println();
 | 
				
			||||||
 | 
					      Serial.print("Calculated <BCC>: ");
 | 
				
			||||||
 | 
					      localBCC = localBCC ^ 0x03;
 | 
				
			||||||
 | 
					      localBCC = localBCC ^ 0xFF;
 | 
				
			||||||
 | 
					      Serial.print(localBCC);
 | 
				
			||||||
 | 
					      Serial.println();
 | 
				
			||||||
      Serial.println();
 | 
					      Serial.println();
 | 
				
			||||||
      Serial.println("Changing back baudrate to 300");
 | 
					      Serial.println("Changing back baudrate to 300");
 | 
				
			||||||
      Serial2.updateBaudRate(300);
 | 
					      Serial2.updateBaudRate(300);
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Calculate BCC checksum
 | 
				
			||||||
 | 
					    if (firstBCCbyte) {
 | 
				
			||||||
 | 
					      firstBCCbyte = false;
 | 
				
			||||||
 | 
					      localBCC = r ^ 0xFF;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      localBCC = localBCC ^ r;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    Serial.write(r);
 | 
					    Serial.write(r);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user