生活随笔
收集整理的這篇文章主要介紹了
DSP学习--BCC校验(异或校验)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
檢驗值(LRC) Data 部分數據各字節異或值。
參看:BCC校驗(異或校驗)在線計算
#include <stdio.h>
#include <string.h>
#include <stdint.h>static void strToHex(char* pbDest
, char* pbSrc
, int nLen
, int* frameLen
)
{uint32_t tmpLen
= 0;char h1
, h2
;char s1
, s2
;char tmppbSrc
[65535] = { 0 };for (int j
= 0; j
< nLen
; j
++){if (pbSrc
[j
] != '\0'){if (pbSrc
[j
] != ' '){tmppbSrc
[tmpLen
] = pbSrc
[j
];tmpLen
++;}if (pbSrc
[j
] == '\n'){pbSrc
[j
] = '\0';}}else{break;}}*frameLen
= tmpLen
/ 2;for (uint32_t i
= 0; i
< tmpLen
/ 2; i
++){h1
= tmppbSrc
[2 * i
];h2
= tmppbSrc
[2 * i
+ 1];s1
= toupper(h1
) - 0x30;if (s1
> 9){s1
-= 7;}s2
= toupper(h2
) - 0x30;if (s2
> 9){s2
-= 7;}pbDest
[i
] = s1
* 16 + s2
;}
}static void check_bit(uint8_t* Databuf
, uint32_t frameLen
, uint8_t* CheckBit
)
{uint8_t data_check
= 0, cfgTail
= 0;for (uint32_t i
= 0; i
< frameLen
; i
++){cfgTail
= cfgTail
^ Databuf
[i
];}data_check
= cfgTail
;printf("校驗碼為: %02X\n", data_check
);sprintf(CheckBit
, "%c%02X", ' ', data_check
);printf("\n\n\n[帶校驗碼數據已保存到 CRC.txt]\n");
}static int Get_check_bit(void)
{char CRCData
[65536] = { 0 };char CRCData1
[65536] = { 0 };char CheckBit
[20] = { 0 };FILE
* file
= NULL;file
= fopen
("CRC.txt", "a+");if (NULL == file
){printf("file open fault!\n");return -1;}if (fgets(CRCData
, 65536, file
) != NULL){if (fgets(CRCData1
, 65536, file
) != NULL){if (strcmp(CRCData1
, "\n") == 0){printf("[ERROR: 需要校準的數據,換行太多了!]\n");getchar();}if (strcmp(CRCData1
, "Check Out Data:\n") == 0){printf("[ERROR: 數據已經校準了!]\n");getchar();}}int frameLen
= 0;char buf
[65535] = { 0 };strToHex(buf
, CRCData
, sizeof(buf
), &frameLen
);check_bit(buf
, frameLen
, CheckBit
);CRCData
[frameLen
* 3 - 1] = '\0';}strcat(CRCData
, CheckBit
);fputs("\nCheck Out Data:\n", file
);fputs(CRCData
, file
);fclose(file
);return 0;
}int main(int argc
, char* argv
[])
{printf("[注意: CRC.txt 文件內不能包含校驗碼]\n\n\n");Get_check_bit();getchar();return 0;
}
測試:
CRC
.txt 內容:
30 01 03
Check Out Data
:
30 01 03 32
總結
以上是生活随笔為你收集整理的DSP学习--BCC校验(异或校验)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。