CRC8 CAPL Script in CANOE not calculating CRC correctly

391 Views Asked by At

I have compiled a script in CAPL to operate in CANOE and it needs to calculate a CRC8 checksum. I have done this but it is calculating the wrong values.

This is the code provided by the plant to implement the CRC check

A CRC-8 calculation should be performed on bytes 1 to 7 of the CAN frames using the following
polynomial:
x^8 + x^4 + x^3 + X^2 + 1.
The following example is given to illustrate the calculation:
CRC Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Byte 6 Byte 7
(tbc) 0x5E 0x01 0x44 0xF1 0x10 0x11 0xFE
The following source code can be used to calculate the CRC value:
void crc8(char newByte, char * crc) {
int i;
*crc = *crc ^ newByte;
for(i=0; i < 8; i++){
*crc = (*crc & 0x80)? (*crc <<1) ^ POLYNOMIAL : (*crc <<1);
}
}

This is how it is implemented in my CAPL script

includes
{
}
variables
{
  byte crcTable[256];
  message CAN1. msg;
  int counter = 0;
  msTimer timer10; // Timer variable declaration
  byte buf[7]; // Declaration of buf variable
}
void initCRCJ1850()
{
  byte _crc;
  int i;
  int bit;
  for(i = 0 ; i < 0x100 ; i++)
  {
    _crc = i;
    for(bit = 0 ; bit < 8 ; bit++) _crc = (_crc & 0x80) ? ((_crc << 1) ^ 0x1D) : (_crc << 1);
    crcTable[i] = _crc;
  }
}
void rollOverCounter()
{
  if (counter < 255)
  {
    counter++;
  }
  else
  {
    counter = 0;
  }
}
byte CalcCRC(byte buf[], int len)
{
  byte crc;
  int i = 0;
  crc = 0xFF;
  for(i = 0 ; i < len ; i++)
  {
    crc = crcTable[crc ^ buf[i]];
  }
  return ~crc;
}
on start {
  setTimerCyclic(timer10, 10);
  Msg.Control_1_Alive = counter;
  initCRCJ1850();
}
on timer timer10
{
  rollOverCounter();
  
  Msg.Control_1_Alive = counter;

  buf[0] = Msg.byte(1);
  buf[1] = Msg.byte(2);
  buf[2] = Msg.byte(3);
  buf[3] = Msg.byte(4);
  buf[4] = Msg.byte(5);
  buf[5] = Msg.byte(6);
  buf[6] = Msg.byte(7);
  Msg.Control_1_CRC = CalcCRC(buf, 7);
  output(Msg); // Transmit the CAN message
}

The CRC check is being implemented but the wrong calculation is being performed it seems as i'm getting a CRC error from the plant. Any help would be greatly appreciated!

0

There are 0 best solutions below