操作系统:Debian7.5
前言
很多设备的通信命令中,都会用到校验,常用的有crc8,crc16等。现在封装一下crc16的实现。
一,源码
1,《crc.h》
-
#ifndef CRC_H
-
#define CRC_H
-
-
-
#include "commontypes.h"
-
-
#define CRC_INIT 0xFFFF
-
-
-
uint16 crc_polynomial(uint8 b, uint16 acc);
-
uint16 crc_sum(uint8* message, uint16 length, uint16 crc);
-
uint32 crc_verify(uint8* message, uint16 length);
-
void crc_append(uint8* message, uint16 length);
-
-
#endif
2,《crc.c》
-
//!-------------------------------------------------------------------
-
//! Include Files
-
//!-------------------------------------------------------------------
-
//#include "PR9200.h"
-
#include "crc.h"
-
-
-
//!-------------------------------------------------------------------
-
//! Definitions
-
//!-------------------------------------------------------------------
-
-
-
-
//!-------------------------------------------------------------------
-
//! Global Data Declaration
-
//!-------------------------------------------------------------------
-
-
-
//!-------------------------------------------------------------------
-
//! Fuction Definitions
-
//!-------------------------------------------------------------------
-
-
-
//!---------------------------------------------------------------
-
//! @brief
-
//! Polynomial functions for CRC (X16 + X12 + X5 + 1)
-
//!
-
//! @param
-
//! b: a byte data
-
//! acc: accumulated value
-
//!
-
//! @return
-
//! accumulated value
-
//!
-
//!---------------------------------------------------------------
-
uint16 crc_polynomial(uint8 b, uint16 acc)
-
{
-
acc = (uint8)(acc >> 8) | (acc << 8);
-
acc ^= b;
-
acc ^= (uint8)(acc & 0xff) >> 4;
-
acc ^= (acc << 8) << 4;
-
acc ^= ((acc & 0xff) << 4) << 1;
-
-
return acc;
-
}
-
-
//!---------------------------------------------------------------
-
//! @brief
-
//! Caculate 16-bit CRC on all the messages
-
//!
-
//! @param
-
//! * msg: data pointer
-
//! length: data length
-
//! crc: CRC initial value(0xFFFF)
-
//!
-
//! @return
-
//! crc value
-
//!
-
//!---------------------------------------------------------------
-
uint16 crc_sum(uint8* msg, uint16 length, uint16 crc)
-
{
-
uint16 i;
-
-
for(i = 0; i < length; i++) {
-
crc = crc_polynomial(msg[i],crc);
-
}
-
return crc;
-
}
-
-
//!---------------------------------------------------------------
-
//! @brief
-
//! Check the CRC validity
-
//!
-
//! @param
-
//! * msg: data pointer
-
//! length: data length
-
//!
-
//! @return
-
//! valid(0) / invalid(Not 0)
-
//!
-
//!---------------------------------------------------------------
-
uint32 crc_verify(uint8* msg, uint16 length)
-
{
-
uint16 expected;
-
-
expected = crc_sum(msg, length - 2, CRC_INIT);
-
return !( ( expected & 0xff) == msg[length - 1] &&
-
((expected >> 8) & 0xff) == msg[length - 2] );
-
}
-
-
-
//!---------------------------------------------------------------
-
//! @brief
-
//! Append the CRC value to the end of data
-
//!
-
//! @param
-
//! * message: data pointer
-
//! length: data length
-
//!
-
//! @return
-
//! None
-
//!
-
//!---------------------------------------------------------------
-
void crc_append(uint8* message, uint16 length)
-
{
-
#ifdef __FEATURE_ENABLE_RCP_CRC__
-
-
uint16 crc;
-
-
crc = crc_sum(message, length, CRC_INIT);
-
message[length+1] = (uint8)(crc & 0xff);
-
message[length] = (uint8)((crc >> 8) & 0xff);
-
-
#endif
-
}
二,测试结果
三,源码包
crc16.rar
阅读(3317) | 评论(0) | 转发(1) |