- 论坛徽章:
- 0
|
各位大哥大姐,请帮我调试一下这个程序,错误很多的,帮帮忙了,谢谢,是一个文件奇偶校验码的程序:
main()
{
int i=0,j=0;
int get,flag;
int result;
int length;
DWORD index[5]={0,0,0,0,0};
result = init_com_port(com_port1);
OutBuf[1] = 0x01;
OutBuf[2] = 0x00;
OutBuf[3] = 0x00;
OutBuf[4] = 0x01;
OutBuf[5] = 0x00;
flag=0;i=1;
while(i=1)
{
OutBuf[0] = 0x01;
CRC16(OutBuf,6);
Sleep(DELAY_TINE);
write_to_com(com_port1);
Sleep(DELAY_TINE);
index[0]=read_from_com(com_port1,0);
OutBuf[0] = 0x02;
CRC16(OutBuf,6);
Sleep(DELAY_TINE);
write_to_com(com_port1);
Sleep(DELAY_TINE);
index[1]=read_from_com(com_port1,index[0]);
if(flag==0)
{
for(j=0;j<REAL_LENGTH;j++)
OldBuf[j]=InBuf[j];
length=index[0]+index[1]+index[2]+index[3]+index[4];
ofstream outfile(FILE_PATH);
for(j=0;j<length;j++)
outfile<<InBuf[j];
outfile.close;
cout<<"\nWrite File To Save OK\n";
flag++;
}
WRITE_FILE_FLAG=0;
for(j=0;j<REAL_LENGTH;j++)
if(OldBuf[j]!=InBuf[j])
{
WRITE_FILE_FLAG=1;
OldBuf[j]=InBuf[j];
}
if(WRITE_FILE_FLAG==1)
{
length=index[0]+index[1]+index[2]+index[3]+index[4];
ofstream outfile(FILE_PATH);
for(j=0;j<length;j++)
outfile<<InBuf[j];
outfile.close;
cout<<"\nWrite File To Save OK\n";
}
if(kbhit())
{
get=0;
get=getch();
if(get == 0)get=getch();
if(get == ESC break;
}
}
}
CRC16(unsigned char buf[], int lgn)
{
unsigned char parity_tab[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
unsigned char bbl, ddl, bbh, ddh, tt, tt1, tt2;
int i, j;
lgn+=2 ;
bbl = 0xff;
ddl = 0xff;
bbh = 0xff;
ddh = 0xff;
for (i = 0; i < lgn - 2; i++)
{
tt = buf;
bbl = bbl ^ bbl;
ddl = ddl ^ tt;
tt2 = 0;
for (j = 0; j < 8; j++)
{
if ((ddl & parity_tab[j]) != 0)
tt2 += 1;
}
if ((tt2 & 0x01) != 0)
bbl = 0x07;
bbh = ddl;
if ((bbl & 0x01) != 0)
tt = 1;
else
tt = 0;
if ((bbh & 0x01) != 0)
tt1 = 1;
else
tt1 = 0;
bbh = bbh >;>; 1;
bbl = bbl >;>; 1;
if (tt == 1)
bbh = bbh | 0x80;
if (tt1 == 1)
bbl = bbl | 0x80;
bbh = bbh ^ ddl;
if ((bbl & 0x01) != 0)
tt = 1;
else
tt = 0;
if ((bbh & 0x01) != 0)
tt1 = 1;
else
tt1 = 0;
bbh = bbh >;>; 1;
bbl = bbl >;>; 1;
if (tt == 1)
bbh = bbh | 0x80;
if (tt1 == 1)
bbl = bbl | 0x80;
bbl = bbl ^ ddh;
ddl = bbl;
ddh = bbh;
}
buf[lgn - 2] = ddl;
buf[lgn - 1] = ddh;
}
急急等 |
|