本帖最后由 IFX_Lingling 于 2024-2-22 15:20 编辑
在Cypress的 .cyacd 文件中,每一行的校验和(checksum)是通过一个简单的过程计算出来的。校验和的计算过程如下:
1. 首先,把每一行的所有字节值加在一起,包括记录类型、数据长度、地址和数据本身。
2. 然后,取这个和的最后一个字节。也就是说,如果你的和是0x1234,那么你需要取的是0x34。
3. 接下来,取这个字节的二进制反码。二进制反码是通过把所有的1变为0,所有的0变为1得到的。例如,如果你的字节是10110011,那么它的二进制反码就是01001100。
4. 最后,把这个二进制反码加1。这就是你的校验和。
For example:
04C811931100
:00004D008000100020912600006D2800006D28000080B500AF024B83F3088800F0F1F8C0460010002080B500AF0D4B0D4A12688021C9020A431A60202000F09EF9094B0A4A1A600A4B802212061A60054B802212031A60074B00221A60C046BD4680BDC04600010B4004010B400600008008010B400C020B4080B582B000AF72B6FB1D002228
最后的0x28是checksum的值,下面是验证这个值的步骤:
1:第一步把这一行的数据变成16进制数组的形式:
data = [0x00,0x00,0x4D,0x00,0x80,0x00,0x10,0x00,0x20,0x91,0x26,0x00,0x00,0x6D,0x28,0x00,0x00,0x6D,0x28,0x00,0x00,0x80,0xB5,0x00,0xAF,0x02,0x4B,0x83,0xF3,0x08,0x88,0x00,0xF0,0xF1,0xF8,0xC0,0x46,0x00,0x10,0x00,0x20,0x80,0xB5,0x00,0xAF,0x0D,0x4B,0x0D,0x4A,0x12,0x68,0x80,0x21,0xC9,0x02,0x0A,0x43,0x1A,0x60,0x20,0x20,0x00,0xF0,0x9E,0xF9,0x09,0x4B,0x0A,0x4A,0x1A,0x60,0x0A,0x4B,0x80,0x22,0x12,0x06,0x1A,0x60,0x05,0x4B,0x80,0x22,0x12,0x03,0x1A,0x60,0x07,0x4B,0x00,0x22,0x1A,0x60,0xC0,0x46,0xBD,0x46,0x80,0xBD,0xC0,0x46,0x00,0x01,0x0B,0x40,0x04,0x01,0x0B,0x40,0x06,0x00,0x00,0x80,0x08,0x01,0x0B,0x40,0x0C,0x02,0x0B,0x40,0x80,0xB5,0x82,0xB0,0x00,0xAF,0x72,0xB6,0xFB,0x1D,0x00,0x22]
2:
在python3.11里面运行下面这段代码的输出是0x28。
def calculate_checksum(array):
sum = 0
for b in array:
sum += b
sum = sum & 0xFF
checksum = ((~sum + 1) & 0xFF)
return checksum
# 创建一个字节数组
data = [0x00,0x00,0x4D,0x00,0x80,0x00,0x10,0x00,0x20,0x91,0x26,0x00,0x00,0x6D,0x28,0x00,0x00,0x6D,0x28,0x00,0x00,0x80,0xB5,0x00,0xAF,0x02,0x4B,0x83,0xF3,0x08,0x88,0x00,0xF0,0xF1,0xF8,0xC0,0x46,0x00,0x10,0x00,0x20,0x80,0xB5,0x00,0xAF,0x0D,0x4B,0x0D,0x4A,0x12,0x68,0x80,0x21,0xC9,0x02,0x0A,0x43,0x1A,0x60,0x20,0x20,0x00,0xF0,0x9E,0xF9,0x09,0x4B,0x0A,0x4A,0x1A,0x60,0x0A,0x4B,0x80,0x22,0x12,0x06,0x1A,0x60,0x05,0x4B,0x80,0x22,0x12,0x03,0x1A,0x60,0x07,0x4B,0x00,0x22,0x1A,0x60,0xC0,0x46,0xBD,0x46,0x80,0xBD,0xC0,0x46,0x00,0x01,0x0B,0x40,0x04,0x01,0x0B,0x40,0x06,0x00,0x00,0x80,0x08,0x01,0x0B,0x40,0x0C,0x02,0x0B,0x40,0x80,0xB5,0x82,0xB0,0x00,0xAF,0x72,0xB6,0xFB,0x1D,0x00,0x22]
# 计算校验和
checksum = calculate_checksum(data)
# 打印校验和
print('The checksum is: ', hex(checksum))
Python3.11运行结果:
>>> def calculate_checksum(array):
... sum = 0
... for b in array:
... sum += b
... sum = sum & 0xFF
... checksum = ((~sum + 1) & 0xFF)
... return checksum
...
>>> # 创建一个字节数组
>>> data = [0x00,0x00,0x4D,0x00,0x80,0x00,0x10,0x00,0x20,0x91,0x26,0x00,0x00,0x6D,0x28,0x00,0x00,0x6D,0x28,0x00,0x00,0x80,0xB5,0x00,0xAF,0x02,0x4B,0x83,0xF3,0x08,0x88,0x00,0xF0,0xF1,0xF8,0xC0,0x46,0x00,0x10,0x00,0x20,0x80,0xB5,0x00,0xAF,0x0D,0x4B,0x0D,0x4A,0x12,0x68,0x80,0x21,0xC9,0x02,0x0A,0x43,0x1A,0x60,0x20,0x20,0x00,0xF0,0x9E,0xF9,0x09,0x4B,0x0A,0x4A,0x1A,0x60,0x0A,0x4B,0x80,0x22,0x12,0x06,0x1A,0x60,0x05,0x4B,0x80,0x22,0x12,0x03,0x1A,0x60,0x07,0x4B,0x00,0x22,0x1A,0x60,0xC0,0x46,0xBD,0x46,0x80,0xBD,0xC0,0x46,0x00,0x01,0x0B,0x40,0x04,0x01,0x0B,0x40,0x06,0x00,0x00,0x80,0x08,0x01,0x0B,0x40,0x0C,0x02,0x0B,0x40,0x80,0xB5,0x82,0xB0,0x00,0xAF,0x72,0xB6,0xFB,0x1D,0x00,0x22]
>>>
>>> # 计算校验和
>>> checksum = calculate_checksum(data)
>>>
>>> # 打印校验和
>>> print('The checksum is: ', hex(checksum))
The checksum is: 0x28
>>>
|
|