I am trying to connect to this device with the UDP interface using RDT: https://www.ati-ia.com/products/ft/ft_NetFT.aspx. They provided a sample in C that I am trying to convert to C# so I can pipe this sensor into an existing C# app I am currently developing.
I can connect to it just fine but I can't seem to successfully send it the required request for it to start sending data.
On page 73 of the manual (https://www.ati-ia.com/app_content/documents/9620-05-NET%20FT.pdf), it gives the structure of the request data to send:
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) } I am not sure how to create this structure in C# and send it appropriately. I've tried the following code among many other variants with no luck.
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(); } } Any help would be greatly appreciated, thanks in advance.
request[0] = (byte)IPAddress.HostToNetworkOrder(0x1234);is definitely wrong, you are trying to fit 2-bytes-0x1234 into single byteHostToNetworkOrder()is required. There's a good chance it is, but you didn't quote any part of the documentation that definitely indicates it is. Make sure you understand what that method does and why you might use it. The next thing is to construct a sequence of bytes that contains the data you want. See marked duplicates.