Search code examples
c#checksum

c# 2 byte calculation checksum


I need help with a checksum calculation. This is (not my code!) but from specification http://www.leupamed.at/?wpdmact=process&did=NC5ob3RsaW5r

private void CalcCheckSum(string msg, out byte checksum1, out byte checksum2)
{
byte cs1 = 0;
byte cs2 = 0;
// Always use "\n" as line break when calculating the checksum.
msg = msg.Replace("\r\n", "\n"); // Find and replace CR LF with LF
msg = msg.Replace("\r", "\n"); // Find and replace CR with LF.
for (int i = 0; i < msg.Length; i++)
{
cs1 += (byte) msg[i];
cs2 += cs1;
}
checksum1 = cs1;
checksum2 = cs2;
}

I must create a packet like this:

<!--:Begin:Chksum:1:--><!--:Ack:Msg:3:0:--><!--:End:Chksum:1:184:62:-->

The string <!--:Ack:Msg:3:0:--> is the actual data, I must calculate two checksum bytes (184 and 62) and insert these into the final packet (as seen above).

But my result from the calculation is 10 and 62

var msg = "<!--:Ack:Msg:3:0:-->";
byte checksum1 = 0;
byte checksum2 = 0;
CalcCheckSum(msg, out checksum1, out checksum2);

I don't now how to calculate correct checksum values. This is checksum for response. Not for validating request. I can't upload image due to low reputation, so look at last line in specification: https://drive.google.com/file/d/0B_Gs9q9SJteadVRwSVc1a2FmUTg/edit?usp=sharing This acknowledge message is independent on request. Only it must be response to request message ID 3.

Solution?

After calculating checksum:

checksum1 = 256 - (10 + 62) = 184

checksum2 = 62

Device communicating without problem, now.


Solution

  • After calculating checksum:

    checksum1 = 256 - (10 + 62) = 184

    checksum2 = 62

    Probably this question is too specific and no one has experience with this type of checksum calculation. Device communicating without problem, now.