CANoe CAPL 之 Checksum and Counter

参考文献:

1、CANoe帮助文档:

CAPL Functions » CANoe IL » applILTxPending

Example

Calculation of a checksum and a message counter:

dword applILTxPending (long aId, dword aDlc, byte data[])
{
  dword i;
  byte xor;
  
  /* Message 0x1A0 contains a XOR checksum in Byte 0. 
  /* Message 0x1A1 contains a 4-Bit message counter in the first 4 Bits of Byte 7.
  /* It will be calculated from the other data bytes of the message.
  */
  
  if(aId == 0x1A0)
  {
    // calculate checksum
    xor = 0x00;
    for(i = 1; i < aDlc; ++i) {
      xor = xor ^ data[i];
    }
    // set the new checksum
    data[0] = xor;

    // get the old counter value
    i = (data[7] & 0xF0) >> 4;
    data[7] &= 0x0F;
    // increment
    i++;
    i = i % 16;
    //set the new message counter
    data[7] |= (i << 4) & 0xF0;
  }
  return 1; // don't prevent sending of the message
}
原文地址:https://www.cnblogs.com/zinthewind/p/14389884.html