LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

crc 8

Hi,
I am trying to calculate the crc for the following hex string:
61 6A F1 01 00
I know the crc should be 0A, but I have been unable to produce this value.  I was looking over the CRC subvi's in the Data_validating_functions.llb.  I understand that the for loop sets the current bit to zero and XOR the remainder to zero, but I am confused as to what the array outside the for loop is doing.  I don't understand why I am indexing this array with my data array.  I am using the "U8" string to byte array to feed my values into the CRC-8 subvi.  The polynomial expression I need to use is: x^8 + x^4 + x^3+ x^2 + 1. 
 
Thanks,
Adam
0 Kudos
Message 1 of 22
(21,216 Views)

Your algorithm appears to be all wrong to me, along with the values your using for the calculation.

Firstly, have a look at the following web page on CRC calculations to get a better idea of what your trying to achieve:

http://en.wikipedia.org/wiki/Cyclic_redundancy_check#The_mathematics_of_CRCs

Then, see the attached VI which I wrote to perform a CRC using a 12th order polynomial. Your version will need to be slightly different to this as you are wanting an 8th order polynomial.

 

0 Kudos
Message 2 of 22
(21,198 Views)

Hi Adam,

      Using your VI and the "rotate left" from xntricity's VI, I came up with a simple CRC VI the produces 0x0A from your test-data. Smiley Happy

I didn't understand how to use the polynomial - can you supply more info?

Cheers!

 

"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
0 Kudos
Message 3 of 22
(21,193 Views)
here is  one explaination, but in a nutshell, the exponents are the boolean taps to xor.


Paul
0 Kudos
Message 4 of 22
(21,151 Views)

Hi,

Thanks for the vi.  The program is supposed to send a string of hex commands to a OBDII port on a automobile.  The CRC has to be used because that is how GM checks the commands sent a received by the ECU.  I read through web page provide, and added some code to divide by the polynominal and came up with the smae value as tbd's vi.  The vi worked for one of the commands, but not for the rest.  I was unable to open the "Perfom CRC on 1D data array" vi becuase I am using LV 8.0.  I don't understand what is wrong, the math appears to be correct.  Here is the updated vi, and a list of commands including the correct CRC values.

Thanks,

Adam

Download All
0 Kudos
Message 5 of 22
(21,131 Views)

Hi,

I found some C code on-line which calculates the CRC.  Here is the code:

 

unsigned char
crc8(unsigned char *data,int length)
{
unsigned long crc;
int f,bit;

crc = 0xFF;
for ( f=0 ; f<length ; f++ ) {
  crc ^= data[f];
  for ( bit=0 ; bit<8 ; bit++ ) {
   if ( (crc&0x80)!=0 ) {
    crc <<= 1;
    crc ^= 0x1D;
   }
    else
      crc <<= 1;
   }
}
return (~crc)&0xFF;
}

//unsigned char pkt[] = {0x68,0x6A,0xF1,0x01,0x00};    // VPW
unsigned char pkt[] = {0x61,0x6A,0xF1,0x01,0x00};      // PWM

void main()
{
printf("0x%X\n", crc8((unsigned char*)pkt,5));
}

Here is some more information about CRC calculation I found on the same site:

  • Calulates the SAE-J1850 Cyclic Redundancy Check byte (CRC)
  • Note: The 0x11D constant used in the routine is derived from
  • the mandated CRC division polynomial of: x^8+x^4+X^3+X^2+1, which
  • corresponds to bit positions 8,4,3,2 and 0.
  • Those bit positions form the binary number: 100011101 which is $11D.
  • I believe that $1D could be used instead because we are only
  • generating an 8-bit CRC. I'm not sure why SAE mandated more than
  • 8 bits for the polynomial.

I am in the process of trying to port this code structure to labview.

Adam

0 Kudos
Message 6 of 22
(21,110 Views)

HI Adam,

I've attached an image of the VI I sent previously for you to have a look at. I think all you'll need to do is to change the 0f01 value used in the program with the value for your polynomial. The algorithm I used to develop this VI is as follows:

Begin

crc = 0

for each byte (in the data) do

crc=crc XOR data_byte

for i=1 to 8 do

crc_lsb = lsb of crc (lsb = Least significant bit)

crc=crc shifted right by 1 bit with zero msb

if crc_lsb = 1

crc=crc XOR 0x0f01

end if

end for

end for

End

 

Regards,

Andy

0 Kudos
Message 7 of 22
(21,099 Views)
Message 8 of 22
(21,098 Views)
0 Kudos
Message 9 of 22
(21,099 Views)

Hi,

Thanks for the pictures, but I am not able to open the vi you sent me because I have LV 8.0, and it was saved in LV 8.2.  Would it be possible to post the vi for LV 8.0?  Am I right in assuming the value of my polynomial to be 0x11D, from my prevoius post?

 

Thanks,

Adam

0 Kudos
Message 10 of 22
(21,089 Views)