Vb.net calculate checksum

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.
rflorencioAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.