Modbus和C# - 读取响应的问题

时间:2010-09-13 08:02:36

标签: c# modbus

我是modbus的新手,需要一些帮助。我正在尝试使用modbus和串行通信进行连接。到目前为止,我设法发送数据,但我无法得到任何数据。以下是我的代码。

构建数据包

        private byte[] BuildPacket(int meter_address,int function,int table_name,int table_offset,int high_byte, int low_byte)
    {
        try
        {
            byte[] packet = new byte[6];
            packet[0] = Convert.ToByte(meter_address);
            packet[1] = Convert.ToByte(function);
            packet[2] = Convert.ToByte(table_name);
            packet[3] = Convert.ToByte(table_offset);
            packet[4] = Convert.ToByte(high_byte);
            packet[5] = Convert.ToByte(low_byte);

            byte[] checksum = DoCheckSum(packet);

            byte[] sendPacket = new byte[8];
            sendPacket[0] = packet[0];
            sendPacket[1] = packet[1];
            sendPacket[2] = packet[2];
            sendPacket[3] = packet[3];
            sendPacket[4] = packet[4];
            sendPacket[5] = packet[5];
            sendPacket[6] = checksum[0];
            sendPacket[7] = checksum[1];

            return sendPacket;

        }
        catch (Exception)
        {

            throw;
        }
    }

用于modbus的校验和

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

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

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

                    if (CRCLSB == 1)
                        CRCFull = (ushort)(CRCFull ^ 0xA001);
                }
            }
            byte[] crcByte = new byte[2];
            crcByte[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
            crcByte[0] = CRCLow = (byte)(CRCFull & 0xFF);
            return crcByte;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

通过串口和modbus连接

 public void ConnectSerialModBus(string COM, int baud)
    {
        SerialPort port = new SerialPort(COM, baud, Parity.None, 8, StopBits.One);
            if (!(port.IsOpen))
            {
                byte[] sendPacket = BuildPacket(3, 4, 11, 0, 1, 200);
                port.Open();
                port.RtsEnable = false;
                port.Handshake = Handshake.None;
                //SEND PACKET TO DEVICE
                port.Write(sendPacket, 0, sendPacket.Length);

                #region RECEIVE DATA FROM SERIAL
                //MAKE PROCESS STOP FOR 5sec
                Thread.Sleep(3000);
                port.DiscardOutBuffer();
                port.DiscardInBuffer();
                port.RtsEnable = true;
                int size = port.ReadBufferSize;

                byte[] readingbyte = new byte[size];

                port.Read(readingbyte, 0, readingbyte.Length);
                string reading = Encoding.GetEncoding("Windows-1252").GetString(readingbyte);

                port.Close();
                port.Dispose();
                #endregion
            }
    }

问题在于,在阅读响应时,程序会卡住。如果可能的话请帮我弄清楚它有什么问题。

1 个答案:

答案 0 :(得分:1)

找到问题的解决方案,问题在于thread.sleep。给它3秒,这对于rtf接收数据包来说太多了。改为10毫秒,工作正常。