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

发布于 2024-09-19 02:26:00 字数 3342 浏览 5 评论 0原文

我是 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
            }
    }

问题是在读取响应时,程序会卡住。如果可能的话请帮我找出问题所在。

I am a newbie to modbus and need some help. I am trying to connect using modbus and serial communication. so far i managed to send data but i am unable to get any. the following is my code.

Building packet

        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;
        }
    }

Checksum for 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;
        }
    }

Connection through serial and 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
            }
    }

The problem is when it comes to reading the response, the program gets stuck. if possible please help me out figure what is wrong with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生再见 2024-09-26 02:26:00

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

found a solution to the problem, the problem was with the thread.sleep. was giving it 3secs which is too much for the rtf to receive the packet. changed to 10ms and worked fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文