无法通过UDP连接到设备

时间:2019-11-01 17:31:05

标签: c# udp

我正在尝试使用RDT https://www.ati-ia.com/products/ft/ft_NetFT.aspx通过UDP接口连接到此设备。 他们提供了一个我要转换为C#的C语言示例,因此我可以将此传感器传递到我当前正在开发的现有C#应用程序中。

我可以很好地连接到它,但似乎无法成功发送它所需的请求以开始发送数据。

手册(https://www.ati-ia.com/app_content/documents/9620-05-NET%20FT.pdf的第73页上,它给出了要发送的请求数据的结构:

All RDT requests use the following RDT request structure: 
{ 
    Uint16 command_header = 0x1234; // Required 
    Uint16 command;         // Command to execute   
    Uint32  sample_count;   //Samples   to output(0=infinite) 
}

我不确定如何在C#中创建此结构并适当地发送它。我在很多其他变体中尝试了以下代码,但是没有运气。

class Program
{
    static void Main(string[] args)
    {
        byte[] request = new byte[8];           

        // This is the example C code that was provided with the device.
        /* The request data sent to the Net F/T. */
        //*(uint16*)&request[0] = htons(0x1234); /* standard header. */
        //*(uint16*)&request[2] = htons(2); /* per table 9.1 in Net F/T user manual. */
        //*(uint32*)&request[4] = htonl(1); /* see section 9.1 in Net F/T user manual. */

        // This is my attempt at recreating the above
        request[0] = (byte)IPAddress.HostToNetworkOrder(0x1234);
        request[2] = (byte)2;
        request[4] = (byte)0;

        // 49152 is the right port number but not sure which of the below it belongs to.
        UdpClient client = new UdpClient(49152);
        IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.1.1"), 49152); 

        client.Send(request, 8, ip);
        byte[] received = client.Receive(ref ip);
        Console.WriteLine(Encoding.UTF8.GetString(received));

        Console.ReadKey();

    }
}

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我会尝试其中一种变体:

request[0] = 0x12;
request[1] = 0x34;
request[2] = 0;
request[3] = 2;
request[4] = 0;
request[5] = 0;
request[6] = 0;
request[7] = 0;

或其他订单

request[0] = 0x34;
request[1] = 0x12;
request[2] = 2;
request[3] = 0;
request[4] = 0;
request[5] = 0;
request[6] = 0;
request[7] = 0;