奇偶校验的程序

来源:互联网 发布:淘宝高佣金 手机查找 编辑:程序博客网 时间:2024/06/11 14:13
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[i];
      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;
} /*** End of CRC16 ***/ 
原创粉丝点击