当前位置:   article > 正文

2021-08-28 各种 hex 校验 ,加总异或校验,总加和校验 ,16 CRC 低字节在前,16 CRC 高字节在前,8 CRC,32 CRC 低字节在前,32 CRC 高字节在前_16crc,低字节在前

16crc,低字节在前
def uchar_checksum_CRC(input_str, byteorder='little'):
    """
    总加和校验
    @param input_str: 总加和校验
    @param byteorder: 大/小端
    """
    try:
        data = hex_str(input_str)
        length = len(data)
        checksum = 0
        for i in range(0, length):
            checksum += int.from_bytes(data[i:i + 1], byteorder, signed=False)
            checksum &= 0xFF  # 强制截断

        x_str = input_str + " " + str(('%0#4x' % (checksum,))[2:])
        return x_str.upper()
    except Exception as e:
        print('uchar_checksum_CRC: ',e)
        return None



def calc_chksums_CRC(input_s_):
    """ 加总异或校验 """
    try:
        checksum = 0
        data = hex_str(input_s_)
        for el in data:
            checksum ^= (el)
            #checksum ^= ord(el)
        # print (checksum, hex(checksum), chr(checksum))
        R_data = input_s_ + ' ' + str(('%0#4x' % (checksum,))[2:])
        return R_data.upper()

    except Exception as e:
        print("calc_chksums_CRC: ",e)
        return None



def crc16_modbus_crc(s):
    """ 16 CRC 低字节在前  CRC16/MODBUS"""

    def get_crc_value(s, crc16):
        """ 16 CRC 低字节在前  CRC16/MODBUS """
        try:
            data = s.replace(' ', '')
            crc_out = hex(crc16(unhexlify(data))).upper()
            str_list = list(crc_out)
            if len(str_list) == 5:
                str_list.insert(2, '0')  # 位数不足补0
            crc_data=''.join(str_list[2:]).upper()
            # crc_data[2:] + ' ' + crc_data[:2]
            return crc_data[2:] + crc_data[:2]
        except Exception as e:
            print('get_crc_value: ', e)
            return None
    try:
        crc16 = mkCrcFun(0x18005, rev=True, initCrc=0xFFFF, xorOut=0x0000)
        #return get_crc_value(s, crc16)
        data_r = s + " " + get_crc_value(s, crc16)
        return data_r.upper()
    except Exception as e:
        print('crc16_modbus_crc: ',e)
        return None



def crc16_modbus_crc_high(s):
    """ 16 CRC 高字节在前  CRC16/MODBUS"""
    def get_crc_value_high(s, crc16):
        """ 16 CRC 高字节在前 """
        try:
            data=s.replace(' ', '')
            crc_out=hex(crc16(unhexlify(data))).upper()
            str_list=list(crc_out)
            if len(str_list) == 5:
                str_list.insert(2, '0')  # 位数不足补0
            crc_data=''.join(str_list[2:]).upper()
            # crc_data[2:] + ' ' + crc_data[:2]
            return crc_data[:2] + crc_data[2:]
        except Exception as e:
            print('get_crc_value_high: ', e)
            return None
    try:
        crc16 = mkCrcFun(0x18005, rev=True, initCrc=0xFFFF, xorOut=0x0000)
        # return get_crc_value_high(s, crc16)
        data_r = s + " " + get_crc_value_high(s, crc16)
        return data_r.upper()
    except Exception as e:
        print('crc16_modbus_crc_high: ',e)
        return None



def crc_8_crc(CRC_8_str):
    """ 8 CRC CRC-8校验  # CRC-8 ,CRC-8/ITU, CRC-8/ROHC,CRC-8/MAXIM """
    try:
        crc8 = crcmod.predefined.Crc('crc-8')
        crc8.update(bytes().fromhex(CRC_8_str))
        #return (hex(crc8.crcValue))
        data_r = CRC_8_str + " " + str(('%0#4x' % (crc8.crcValue,))[2:]) #(hex(crc8.crcValue))
        return data_r.upper()
    except Exception as e:
        print('crc_8_crc: ',e)
        return None

def crc_32_high_byte_crc(CRC_32_str):
    """ CRC-32高字节在前 ,CRC-8/ITU, CRC-8/ROHC,CRC-8/MAXIM """
    try:
        crc32 = crcmod.predefined.Crc('crc-32')
        crc32.update(bytes().fromhex(CRC_32_str))
        data_r = CRC_32_str + " " + str(('%0#10x' % (crc32.crcValue,))[2:]) #(hex(crc8.crcValue))
        return data_r.upper()
    except Exception as e:
        print('crc_32_high_byte_crc: ',e)
        return None


def crc_32_low_byte_crc(CRC_32_str):
    """ CRC-32低字节在前  ,CRC-8/ITU, CRC-8/ROHC,CRC-8/MAXIM """
    try:
        crc32 = crcmod.predefined.Crc('crc-32')
        crc32.update(bytes().fromhex(CRC_32_str))
        crc32_s = str(('%0#10x' % (crc32.crcValue,))[2:])  #D88D2B5E
        crc32_str = (crc32_s[-2:]) + (crc32_s[4:6])  + (crc32_s[2:4]) + (crc32_s[0:2])
        data_r = CRC_32_str + " " + crc32_str  #(hex(crc8.crcValue))
        return data_r.upper()
    except Exception as e:
        print('crc_32_low_byte_crc: ',e)
        return None



def hex_str(input_str):
    """# hex发送"""
    try:
        input_s = input_str.strip()
        send_list = []
        while input_s != '':
            try:
                num = int(input_s[0:2], 16)
            except ValueError:
                return None
            input_s = input_s[2:].strip()
            send_list.append(num)
        input_s_2 = bytes(send_list)
        # print(input_s_2)
        return input_s_2
    except Exception as e:
        print('hex_str: ',e)
        return None

if __name__ == '__main__':
    # http://www.metools.info/code/c15.html 在线编码网站
    input_s_3 = 'FA 06 D0 20 01 06 00 00 F7 EE DC B8 70 E0 C0 80'
    print("加总异或校验: ",calc_chksums_CRC(input_s_3))
    print("总加和校验: ",uchar_checksum_CRC(input_s_3))
    print("16 CRC 低字节在前: ",crc16_modbus_crc(input_s_3))
    print("16 CRC 高字节在前: ",crc16_modbus_crc_high(input_s_3))
    print("8 CRC: ",crc_8_crc(input_s_3))
    print("32 CRC 低字节在前: ", crc_32_low_byte_crc(input_s_3))
    print("32 CRC 高字节在前: ", crc_32_high_byte_crc(input_s_3))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/113002
推荐阅读
相关标签
  

闽ICP备14008679号