C# modbus CRC16校验

        #region CRC Computation< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />

        private void GetCRC(byte[] message, 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 < (message.Length) - 2; i++)

            {

                CRCFull = (ushort)(CRCFull ^ message[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

//----------------------------------------------------------------------------

        #region Build Message

        private void BuildMessage(byte address, byte type, ushort start, ushort registers, ref byte[] message)

        {

            //Array to receive CRC bytes:

            byte[] CRC = new byte[2];

 

            message[0] = address;

            message[1] = type;

            message[2] = (byte)(start >> 8);

            message[3] = (byte)start;

            message[4] = (byte)(registers >> 8);

            message[5] = (byte)registers;

 

            GetCRC(message, ref CRC);

            message[message.Length - 2] = CRC[0];

            message[message.Length - 1] = CRC[1];

        }

        #endregion

//---------------------------------------------------------------------

        #region Function 16 - Write Multiple Registers

        public bool SendFc16(byte address, ushort start, ushort registers, short[] values)

        {

            //Ensure port is open:

            if (sp.IsOpen)

            {

                //Clear in/out buffers:

                sp.DiscardOutBuffer();

                sp.DiscardInBuffer();

                //Message is 1 addr + 1 fcn + 2 start + 2 reg + 1 count + 2 * reg vals + 2 CRC

                byte[] message = new byte[9 + 2 * registers];

                //Function 16 response is fixed at 8 bytes

                byte[] response = new byte[8];

 

                //Add bytecount to message:

                message[6] = (byte)(registers * 2);

                //Put write values into message prior to sending:

                for (int i = 0; i < registers; i++)

                {

                    message[7 + 2 * i] = (byte)(values[i] >> 8);

                    message[8 + 2 * i] = (byte)(values[i]);

                }

                //Build outgoing message:

                BuildMessage(address, (byte)16, start, registers, ref message);

               

                //Send Modbus message to Serial Port:

                try

                {

                    sp.Write(message, 0, message.Length);

                    GetResponse(ref response);

                }

                catch (Exception err)

                {

                    modbusStatus = "Error in write event: " + err.Message;

                    return false;

                }

                //Evaluate message:

                if (CheckResponse(response))

                {

                    modbusStatus = "Write successful";

                    return true;

                }

                else

                {

                    modbusStatus = "CRC error";

                    return false;

                }

            }

            else

            {

                modbusStatus = "Serial port not open";

                return false;

            }

        }

        #endregion

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值