You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
447 B
JavaScript

5 months ago
// CRC 校验模块
// 计算数据包的 CRC 校验值
function calculateCRC(dataPacket) {
let sum = 0;
for (let i = 0; i < 15; i++) {
sum += dataPacket[i];
}
return sum & 0xFF; // 取最低8位
}
// 校验CRC值
function validateCRC(dataPacket) {
const receivedCRC = dataPacket[15];
const calculatedCRC = calculateCRC(dataPacket);
return receivedCRC === calculatedCRC;
}
module.exports = {
calculateCRC,
validateCRC
};