免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8345 | 回复: 2
打印 上一主题 下一主题

[C] 怎么用C语言编写对文件作奇偶校验的程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-03-14 00:54 |只看该作者 |倒序浏览
有哪位高手知道怎么用C语言编写对文件作奇偶校验的程序??

论坛徽章:
0
2 [报告]
发表于 2003-03-14 10:31 |只看该作者

怎么用C语言编写对文件作奇偶校验的程序

main()
{
int         i=0,j=0;
int         get,flag;
int         result;
int         length;
DWORD       index[5]={0,0,0,0,0};

/* 設定 com port 的初始狀態 */
result = init_com_port(com_port1);

/*** Write Buffer ***/
OutBuf[1] = 0x01;
OutBuf[2] = 0x00;
OutBuf[3] = 0x00;
OutBuf[4] = 0x01;
OutBuf[5] = 0x00;

flag=0;i=1;
while(i=1)
{  
  //First Station
  OutBuf[0] = 0x01;
  CRC16(OutBuf,6);
  // 先將要輸出的資料放在 OutBuf
  Sleep(DELAY_TINE);
  write_to_com(com_port1);
  // 讀到的資料在 InBuf
  Sleep(DELAY_TINE);
  index[0]=read_from_com(com_port1,0);

  //Second Station
  OutBuf[0] = 0x02;
  CRC16(OutBuf,6);
  // 先將要輸出的資料放在 OutBuf
  Sleep(DELAY_TINE);
  write_to_com(com_port1);
  //讀到的資料在 InBuf
  Sleep(DELAY_TINE);
  index[1]=read_from_com(com_port1,index[0]);
  
  //Third Station
  //OutBuf[0] = 0x03;
  //CRC16(OutBuf,6);
  // 先將要輸出的資料放在 OutBuf
  //Sleep(DELAY_TINE);
  //write_to_com(com_port1);
  // 讀到的資料在 InBuf
  //Sleep(DELAY_TINE);
  //index[2]=read_from_com(com_port1,index[1]);
  
  //Fourth Station
  //OutBuf[0] = 0x04;
  //CRC16(OutBuf,6);
  // 先將要輸出的資料放在 OutBuf
  //Sleep(DELAY_TINE);
  //write_to_com(com_port1);
  // 讀到的資料在 InBuf
  //Sleep(DELAY_TINE);
  //index[3]=read_from_com(com_port1,index[2]);

  //Fiveth Station
  //OutBuf[0] = 0x05;
  //CRC16(OutBuf,6);
  // 先將要輸出的資料放在 OutBuf
  //Sleep(DELAY_TINE);
  //write_to_com(com_port1);
  // 讀到的資料在 InBuf
  //Sleep(DELAY_TINE);
  //index[4]=read_from_com(com_port1,index[3]);

  //Assign Read PLC data to OldBuf
  if(flag==0)
  {
     for(j=0;j<REAL_LENGTH;j++)
             OldBuf[j]=InBuf[j];
     //Initial write read data to file
     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++;
  }

  //if OldBuf not equail InBuf then write data to File
  WRITE_FILE_FLAG=0;
  for(j=0;j<REAL_LENGTH;j++)
          if(OldBuf[j]!=InBuf[j])
          {
         WRITE_FILE_FLAG=1;
         OldBuf[j]=InBuf[j];
      }

  //Write read data to file
  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";
  }
  
  //Exit Program
  if(kbhit())
  {
         get=0;
     get=getch();
     if(get == 0)get=getch();
     if(get == ESC ) break;
  }

} // while loop

}   /*** End of main ***/


/******************************************************
*** Function Name : CRC16                          ***
*** Function Descr: Cyclic Redundancy Check        ***
*** Remark                                         ***
******************************************************/
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 &amp; parity_tab[j]) != 0)
               tt2 += 1;
          }
      if ((tt2 &amp; 0x01) != 0)
          bbl = 0x07;

      bbh = ddl;

      if ((bbl &amp; 0x01) != 0)
          tt = 1;
      else
          tt = 0;
      if ((bbh &amp; 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 &amp; 0x01) != 0)
         tt = 1;
     else
         tt = 0;
     if ((bbh &amp; 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;
} /*** End of CRC16 ***/

论坛徽章:
0
3 [报告]
发表于 2003-03-25 20:50 |只看该作者

怎么用C语言编写对文件作奇偶校验的程序

文件奇偶校证 是什么意思?
本人对c程序的buffer编写不懂(如设置
/*** Write Buffer ***/
OutBuf[1] = 0x01;
OutBuf[2] = 0x00;
OutBuf[3] = 0x00;
OutBuf[4] = 0x01;
OutBuf[5] = 0x00;

推荐些书,或解释一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP