[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Vb.net calculate checksum

Posted on 2014-08-20
2
Medium Priority
?
797 Views
Last Modified: 2014-08-20
I'm trying to communicate in vb.net with a scale, through the serial port. I can already communicate with the example provided by the manufacturer. However I do not know how to calculate the checksum of data sent to the scale.

Example supplied by the manufacturer:


1.1      Protocol
PC                                      Scale

                        98PPPPPCCRLF

            
Where

      98:       0x39h y 0x38h
PPPPP: 5 dígit for price
C:      Checksum, logical sum (XOR) of all preceding characters
CR:      0x0Dh                  LF: 0x0Ah

The scale expects the computer to send a message:
(Caracteres in ASCII)

      98PPPPPCCRLF
Where:
      9:      0x39h
      8:      0x38h
      PPPPP:      5 caracteres para el precio.
      C: Checksum.
      CR       0x13h
LF       0x10h

Checksum is an XOR, exclusive OR of all characters sent earlier, which is calculated as follows:
      0      XOR      0      0
      0      XOR      1      1
      1      XOR      0      1
      1      XOR      1      0


Will be doing XOR of all characters sent in hexadecimal, one by one:

Were sent 9 8 1 2 3 4 5 C CR LF
The value of C would:

      9:      0 0 1 1 1 0 0 1            (0x39h)
      8:      0 0 1 1 1 0 0 0            (0x38h)
            0 0 0 0 0 0 0 1
      1:      0 0 1 1 0 0 0 1            (0x31h)
            0 0 1 1 0 0 0 0
      2:      0 0 1 1 0 0 1 0            (0x32h)
            0 0 0 0 0 0 1 0
      3:      0 0 1 1 0 0 1 1            (0x33h)
            0 0 1 1 0 0 0 1
      4:      0 0 1 1 0 1 0 0            (0x34h)
            0 0 0 0 0 1 0 1
      5:      0 0 1 1 0 1 0 1            (0x35h)
            0 0 1 1 0 0 0 0            (0x30h)
The value of C would be 0 (0x30h), then the frame should be sent: 9 8 1 2 3 4 5 0 CR LF.
0
Comment
Question by:rflorencio
2 Comments
 
LVL 25

Accepted Solution

by:
SStory earned 2000 total points
ID: 40272499
I'm not sure I understand your problem, but. It seems you should get the part to be XORed into a string. Assuming you have that, the following should help you:

    Sub Main()
        Dim Part As String = "9812345"
        Dim Sum As Integer = -1
        For Each c As Char In Part
            If Char.IsDigit(c) Then
                If Sum = -1 Then
                    'first digit
                    Sum = Val(c)
                Else
                    Sum = Sum Xor Val(c)
                End If
            Else
                Console.WriteLine(String.Format("Invalid char present (non-digit) {0}", c))
            End If
        Next

        Console.WriteLine(Sum)
        Console.Read()
    End Sub

Open in new window

0
 

Author Closing Comment

by:rflorencio
ID: 40272918
After some testing I think is really what I needed. Great.
Thanks
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 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