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;

原文地址:https://www.cnblogs.com/wd520/p/12511199.html