Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Calculating a COM port checksum value

Posted on 2006-07-13
6
2,009 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
centeredAverage challenge 8 179
has12 challenge 13 73
Non-recursive backtracking, using a stack 1 172
Python 3.5.2 32 virtualenv problems 3 67
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

861 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