LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

sum ascii checksum

Solved!
Go to solution

Hi, I have to create a checksum with my AScii commands, The Checksum is the unsigned sum of the ASCII values reduced to the lower 8 bits. these 8 bits are then sent as a hexidecimal number in ASCII. I'm thinking that this means I convert all characters in my ASCII command to 8 bits and sum them. I then use a modulus of 255 and the remaining 8 bits are converted to hex and added to my command as ASCII characters. 1/ Does this sound correct and 2/ are there examples of this. I've checked a few out but not sure if they are correct for me.

0 Kudos
Message 1 of 12
(13,097 Views)

If your command string is a sequence of human-readable characters (like "VOLT 35"), and you need the 8-bit sum of the ASCII values then all you need is to feed the string to a String to Byte Array function, followed by an Add Array Elements function. This will give you a U8 value. You can then append that to the command string (feed the U8 to a Type Cast and then concatenate the single character to your original string.

 

Something like this (sorry, my Code Capture Tool isn't working right now)

Message 2 of 12
(13,092 Views)

The 8 bits are sent as a hexidecimal number in ASCII and the checksum will be the last 2 characters in the command . Will the U8 give me a 2 digit hexidecimal number in ASCII. Sorry if this sounds dense, all new to me.

0 Kudos
Message 3 of 12
(13,088 Views)

@miggins wrote:

The 8 bits are sent as a hexidecimal number in ASCII and the checksum will be the last 2 characters in the command .


This makes zero sense. You can't send an 8-bit value as 2 characters.

 

Is this some command protocol for an instrument? If so, please attach the actual documentation so we can see exactly what it says, rather than trying to interpret your interpretation, which is clearly incomplete/inaccurate.

0 Kudos
Message 4 of 12
(13,085 Views)

I dont have the actual spec of the instrument as it hasn't been built yet. I will write what I have. It will have a host pc with usb to rs232 interface. all commands will be ASCII strings. At the end of each string, an 8bit hex checksum seperated by a blank is added. It will always consist of 2 lowercase characters, it is illegal to suppress leading zeroes. The checksum is the unsigned sum of the ASCII values of all previous characters, including all blanks, reduced to the lower 8 bits. these 8 bits are then sent as a hexidecimal number in ASCII. The checksum is always the last two characters before the terminating linefeed.

0 Kudos
Message 5 of 12
(13,079 Views)
Solution
Accepted by topic author miggins

Something like this?  The format string format of %02x will format a number into a hexidecimal number with leading 0s, minimal length of 2 characters.  Since you specified lower case, I had to add the To Lowercase function.


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 6 of 12
(13,073 Views)

Cross,

I was searching for a checksum VI and found this. However, what this does is take each ASCII character of the string, convert it to a hex byte and sum it; eg. my hex 'string' is one hex byte 0x3C, and my checksum is 76, which is ASCII '3' + ASCII 'C'. Were my string to contain several hex zeros, I'd get decimal 60 (2 x ASCII '0') added to the checksum for each byte of value 0x00.  What I am looking for should read the entire byte - '3C' and, in the single byte example, return 0x3C or decimal 60.

 

Or am misunderstanding how an 8 bit checksum should be computed? (which is a distinct possibility - I'm a hardware guy, and checksums are for the bit-twiddlers Smiley Wink )

 

d

0 Kudos
Message 7 of 12
(12,902 Views)

I think you're "double tapping" some information, techd. If your hex character is 0x3C it is 3C(76), not 3 and C. 3 is 0x21 and C is 0x37 (if memory serves) so if you stick to that idea you'll get into a recursive loop. 🙂

 

0x3C is simply another way of writing ascii(76) or '<'

 

The checksum of "String" is 83+116+114+105+110+103 (the ascii values of the characters) = 631 (0x0277), and if using only the lower byte (as the previous thread), it becomes 119 (0x77).

 

Hope that helps. 🙂

 

/Y

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

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 8 of 12
(12,898 Views)

@techd wrote:

Cross,

I was searching for a checksum VI and found this. However, what this does is take each ASCII character of the string, convert it to a hex byte and sum it; eg. my hex 'string' is one hex byte 0x3C, and my checksum is 76, which is ASCII '3' + ASCII 'C'. Were my string to contain several hex zeros, I'd get decimal 60 (2 x ASCII '0') added to the checksum for each byte of value 0x00.  What I am looking for should read the entire byte - '3C' and, in the single byte example, return 0x3C or decimal 60.

 

Or am misunderstanding how an 8 bit checksum should be computed? (which is a distinct possibility - I'm a hardware guy, and checksums are for the bit-twiddlers Smiley Wink )

 

d


I all depends on your spec.  I've seen so many variations of checksums it would make you head spin.  The spec given here was a string of binary data as an input and to append a checksum whose value was converted to ASCII.

 

If you want to keep the checksum in binary, then you don't want to use the format string.  You will want to use a build array and a Byte Array to String.

 


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 9 of 12
(12,878 Views)

Ah, yes, good point crossrulz. There is no single checksum rule, it all depends on the rules set up. A common variant is adding the sent messages ascii and using one or two bytes of the result, but e.g. serial port often use 1 bit as checksum; if the message has odd or even number of bits.

 

/Y

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

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 10 of 12
(12,866 Views)