def crc_cal(num, binary=True, bit_num=10): if binary: num = bit_str2num(num) byte_arr = bytearray(num.to_bytes(2, 'big')) crc = Crc4Itu.calc(byte_arr) if binary: return num2bin(crc, 4) else: return crc
def crc_validate(num, crc, binary=True, bit_num=10): if binary: num = bit_str2num(num) crc = bit_str2num(crc) hex_byte = bytes([crc]) byte_arr = bytearray(num.to_bytes(2, 'big')) + hex_byte new_crc = Crc4Itu.calc(byte_arr) if new_crc == 0: return True return False