LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Bitwise left shift in formula node.

Solved!
Go to solution

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

0 Kudos
Message 1 of 21
(2,525 Views)

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.)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO
0 Kudos
Message 2 of 21
(2,521 Views)

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 


"Should be" isn't "Is" -Jay
Message 3 of 21
(2,519 Views)

@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.


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 4 of 21
(2,507 Views)

I wouldn't call this spaghetti code.

 

CRC-32.png



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 5 of 21
(2,495 Views)

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!

0 Kudos
Message 6 of 21
(2,478 Views)

@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.


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 7 of 21
(2,469 Views)

I've verified the one I posted against multiple sources. It is accurate.



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 8 of 21
(2,460 Views)

@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.


"Should be" isn't "Is" -Jay
0 Kudos
Message 9 of 21
(2,457 Views)

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]);
      }


   
      

 

 

 

 

0 Kudos
Message 10 of 21
(2,433 Views)