更新校验函数

This commit is contained in:
CSSC-WORK\murmur 2024-09-12 16:44:24 +08:00
parent 036a74c7ba
commit f6a6dd5d29

36
tcl.c
View File

@ -143,34 +143,44 @@ void retryTimerCallback(MultiTimer *timer, void *userData)
printf("Timer 1 fired at %lu ms\n", getPlatformTicks()); printf("Timer 1 fired at %lu ms\n", getPlatformTicks());
} }
/// @brief /// @brief 计算异或XOR校验
/// @param data /// @param data
/// @param len /// @param len
/// @return 0->校验失败 1->校验成功 /// @return 校验结果
int crc(uint8_t *data, uint8_t len) uint8_t bccCRC(uint8_t *data, uint8_t len)
{ {
return 0; uint8_t crc = 0;
for (int i = 0; i < len; i++)
{
crc ^= data[i];
}
return crc;
} }
/// @brief 检查数据有效性 /// @brief 检查数据有效性
/// @param data /// @param data
/// @param len /// @param len
/// @return 1->有效 0->无效 /// @return 0->有效 1->帧头无效 2->帧尾无效 3->校验位无效
int isDataValid(uint8_t *data, uint8_t len) int chkDataValid(uint8_t *data, uint8_t len)
{ {
// 按帧发送数据 // 按帧发送数据
// 判断帧的完整性及校验位 // 判断帧的完整性及校验位
if (data[0] != 0x5A || data[1] != 0xA5 || data[len - 1] != 0xED) // 帧头、帧尾校验 if (data[0] != 0x5A || data[1] != 0xA5) // 帧头校验
{ {
return 0; return 1;
} }
if (crc(data + 2, len - 2) != 0) // 校验位校验 if (data[len - 1] != 0xED) // 帧尾校验
{ {
return 0; return 2;
} }
return 1; if (bccCRC(data + 2, len - 4) != data[len - 2]) // 校验位校验
{
return 3;
}
return 0;
} }
/// @brief 用户接口接收数据回调 /// @brief 用户接口接收数据回调
@ -208,7 +218,7 @@ void userRecDataCallback(uint8_t *data, uint8_t len)
} }
else else
{ {
if (isDataValid(data, len)) if (chkDataValid(data, len))
{ {
lineTransmitData(data, len); lineTransmitData(data, len);
} }
@ -248,7 +258,7 @@ void lineRecCallback(uint8_t *data, uint8_t len)
} }
else else
{ {
if (isDataValid(data, len)) if (chkDataValid(data, len))
{ {
userTransmitData(data, len); userTransmitData(data, len);
} }