Moxa没有回复Modbus TCP套接字类

本文关键字:TCP 套接字 Modbus 回复 Moxa | 更新日期: 2023-09-27 18:00:28

好的,这是我的TCP连接代码:

        //sck.Bind(new IPEndPoint(0, 1234));
        try
        {
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(IP_AddressTXT.Text), Convert.ToInt16(IP_PortTXT.Text));
            sck.Connect(localEndPoint);
            //sck.h
        }
        catch( Exception e)
        {
            MessageBox.Show("Unable to connect to remote end point! ", e.Message);
            return;
        }
        MessageBox.Show("Connected to : " + IP_AddressTXT.Text);
        Enter1.Text = "connected...";  }

这样零件就可以工作了。。它连接。。。

这是我的发送代码:

public void Send( byte[] buffer, int offset, int size, int timeout)
    {
        int startTickCount = Environment.TickCount;
        int sent = 0;  // how many bytes is already sent
            try
            {
                sent = sck.Send(buffer);
            }
            catch (SocketException ex)
            {
                MessageBox.Show("error", ex.Message); // any serious error //occurr
            }
    }

我用了WireShark,上面写着我要发送这个:

0000   00 90 e8 30 f8 e0 34 e6 d7 15 0d d5 08 00 45 00  ...0..4.......E.
0010   00 30 3c a3 40 00 80 06 60 fa a9 fe 08 2d a9 fe  .0<.@...`....-..
0020   01 01 fe 20 10 a4 a6 74 fa 75 17 c7 ca e0 50 18  ... ...t.u....P.
0030   40 29 17 9a 00 00 01 03 01 a6 00 01 65 d5        @)..........e.

01 03 a6 00 01 65 d5是我的modbus消息。

现在我确实在Wireshark上看到了一个来自Moxa的回复,它是:

0000   34 e6 d7 15 0d d5 00 90 e8 30 f8 e0 08 00 45 00  4........0....E.
0010   00 28 16 ec 40 00 80 06 86 b9 a9 fe 01 01 a9 fe  .(..@...........
0020   08 2d 10 a4 fe 85 09 13 68 d7 c4 1c 89 ee 50 10  .-......h.....P.
0030   83 24 00 66 00 00 00 00 00 00 00 00              .$.f........

但当我去读那条消息时,当我调用函数Receive时,程序只是坐在那里,不做任何

这是我的读取功能:

public void Receive(byte[] buffer, int offset, int size, int timeout)
    {
        int startTickCount = Environment.TickCount;
        int received = 0;  // how many bytes is already received
        //sck.Listen(100);
            try
            {
                received = sck.Receive(buffer);
            }
            catch (SocketException ex)
            {
                MessageBox.Show("error", ex.Message);
            }
        processingData(buffer);
    }

我做错了什么?为什么它只是停留在received=sck。接收(缓冲);什么都不做?

Moxa没有回复Modbus TCP套接字类

您正在通过Modbus TCP连接发送Modbus RTU PDU,这将不起作用。您需要构建一个带有MBAP报头的Modbus TCP PDU。您可以在此处查看详细信息:

http://www.modbus.org/docs/Modbus_Messaging_Implementation_Guide_V1_0b.pdf