07-17-2012 08:13 AM
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.
Solved! Go to Solution.
07-17-2012 08:28 AM
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)
07-17-2012 08:49 AM
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.
07-17-2012 08:57 AM
@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.
07-17-2012 09:15 AM
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.
07-17-2012 09:25 AM - edited 07-17-2012 09:27 AM
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.
10-11-2012 09:12 AM
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 )
d
10-11-2012 09:32 AM
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
10-12-2012 07:23 AM
@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 )
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.
10-12-2012 08:28 AM
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