Calculate Checksum For ASTM Frame
The structure of a frame is something like below
1[STX][F#][Text][ETX][CHK1][CHK2][CR][LF]
If the record is longer than the maximum number of characters, it is divided into 2 or more frames. The intermediate frame text termination code is [ETB]
, and the final frame text termination code is [ETX]
, as shown below
1[STX][F#][Text][ETB][CHK1][CHK2][CR][LF]2[STX][F#][Text][ETB][CHK1][CHK2][CR][LF]3......4[STX][F#][Text][ETX][CHK1][CHK2][CR][LF]
[CHK1][CHK2]
is expressed by characters "0" - "9" and "A" - "F". Characters beginning from the character following [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
.
Suppose the frame is stored in input
, we can calculate the cheksum as below
1function calculateChecksum(input) {2 return input3 .slice(1)4 .split("")5 .reduce((a, b) => a + b.charCodeAt(0), 0)6 .toString(16)7 .slice(-2)8 .toUpperCase()9 .padStart(2, "0");10}1112var foo =13 "\x021H|\\^&|||CT-90^00-15 ^00001^^^^BD934079||||||||E1394-97|20171104070923\x0D\x03";14console.log(calculateChecksum(foo)); // '58'1516foo = "\x023O|1|||||20171104070923|||||N||||||||||||||F|||||\x0D\x03";17console.log(calculateChecksum(foo)); // 'A4'