#include <stdio.h>
unsigned short calc_crc(unsigned char* bytes)
{
unsigned short real_crc=0x0000;
unsigned short tmp_crc=0x0000;
for(int i=2;i<7;i++)
{
tmp_crc = (bytes[i])<<(6-i);
real_crc=(real_crc + tmp_crc);
}
while(real_crc>0xFF)
{
real_crc = ((real_crc & 0xFF00)>>8) + (real_crc & 0x00FF);
}
return real_crc;
}
void main(void)
{
unsigned char bytes[9];
int number=0,all=0,bad=0;
unsigned short __CRC;
FILE *file = fopen("list.txt","r");
FILE *file_res = fopen("list_res.txt","w+");
while(!feof(file))
{
all++;
fscanf(file,"%d %hx %hx %hx %hx %hx %hx %hx %hx %hx\n",&number,&bytes[0],&bytes[1],&bytes[2],&bytes[3],&bytes[4],&bytes[5],&bytes[6],&bytes[7],&bytes[8]);
__CRC = calc_crc(bytes);
if(bytes[7] != __CRC)
{
bad++;
fprintf(file_res,"%06d 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x : 0x%02x\n",bad,bytes[0],bytes[1],bytes[2],bytes[3],bytes[4],bytes[5],bytes[6],bytes[7],bytes[8],__CRC);
}
}
printf("%03.2f%% not good\n",((float)bad/(float)all)*100.0);
fclose(file_res);
fclose(file);
scanf("%d",&number);
} |