@AlexSer

Как посчитать контрольную сумму пакета на С++?

Программирую в QT(С++) обмен с устройством через serial port.
Устройство принимает данные по протоколу ASTM 1381-95. Так как я новичок в с++, то появились вопросы.
Как посчитать контрольную сумму отправляемого пакета?
Если я делаю запрос с устройство приходит такой вид пакетов:
1H|\^&|||ACL9000|||||||P|1|2020
0210111653
87
2Q|1|ALL||||||||O
C4
3L|1|N
06

Протоколе описана схема сообщения:
<STX>FN text<ETX>C1 C2<CR> <LF>
где С1 С2 контрольная сумма пакета. Соответственно 87, С4, 06 это контрольные суммы, я думаю.
Помогите посчитать их.
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 isadded 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
  • Вопрос задан
  • 405 просмотров
Решения вопроса 1
@tugo
6.3.3.1 The checksum is initialized to zero with thecharacter.

Хз что хотели этим сказать. Нужна картинка pdf стандарта.
The first character used in computing the checksum is the frame number. Each character in the message text isadded to the checksum (modulo 256). The computation for the checksum does not include, the checksum characters,or the trailingand.

Вроде бы
char frameNumber = ....;
char crc = frameNumber;
for (int i = 0; i <  messageSize; ++i)
{
    crc += message[i]
}

Надо экспериментировать.
Ну и перевести посчитанную контрольную сумму в ASCII представление, как написано.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы