LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Help creating a checksum

Hi all,

 

It's been a few years since I did any LabVIEW and I'm a bit rusty. I'm trying to replicate a C# function which calculates a checksum to append on the end of a serial line message.

 

The C# code is as follows:

 

        private void doChecksum()
        {
            byte data = 0;
            for (int i = 0; i < length+3; i++)
            {
                data -= buffer[i];
            }
            buffer[index] = (byte)data;
        }

 

When the array 'buffer' is populated with:

 

AA

55

00

01

00

 

the resulting values of data on each pass of the loop are as follows:

 

0x56
0x01
0x01
0xfa
0xfa

 

I'm having a real headache with the data representations in order for the -= calculation to output the values above. I can get it to work using the attached .vi until the 4th loop, whereby 'data' then bottoms out at 0 and doesn't go 'negative' as above.

 

After that checksum is finished, I need the value converted back to a hex string to append to the end of the hex string array message. For the example attached, the final checksum string should equate to 0xFA but it's calculating as 0x30.

 

Can anyone (with more patience than I) figure this out?

 

Thanks

0 Kudos
Message 1 of 7
(4,212 Views)

Try this attached VI I found on the LabVIEW code exchange.

 

 

========================
=== Engineer Ambiguously ===
========================
Message 2 of 7
(4,203 Views)

Your code is much more complicated than it should be. You don't need to create an array of strings when you simply want to add characters to an existing string. "Command" should be a string, not an array of strings, and you can combine the AA5500 into a single string as well.

 

Ignoring all that, however... you need to look at your incoming data. It's not "bottoming out" at 0, that's the actual result of the calculation. Let's go through it. You start with "AA 55 00 01 00" in hex. In decimal, as signed 8-bit values, that's "-86 85 0 1 0". So, on successive loop iterations you have:

0 - -86 = 86

86 - 85 = 1

1 - 0 = 1

1 - 1 = 0

0 - 0 = 1

If you're expecting a different result, I suspect the problem is with the input data. It is also possible that there's an issue with the way you rewrote the C# code into LabVIEW, since your C# code doesn't define "length" and "index" - but I would start by checking your input data.

0 Kudos
Message 3 of 7
(4,189 Views)

The input data is correct and the checksum that is calculated by the C# code is also correct. I think the issue is down to the signing.

 

Length and index are defined elsewhere in the code but aren't important for this - I just need the LabVIEW to calculate the CRC value the same as the C# code somehow.

0 Kudos
Message 4 of 7
(4,144 Views)

As i interpret the C# code it's basically this:

Checksum.png

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 5 of 7
(4,141 Views)

Correction, if you want the array as output.

Checksum2.png

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 6 of 7
(4,137 Views)

@richellis2001 wrote:

The input data is correct and the checksum that is calculated by the C# code is also correct. I think the issue is down to the signing.


I don't see how this can be true. Do the math yourself by hand. You'll get the same result if you start with an unsigned value. I don't have a C# environment installed, but I just wrote the same in C and ran it:

#include <stdio.h>

void main() {
        char message[] = {0xAA, 0x55, 0x00, 0x01, 0x00};
        int i;
        unsigned char data = 0;
        for (i=0; i<5; ++i){
                data -= message[i];
                printf("%d\n",data);
        }
}

and it produces exactly the same results as your LabVIEW code.

0 Kudos
Message 7 of 7
(4,114 Views)