# 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.
###### 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.

Commented:
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)
End Sub
``````
0

Experts Exchange Solution brought to you by

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

Author Commented:
After some testing I think is really what I needed. Great.
Thanks
0
###### 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.