Vaisala CRC16


CRC-16 COMPUTATION
  The computation of the CRC is performed on the data response before parity is added. All operations are assumed to be on 16 bit unsigned integers. The least significant bit is on the right. Numbers preceded by 0x are in hexadecimal. All shifts shift in a zero. The algorithm is:
Initialize the CRC to zero. For each character beginning with the address, up to but not including the carriage return (<cr>), do as follows:
{
  Set the CRC equal to the exclusive OR of the character and itself
  for count =1 to 8
  {
    if the least significant bit of the CRC is one
    {
      right shift the CRC one bit
      set CRC equal to the exclusive OR of 0xA001 and itself
    }else{
      right shift the CRC one bit
    }
  }
}


Encoding the CRC as ASCII Characters
  The 16 bit CRC is encoded to three ASCII characters by using the following algorithm:
  1st character = 0x40 OR (CRC shifted right 12 bits)
  2nd character = 0x40 OR ((CRC shifted right 6 bits) AND 0x3F)
  3rd character = 0x40 OR (CRC AND 0x3F)

  The three ASCII characters are placed between the data and <cr><lf>.
  Parity is applied to all three characters, if selected for the character frame.
  The CRC computation code is added to the end of the response, if the first letter of the command is sent by using lower case.


void CRC16(BYTE *Array, BYTE *Rcvbuf,unsigned int Len)
{
  unsigned int IX,IY,CRC;
  CRC=0;
  if (Len<=0)
  {
    CRC = 0;
  }else{
    Len--;
    for (IX=0;IX<=Len;IX++)
    {
      CRC=CRC^(unsigned int)(Array[IX]);
      for(IY=0;IY<=7;IY++)
      {
        if ((CRC&1)!=0)
        {
          CRC=(CRC>>1)^0xA001;
        }else{
          CRC=CRC>>1;
        }
      }
    }
  }
   Rcvbuf[0] = 0x40 | (CRC >> 12);//高位置
   Rcvbuf[1] = 0x40 | ((CRC >> 6) & 0x3F);//中位置
   Rcvbuf[2] = 0x40 | (CRC & 0x3F);//低位置
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值