Solved

Calculating a COM port checksum value

Posted on 2006-07-13
6
1,945 Views
Last Modified: 2013-11-25
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?
0
Comment
Question by:Thread7
6 Comments
 
LVL 1

Author Comment

by:Thread7
ID: 17103515
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
0
 
LVL 1

Author Comment

by:Thread7
ID: 17104903
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
0
 
LVL 8

Expert Comment

by:hiteshgupta1
ID: 17106594
IF ur problem has been solved then u can post a question in community support TA to delete this question
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17176611
No need to delete. PAQ with points refund would be more appropriate. The solution looks well worth keeping.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 17405205
PAQ / Refund
ee ai construct, community support moderator
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
socket programming 8 32
Mydac connection data base issue 3 107
canBalance challenge 34 65
WPF issue with Trigger 2 68
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now