LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

CRC-16 CCITT Calculation

Solved!
Go to solution

Turn it into a subVI and call it.  Pass in whatever data you need to into it.  It is just like reusing any other piece of code.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 21 of 30
(5,658 Views)

Old thread, I know, but I'm looking at a similar problem...I need to turn the C routine below into a subvi. I've tried many different variations of many different solutions and cannot get the CRC shown in below to match. Does this make any sense to anyone here?

 

CRC is a 16-bit CRC of sync, address, command type, length, and data fields. The algorithm used is CCITT-16 (0x1021 or 0x8408 reversed).The remainder is initialized to all 1’s (0xFFFF) and the CRC is inverted before being sent, LSB first.  (NOTE: Previous versions of this document specified MSB first, but actual implementations have been using LSB-first.)  Following is a sample C routine which implements the algorithm using the “reversed” technique:

 

WORD crc16_lsb(BYTE *pData, WORD length)

{

   BYTE i;

   WORD data, crc;

   crc = 0xFFFF;

 

   if (length == 0)

      return 0;

  

   do

   {

      data = (WORD)0x00FF & *pData++;

      crc = crc ^ data;

 

      for (i = 8; i > 0; i--)

      {

         if (crc & 0x0001)

            crc = (crc >> 1) ^ 0x8408;

         else

            crc >>= 1;

      }

   }

   while (--length);

 

   crc = ~crc;

   return (crc);

}

 

For example, a Status Request command:

      1b ff 91 00

Would generate the following CRC:

      1c 39

0 Kudos
Message 22 of 30
(5,215 Views)
Solution
Accepted by wiebe@CARYA

With the right parameters, this works perfectly with my standard CRC calculator.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 23 of 30
(5,190 Views)

Thanks, crossrulz. You're right, it works exactly as it should. You da man!

0 Kudos
Message 24 of 30
(5,179 Views)

Hi,

I noticed that the marked solution is not the correct one, at least for me. If it is so for others as well, could someone please mark the correct reply as the solution? It is a bit misleading as it is.

So the correct one according to my tests iiis:

https://forums.ni.com/t5/LabVIEW/CRC-16-CCITT-Calculation/m-p/3224707/highlight/true#M937083

0 Kudos
Message 25 of 30
(4,052 Views)

@bela.komoroczy wrote:

Hi,

I noticed that the marked solution is not the correct one, at least for me. If it is so for others as well, could someone please mark the correct reply as the solution? It is a bit misleading as it is.

So the correct one according to my tests iiis:

https://forums.ni.com/t5/LabVIEW/CRC-16-CCITT-Calculation/m-p/3224707/highlight/true#M937083


I did mark that one as 2nd solutions. I can't unmark the other one. Not sure if a moderator can do that, or if only OP has the power to do that...

 

Not that helpful as it is, as there's no "go to next solution" button...

Message 26 of 30
(4,045 Views)

@bela.komoroczy wrote:

Hi,

I noticed that the marked solution is not the correct one, at least for me. If it is so for others as well, could someone please mark the correct reply as the solution? It is a bit misleading as it is.


That is part of the problem with people resurrecting old threads.  The post that was originally marked as the solution was the solution for the OP.  Somebody later resurrected the thread with a different problem.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 27 of 30
(4,033 Views)

@crossrulz wrote:

@bela.komoroczy wrote:

Hi,

I noticed that the marked solution is not the correct one, at least for me. If it is so for others as well, could someone please mark the correct reply as the solution? It is a bit misleading as it is.


That is part of the problem with people resurrecting old threads.  The post that was originally marked as the solution was the solution for the OP.  Somebody later resurrected the thread with a different problem.


I figured that was going on here (TL;DR).

 

Anyway, you have an extra solution, and I can't unmark it as solution anyway!

0 Kudos
Message 28 of 30
(4,029 Views)

I see your point. Anyway, the "good" one is aslo marked, I hope it helps people like me.

0 Kudos
Message 29 of 30
(4,023 Views)

I ran into this same problem several years ago. I am not a C programmer whereas I was given the C code that generated the CRC. I had a C program explain in detail what the C code was doing and I created a LabVIEW vi that worked. Once you understand what is being done in the C code, you will be able to reproduce the algorithm in LabVIEW.

0 Kudos
Message 30 of 30
(3,532 Views)