打印
[PSoC™]

用Python验证PSoC Creator生成的cyacd每行的checksum

[复制链接]
798|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
IFX_Lingling|  楼主 | 2024-2-22 15:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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
>>>



使用特权

评论回复
沙发
呐咯密密| | 2024-2-22 15:08 | 只看该作者
过程讲解很详细,感谢

使用特权

评论回复
板凳
IFX-WellinZ| | 2024-2-28 17:30 | 只看该作者
很棒!讲的非常清楚了

使用特权

评论回复
地板
ClarkLLOTP| | 2024-2-28 17:45 | 只看该作者
这个是干嘛用的啊

使用特权

评论回复
5
IFX_Lingling|  楼主 | 2024-9-12 09:24 | 只看该作者
ClarkLLOTP 发表于 2024-2-28 17:45
这个是干嘛用的啊

这个主要是为了验证一下checksum的计算,同时提供了计算checksum的方法。

使用特权

评论回复
6
suncat0504| | 2024-9-15 15:39 | 只看该作者
好家伙,用MicroPython编程,确实方便啊

使用特权

评论回复
7
shenxiaolin| | 2024-9-20 18:32 | 只看该作者
有个算法就好了,

使用特权

评论回复
8
szt1993| | 2024-9-24 12:30 | 只看该作者
校验和(checksum)是通过一个简单的过程计算出来

使用特权

评论回复
9
OKAKAKO| | 2024-9-28 16:37 | 只看该作者
校验和是通讯经常使用的一种手段

使用特权

评论回复
10
AdaMaYun| | 2024-9-30 11:26 | 只看该作者
checksum现在又硬件校验更方便准确

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:
简介:关于英飞凌——我们致力于打造一个更加便利、安全和环保的世界,在赢得自身成功发展的同时,积极践行企业社会责任。

105

主题

189

帖子

5

粉丝