expo-ble模块测试demo 联调BLE模块 take 2
This commit is contained in:
@@ -5,13 +5,19 @@ export class ProtocolManager {
|
||||
|
||||
static calculateChecksum(frameData: Uint8Array): number {
|
||||
let sum = 0;
|
||||
console.debug(`[ProtocolManager] Calculating checksum for frame count: ${frameData.length}`);
|
||||
// Checksum is calculated on all bytes except the last one (which is the checksum itself)
|
||||
// But here we are calculating FOR the last byte.
|
||||
// Example: 0xA0 03 00 01 01 5B
|
||||
// 0xA0 + 0x03 + 0x00 + 0x01 + 0x01 = 0xA5
|
||||
// 0 - 0xA5 = 0x5B
|
||||
for (let i = 0; i < frameData.length; i++) {
|
||||
sum += frameData[i];
|
||||
}
|
||||
console.debug(`[ProtocolManager] Checksum calculated: 0 - ${sum} = ${(0 - sum) & 0xff}`);
|
||||
return (0 - sum) & 0xff;
|
||||
const checksumV1 = (0 - sum) & 0xff
|
||||
const checksum = (~sum + 1) & 0xff
|
||||
console.debug(`[ProtocolManager] Checksum V1 calculated: 0 - ${sum} = ${checksumV1}`);
|
||||
console.debug(`[ProtocolManager] Checksum calculated: 0 - ${sum} = ${checksum}`);
|
||||
return checksum;
|
||||
}
|
||||
|
||||
static verifyChecksum(frameData: Uint8Array, expectedChecksum: number): boolean {
|
||||
|
||||
Reference in New Issue
Block a user