Solved

Calculating a COM port checksum value

Posted on 2006-07-13
6
2,031 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
[X]
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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
My project did see openJDK that I installed. What could be the problem 7 170
bunnyEars2 challenge 6 182
delphi parse string to params 3 177
Message not shown 5 103
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 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