Nuclear Projects Logo

Receiving data from X-Plane in C#

Here we go...

The first step to receiving the UDP data from X-Plane was to get some general UDP code working. I was very happy to find that within C# were some very powerful, yet simple commands for doing just this.

I found this C# code for receiving binary data from a UDP client from this site, as shown below. This is for a consule application, and works great using Microsoft Visual C# 2008 Express:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MainClass
{
   public static void Main()
   {
      byte[] data = new byte[1024];
      IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
      UdpClient newsock = new UdpClient(ipep);

      Console.WriteLine("Waiting for a client...");

      IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

      byte[] data1 = newsock.Receive(ref sender);
      int test1 = BitConverter.ToInt32(data1, 0);
      Console.WriteLine("test1 = {0}", test1);

      byte[] data2 = newsock.Receive(ref sender);
      double test2 = BitConverter.ToDouble(data2, 0);
      Console.WriteLine("test2 = {0}", test2);

      byte[] data3 = newsock.Receive(ref sender);
      int test3 = BitConverter.ToInt32(data3, 0);      
      Console.WriteLine("test3 = {0}", test3);

      byte[] data4 = newsock.Receive(ref sender);
      bool test4 = BitConverter.ToBoolean(data4, 0);
      Console.WriteLine("test4 = {0}", test4.ToString());

      byte[] data5 = newsock.Receive(ref sender);
      string test5 = Encoding.ASCII.GetString(data5);
      Console.WriteLine("test5 = {0}", test5);

      newsock.Close();
   }
}

I then began experimenting with that code, some trial-and-error tests, and was able to come up with the following code that works perfectly for reading X-Plane UDP data sentences. This code will read a single transmission, then wait for a keypress to close. This example assumes the X-Plane's IP for Data Output was set to use your localhost IP (127.0.0.1) and use port 49003.

//// This code works well so far for reading data from X-Plane

using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDPTest2
{
    class Program
    {
        static void Main(string[] args)
        {

            byte[] data = new byte[1024];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 49003);
            UdpClient newsock = new UdpClient(ipep);

            Console.WriteLine("Waiting for a client...");

            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);

                Console.WriteLine("X-Plane Data Read: \n\n");
                data = newsock.Receive(ref sender);

                for (int index = 0; index < data.Length; index++)
                {
                    Console.Write("{0},", data[index]);
                }

            Console.ReadKey(true); // Wait for any keypress before closing
            newsock.Close();
        }
    }
}

That's it! Simple as that. I have a couple using statements commented out that aren't needed for this code, but may be needed for any additions or whatever, so I left them in.

Here is the .exe for the above code (with some text added to the console screen better clarity): UDP_Receive.exe

This will read UDP data over the localnet on Port 49003 (port we setup on X-Plane). There are many reasons why this may not work (opperating system other than Windows XP, newer/older version of X-Plane, etc.). But since I already had this compiled, why not throw it up here?




Continue to learn about sending data...