Search code examples
checksum

Checksum calculation in visual basic


I want to communicate with a medical analyzer and send some messages to it. But the analyzer requests a control character or checksum at the end of the message to validate it.

You'll excuse my limited knowledge of English, but according to the manual, here is how to calculate that checksum:

The control character is the exclusion logic sum (exclusive OR), in character units, from the start of the text to the end of the text and consists of a 1 byte binary. Moreover, since this control character becomes the value of 00~7F by hexadecimal, please process not to mistake for the control code used in transmission.

So please can you tell me how to get this control character based on those informations. I did not understand well what is written because of my limited English.

I'm using visual basic for programming

Thanks


Solution

  • The manual isn't in very good English either. My understanding:

    The message you will send to the device can be represented as a byte array. You want to XOR together every single byte, which will leave you with a one byte checksum, to be added to the end of the message. (byte 1 XOR byte 2 XOR byte 3 XOR ....)

    Everything after "Moreover" appears to say "If you are reading from the device, the final character is a checksum, not a command, so do not treat it as a command, even if it looks like one.". You are writing to the device, so you can ignore that part.

    To XOR (bear in mind I don't know VB):

    have a checksum variable, set to 0. Loop over each byte of the message, and XOR the checksum variable with that byte. Store the result back in the checksum.