A
A
AlexSer2020-02-18 08:19:27
Qt
AlexSer, 2020-02-18 08:19:27

How to calculate the checksum of a package in C++?

I am programming in QT(C++) exchange with the device via serial port.
The device accepts data according to the ASTM 1381-95 protocol. Since I'm new to c++, I have some questions.
How to calculate the checksum of a sent packet?
If I make a request from the device, this type of packet comes up:

1H|\^&|||ACL9000|||||||P|1|2020
0210111653
87
2Q|1|ALL||||||||O
C4
3L|1|N
06

The protocol describes the message scheme:
<STX>FN text<ETX>C1 C2<CR> <LF>
where C1 C2 is the checksum of the packet. Accordingly, 87, C4, 06 are checksums, I think.
Help me count them.
6.3.3.1 The checksum is initialized to zero with thecharacter. The first character used in computing the checksum is the frame number. Each character in the message text is added to the checksum (modulo 256). The computation for the checksum does not include, the checksum characters, or the trailingand.6.3.3.2 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 hex a decimal representation. The two ASCII characters a retransmitted as the checksum, with the most significant character first.6.3.3.3 For example, a checksum of 122 can be represented as 01111010 in binary or 7A in hexadecimal. The checksum is transmitted as the ASCII character 7 followed by the character A

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tugo, 2020-02-19
@AlexSer

6.3.3.1 The checksum is initialized to zero with thecharacter.

Xs what they wanted to say. Need a pdf image.
The first character used in computing the checksum is the frame number. Each character in the message text is added to the checksum (modulo 256). The computation for the checksum does not include, the checksum characters, or the trailingand.

It seems to be
char frameNumber = ....;
char crc = frameNumber;
for (int i = 0; i <  messageSize; ++i)
{
    crc += message[i]
}

We must experiment.
Well, translate the calculated checksum into an ASCII representation, as written.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question