Jump to content

Checksum involving Hex addition


 Share

Recommended Posts

Hi everybody.

I'm trying to connect one computer to one Keyence's measuring system using an RS-323 COM Port.

This Keyence system needs that the data sent to it via serial port has a checksum.

The checksum is based on the ASCII value of the characters that you want to send.

The instruction manual claims that you have to calculate a checksum doing this:

 
 
 
 
Quote

Lets going to suppose that you want to send the Command "SA" (it's just a status request)

The instruction manual claims:

Sending of a “SA” command


To send a SA command, we must send:
HEADER: 
2 bytes SA. “S” = 0x053. “A” = 0x041
Separator: 1 byte, espace = 0x20
Field 1: 
Checksum
Delimitator: 2 bytes: “CR” = 0x0d. “LF” = 0x0a
Checksum:
SA “ “
Total Adding:
0x53 (S) + 0x41 (A) + 0x20 (esp) = 0xB4

The checksum equals to the last to digits in hex: B4. You have to send the ASCII characters "B" and "4"

I have tried to get the Hex value from the ASCII value of every character, but I've got no luck doing the addition:

#include <String.au3>

Local $result

Local $result0 = InputBox("CHECKSUM", "ENTER STRING", "SA ", "", - 1, -1)

Local $result1 = StringSplit($result0,"")

For $i = 1 To $result1[0]
    Consolewrite($i & " = " & $result1[$i] & " HEX WOULD BE " & Hex(Asc($result1[$i])) &  " TYPE " & VarGetType(Hex(Asc($result1[$i]))) & @CRLF)
    $result += Hex(Asc($result1[$i]))
Next

ConsoleWrite("!JUST ADDING = " & $result & @crlf)
ConsoleWrite("!Type = " & VarGetType($result) & @crlf)
ConsoleWrite("> ADDING IN HEX 0x" & Hex($result,3) & @crlf)
ConsoleWrite("> Type = " & VarGetType(Hex($result,3)) & @crlf)
ConsoleWrite("< ADDING IN DEC " & Dec($result) & @crlf)
ConsoleWrite("+ ADDING IN DEC/HEX " & Dec(Hex($result)) & @crlf)
ConsoleWrite("- ADDING IN HEX/DEC " & Hex(Dec($result)) & @crlf)
ConsoleWrite("> ADDING IN ASC " & Asc($result) & @crlf)
ConsoleWrite("< ADDING IN ASC/HEX " & Asc(Hex($result)) & @crlf)
ConsoleWrite("+ ADDING IN ASC/DEC " & Asc(Dec($result)) & @crlf)
ConsoleWrite("! ADDING IN HEX/ASC " & Hex(Asc($result)) & @crlf)
ConsoleWrite("ADDING IN ASC/DEC/HEX " & Asc(Dec(Hex($result))) & @crlf  & @crlf & @crlf)

And I got this in the SciTE console:

1 = S HEX WOULD BE 00000053 TYPE String
2 = A HEX WOULD BE 00000041 TYPE String
3 =   HEX WOULD BE 00000020 TYPE String
!JUST ADDING = 114
!Type = Double
> ADDING IN HEX 0x000
> Type = String
< ADDING IN DEC 276
+ ADDING IN DEC/HEX 4637722453773123584
- ADDING IN HEX/DEC 00000114
> ADDING IN ASC 49
< ADDING IN ASC/HEX 52
+ ADDING IN ASC/DEC 50
! ADDING IN HEX/ASC 00000031
ADDING IN ASC/DEC/HEX 52

I'm a little bit confused, as my hex values are not "0xSOMETHING".

I've been doing some research in the forum (I don't like to ask as soon as I get stuck), and I've seen that some people were having problems due to the Var Type.

It's weird, but my hex values are returned as strings (Same as $result after the loop).

Any help would be truly appreciated.

 

Greets from Barcelona

Link to comment
Share on other sites

I don't quite understand your question/issue.  Are you trying to figure out how to calculate the checksum or are you trying to figure out why you are not seeing "0x" when you display hex/binary values? 

This little snippet is one way to calculate the checksum according to your specifications. :)  The 2 pairs of ConsoleWrites produce the same output.  They just do it using different functions.  For more complex outputs, I prefer to use the StringFormat function.  So one uses the StringFormat function and the other uses the Hex function, like in your script.  Note that the Hex function uses the 2nd parameter, that tells it how many characters to output, which in this case is 2.

As for calculating the checksum, I think you may have been over-thinking it a bit.  You had the right idea in summing the ASC values.  You should have just sum the integers values first and then convert the resulting integer to its hex representation (83 + 65 + 32 = 180).  180 in base10 (decimal) is B4 in base16 (hex).  Instead, you were trying to add the hex string representation of the integers ("53" + "41" + "20" = 114). 

#include <Constants.au3>

example()

Func example()
    Local $sString = "SA "
    Local $iChecksum = 0

    $aChars = StringSplit($sString, "")

    For $i = 1 To $aChars[0]
        $iChecksum += Asc($aChars[$i])

;~      ConsoleWrite(StringFormat('"%s" = %i (0x%X)', $aChars[$i], Asc($aChars[$i]), Asc($aChars[$i])) & @CRLF)
        ConsoleWrite('"' & $aChars[$i] & '" = ' & Asc($aChars[$i]) & " (0x" & Hex(Asc($aChars[$i]), 2) & ")" & @CRLF)
    Next

    ConsoleWrite(StringFormat("$iChecksum = %i (0x%X)", $iChecksum, $iChecksum) & @CRLF)
;~  ConsoleWrite("$iChecksum = " & $iChecksum & " (0x" & Hex($iChecksum, 2) & ")" & @CRLF)
EndFunc

Output:

"S" = 83 (0x53)
"A" = 65 (0x41)
" " = 32 (0x20)
$iChecksum = 180 (0xB4)

 

Edited by TheXman
Fixed a typo
Link to comment
Share on other sites

6 hours ago, TheXman said:

You should have just sum the integers values first and then convert the resulting integer to its hex representation (83 + 65 + 32 = 108).  108 in base10 is B4 in base16 (hex). 

little typo : 180 (as in Output), not 108 ;).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

7 minutes ago, Musashi said:

little typo : 180 (as in Output), not 108 ;).

Thanks. :yes:  The typos have been corrected.

Edited by TheXman
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...