LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Packet Question: How to calculate CRC

Solved!
Go to solution

Hi to all, i tryied to find an answer on how to calculate the CRC on a RS232 packet received, its really driving me crazy!

 

im a newby, learnt how to use labview reading on different forums, and i had managed to design really good apps, but im stucked in this, hope some one can help me...

 

ok, so im receiving 15byte packets constantly:

 

A5 09 0C 00 3F 61 05 1E 10 A0 00 05 00 04 05

A5 09 0C 00 3F 61 05 1E 10 A0 00 05 0C 2F 03 

A5 09 0C 00 3F 61 05 1E 10 A0 09 05 09 04 09

 

the provider for the hardware gave me the following code to calculate the CRC:

CRC Check Program:

#define unsigned char uchar
uchar CRC_Bitwise8(uchar *buf, uchar size)
{
  uchar i,j,b;
  uchar crc=0;
  for (j=0;j<size;j++)
{
    b=*buf++;
    for (i=0;i<8;i++)
   {
      if(((crc^b)&0x01)!=0)
      crc^=0x18;
      crc>>=1;
      b>>=1; 
    } 
  }   
  return crc;
}

 

so my question is, how can i insert this code in labview? any example? 

 

thanks in advance, this forums really helped me learn labview in a practical manner!
     

0 Kudos
Message 1 of 17
(6,761 Views)

Do a search, there are numerous threads disussing this topic and lots of examples available

 

 

Christian

0 Kudos
Message 2 of 17
(6,749 Views)

thanks, but searching didnt gave me any clue, it seems im really a starter in CRC calculation, and i really dont understand much how to calulate from the formula i wrote above.

can someone help me on this? i just need it in order to read from a sensor correctly.

 

thanks!

0 Kudos
Message 3 of 17
(6,737 Views)

You find all the information about CRC online, e.g. here and there are lots of LabVIEW examples available.

 

 

Christian

0 Kudos
Message 4 of 17
(6,734 Views)

thanks again christian, im studying those examples, but im confused as how to know and translate the code i wrote above in order to know what type of CRC calculation i need to make, or how to translate it to LV

 

many thanks!

0 Kudos
Message 5 of 17
(6,728 Views)

I think the code you posted is CRC 8. Let's have a look at the examples here.

 

 

Christian

0 Kudos
Message 6 of 17
(6,723 Views)

LabVIEW has all of the same programming primitives (loops, logical operators, comparisons, etc.) used in the above piece of code. You simply have to pick the pieces apart and write them using native LabVIEW constructs. If you are that unfamiliar with LabVIEW itself I suggest going through the online tutorials to help familiarize yourself with the basics.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 7 of 17
(6,716 Views)

Thanks Mark, im studying lots of labview tutorials, and i really learnt a lot from them!

just the code above to calculate CRC is really getting me crazy, i really cant understand how to calculate crc on the packets received and its really frustrating.

if someone can help me understand how to calculate CRC in Labview from the above code i will really apprecieate it! or just a hint, i really dont know from where to start!

 

thanks!

0 Kudos
Message 8 of 17
(6,707 Views)

What is the device?  Is the manula online somewhere?  I have a CRC package I posted in this thread that is very flixible and should handle what you need.  However, you need to know which bytes in your message are included in the CRC calculation.

0 Kudos
Message 9 of 17
(6,699 Views)
Solution
Accepted by topic author nickpuy

You can use my code from the thread I referenced.  To play with a web version of my code go here. My code is based off the code on that website.

 

For my code, you will need the following settings:

 

CRC Calculation:  Other (specify)

CRC Order:  8

CRC Polynomial:  0x30

Initial CRC Value:  0

Final CRC XOR Value:  0

Reflect Data Preprocessing:  TRUE

Reflect Data Before Final XOR:  TRUE

 

In your messages, the A5 09 0C is definitely a header.  I think the following 00 is also part of the header, but without the manual, I cannot confirm.  00 will not affect the CRC calculation, so I get the same reuslts with it and without it.  So, in your first example, I use 3F 61 05 1E 10 A0 00 05 00 04 and the result is 0x05.

0 Kudos
Message 10 of 17
(6,696 Views)