zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    Single Number II
    Best Time to Buy and Sell Stock
    Linked List Cycle
    Single Number
    Max Points on a Line
    Strategy
    LRU Cache
    Word Break II
    Text Justification
    Median of Two Sorted Arrays
  • 原文地址:https://www.cnblogs.com/wd520/p/12511199.html
Copyright © 2011-2022 走看看