新闻  |   论坛  |   博客  |   在线研讨会
与各种PLC通信时,检验算法子程序集
张阳 | 2008-05-23 12:15:36    阅读:1517   发布文章

和检验程序:

char *plc_ADD(const uchar *pstr,uchar icount,uchar *pADD)
{
 uchar i,ch,ct,temp;
 if(pstr==NULL || pADD==NULL)
  return NULL;
 ch=*pstr;
 for(i=1;i {
  ch +=*(pstr+i);
 }
 temp=ch;
 ct=temp>>4;
 if(ct>9)
  ct+='A'-10;
 else
  ct+='0';
 *pADD=ct;
 ct=ch&0x0F;
 if(ct>9)
  ct+='A'-10;
 else
  ct+='0';
 *(pADD+1)=ct;
 return pADD; 
}

FCS检验程序:

char *Omron_FCS(const uchar *pstr,uchar icount,uchar *pFCS)
{
 uchar i,ch,ct,temp;
 if(pstr==NULL || pFCS==NULL)
  return NULL;
 ch=*pstr;
 for(i=1;i {
  ch ^=*(pstr+i);
 }
 temp=ch;
 ct=temp>>4;
 if(ct>9)
  ct+='A'-10;
 else
  ct+='0';
 *pFCS=ct;
 ct=ch&0x0F;
 if(ct>9)
  ct+='A'-10;
 else
  ct+='0';
 *(pFCS+1)=ct;
 return pFCS; 
}
LRC检验程序:
char *plc_LRC(const uchar *pstr,uchar icount,uchar *pLRC)
{
 uchar temp,i,ct,ch;
 if(pstr==NULL || pLRC==NULL)
  return NULL;
 for(i=0,ch=0;i {
  ct=valstr(pstr+i);
  temp=valstr(pstr+i+1);
  ch+=(ct<<4)|(temp&0x0F);
 }
 ch=0xff-ch+1;
 temp=ch;
 ct=temp>>4;
 if(ct>9)
  ct+='A'-10;
 else
  ct+='0';
 *pLRC=ct;
 ct=ch&0x0F;
 if(ct>9)
  ct+='A'-10;
 else
  ct+='0';
 *(pLRC+1)=ct;
 return pLRC;
}

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客