[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Vb.net calculate checksum

Posted on 2014-08-20
1
Medium Priority
?
3,387 Views
Last Modified: 2014-08-21
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
1 Comment
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40274319
Hi rflorencio;

The below code snippet will return the check sum of the scale data.

Dim input As String = "The data from the scale"
Dim hexData() As Byte = System.Text.Encoding.ASCII.GetBytes(input)

'' After calling the function GetCheckSum the variable will 
'' contain &H30 using your test data
Dim checkSum As Byte = GetCheckSum(hexData)

Private Function GetCheckSum(ByVal hexData() As Byte) As Byte
    '' The value to be returned to the caller                                                        
    Dim checkSum As Byte = 0;
    '' This is here so that we wait til the second value is being processed
    Dim isFirst As Boolean = True                           
    
    '' Calcuate the check sum. Do not process the last three characters and 
    '' the reason for -4 on the first line                                                        
    For i As Integer = 0 To hexData.Length - 4
        '' If checkSum does not already have a value then skip this iteration
        If isFirst Then                                     
            isFirst = False                                 
            checkSum = hexData(i)                           
            Continue For                                    
        End If
        '' Calculating the checkSum
        checkSum = checkSum Xor hexData(i)                  
    Next                                                    
                                                            
    Return checkSum                                         
                                                            
End Function  

Open in new window

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 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