Modbus TCP套接字类没有回复Moxa

时间:2015-06-29 21:08:45

标签: c# sockets tcp modbus

这是我的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消息。

现在我确实看到Moxa对Wireshark的回复是:

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........

但是当我去阅读那条消息时,当我调用该函数时,接收程序只是坐在那里并且什么都不做

这是我的阅读功能:

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);
    }
我做错了什么? 为什么它只是坐在receive = sck.Receive(buffer);从来没有做过什么?

1 个答案:

答案 0 :(得分:0)

您正在通过Modbus TCP连接发送Modbus RTU PDU,但这不起作用。 您需要使用MBAP标头构建Modbus TCP PDU。您可以在此处查看详细信息:

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