ModBus RTU协议CRC校验方式最简实现

  private byte []  GetCRC( byte [] byteData)
        {
                   byte[] CRC= new byte[2];

                    UInt16 wCrc =0xFFFF;
                    for(int i=0; i< byteData.Length ; i++)
                    {
                        wCrc ^= Convert.ToUInt16 (byteData[i]);
                        for(int j=0; j<8; j++)
                        {
                            if ((wCrc & 0x0001)==1)
                            {
                               wCrc >>= 1;
                               wCrc ^= 0xA001;//异或多项式
                            }
                            else
                            {
                               wCrc >>= 1;
                            }
                        }
                    }

                    CRC[1] = (byte)((wCrc & 0xFF00) >> 8);//高位在后
                    CRC[0] = (byte)(wCrc & 0x00FF);       //低位在前
                    return CRC;

        }
    }

你可能感兴趣的:(c#)