Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Modbus TCP套接字类别号由Moxa回复_C#_Sockets_Tcp_Modbus - Fatal编程技术网

C# Modbus TCP套接字类别号由Moxa回复

C# Modbus TCP套接字类别号由Moxa回复,c#,sockets,tcp,modbus,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);

好的,这是我的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.
但是当我去读那条消息,调用函数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.Receive(缓冲区);而且从不做任何事情?

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


请求末尾的65 d5是CRC吗?好吧,这很奇怪,您不应该在Modbus TCP中发送CRC。你好像在读一些记录。你能告诉我他们的地址吗?sck到底是什么类型的?为什么直接使用套接字而不是TcpClient?sck是一个套接字对象@Tobias KnaussI不知道TcpClient。有没有一个例子?
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);
    }