123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #include "crc8.h"
- /**
- * @funtion:crc8多项式冗余校验
- * @param 1:pData,计算数据源地址
- * @param 2:dataLen,计算数据源长度
- * @param 3:initialValue,crc结果初值
- * @param 4:polynomial,多项式
- *
- * @return :校验结果
- */
- unsigned char crc8( unsigned char *pData,
- unsigned int dataLen,
- unsigned char initialValue,
- unsigned char polynomial )
- {
- unsigned char i;
- unsigned char crc;
- crc = initialValue;
- while (dataLen --)
- {
- crc ^= *pData ++;
- for( i = 0; i < 8; i++ )
- {
- if(crc & 0x80)
- {
- crc <<= 1; // shift left once
- crc ^= polynomial; // XOR with polynomial
- }
- else
- {
- crc <<= 1; // shift left once
- }
- }
- }
- return crc;
- }
- /**
- * @funtion :针对温湿度传感器sht3X系列的crc8校验
- * @param 1:pData,计算数据源地址
- * @param 2:dataLen,计算数据源长度
- * @param 3:targetCRC,对比结果CRC
- *
- * @return :对比校验结果,=1校验成功,=0校验失败
- */
- unsigned char crc8_ger( unsigned char *pData,
- unsigned int dataLen)
- {
- return crc8(pData, dataLen, 0xff, 0x31);
- }
- /**
- * @funtion :针对温湿度传感器sht3X系列的crc8校验
- * @param 1:pData,计算数据源地址
- * @param 2:dataLen,计算数据源长度
- * @param 3:targetCRC,对比结果CRC
- *
- * @return :对比校验结果,=1校验成功,=0校验失败
- */
- int crc8_gernCheckT( unsigned char *pData,
- unsigned int dataLen,
- unsigned char targetCRC)
- {
- if (crc8(pData, dataLen, 0xff, 0x31) == targetCRC)
- {
- return 1;
- }
- return 0;
- }
- /**
- * @funtion :针对温湿度传感器sht3X系列的crc8校验
- * @param 1:pData,计算数据源地址
- * @param 2:dataLen,计算数据源长度
- * @param 3:targetCRC,对比结果CRC
- *
- * @return :对比校验结果,=1校验成功,=0校验失败
- */
- int crc8_sht3x( unsigned char *pData,
- unsigned int dataLen,
- unsigned char targetCRC)
- {
- if (crc8(pData, dataLen, 0xff, 0x31) == targetCRC)
- {
- return 1;
- }
- return 0;
- }
- /**
- * @funtion :针对温湿度传感器sht2X系列的crc8校验
- * @param 1:pData,计算数据源地址
- * @param 2:dataLen,计算数据源长度
- * @param 3:targetCRC,对比结果CRC
- *
- * @return :对比校验结果,=1校验成功,=0校验失败
- */
- int crc8_sht2x( unsigned char *pData,
- unsigned int dataLen,
- unsigned char targetCRC)
- {
- if (crc8(pData, dataLen, 0x00, 0x31) == targetCRC)
- {
- return 1;
- }
- return 0;
- }
- /**
- * @funtion :\
- * @param 1:pData,计算数据源地址
- * @param 2:dataLen,计算数据源长度
- * @param 3:targetCRC,对比结果CRC
- *
- * @return :对比校验结果,=1校验成功,=0校验失败
- */
- int cmp_crc8( uint8_t *pData,
- uint16_t dataLen,
- uint8_t targetCRC)
- {
- if (crc8(pData, dataLen, 0x55, 0x07) == targetCRC)
- {
- return 1;
- }
- return 0;
- }
- /**
- * @funtion :\
- * @param 1:pData,计算数据源地址
- * @param 2:dataLen,计算数据源长度
- *
- * @return :返回CRC结果
- */
- int get_crc8( uint8_t *pData,
- uint16_t dataLen)
- {
- return crc8(pData, dataLen, 0x55, 0x07);
- }
- bool checkFramLegal(uint8_t *srcBuffer, uint8_t srcLen)
- {
- bool ret = false;
- uint8_t len;
- uint8_t cmd;
- uint8_t crc1;
- // uint8_t crc2;
- len = srcBuffer[0];
- cmd = srcBuffer[1];
- crc1 = srcBuffer[len];
- // crc2 = srcBuffer[len];
- if (len == (srcLen - 1))
- {
- if ((cmd & 0x80) == 0)
- {
- if (cmp_crc8(srcBuffer, srcLen - 1, crc1))
- {
- ret = true;
- // ret = cmp_crc8(srcBuffer, srcLen - 1, crc2);
- }
- }
- else
- {
- ret = true;
- }
- }
- return ret;
- }
- void completFramParams(uint8_t *srcBuffer, uint8_t srcLen)
- {
- uint8_t *head;
- uint8_t *len;
- uint8_t *crc1;
- // uint8_t *crc2;
- uint8_t *end;
- len = &srcBuffer[0];
- crc1 = &srcBuffer[srcLen - 1];
- // crc2 = &srcBuffer[srcLen - 1];
- *len = srcLen - 1;
- *crc1 = get_crc8(srcBuffer, srcLen - 1);
- // *crc2 = get_crc8(srcBuffer, srcLen - 1);
- }
|