您的位置:首页 > 其它

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

2011-05-11 13:22 337 查看
  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;

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  byte