Jump to content

Hex - Add together for Checksum


 Share

Recommended Posts

I am trying to create a checksum string for rs232 in hex. It's for screen control of a Samsung screen. They use a 2 byte equivalent and I am trying to take that string and add each of the 2 bytes to together to create a checksum value and add that onto the end of the string. With that said, the first 2 bytes don't get added to the rest of the string to create the checksum and I'm having a bit of a hard time in trying to get this.

 

So for example a full hex string is

AAFF010341110055

The header is AA

The display ID is 01 (this number can vary)

The actual checksum value is 55  (If you add FF 01 03 41 11 00 together in hex). This is where the result of the script needs to end up.

I only posted partial code since beyond this, whatever I try I can't seem to get to work.

I was trying for, next loops, to trim and add

then I figured I'd create an array...then add each line but didn't have any success

I imagine this'll be cake for some of the more advanced coders :)

#include <String.au3>
#include <Array.au3>


$DisplayID = 01
$test = chr(dec("AA")) & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41"))  & chr(dec("11")) & chr(dec("00")) & chr(dec("55"))  ;AA FF 0[DISPLAYID] 03 41 11 00 55 
 
Checksum($test)
 



Func Checksum($x)
    Local $checksum = $x
    local $hex
    ConsoleWrite("Original string = " & _StringToHex($checksum) & @crlf)
    $checksum = StringStripWS($checksum,1)
    ConsoleWrite("StringStripWS leading edge = " & _StringToHex(StringStripWS($checksum,1)) & @CRLF) ;take any leading edge of the command WS out
    $checksum = StringStripWS($checksum,2)
    ConsoleWrite("StringStripWS trailing edge = " & _StringToHex(StringStripWS($checksum,2)) & @CRLF) ;take any trailing edge of the command WS out
    $checksum = StringTrimLeft($checksum,2)    ;removes first hex value on left from String aka AA - the header of the command
    ConsoleWrite("StringTrimLeft $checksum = " & _StringToHex($checksum) & @crlf)
    $checksum = StringTrimRight($checksum,2)    ;removes first hex value on right from String aka existing checksum
    ConsoleWrite("StringTrimRight $checksum = " & _StringToHex($checksum) & @crlf)
        $str = StringToASCIIArray($checksum)
       _ArrayDisplay($str)


;from here ?????????????

wend
Link to comment
Share on other sites

I'm not exactly sure how the checksum operation works, but I did notice if you add each byte together you end up with 5556.  Could it be that the ID should not be included in the checksum?  Do you have any other code examples we can inspect?

edit: typo

$test = "AAFF010341110055"
Checksum($test)
Func Checksum($x)
   Local $hex = StringMid($x, 7, 6)
   ConsoleWrite($hex & @CRLF)
   $checksum = 0
   For $x = 1 to 5 Step 2
   $checksum += Dec(StringMid($hex, $x, 2))
   Next
   ConsoleWrite(Hex($checksum,2) & @CRLF)
EndFunc

Just another observation...FF - 55 = AA

:blink:

Edited by spudw2k
Link to comment
Share on other sites

well you are doing it with ascii characters...and since I've tried doing it that way with the commmg.au3 but have been unsuccessful I've been hard coding the conversions into the variable declarations. and it's been the only way that I got it to send the full hex variable and for the Samsung screen to take it successfully...even though it appears to the eye the same

with that said, your way is not exactly the way i am trying to get it to work. I combined yours and my original in hopes to try and get it...but I am having trouble with the "+=" line of code ..and since the variables can vary in terms of length, the stringlen command would be needed rather than a hardcoded length on the stringmid. I've been trying to do this in hex but I may not be adding it in using the correct addition operator

Interesting that FF-55 equals AA...I wonder if that is pattern to something ..hmm

If you set your windows calc to hex and byte and add FF through 00 you get 55 not 56. I've made sure a bunch of times.

I added a bunch of other variables so you can see...same concept, AA is the header and the last hex bytes are the checksum

any ideas?

#include <String.au3>
#include <Array.au3>



$DisplayID = 01
$test = chr(dec("AA")) & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41"))  & chr(dec("11")) & chr(dec("00")) & chr(dec("55"))  ;AA FF [DISPLAYID] 03 41 11 00 55 


        Global $TxStringSetPowerOn = chr(dec("AA")) & chr(dec("11")) & chr(dec($DisplayID))  & chr(dec("01")) & chr(dec("01"))  & chr(dec("14"))  ;AA 11 [DISPLAYID] 01 01 14
        Global $TxStringSetPowerOnResponse = chr(dec("AA")) & chr(dec("FF")) & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41"))  & chr(dec("11")) & chr(dec("01")) & chr(dec("56"))  ;AA FF [DISPLAYID] 03 41 11 01 56
        Global $TxStringSetPowerOff = chr(dec("AA")) & chr(dec("11")) & chr(dec($DisplayID))  & chr(dec("01")) & chr(dec("00"))  & chr(dec("13"))   ;AA 11 [DISPLAYID] 01 00 13
        Global $TxStringSetPowerOffResponse = chr(dec("AA")) & chr(dec("FF")) & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41"))  & chr(dec("11")) & chr(dec("00")) & chr(dec("55"))  ;AA FF [DISPLAYID] 03 41 11 00 55 
        Global $TxStringQueryPower = chr(dec("AA")) & chr(dec("11")) & chr(dec($DisplayID))  & chr(dec("00")) &   chr(dec("12"))   ; AA 11 01 00 12
        Global $TxStringQueryPowerResponseOn = chr(dec("AA")) & chr(dec("FF")) & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41"))  & chr(dec("11")) & chr(dec("01")) & chr(dec("56"))  ;AA FF [DISPLAYID] 03 41 11 01 56
        Global $TxStringQueryPowerResponseOff = chr(dec("AA")) & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41"))  & chr(dec("11")) & chr(dec("00")) & chr(dec("55"))   ;AA FF [DISPLAYID] 03 41 11 00 55
        Global $TxStringSetInput =  chr(dec("AA")) & chr(dec("14")) & chr(dec($DisplayID))  & chr(dec("01")) & chr(dec("21"))  & chr(dec("37"))    ;AA 14 [DISPLAYID] 01 21 37  <---hdmi input
        Global $TxStringSetInputResponse = chr(dec("AA")) & chr(dec("FF")) & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41"))  & chr(dec("14"))  & chr(dec("21"))   & chr(dec("79")) ;AA FF [DISPLAYID] 03 41 14 21 79    <---hdmi input
        Global $TxStringQueryInput = chr(dec("AA")) & chr(dec("14")) & chr(dec($DisplayID))  & chr(dec("00")) &   chr(dec("15"))    ; AA 14 [DISPLAYID] 00 15
        Global $TxStringQueryInputResponse = chr(dec("AA")) & chr(dec("FF")) & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41"))  & chr(dec("14")) & chr(dec("21")) & chr(dec("79"))   ;AA FF [DISPLAYID] 03 41 14 21 79    <---hdmi input
        Global $TxStringSetVolume =  chr(dec("AA")) & chr(dec("12")) & chr(dec($DisplayID))  & chr(dec("01")) &  chr(dec($Volume)) & chr(dec("59"))  ;AA 12 [DISPLAYID] 01 [volume] 59
        Global $TxStringSetVolumeResponse =  chr(dec("AA")) & chr(dec("FF"))  & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41")) & chr(dec("12")) &  chr(dec($Volume)) & chr(dec("9B"))   ;AA FF [DISPLAYID] 03 41 12 [volume] 9B   
        Global $TxStringQueryVolume = chr(dec("AA")) & chr(dec("12")) & chr(dec($DisplayID))  & chr(dec("00")) &   chr(dec("13"))    ;AA 12 [DISPLAYID] 00 13
        Global $TxStringQueryVolumeResponse = chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41")) &  chr(dec("12")) & chr(dec($Volume))     ;chr(dec("AA")) & chr(dec("FF"))  & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41")) &  chr(dec("12")) & chr(dec($Volume)) & chr(dec("9B"))     ;AA FF [DISPLAYID] 03 41 12 [volume] 9B   






Checksum($test) 
 

Func Checksum($x)
    Local $checksum = $x
    local $hex
    ConsoleWrite("Original string = " & _StringToHex($checksum) & @crlf)
    $checksum = StringStripWS($checksum,1)
    ConsoleWrite("StringStripWS leading edge = " & _StringToHex(StringStripWS($checksum,1)) & @CRLF) ;take any leading edge of the command WS out
    $checksum = StringStripWS($checksum,2)
    ConsoleWrite("StringStripWS trailing edge = " & _StringToHex(StringStripWS($checksum,2)) & @CRLF) ;take any trailing edge of the command WS out
    $checksum = StringTrimLeft($checksum,2)    ;removes first hex value on left from String aka AA - the header of the command
    ConsoleWrite("StringTrimLeft $checksum = " & _StringToHex($checksum) & @crlf)
    $checksum = StringTrimRight($checksum,1)    ;removes first hex value on right from String aka existing checksum
    ConsoleWrite("StringTrimRight $checksum = " & _StringToHex($checksum) & @crlf)
;~  $str = StringToASCIIArray($checksum)
;~  _ArrayDisplay($str)
    Global $addchecksum = 0
    ConsoleWrite("Stringlen: " & StringLen(_StringToHex($checksum)) & @CRLF)
    ConsoleWrite("Stringlen-nonconv: " & StringLen($checksum) & @CRLF)
    ConsoleWrite("dec Stringmid: " & _StringToHex(dec(StringMid($checksum, 1, 2))) & @CRLF)
    ConsoleWrite("Stringmid: " & _StringToHex(StringMid($checksum, 1, 2)) & @CRLF)
    ConsoleWrite("dechardcode Stringmid: " & _StringToHex(StringMid($checksum, 1, 2)) & @CRLF)
    ;$checksum = chr(dec($checksum,2))
    For $x = 1 to StringLen($checksum) Step 2  
        ;$addchecksum += Dec(StringMid($checksum, $x, 2))
        $addchecksum += _StringToHex(StringMid($checksum, $x, 2))
        ConsoleWrite("for loop:" & _StringToHex($addchecksum) & " $x: " & $x & @CRLF)
    Next   
    ConsoleWrite("$hex:" & Hex($addchecksum,2) & @CRLF)
    
EndFunc
Link to comment
Share on other sites

You want to exclude the header(AA)  and the crc(55) in the string you pass to the checksum function i think:

Edit, i think you use the _StringToHex function wrong, your string is already a Hex representation?

$test = "FF0103411100"
Checksum($test)

Func Checksum($String)
    $checksum = 0
    For $idx=1 to StringLen($String) Step 2
        $checksum += Dec(StringMid($String, $idx, 2))       
    Next
    ConsoleWrite("Checksum: "& Hex($checksum,2) &@CRLF)
    Return Hex($checksum,2)
EndFunc
Edited by Geir1983
Link to comment
Share on other sites

Yes...its a hex representation...and I can/will exclude the AA and the 55 ...and subsequent headers and checksums for the other variables. That's the easy part. I have that apart of the trims in my examples. The addition is the hardpart

But it seems like you are taking a variable as a string while I am using the 2 character length of a hex value and converting it because for some odd reason this is the only way that I've gotten the CommMG and Samsung accept the send/receive. There may be a better way to do this, and I am sure there is, but I haven't been able to get that to work (believe me I pulled my hair out). The way you are doing it takes a string and uses it to run through the function. But those values are different since you're using a 2 char expression.

The below is what I am trying to convey about the different values. It seems like a conversion needs to take place, but the func breaks because it seems like its adding the wrong "type" of value.

The same:

ConsoleWrite("chr: " & chr(dec("FF")) &  @CRLF)
ConsoleWrite("hextostring: " & _HexToString("FF") & @CRLF)

those values are different from

ConsoleWrite("string: " & "FF" & @CRLF)

Sorry if I am not explaining this correctly.

I appreciate the help

Link to comment
Share on other sites

The reason your chr and _HexToString examples are producing that result is because you are converting the two hex chars (one byte) into an ASCII character.  In both my and Geir's examples we are converting the HEX characters into a decimal value, adding bytes together then converting the decimal value into a HEX representation. 

Here is another adding example just to help visualize.

Below I create variables $a and $b and set then to a true binary/hex value of FF.

I then add them together as $c and the result is printed.  As you can see the sum of the two HEX values is a decimal value of 510, or a HEX value of 1FE.

$a = 0xFF
$b = 0xFF
$c = $a + $b
ConsoleWrite("Dec: " & $c & @CRLF)
ConsoleWrite("Hex: " & Hex($c) & @CRLF)

I don't know if that is helpful or meaningful to you or not.  Hopefully it doesn't add to the confusion.

 

Edited by spudw2k
Link to comment
Share on other sites

But it seems like you are taking a variable as a string

 

Look at what Dec() does, it takes a hexadecimal string and turns it into a number that you can do mathematical operations on. The string is not really an ascii string it is meant to represent data in hex. The _StringToHex will return the hex representation of every ascii charachter in it, for example if you enter _StringToHex("FF") it will return 4646, the Dec("FF") will return 255.

Edit: I see you are using _HexToString, this is returning a string, not an integer. You cannot do mathematical operations on a string the same way as you would do a int variable.

Dec

Returns a numeric representation of a hexadecimal string.

Edited by Geir1983
Link to comment
Share on other sites

thank you for that explanation I think I understand

But ...question

my variable is an ascii string:.

$test = chr(dec("AA")) & chr(dec("FF")) & chr(dec($DisplayID))  & chr(dec("03")) & chr(dec("41"))  & chr(dec("11")) & chr(dec("00")) & chr(dec("55"))  ;AA FF [DISPLAYID] 03 41 11 00 55 

When I do stringtohex in console, that's when I get the hex equivalent of this string..of course. I was only using that as consolewrites to track my variable as it made its way through the script. In my last example, I was testing the stringtohex conversion to see if it made a difference in the addition func, and it didn't. By your explanation that makes sense as to why.

BUT when this variable is passed to either one of your functions, as itself - as a string, I don't get results on the addition line of code. That is where it breaks for me. If I am following correctly...shouldn't the dec conversion occurr in the line below for addition/numeric purposes?

$addedchecksum += dec(StringMid($checksum, $idx, 2))

I think I am getting closer with the code I attached passing my string throught the func but it seems that I cannot get the final result back to hex. mainly because it seems when you add the decimal values of these, its over the 255 range.

again I appreciate the help...all these different conversions are a bit hard for me to follow as to what it needs to be to add etc...so sorry if I am slow to pick this up.

For $idx=1 to StringLen($checksum) Step 1  
        ConsoleWrite(@CRLF & "_stringtohex conversion prior to dec: " & _stringtohex(StringMid($checksum, $idx, 1)) & @CRLF)
        $addedchecksum += dec(_stringtohex(StringMid($checksum, $idx, 1)))
        ConsoleWrite("for loop Checksum: "& $addedchecksum &@CRLF)              
    Next     
    ConsoleWrite("Final Checksum: " & Hex($addedchecksum,2) &@CRLF)  
    Return Hex($addedchecksum,2)
Link to comment
Share on other sites

I think the _StringToHex function is leading to your confusion.  What the function actually does is convert ascii character values into a HEX representation.  It does not convert a string containing HEX characters into HEX values.
To illustrate this see below:

#include <String.au3>

;This is a string that contains HEX characters
Local $sHEXString = "0123456789ABCDEF"
Local $sHex = _StringToHex($sHEXString) ; Convert the string to a hex string.
ConsoleWrite("Original String: " & $sHEXString & @TAB & " Hex: " & $sHex & @CRLF)

;This converts the HEX values back into the original string of HEX characters
$sString = _HexToString($sHex) ; Convert the hex string back to the original string.
ConsoleWrite("Hex: " & $sHex & @TAB & "String: " & $sString & @CRLF)

;Here I am manually doing what _StringToHex does for visualizing.
$aHEX = StringSplit($sHEXString, "")
For $x = 1 to $aHEX[0]
   ConsoleWrite("Chr: " & $aHEX[$x] & @TAB & _
   "Asc: " & Asc($aHEX[$x]) & @TAB & _
   "Hex: " & Hex(Asc($aHEX[$x]),2) & @CRLF)
Next

Don't use Chr() and don't use _StringToHex.  They convert the HEX characters in your string into ascii values.  Instead convert the pairs of HEX chars into decimal, add them together, then convert back into HEX characters.
 

Edited by spudw2k
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...