Sign in to follow this  
Followers 0
vinny

CRC 8 Calculator

3 posts in this topic

Hi I am fairly new to TWINCAT and am trying to translate a bit of code for a checksum CRC8 calculator. I have the following code, which I think is in C# (which I have no experience in whatsoever) unsigned char CalcCRC8(unsigned char *Data) { unsigned char LoopCntr; unsigned char A; unsigned char B; unsigned char CRC8 = 0; B = 0; while (*(Data + B)){ A = *(Data + B); for (LoopCntr = 0; LoopCntr < 8; LoopCntr++, A >>= 1){ // 8 bit loop if ((A ^ CRC8) & 0x01){ // test bit 0 of (Data XOR CRC8) CRC8 ^= 0x18; // toggle bits 3 and 4 of CRC8 CRC8 >>= 1; // rotate right CRC8, 1 time CRC8 |= 0x80; // set bit 7 of CRC8 } else { CRC8 >>= 1; // rotate right CRC8, 1 time } } B++; } return CRC8; // return CRC8 } I would be most grateful if someone could translate it for me! Thank you very much

Share this post


Link to post
Share on other sites
Here's one version. In that code of yours, the function reads the data with the "data" pointer one byte at a time and does the CRC check until it hits 0x00 byte, which would be the end of the string. Also i do not know C#, but what i fould out is that there is no unsigned char in C#, rather you're supposed to use byte, so my version uses byte. Also there is no rotate in C# according to what i found, but the >> is a shift, so that comment in the code is not true. Also instead of using the pointer way of doing it, the data is passed to this function as a parameter (in this example, it's a byte array from 0 to 2, giving 3 bytes). You can change that however you need. You maybe should also rename the variables better than A and B, i just did that, so you could better compare the C# code and the ST code. Hopefully you have some way of verifying the result. I just quickly checked that it does what the comments say, but there could be bug(s). Here's your code with my added comments: unsigned char CalcCRC8(unsigned char *Data) { unsigned char LoopCntr; unsigned char A; unsigned char B; unsigned char CRC8 = 0; B = 0; // B is the index for pointing to new byte while (*(Data + B)){ // while pointer points to data that isn't 0x00, which would mark the end of the "string" A = *(Data + B); // copy the data to A for (LoopCntr = 0; LoopCntr < 8; LoopCntr++, A >>= 1){ // 8 bit loop if ((A ^ CRC8) & 0x01){ // test bit 0 of (Data XOR CRC8) CRC8 ^= 0x18; // toggle bits 3 and 4 of CRC8 CRC8 >>= 1; // rotate right CRC8, 1 time CRC8 |= 0x80; // set bit 7 of CRC8 } else { CRC8 >>= 1; // rotate right CRC8, 1 time } } B++; // Byte handled, increment index for next byte } return CRC8; // return CRC8 } Here's the code for twincat: FUNCTION CRC8 : BYTE VAR_INPUT Data : ARRAY[0..2] OF BYTE; END_VAR VAR B : BYTE; (* data index *) LoopCntr: BYTE; (* Bit loop counter *) A: BYTE; (* Temporary data byte *) END_VAR CRC8:= 0; (* reset CRC *) B:= 0; (* Reset index *) WHILE ( B < 3 ) DO (* while not end of data *) A:= data[B]; (* Read data to A *) LoopCntr:= 0; (* Reset loop counter *) WHILE ( LoopCntr < 8 ) DO (* 8 bit loop *) IF (((A XOR CRC8) AND 16#01) > 0) THEN (* test bit 0 OF (Data XOR CRC8) *) CRC8:= CRC8 XOR 16#18; (* toggle bits 3 AND 4 OF CRC8 *) CRC8:= SHR(CRC8, 1); (* shift RIGHT CRC8, 1 TIME *) CRC8:= CRC8 OR 16#80; (* set bit 7 OF CRC8 *) ELSE CRC8:= SHR(CRC8, 1); (* shift right CRC8, 1 time *) END_IF; LoopCntr:= LoopCntr + 1; (* Increase loop counter for next bit *) A:= SHR(A,1); (* Shift data 1 bit right *) END_WHILE; B:= B + 1; (* Increment index for next byte *) END_WHILE; Edited by JouniK
1 person likes this

Share this post


Link to post
Share on other sites
You Sir are a Gentleman and a Scholar! Yeah that code was provided in a fairly useless manual and I couldn't get my head around the source but thank you so much for doing what you have! In ST I can at least make sense of what is happening and try to solve the issue.. And yes I do have a few commands and their predefined crc8 values which i can refer against. If you are ever in England, I owe you a pint of beer! Thanks again!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0