ni.com is currently experiencing slowness/issues due to ongoing maintenance.
Support teams are actively working on the soonest resolution.
ni.com is currently experiencing slowness/issues due to ongoing maintenance.
Support teams are actively working on the soonest resolution.
09-19-2018 09:43 AM
I am attempting to convert a CRC calculator, written in C, to a LabVIEW VI.
It looked easy to cut and paste the relevant calculations into a formula node, but I have come unstuck.
in C I can do:
ushort value = (ushort)(I<<8)
The second 'ushort' is there to remove any overflow characters by chopping it back to 8 bits. I tried replacing the ushort with uInt8 but no joy.
If I do without the (ushort) LabVIEW recognises the overflow and rounds the byte down to FF, which is not what I want.
The LabVIEW logical shift operator does work correctly, but, as the code is more subtle and clever than I am, I don't really want to go down the spaghetti route if I can avoid it.
BTW, don't think I hadn't thought to convert the C code to a DLL and import it that way, but I would need outside help with that, plus the complication of supporting the code.
Any help much appreciated,
Regards,
Ian
Solved! Go to Solution.
09-19-2018 09:54 AM
Hi Ian,
what about an AND 0xFF to mask the lower 8 bits (when working with U16)?
(When you left shift by 8 bits the lower 8 bits should be zero anyway.)
I don't really want to go down the spaghetti route if I can avoid it.
When you follow the StyleGuide you don't produce any spaghetti code! 😄
(As you ask in a LabVIEW forum you will get more help when you would use plain LabVIEW.)
09-19-2018 09:57 AM
Oh just search the forums for CRC.
There are many examples. Then, what polynomial? How many bits? Any code from the far end? Not every CRC algorithm meets standards. I've seen some embedded developers screw it up royally
09-19-2018 10:19 AM
@Barionics wrote: I don't really want to go down the spaghetti route if I can avoid it.
There are several examples in these forums for CRC algorithms that are not spaghetti at all. My baseline CRC code is just 2 FOR loops with shift registers, bit shifting, XOR, and a case structure. If you can supply the C code and example results (input ABCD yields and output of XY), then we can give it a try with what we already have.
09-19-2018 10:49 AM
I wouldn't call this spaghetti code.
09-19-2018 01:04 PM
Thanks Mark,
I'll try that out when I get to work tomorrow. I must confess that I have been downloading various code samples from the forums only to find that they give different different answers even though they claim to be the same standard.
Ohh yes! And for the record, I have 16 years doing LabVIEW, CLD,CTD and very nearly CLA (63% I couldn't believe it!) but that doesn't mean I don't get fed up with it from time to time!
09-19-2018 01:44 PM - edited 09-19-2018 01:44 PM
@Barionics wrote:
only to find that they give different different answers even though they claim to be the same standard.
The problem is that there are many variations of CRCs (8-bit, 16-bit, 32-bit, shift left, shift right, different initial CRCs, Invert or not the final CRC, polynomials, etc). That is why I asked for a sample of inputs vs outputs.
Here is what my standard 16-bit CRC code is. I can usually find the right combination of settings with this VI. If not, then I want to see the C source code to figure out what they did differently.
09-19-2018 03:28 PM
I've verified the one I posted against multiple sources. It is accurate.
09-19-2018 04:14 PM
@JÞB wrote:
Oh just search the forums for CRC.
There are many examples. Then, what polynomial? How many bits? Any code from the far end? Not every CRC algorithm meets standards. I've seen some embedded developers screw it up royally
Yup.
09-20-2018 02:10 AM - edited 09-20-2018 02:24 AM
Thanks for your two solutions guys. I gather that I should be able to drag and drop them into LabVIEW, but that doesn't look to be working on my PC. Is there some trick I need to know?
Here is the C algorithm which I am attempting to duplicate, which looks very like Mark's solution:
uInt8 table[256];
uInt16 i;
uInt8 b;
uInt8 value;
uInt8 crc;
for (i = 0; i < 256; i++) {
value = (i << 8);
for (b = 0; b < 8; b++) {
if ((value & 0x8000) != 0) {
value <<= 1;
value ^= 0x1021;
} else {
value <<= 1;
}
}
table[i] = value;
}
/**************************************************************/
for(i = 0;i<256;i++) {
b= bytes[i];
crc = ((crc << 😎 ^ table[(crc >> 😎 ^ b]);
}