void calc_bcc(uint8_t *pdata,uint16_t length,uint8_t *bcc_h,uint8_t *bcc_l)
{
uint8_t ret =0;
for(uint8_t i =0; i<length; i++)
{
ret = ret ^ *(pdata+i);
}
if((ret&0x0F) >= 0x0A)
{
*bcc_l = 0x37 + ret%16;
}
else
{
*bcc_l = 0x30 + ret%16;
}
if((ret&0xF0) >= 0xA0)
{
*bcc_h = 0x37 + ret/16;
}
else
{
*bcc_h = 0x30 + ret/16;
}
}
说明:这个bcc校验算出来0x0F,在协议里是把其当成字符处理,成了0x30,0x46.
这种校验方法常见于读卡器。
使用方法:
calc_bcc(recvbuf,RECV_LENG-3,&bcc_h,&bcc_l);
if((bcc_h ==recvbuf[RECV_LENG-3])&&(bcc_l == recvbuf[RECV_LENG-2])) // bcc CHECK
评论0