最近在使用超声波液位计对他的校验甚是不解,函数有厂家提供如下,我得出的校验值和厂家老不一致?
厂家数据01 03 0000 0002 c40b c40b是校验值,高低互换
我算出来是dbcd
#include <reg51.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
#define POLYNOME 0xA001
uint crc;
k[]={0x01,0x03,0x00,0x00,0x00,0x02};
uint crc_ws(uchar *command, int len )
{
int i,j;
crc=0xffff;
for(j=0;j<len;j++)
{
crc=crc^command[j];
for(i=0;i<8;i++)
{
if((crc&0x0001)==1){
crc=crc>>1;
crc=crc^POLYNOME;
}else{
crc=crc>>1;
}
}
}
return(crc);
}
main()
{ uint t;
t=crc_ws(k,6);
while(1)
{}
} |