crc16 modbus校验亲测可用


unsigned short crc(unsigned char addr)
{
  int i,j;
  unsigned tmp = 0xffff;
  unsigned char buff[6] = {0, 0x03, 0x00, 0x00, 0x00, 0x02};
  buff[0] = addr;

  for(i=0; i<6; i++)
  {
    tmp = buff[i]^tmp;
    for(j=0; j<8; j++)
    {
      if(tmp&0x01)
      {
        tmp = tmp>>1;
        tmp = tmp^0xA001;
      }
      else
      {
        tmp = tmp>>1;
      }
    }
  }
  return tmp;
}

 

返回的是16位的数

校验低八位 :tmp&0xFF;

校验高八位:tmp>>8;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM