C# CRC16校验问题

     private int crc16_modbus(byte[] modbusdata, int length)
        {
            int i, j;
            int crc = 0xffff; //0xffff or 0
            try
            {
                for (i = 0; i < length; i++)
                {
                    crc ^= modbusdata[i] & 0xff;
                    for (j = 0; j < 8; j++)
                    {
                        if ((crc & 0x01) == 1)
                        {
                            crc = (crc >> 1) ^ 0xa001;
                        }
                        else
                        {
                            crc >>= 1;
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return crc;
        }





      //发指令执行启动和暂停
        private void button2_Click(object sender, EventArgs e)
        {


            if (button2.Text == "启动")
            {
                // string CrcStr = Utils.get_CRC16_C(Utils.ConvertHexToBytes("A0030100B0"));
                //计算得出校验码为:F429          A0 03 01 00 B0 F4 29
                // Console.WriteLine(CrcStr);


                SendBuffer[0] = 0xA0;//开始
                SendBuffer[1] = 0x03;//功能码
                SendBuffer[2] = 0x01;//长度
                SendBuffer[3] = 0x02;//数据
                SendBuffer[4] = 0xB0;//结束
                                   
                int crc = crc16_modbus(SendBuffer, SendBuffer.Length - 2);
                SendBuffer[5] = (byte)((crc >> 8) & 0xff);
                SendBuffer[6] = (byte)(crc & 0xff);
                 Console.WriteLine(SendBuffer[5]);
                Console.WriteLine(SendBuffer[6]);
                SendCnt = 7;
                button2.Text = "暂停";
            }




我用crc16小工具计算的A0 03 01 02 B0得出:F429  但是我现在这样写并不是,而是3C 79,哪位大侠帮我看一下

//将下面的代码替换红框中的代码
crc = crc ^ modbusdata[i];
for (i = 0; i < 8; i++)
{
    int TT;
    TT = crc & 1;
    crc = crc >> 1;
    crc = crc & 0x7fff;
    if (TT == 1)
    {
        crc = crc ^ 0xa001;
    }
    crc = crc & 0xffff;
}


 crc = crc ^ modbusdata[i];是放在for里面?

是,弄错了

框框打错范围了



好像不对啊

看不出其他问题