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

Posted on 2009-02-10
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?
Question by:KJLC
    1 Comment
    LVL 18

    Accepted Solution

    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
    covers well this subject.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now