From a8377b09943b2000d40e7e8e3a5ee02cf96c08ff Mon Sep 17 00:00:00 2001 From: Zifeng1997 <58804221+xzf89718@users.noreply.github.com> Date: Mon, 14 Nov 2022 17:18:02 +0800 Subject: [PATCH] CRC8 check update --- AHT20_sensor_control/AHT20_sensor_wrapper.py | 6 +-- AHT20_sensor_control/crc8_helper.py | 49 ++++++++++++++++++-- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/AHT20_sensor_control/AHT20_sensor_wrapper.py b/AHT20_sensor_control/AHT20_sensor_wrapper.py index 1774059..9a1517a 100644 --- a/AHT20_sensor_control/AHT20_sensor_wrapper.py +++ b/AHT20_sensor_control/AHT20_sensor_wrapper.py @@ -195,9 +195,9 @@ def CheckCRC(self): """ # Get all 6 bytes data and 1 bytes CRC code frame_for_CRC8_check = 0x00 - for i in range(0, 7): - frame_for_CRC8_check << 8 | self.all_data[i] - isCRCOK = AHT20_crc8_check(frame_for_CRC8_check) + # for i in range(0, 7): + # frame_for_CRC8_check << 8 | self.all_data[i] + isCRCOK = AHT20_crc8_check(self.all_data) if (isCRCOK): return AHT20Status.AHT20_CRCOK else: diff --git a/AHT20_sensor_control/crc8_helper.py b/AHT20_sensor_control/crc8_helper.py index 79b57cc..5401ee8 100644 --- a/AHT20_sensor_control/crc8_helper.py +++ b/AHT20_sensor_control/crc8_helper.py @@ -1,6 +1,49 @@ +# A total of 6 * 8 bits data need to check +N_DATA = 6 +# 1 * 8 bits CRC +N_CRC = 1 +# Initial value. Equal to bit negation the first data (status of AHT20) +INIT = 0xFF +# Useful value to help calculate +LAST_8_bit = 0xFF -def AHT20_crc8_check(int_data): + +# Devide number retrieve from CRC-8 MAXIM G(x) = x8 + x5 + x4 + 1 +CRC_DEVIDE_NUMBER = 0x131 + +# Data and CRC taken from AHT20, use this for testing? +TEST_DATA = [[28, 184, 245, 165, 156, 208, 163], [28, 185, 16, 149, 156, 83, 112], [ + 28, 184, 249, 85, 156, 114, 213], [28, 185, 9, 53, 156, 54, 45], [28, 185, 70, 117, 156, 189, 33] +, [28, 185, 64, 165, 156, 61, 209]] + + +def AHT20_crc8_check(all_data_int, init_value=0xFF): + """ + The input data shoule be: + Status Humidity0 Humidity1 Humidity2|Temperature0 Temperature1 Temperature2 CRCCode + In python's int64. + """ + # Preprocess all the data and CRCCode from AHT20 + DATA_FROM_AHT20 = 0x00 + # Preprocessing the first data (status) + DATA_FROM_AHT20 = DATA_FROM_AHT20 | all_data_int[0] + print(bin(DATA_FROM_AHT20)) + DATA_FROM_AHT20 = DATA_FROM_AHT20 ^ init_value + print(bin(DATA_FROM_AHT20)) + for i_data in range(1, N_DATA + N_CRC): + DATA_FROM_AHT20 = DATA_FROM_AHT20 << 8 | all_data_int[i_data] + print(bin(DATA_FROM_AHT20)) + mod_value = DATA_FROM_AHT20 % CRC_DEVIDE_NUMBER + print(mod_value) + if (mod_value == 0): + return True + else: + return False + + +def AHT20_crc8_calculate(all_data_int): pass -def AHT20_crc_calculate(int_data): - pass \ No newline at end of file +if __name__ == "__main__": + for data in TEST_DATA: + AHT20_crc8_check(data, INIT) \ No newline at end of file