Mod总线通讯“crc功能”C#

时间:2015-07-06 10:54:25

标签: c# crc modbus

我尝试制作一个modbus应用程序,但我需要一些帮助适合自动CRC功能。

问题是:当我尝试读取CRC[1]CRC[0]以将包含的值放在我的十六进制字符串的末尾时,我收到编译错误。

我将此函数用于crc:

   #region CRC Computation
   void GetCRC(byte[] comBuffer, ref byte[] CRC)
   {
        //Function expects a modbus message of any length as well as a 2 byte CRC array in which to 
        //return the CRC values:

        ushort CRCFull = 0xFFFF;
        byte CRCHigh = 0xFF, CRCLow = 0xFF;
        char CRCLSB;

        for (int i = 0; i < (comBuffer.Length) - 2; i++)
        {
            CRCFull = (ushort)(CRCFull ^ comBuffer[i]);

            for (int j = 0; j < 8; j++)
            {
                CRCLSB = (char)(CRCFull & 0x0001);
                CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF);

                if (CRCLSB == 1)
                    CRCFull = (ushort)(CRCFull ^ 0xA001);
            }
        }
        CRC[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
        CRC[0] = CRCLow = (byte)(CRCFull & 0xFF);
    }
    #endregion

我想在字符串的末尾使用CRC [1]和CRC [0],我如何在以下代码中使用:

comPort.Write(newMsg, 0, newMsg.Length, CRC[1], CRC[0]);



case TransmissionType.Hex:
                try
                {
                    //convert the message to byte array
                    byte[] newMsg = HexToByte(msg);
                    //send the message to the port
                    comPort.Write(newMsg, 0, newMsg.Length, CRC[1], CRC[0]);
                    //convert back to hex and display
                    DisplayData(MessageType.Outgoing, ByteToHex(newMsg)+ "\n");

                }

0 个答案:

没有答案