# How to calculate the checksum for Laboratory Information System (LIS) frames?

I am developing instrument driver and I want to know how to calculate checksum for a Laboratory Information System (LIS) frame.

Explanation:

Expressed by characters [0-9] and [A-F].

Characters beginning from the character after [STX] and until [ETB] or [ETX] (including [ETB] or [ETX]) are added in binary.

The 2-digit numbers, which represent the least significant 8 bits in hexadecimal code, are converted to ASCII characters [0-9] and [A-F].

The most significant digit is stored in CHK1 and the least significant digit in CHK2.

This is sample frame :

``````<STX>2Q|1|2^1||||20011001153000<CR><ETX><CHK1><CHK2><CR><LF>
``````

and I want to know what is value of chk1 and chk2 and I am new in this so I am totally blank about how to calculate checksum

I am not getting above 3rd and 4th point.

Can anyone provide sample code for c#?

• Is this for an LIS system? Do you have access to the standard? Jun 11, 2012 at 5:03
• What sort of checksum does the instrument support. There are lots of different ways to generate a checksum. SHA1 and MD5 are two of the ways. You could also just figure out what the sum of all the bytes as an integer value. Jun 11, 2012 at 12:43
• @Ramhound this is custom algorithm, which is describe above. and nothing describe about which type of algorithm should used only specified above explanation in documentation. Jun 11, 2012 at 13:00
• @MattH yes this is connected with LIS system but i want to communicate with laboratory instrument which send data with checksum so i need to check whether this data completely received or not. Jun 11, 2012 at 13:02

## 2 Answers

Speaking as someone who has developed an entire LIS communication protocol from serial driver to strongly typed entities for a machine used in hundreds of different labs.

I've come to a lot of conclusions (short list):

1. The LIS Spec is bullshit (every LIS vendor has their own implementation which differs from every other) every hospital/lab LIS differs in the way the handshaking is written.
2. The Frame "Checksum" is nothing more than a stupid calculation which is meant to make people who wrote the spec feel better (it is in no way a good checksum as it can be easily overcome)
3. Whoever wrote the "State Machine" documentation for the LIS spec was a hack. As it does not properly convey any state nor transition (it is best to extrapolate your own stat machine than try to make sense of the drawing)

That being said:

http://www.hendricksongroup.com/code_003.aspx

Is a really simple example of how to calculate a checksum for an LIS frame.

If you are writing an LIS Driver my advice to you is to get LOTS of logs from the vendor (so that you may see exactly the communication taking place) you are connecting to and write LOTS of unit tests to match those logs, (that and if it needs to be for more than one vendor to make it EXTREMELY flexible). Oh and add lots of logging for yourself.

Finally I got answer.

Here is c# code for calculate checksum.

``````private string CalculateChecksum(string dataToCalculate)
{
byte[] byteToCalculate = Encoding.ASCII.GetBytes(dataToCalculate);

int checksum = 0;

foreach (byte chData in byteToCalculate)
{
checksum += chData;
}

checksum &= 0xff;

return checksum.ToString("X2");
}
``````
• So string 'ad' has the same checksum as 'bc' ? That's not a very good checksum. How about not reinventing a wheel and just going with a a CRC or Adler or MD5, or SHA-family... plenty of choices out there. Jul 2, 2012 at 16:42
• Basically, isn't this the slightly modified code that is linked in maxfridbe's answer? Did you really need to rewrite it and accept your own answer? Jul 2, 2012 at 21:19
• @Marcin and Ramhound: Since apparently this is the only checksum the device on the other end of the LIS network will accept, then any other checksum -- even those that are technically better -- won't work on that network. I don't see how it is helpful to point out a bunch of "choices" that won't work with the original poster's hardware. Jul 3, 2012 at 2:55
• @PBrando what ever link given by @ maxfridbe and i used it but that code not working so i find solution for that and i post my solution and it tested on instrument and working fine. and one more thing what ever i given my solution which exact solution for my question description.and i am appreciate for solution of @ maxfridbe that one clear understating. Jul 3, 2012 at 5:30