ASTM checksum



Could I have some help calculating a checksum for data transmission to a lab machine? Here are the ASTM specs for the checksum:

The checksum is computed by adding the binary values of the characters, keeping the least significant eight bits of the result. Each character in the message text is added to the checksum (modulo 256). The checksum is an integer represented by eight bits, it can be considered as two groups of four bits. The groups of four bits are converted to the ASCII characters of the hexadecimal representation. The two ASCII characters are transmitted as the checksum, with the most significant character first.

This is a sample message, where '40' is the checksum and the checksum is calculated using the 7 through to the <ETX>:
<STX>7L|1|N<CR><ETX>40<CR><LF>


Thus the checksum is calculated with this portion of the string:
7L|1|N<CR><ETX>

Of course the <CR> and <ETX> are sent as chr(13) and chr(03), not as they appear in the email here.

I have been working with unpack and the % operator but to no avail.

TIA
Spin
.



Relevant Pages

  • Re: Hex 0D 0A and serial question
    ... many secrets in sending data to a serial port using MSComm1. ... Your code is adding these characters to Out1 and Out2. ... You don't show how the checksum is ...
    (microsoft.public.vb.general.discussion)
  • Re: Help bits & bytes
    ... This bit of code translates an integer value, ... This bit of code takes a line of characters and an index into the line. ... It calculates the checksum on the line up to the index, ... (This a crap function contract by the way. ...
    (microsoft.public.dotnet.csharp.general)
  • Re: Python for Reverse Engineering
    ... There are 31 characters in the encoding. ... 124 bits are needed to encode the full space because ... this scheme then there's no extra space for a checksum. ... can change based on the index in the string. ...
    (comp.lang.python)
  • Re: ASTM checksum
    ... > Could I have some help calculating a checksum for data transmission to a ... Here are the ASTM specs for the checksum: ... Are the ASTM specs for a cksum different from the "ordinary" POSIX ...
    (comp.lang.perl.misc)