Link to home
Start Free TrialLog in
Avatar of Thread7
Thread7

asked on

Calculating a COM port checksum value

I am having a bit of trouble sending custom COM Port serial data to some LED message boards.  I use VB6 but really this is not programming language specific.  I send the data and then am supposed to send a checksum value.  The commucations manual just says to add the values sent and that is the checksum.  But when I use a comport capture program and look at succesful messages I have sent I cannot figure out how they got the checksum.  Here are some examples:

 00 00 00 00 00 01 46 46 30 30 02 41 30 43 31 39       ......FF00.A0C19
 37 46 30 30 30 30 32 33 30 30 FF FF FF 31 FE 45     7F00002300ÿÿÿ1þE
 FD 41 54 03 30 30 44 37 04                                      ýAT.00D7.  

You can ignore the first 10 values.  The checksum is calculated starting at 02.  Then you are supposed to add everything up until the 03 on the bottom row.  The 30 30 44 37 is the checksum value.  When I add the values between 02 and 03 (including 02 and 03) I get 928h - not 30 30 44 37.

The above example just sends the letter "T" to the board.  (Third character on bottom row).  The junk before and after the "T" is just formatting info.

So next I sent a "U" to the board and captured the output.

 00 00 00 00 00 01 46 46 30 30 02 41 30 43 31 39      ......FF00.A0C19
 37 46 30 30 30 30 32 33 30 30 FF FF FF 31 FE 45    7F00002300ÿÿÿ1þE
 FD 41 55 03 30 30 44 36 04                                     ýAU.00D6.

The checksum actually went down 1 in value instead of up 1 like I would expect.

Any ideas?
Avatar of Thread7
Thread7

ASKER

I found some code that gets me on the right track.   But for the short "T" and "U" it is one off.  With longer sets of data it is quite a ways off.  I'll post below:

Public Function GetCheckSum(ByVal sMessage As String, Optional ByVal bIncludeMessage As Boolean) As String
    '//Check that there is something in the sMessage
    If sMessage = vbNullString Then
        Exit Function
    End If
       
    Dim CheckSum As Long
    Dim i As Integer
       
    For i = 1 To Len(sMessage)
        CheckSum = CheckSum + Asc(Mid(sMessage, i, 1))
    Next
   
    CheckSum = 256 - CheckSum Mod 256
    CheckSum = CheckSum Mod 256
   
    GetCheckSum = Hex(CheckSum)
   
    If (Len(GetCheckSum) = 1) Then
        GetCheckSum = "0" & GetCheckSum
    End If
   
    If bIncludeMessage = True Then
        GetCheckSum = sMessage & GetCheckSum
    End If

End Function
Avatar of Thread7

ASKER

Well I figured it out.  After searching the net I found a shareware program called WinHexcom that lets you calculate checksums for various algorithms.  They had an algorithm they called modulo 2 XOR.  This would always take my strings and calculate a checksum that was just a value of one 01h off.  So I knew that was close even though they don't disclose the algorithm.  After experimenting with the XOR command I replicated the Winhexcom results.  Then I implemented a somewhat crude If - Then  statement to add a 1 if the final binary digit is 0.

Here is the code:

Public Function GetCheckSum(ByVal sMessage As String, Optional ByVal bIncludeMessage As Boolean) As String
    '//Check that there is something in the sMessage
    If sMessage = vbNullString Then
        Exit Function
    End If
       
    Dim CheckSum As Long
    Dim i As Integer
       
    For i = 1 To Len(sMessage)
        CheckSum = CheckSum Xor Asc(Mid(sMessage, i, 1))
    Next
   
    If (CheckSum Mod 2) = 1 Then
      CheckSum = CheckSum - 1
    Else
      CheckSum = CheckSum + 1
    End If
   
   
    GetCheckSum = Hex(CheckSum)
   
    If (Len(GetCheckSum) = 1) Then
        GetCheckSum = "0" & GetCheckSum
    End If
   
    If bIncludeMessage = True Then
        GetCheckSum = sMessage & GetCheckSum
    End If

End Function
IF ur problem has been solved then u can post a question in community support TA to delete this question
Avatar of GrahamSkan
No need to delete. PAQ with points refund would be more appropriate. The solution looks well worth keeping.
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial