You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ISO CAN-FD帧CRC计算异常:Numpy实现与CANalyzer结果不符

CAN-FD CRC计算异常问题

我正在开展ISO CAN-FD中CRC相关研究,在计算实际校验序列时遇到问题。

前期准备

我已仔细阅读规定计算方法的规范文档《ISO 11898-1:2015》。

测试帧与预期结果

我定义了一帧用于CRC计算,帧的比特序列中红色标记的0为填充位。该帧已加入CANalyzer配置,CAPL脚本输出的校验值为 0x6bd。

自定义Numpy实现及问题

我按照ISO文档描述用Numpy实现了CRC算法,但结果与CANalyzer返回值不符。我的实现代码如下(刚接触Numpy,写法可能不标准):

import numpy as np

def crc(data):
    gen_poly = np.array([1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1])
    crc_reg = np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

    for bit in data:
        crc_next = abs(bit - crc_reg[0])
        crc_reg = np.roll(crc_reg, -1)      # Shift one position left (1)
        crc_reg[-1] = 0                     # Shift one position left (2)
        if crc_next == 1:
            reg_new = abs(crc_reg - gen_poly)
            crc_reg = np.copy(reg_new)
    return crc_reg.copy()

尝试的修改方案

我曾猜测可能误解了文档,认为crc_reg和gen_poly应为18位,因此做了如下修改:

  • 帧尾添加18个0而非17个;
  • 生成多项式改为:gen_poly = np.array([1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1]);
  • CRC寄存器改为:crc_reg = np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])(共18位);

但修改后仍未解决问题,反复检查也无法定位错误。

补充输入输出数据

根据建议,提供输入数据和期望输出数组:

import numpy as np

input_data = np.array([
    0,  # SOF
    1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0,    # ID
    0,  # RTR
    0,  # IDE
    1,  # EDL
    0,  # RES
    1,  # BRS
    0,  # ESI
    1, 0, 0, 0,     # DLC
    1, 1, 1, 1, 1, 0, 1, 1, 1,          # Byte 1
    1, 1, 0, 1, 1, 1, 1, 1, 0, 1,       # Byte 2
    1, 1, 1, 1, 0, 1, 1, 1, 1,          # Byte 3
    1, 0, 1, 1, 1, 1, 1, 0, 1, 1,       # Byte 4
    1, 1, 1, 0, 1, 1, 1, 1, 1, 0,       # Byte 5
    1, 1, 1, 1, 1, 0, 1, 1, 1,          # Byte 6
    1, 1, 0, 1, 1, 1, 1, 1, 0, 1,       # Byte 7
    1, 1, 1, 1, 0, 1, 1, 1, 1,          # Byte 8
    1, 1, 0, 0,     # StuffCount + Parity
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0     # 17 bits at zero
])

expected_output = np.array([0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1])    # 0x6bd

内容的提问来源于stack exchange,提问作者Phil Snow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 08:35:28