From ac9508d5a39b1ca5a9df5a73d759b79f81de3133 Mon Sep 17 00:00:00 2001 From: JK-Rolling <89238608+JK-Rolling@users.noreply.github.com> Date: Sun, 10 Oct 2021 11:57:46 +0800 Subject: [PATCH] add CRC8 checksum --- .../DuinoCoin_Utils.ino | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/DuinoCoin_RPI_Arduino_Slave/DuinoCoin_Utils.ino b/DuinoCoin_RPI_Arduino_Slave/DuinoCoin_Utils.ino index fa20c8d..09555d5 100644 --- a/DuinoCoin_RPI_Arduino_Slave/DuinoCoin_Utils.ino +++ b/DuinoCoin_RPI_Arduino_Slave/DuinoCoin_Utils.ino @@ -104,3 +104,27 @@ bool check(String message) } return false; } + +// https://stackoverflow.com/questions/51731313/cross-platform-crc8-function-c-and-python-parity-check +uint8_t crc8( uint8_t *addr, uint8_t len) { + uint8_t crc=0; + for (uint8_t i=0; i>= 1; + if (mix) + crc ^= 0x8C; + inbyte >>= 1; + } + } + return crc; +} + +uint8_t str_crc8( String msg ) +{ + int msg_len = msg.length() + 1; + char char_array[msg_len]; + msg.toCharArray(char_array, msg_len); + return crc8(char_array,msg.length()); +}