We help IT Professionals succeed at work.

VB6 How do you calculate the checksum and split it into 2 bytes the first high a 2nd low?

Medium Priority
860 Views
Last Modified: 2013-11-25
We have 22 bytes to send via rs232 to a device usign vb6, the first byte we think is the length of the message. The last two bytes we think are reserved for the checksum.

How do you calculate the checksum for this and split it into 2 bytes the first high a 2nd low?
Comment
Watch Question

Graphics Expert
CERTIFIED EXPERT
Commented:
There are several ways to calculate a checksum. The simplest one is to add all the bytes together, considering just the lower byte. For example, supose we have a message with three bytes (in hex) :    45  7A   BE
The sum is 45 + 7A +  BE  =  17D, so the checksum is hex 7D.
It is up to you to define how you want the checksum. Of course it can be 5 bytes long if you want.
To send such checksum and the message together, in general we sen in the following sequence:
1. Message lenght.
    If yours is somethig around 20 bytes, then a single byte is enough.
     To minimize risks of future messages to not fit this limitationm you can use 2 bytes
2. Message body
     As many bytes you have informed before.
3. Checksum
     If your purpose is only a simple way to check if the message iwas sent correctly,
     a simple byte is enough. If you want to be more rigorous, use two bytes.
     If you want more than a simple checking, for example, you want a very secure
     checking, you can use a hash, which is a bit more complicated than just add bytes.
Of course, the important thing is to observe the same criteria for both sides, the sender and the receiver.
When sending a two byte number, you decide which byte send first: the lower or the upper. Again, the important is to use the same method to send and to receive.
To split a number greater than 255 (and smaller than 65535) into two bytes, we make:
LOWER = NUMBER Mod 256
UPPER = NUMBER \ 256

To view details on how to send/receive data via RS232, you should use the COM port. The tutorial at http://www.thaiio.com/prog-cgi/0005_vb.html
covers well this subject.
Jose

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.