📄 crc_func.cpp
字号:
// device_usb.cpp : implementation file
//
#include "stdafx.h"
#include "crc_func.h"
#include "crc_uiDlg.h"
#include <stdio.h>
#include <string.h>
#include "global.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struct trans_info transfer_info;
/******************************** crc 16 tx ************************************/
unsigned short crc_16=0x8005;
unsigned short data[1024];
char BinStr[20480]="";
char result[20480]="";
char TempStr[20480]="";
unsigned short crc_5=5;
char str[30] ;
crc_func::~crc_func()
{
}
unsigned short CRC_16(unsigned short *aData,unsigned long aSize)
{
unsigned short sign,a;
unsigned short temp;
int i,j;
a=0xffff;
//aData+=(aSize-1);
sign=0x0001;
for(j=aSize;j>0;j--)
{//printf("%x,",*aData);
for(i=0;i<8;i++)
{ temp=a&0x8000;
temp=temp>>15;
//printf("%x,",(*aData&sign)>>i);
if(((*aData&sign)>>i)^temp)
{
a=a<<1;
a=(a^crc_16);
// printf("%x,",a);
}
else
a=a<<1;
printf("%x,",a);
sign=sign<<1;}
aData++;
sign=0x0001;
}
a^=0xffff;
return a;
}
unsigned long readf_crc16()
{
int i;
FILE *fp;
if((fp=fopen(transfer_info.source,"r"))==NULL)
{
printf("can not open the file!\n");
return -1;
}
for(i=0;!feof(fp);i++)
fscanf(fp,"%x",&data[i]);
if(fclose(fp))
{
printf("can not close the file!\n");
return -1;
}
//printf("%d,",i);
return i;
}
void StrToBinStr(unsigned short a)
{
char *Bin[]={"00","01","10","11"};
int i;
//char BinStr[]="";
unsigned short b[2];
b[0]=a>>4;
b[1]=a&0x0f;
for(i=0;i<=1;i++)
{
strcat(BinStr,Bin[b[i]/4]);
//字符转为Bin后的高四位
strcat(BinStr,Bin[b[i]%4]); //字符转为Bin后的低四位
} //puts(BinStr);
}
int bitstuff()
{
int i,j=0,cnt=1;//结束sync结构的数据1被认为是序列的第一位
for(i=0;i < strlen(BinStr);i++,j++)
{
if(BinStr[i]=='1')
{
result[j]=BinStr[i];
cnt++;
if(cnt==6)
{
j++;
result[j]='0';
cnt=0;
}
}
else if(BinStr[i]=='0')
{
result[j]=BinStr[i];
cnt=0;
}
}
return j;
}
void kf()
{
FILE *fp;
int a=0;
char k[]="10";
if(transfer_info.mode == CRC16_FLAG){
if((fp=fopen(RESULT_FULL_16,"a"))==NULL)
printf("file open error!\n");
}
else{
if((fp=fopen(RESULT_FULL_5,"a"))==NULL)
printf("file open error!\n");
}
fprintf(fp,"%s",transfer_info.dn_str);
fputc(k[0],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.dp_str);
fputc(k[1],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_str);
fputc(k[1],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_delay);
fputc('\n',fp);
if(fclose(fp))
printf("Can not close the file!\n");
}
void jf()
{
FILE *fp;
int b=0;
char j[]="01";
if(transfer_info.mode == CRC16_FLAG){
if((fp=fopen(RESULT_FULL_16,"a"))==NULL)
printf("file open error!\n");
}
else{
if((fp=fopen(RESULT_FULL_5,"a"))==NULL)
printf("file open error!\n");
}
fprintf(fp,"%s",transfer_info.dn_str);
fputc(j[0],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.dp_str);
fputc(j[1],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_str);
fputc(j[1],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_delay);
fputc('\n',fp);
if(fclose(fp))
printf("Can not close the file!\n");
}
void syncf()
{
int i;
for(i=0;i<3;i++)
{
kf();
jf();
}
kf();
kf();
}
void nrzif_tx16(int n)
{
FILE *fp;
int i,p;
if(result[0]=='1')
{ kf();
p=0;}
else if(result[0]=='0')
{jf();
p=1;}
for(i=1;i<n;i++)
{
if(result[i]=='1'&&p==0)
{kf();
p=0;}
else if(result[i]=='1'&&p==1)
{
jf();
p=1;}
else if(result[i]=='0'&&p==0)
{jf();
p=1;}
else if(result[i]=='0'&&p==1)
{kf();
p=0;}
}
if((fp=fopen(RESULT_FULL_16,"a"))==NULL)
printf("file open error!\n");
fprintf(fp,"%s",transfer_info.dn_str);
fputc('0',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.dp_str);
fputc('0',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_str);
fputc('1',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_delay);
fputc('\n',fp);
fprintf(fp,"%s",transfer_info.dn_str);
fputc('0',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.dp_str);
fputc('0',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_str);
fputc('1',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_delay);
fputc('\n',fp);
if(fclose(fp))
printf("Can not close the file!\n");
jf();
}
void kh()
{
FILE *fp;
int a=0;
char k[]="10";
if(transfer_info.mode == CRC16_FLAG){
if((fp=fopen(RESULT_HIGH_16,"a"))==NULL)
printf("file open error!\n");
}
else{
if((fp=fopen(RESULT_HIGH_5,"a"))==NULL)
printf("file open error!\n");
}
fprintf(fp,"%s",transfer_info.dn_str);
fputc(k[0],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.dp_str);
fputc(k[1],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_str);
fputc(k[1],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_delay);
fputc('\n',fp);
if(fclose(fp))
printf("Can not close the file!\n");
}
void jh()
{
FILE *fp;
int b=0;
char j[]="01";
if(transfer_info.mode == CRC16_FLAG){
if((fp=fopen(RESULT_HIGH_16,"a"))==NULL)
printf("file open error!\n");
}
else{
if((fp=fopen(RESULT_HIGH_5,"a"))==NULL)
printf("file open error!\n");
}
fprintf(fp,"%s",transfer_info.dn_str);
fputc(j[0],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.dp_str);
fputc(j[1],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_str);
fputc(j[1],fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_delay);
fputc('\n',fp);
if(fclose(fp))
printf("Can not close the file!\n");
}
void synch()
{ int i;
for(i=0;i<15;i++)
{
kh();
jh();
}
kh();
kh();
}
void nrzih_tx16(int n)
{
int i,p;
if(result[0]=='1')
{ kh();
p=0;}
else if(result[0]=='0')
{jh();
p=1;}
for(i=1;i<n;i++)
{
if(result[i]=='1'&&p==0)
{kh();
p=0;}
else if(result[i]=='1'&&p==1)
{
jh();
p=1;}
else if(result[i]=='0'&&p==0)
{jh();
p=1;}
else if(result[i]=='0'&&p==1)
{kh();
p=0;}
}
if(p==0)
for(i=0;i<8;i++)
jh();
else if(p==1)
for(i=0;i<8;i++)
kh();
}
DWORD crc_func::crc16_tx(void)
{
int k,m;
int j,i;
int n;
unsigned short c;
FILE *fp;
j=readf_crc16();//从data.txt中按字节读取数据存入data全局数组中,j保存读入数据字节数
c=CRC_16(data,j);//求该数据的crc16
printf("--%x",c);
data[j]=c>>8;
data[j+1]=c&0x00ff;//将crc16加入到原数据尾部
for(k=0;k<j;k++)
{
StrToBinStr(data[k]);
}
n=strlen(BinStr);
for(m=1;m<=n/8;m++)
for(k=8*m-1,i=8*(m-1);k>=(m-1)*8;k--,i++)
{
TempStr[i]=BinStr[k];
// putchar(TempStr[i]);
}
strcpy(BinStr,TempStr);
//puts(BinStr);
StrToBinStr(data[j]);
StrToBinStr(data[j+1]);
//将该16进制数组转化位2进制字符串保存在BinStr全局字符数组中
k=bitstuff();//进行位填充,k为填充后的字符数目
puts(result);
//--将数据-crc16结果写入全速文档
if((fp=fopen(RESULT_FULL_16,"a"))==NULL)
printf("can not open file resultf.txt!\n");
for(n=0;n<j;n++)
fprintf(fp,"%x-",data[n]);
fprintf(fp,"%x$",data[j]);
fprintf(fp,"%x",data[j+1]);
fputc('\n',fp);
if(fclose(fp))
printf("can not close the file!\n");
syncf();//加同步位写入文档
nrzif_tx16(k);//进行nrzi编码并加包尾写入文档
//--将数据-crc16结果写入高速文档
if((fp=fopen(RESULT_HIGH_16,"a"))==NULL)
printf("can not open file resultf.txt!\n");
for(n=0;n<j;n++)
fprintf(fp,"%x-",data[n]);
fprintf(fp,"%x$",data[j]);
fprintf(fp,"%x",data[j+1]);
fputc('\n',fp);
if(fclose(fp))
printf("can not close the file!\n");
synch();//加同步位写入文档
nrzih_tx16(k); //进行nrzi编码并加包尾写入文档
return 0;
}
/******************************** crc 5 tx ************************************/
unsigned long Crc5(unsigned long crc)
{
unsigned long sign;
unsigned char a;
unsigned short temp;
int i;
//printf("%x,",crc);
a=0xff;
crc=crc<<5;
sign=0x8000;
for(i=11;i>0;i--)
{ temp=a&0x10;
//printf("%x,",temp);
//printf("%x,",crc&sign);
if((crc&sign)^(temp<<i))
{ a=a<<1;
a=(a^crc_5);
}
else
a=a<<1;
//printf("%x,",a);
sign=sign>>1;
}
a^=0xff;
a&=0x1f;
return a;
}
void readf_crc5()
{
int i;
FILE *fp;
if((fp=fopen(transfer_info.source,"r"))==NULL)
{printf("can not open the file!\n");
}
for(i=6;i>=0;i--)
{ str[i]=fgetc(fp);
}
for(i=10;!feof(fp);i--)
{ str[i]=fgetc(fp);
}
if(fclose(fp))
{printf("can not close the file!\n");
}
//puts(str);
}
unsigned long tox()
{
unsigned long ox=0;
int k,n=1;
if(str[10]=='1')
ox=1;
for(k=9;k>=0;k--)
{
if(str[k]=='1')
ox+=2*n;
n*=2;
//printf("%x,",ox);
}//printf("%x",ox);
return ox;
}
void StrToBinStr_tx5(unsigned long a,char *BinStr5)
{ char *Bin[]={"00","01","10","11"};
int i;
unsigned long b[2];
b[0]=a>>4;
b[1]=a&0x0f;
for(i=0;i<=1;i++)
{
strcat(BinStr5,Bin[b[i]/4]);
//字符转为Bin后的高四位
strcat(BinStr5,Bin[b[i]%4]); //字符转为Bin后的低四位
}
}
int bitstuff_tx5(char *re)
{
int i,j=0,cnt=1;//结束sync结构的数据1被认为是序列的第一位
for(i=0;i<strlen(str);i++,j++)
{
if((str[i])=='1')
{*(re+j)=str[i];
cnt++;
if(cnt==6)
{j++;
*(re+j)='0';
cnt=0;
}
}
else if(str[i]=='0')
{*(re+j)=str[i];
cnt=0;}
}
return j;
}
void nrzif_tx5(int n,char *result_5)
{FILE *fp;
int i,p;
if(result_5[0]=='1')
{ kf();
p=0;}
else if(result_5[0]=='0')
{jf();
p=1;}
for(i=1;i<n;i++)
{
if(result_5[i]=='1'&&p==0)
{kf();
p=0;}
else if(result_5[i]=='1'&&p==1)
{
jf();
p=1;}
else if(result_5[i]=='0'&&p==0)
{jf();
p=1;}
else if(result_5[i]=='0'&&p==1)
{kf();
p=0;}
}
if((fp=fopen(RESULT_FULL_5,"a"))==NULL)
printf("file open error!\n");
fprintf(fp,"%s",transfer_info.dn_str);
fputc('0',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.dp_str);
fputc('0',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_str);
fputc('1',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_delay);
fputc('\n',fp);
fprintf(fp,"%s",transfer_info.dn_str);
fputc('0',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.dp_str);
fputc('0',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_str);
fputc('1',fp);
fputs(" ; ",fp);
fprintf(fp,"%s",transfer_info.rxd_delay);
fputc('\n',fp);
if(fclose(fp))
printf("Can not close the file!\n");
jf();
}
void nrzih_tx5(int n,char * result)
{
int i;
int p = 0;
if(result[0]=='1')
{ kh();
p=0;}
else if(result[0]=='0')
{jh();
p=1;}
for(i=1;i<n;i++)
{
if(result[i]=='1'&&p==0)
{kh();
p=0;}
else if(result[i]=='1'&&p==1)
{
jh();
p=1;}
else if(result[i]=='0'&&p==0)
{jh();
p=1;}
else if(result[i]=='0'&&p==1)
{kh();
p=0;}
}
if(p == 0)
for(i=0;i<8;i++)
jh();
else if(p==1)
for(i=0;i<8;i++)
kh();
}
char result_tx5[] = "";
char s_tx5[8]="";
DWORD crc_func::crc5_tx(void)
{
unsigned long data;
unsigned long hehe;
int i,p;
FILE *fp;
readf_crc5();//将data.txt中11bit数据读入str[]全局数组
//puts(str);
data=tox();//将str数组中数据转换为16进制数
// printf("%x",data);
hehe=Crc5(data);//求出该数据crc5
//hehe=hehe^0x001f;//对crc取反
//printf("%x",hehe);
//--将数据-crc5结果写入全速文档
if((fp=fopen(RESULT_FULL_5,"a"))==NULL)
{
printf("can not open file resultf.txt!\n");}
//fputc('\n',fp);
fprintf(fp,"%x-",data);
fprintf(fp,"%x",hehe);
fputc('\n',fp);
if(fclose(fp))
printf("can not close the file!\n");
//--将数据-crc5结果写入高速文档
if((fp=fopen(RESULT_HIGH_5,"a"))==NULL)
{
printf("can not open file resultf.txt!\n");}
// fputc('\n',fp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -