BCC Checksum calculation
This commit is contained in:
parent
f2b578a708
commit
dd7e55d7a6
@ -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() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
ArduinoOTA.handle();
|
||||
|
||||
|
||||
@ -88,19 +97,34 @@ void loop() {
|
||||
|
||||
if (r == 0x02) {
|
||||
Serial.print("<STX>");
|
||||
firstBCCbyte = true;
|
||||
uint8_t localBCC = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (r == 0x03) {
|
||||
Serial.println("<ETX>");
|
||||
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("Changing back baudrate to 300");
|
||||
Serial2.updateBaudRate(300);
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate BCC checksum
|
||||
if (firstBCCbyte) {
|
||||
firstBCCbyte = false;
|
||||
localBCC = r ^ 0xFF;
|
||||
} else {
|
||||
localBCC = localBCC ^ r;
|
||||
}
|
||||
Serial.write(r);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user