EDN首页   博客首页

日志档案

发表于 2008-5-21 23:27:55

1

标签: PLC  通信检验  LRC  ADD  FCS  

与各种PLC通信时,检验算法子程序集

和检验程序:

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;
}

系统分类: 工业控制   |   用户分类: PLC   |   来源: 原创   |   【推荐给朋友】   |   【添加到收藏夹】

    阅读(548)    回复(0)  

投一票您将和博主都有获奖机会!