Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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# 无法通过UDP连接到设备_C#_Udp - Fatal编程技术网

C# 无法通过UDP连接到设备

C# 无法通过UDP连接到设备,c#,udp,C#,Udp,我正在尝试使用RDT通过UDP接口连接到此设备:。 他们提供了一个C语言的示例,我正在尝试将其转换为C语言,这样我就可以将此传感器导入到我目前正在开发的现有C语言应用程序中 我可以很好地连接到它,但我似乎无法成功地向它发送开始发送数据所需的请求 在手册()的第73页,它给出了要发送的请求数据的结构: All RDT requests use the following RDT request structure: { Uint16 command_header = 0x1234; /

我正在尝试使用RDT通过UDP接口连接到此设备:。 他们提供了一个C语言的示例,我正在尝试将其转换为C语言,这样我就可以将此传感器导入到我目前正在开发的现有C语言应用程序中

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

在手册()的第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();

    }
}

任何帮助都将不胜感激,请提前感谢。

我将尝试以下其中一种:

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;

request[0]=(字节)IPAddress.HostToNetworkOrder(0x1234)
肯定是错误的,您正在尝试将2字节0x1234放入单个字节。首先需要确定是否需要
HostToNetworkOrder()
。很有可能是这样,但您没有引用文档中明确指出这一点的任何部分。确保您了解该方法的作用以及使用它的原因。下一步是构造包含所需数据的字节序列。请参阅标记的副本。