# Calculating CRC

## Recommended Posts

Hi

I have this code in C(and I don't understand it  )

Is there someone that can help me translate it to labview?

Bjarne

typedef uint16_t (*bit_order_16)(uint16_t);

typedef uint8_t (*bit_order_8)(uint8_t);

uint16_t straight_16(uint16_t value)

{

return value;

}

uint16_t reverse_16(uint16_t value)

{

uint16_t reversed = 0;

for (int i = 0; i < 16; ++i)

{

reversed <<= 1;

reversed |= value & 0x1;

value >>= 1;

}

return reversed;

}

uint8_t straight_8(uint8_t value)

{

return value;

}

uint8_t reverse_8(uint8_t value)

{

uint8_t reversed = 0;

for (int i = 0; i < 8; ++i)

{

reversed <<= 1;

reversed |= value & 0x1;

value >>= 1;

}

return reversed;

}

uint16_t crc16(uint8_t const *message, int nBytes,

bit_order_8 data_order, bit_order_16 remainder_order,

uint16_t remainder, uint16_t polynomial)

{

for (int byte = 0; byte < nBytes; ++byte)

{

remainder ^= (data_order(message[byte]) << 8);

for (uint8_t bit = 8; bit > 0; --bit)

{

if (remainder & 0x8000)

{

remainder = (remainder << 1) ^ polynomial;

}

else

{

remainder = (remainder << 1);

}

}

}

return remainder_order(remainder);

}

uint16_t crc16ss(uint8_t const *message, int nBytes, uint16_t initial, uint16_t poly)

{

return crc16(message, nBytes, straight_8, straight_16, initial, poly);

}

uint16_t crc16rr(uint8_t const *message, int nBytes, uint16_t initial, uint16_t poly)

{

return crc16(message, nBytes, reverse_8, reverse_16, initial, poly);

}

uint16_t crc16rs(uint8_t const *message, int nBytes, uint16_t initial, uint16_t poly)

{

return crc16(message, nBytes, reverse_8, straight_16, initial, poly);

}

##### Share on other sites

I would Google â€œCRC16 LabVIEWâ€ first, if I were you, and see if you can find it already done for you.

##### Share on other sites

Hi

I have done that and can't find any CRC calculator that give the same result

I got these data:

0x00, 0x01, 0x02, 0x80, 0xe2, 0x45, 0x00, 0x00, 0x00, 0x00, 0x07, 0xdf, 0x33, 0x00, 0x00, 0x00, 0x0f, 0xeb, 0x02, 0x80, 0xe2, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

and expect this CRC16: 0xcd58

regards Bjarne

Edited by Bjarne Joergensen
##### Share on other sites

Hi

I have done that and can't find any CRC calculator that give the same result

I got these data:

0x00, 0x01, 0x02, 0x80, 0xe2, 0x45, 0x00, 0x00, 0x00, 0x00, 0x07, 0xdf, 0x33, 0x00, 0x00, 0x00, 0x0f, 0xeb, 0x02, 0x80, 0xe2, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

and expect this CRC16: 0xcd58

regards Bjarne

If you can't give me some numbers I can put straight into LabVIEW, I'm not going to even try. You also haven't told us what the polynomial is so we don't know which flavour of CRC it actually is.

Google should have told you that the code is 3/5ths of "this one"

Once you know which flavour just put LabVIEW in front of your search term (e.g. LabVIEW CRC16 xmodem) and you will find plenty of pre-written VIs like "The Inline CRC Reference Library",

##### Share on other sites

Hi ShaunR

-       polynomium 0x755b

-       initial_crc 0xffff

-       reversering af 8-bit input data

-       reversering af 16-bit output crc

##### Share on other sites

Here's something that doesn't work. Maybe someone can spot the bug

Watch out for the OpenG trim whitespace!

crc16.vi

##### Share on other sites

I don't see any issues with the algo at first glance. You did say it used bit reverse (the two other functions) and I don't see you doing that, however.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×