CRC校验

冰杰007 2017-09-27 05:43:47
CRC 校验,生成多项式:
X7+X6+X5+X2+1。怎么写CRC校验算法,数据B0 99 41 81 02 00 02 生成校验码
...全文
841 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
待续_1006 2018-06-07
  • 打赏
  • 举报
回复
这CSDN的广告真的是醉了
qq14923349 2018-06-06
  • 打赏
  • 举报
回复
网上爬个现成的吧?
schlafenhamster 2018-04-18
  • 打赏
  • 举报
回复

//CRC8 多项式为 X8+X2+X1+1 = 1 00000111 = 0x07 ;use "<<" rev= 11100000 =0xE0 use ">>"
// 或者         X8+X5+X4+1 = 1 00110001 = 0x31 rev= 10001100 =0x8C
unsigned char CRC8(unsigned char *ptr,unsigned char len)
{
    unsigned char crc;
    unsigned char i;
    crc = 0;
    while(len--)
    {
       crc ^= *ptr++;
       for(i = 0;i < 8;i++)
       {
           if(crc & 0x80)	crc = (crc << 1) ^ 0x07;
           else				crc <<= 1;
       }
    }
    return crc;
}
#if 0
	BYTE str[] ={0x55,0x55,0x01,0x04,0x00,0x04,0x14};// 0xB5
	BYTE str1[]={0x55,0x55,0x01,0x04,0x01,0x04,0x32};// 0xC2
	BYTE crc;
	crc=CRC8(str,7);
	afxDump.HexDump( "0x", &crc, 1, 1 );
	crc=CRC8(str1,7);
	afxDump.HexDump( "0x", &crc, 1, 1 );
//	afxDump << poly << " poly\n";// =0x51 = 1 01010001  X8+X6+X4+X0
#endif 

// data为指向校验数据的指针,length为长度,返回一个字节的校验码
BYTE CRC8Rev(unsigned char *ptr, unsigned char len)
{
	unsigned char crc = 0;// or 0xFF;
	int i;
	ptr += len-1;
	while(len--)
	{
		crc ^= *ptr--;
		for(i = 8;i > 0; i--)
		{
			if(crc & 1) crc = (crc >> 1) ^ 0xE0;
			else        crc >>= 1;
		}
	}
	return crc;
}
跟随我 2018-04-18
  • 打赏
  • 举报
回复
void CKysoftver2App::ToCRC(BYTE szBuffer[8]) { BYTE d; UINT uj,uk,CRC; CRC=0x0000FFFF; for (uj=0; uj<6; uj++) { CRC=CRC^szBuffer[uj]; for (uk=0; uk<8; uk++) { d=CRC & 0x00000001; CRC=CRC>>1; if (d!=0) CRC=CRC^0x0000A001; } } d=CRC & 0x000000FF; szBuffer[6]=d; d=(CRC>>8) & 0x000000FF; szBuffer[7]=d; //生成CRC校验 }
赵4老师 2017-09-28
  • 打赏
  • 举报
回复
仅供参考:
/***** crc16.c *****/
#include <stdio.h>

#define CRC16_DNP   0x3D65u     // DNP, IEC 870, M-BUS, wM-BUS, ...
#define CRC16_CCITT 0x1021u     // X.25, V.41, HDLC FCS, Bluetooth, ...

//Other polynoms not tested
#define CRC16_IBM   0x8005u     // ModBus, USB, Bisync, CRC-16, CRC-16-ANSI, ...
#define CRC16_T10_DIF   0x8BB7u     // SCSI DIF
#define CRC16_DECT  0x0589u     // Cordeless Telephones
#define CRC16_ARINC 0xA02Bu     // ACARS Aplications

#define POLYNOM     CRC16_DNP   // Define the used polynom from one of the aboves

// Calculates the new crc16 with the newByte. Variable crcValue is the actual or initial value (0).
unsigned short crc16(unsigned short crcValue, unsigned char newByte) {
    int i;

    for (i = 0; i < 8; i++) {
        if (((crcValue & 0x8000u) >> 8) ^ (newByte & 0x80u)){
            crcValue = (crcValue << 1)  ^ POLYNOM;
        } else {
            crcValue = (crcValue << 1);
        }
        newByte <<= 1;
    }
    return crcValue;
}

unsigned short exampleOfUseCRC16(unsigned char *Data, int len){

    unsigned short crc;
    int aux = 0;

    crc = 0x0000u; //Initialization of crc to 0x0000 for DNP
    //crc = 0xFFFFu; //Initialization of crc to 0xFFFF for CCITT

    while (aux < len){
        crc = crc16(crc,Data[aux]);
        aux++;
    }

    return (~crc); //The crc value for DNP it is obtained by NOT operation

    //return crc; //The crc value for CCITT
}

int main() {
    unsigned char d[10]={0,1,2,3,4,5,6,7,8,9};

    printf("0x%04hX\n",exampleOfUseCRC16(d,10));//0x1078
    return 0;
}

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧