CheckSum位求和校验算法

1.Checksum定义


通过将用CAN ID和数据字段计算的值添加到发送数据帧的数据字段中,接收ECU监测接收数据帧的有效性。

  a. Checksum shall be stored to 4 bits (bit 3 to bit 0) of the data field last byte. 
  
  b.  The checksum calculation method shall conform to the following. (Refer to Figure 3.2.) - Checksum that the sum total of all data (excluding checksum) of ID and data field by nibble becomes 0. - All additions for each nibble shall be unsigned calculation. If the calculation result exceeds the nibble, the lower 4 bits shall be used in the next calculation. - The ID of the base format (11 bit ID) shall be calculated as 3 nibbles, where the 12th bit of the ID is virtually expanded to '1' for making 12 bits. - The ID of the extended format (29 bit ID) shall be calculated as 8 nibbles, where the 30th, 31st, and 32nd bits of the ID are virtually expanded to '1' for making 32 bits.
  
  c.  When receiving a checksum added data frame, checksum of the received data frame shall be calculated with the method same as the time of transmission (refer to HFCAN_0158), and if the added value and the calculation result are different, it shall be judged as abnormal. NOTE:According to usage of the data frame signal, it shall be acceptable that the reception ECU does not monitor checksum. Whether or not to monitor and processing when judged as abnormal shall conform to Option sheet.
  
  d. The data frame to which checksum is added shall conform to Option sheet.

在这里插入图片描述

2.需要校验的数据


把需要校验数据的每4个bit相加

a)首先计算 Message ID 的字节(扩展帧4个字节,标准帧2个字节),先高字节后低字节。
b)其次计算在总线上发送的需要保护的信号/信号组, 比如DLC长度为8的Msg,需要计算第一个byte到第7个byte的CRC8, 第8个byte的高4个bit, 根据CAN矩阵的Msg的信号分布,如图1

@[TOC](3.checksum 算法)—基于CANOE的CAPL脚本

byte Calc_checksum(message * msg)
{
int i;
byte sum = 0x00;
byte checksum = 0x00;

if (isExtId(msg.id)) {

//扩展帧4个字节中的1~29bit的每4个Bit相加,同时加上0x0E (第30,31,32个bit置1)

sum = 0x0E;
sum += (msg.id >> 28) & 0x01;
sum += (msg.id >> 24) & 0x0F;
sum += (msg.id >> 20) & 0x0F;
sum += (msg.id >> 16) & 0x0F;
sum += (msg.id >> 12) & 0x0F;
sum += (msg.id >>  8) & 0x0F;
sum += (msg.id >>  4) & 0x0F;
sum += msg.id & 0x0F;

} else {
//标准帧4个字节中的1~11bit的每4个Bit相加,同时加上0x08 (第12个bit置1)

sum = 0x08;
sum += (msg.id >>  8) & 0x0F;
sum += (msg.id >>  4) & 0x0F;
sum += msg.id & 0x0F;

}

for (i = 0; i <= msg.dlc - 2; i++)
{
// DLC长度为8的Msg,需要计算第一个byte到第7个byte的CRC, 以及第8个byte的高4位bit;每4个bit相加,从第一个byte的高4bit

sum += (msg.byte(i) >> 4) & 0x0F;
sum += msg.byte(i) & 0x0F;

}
sum += (msg.byte(msg.dlc - 1) >> 4) & 0x0F;
sum = sum & 0x0F;
checksum = (0x10 - sum) & 0x0F;
return checksum;
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值