Nuclear Projects Logo

Sending data to X-Plane in C#

We can receive, now to send...

On my last page, I showed how to receive data from X-Plane. This page will show how to send data. Just as I did before, I started with some generic UDP code and went from there. Here's the original code for sending binary data from UDP I found here:

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

class MainClass
{
   public static void Main()
   {
      byte[] data = new byte[1024];
      string stringData;
      UdpClient server = new UdpClient("127.0.0.1", 9050);

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

      int test1 = 45;
      double test2 = 3.14159;
      int test3 = -1234567890;
      bool test4 = false;
      string test5 = "This is a test.";

      byte[] data1 = BitConverter.GetBytes(test1);
      server.Send(data1, data1.Length);

      byte[] data2 = BitConverter.GetBytes(test2);
      server.Send(data2, data2.Length);

      byte[] data3 = BitConverter.GetBytes(test3);
      server.Send(data3, data3.Length);

      byte[] data4 = BitConverter.GetBytes(test4);
      server.Send(data4, data4.Length);

      byte[] data5 = Encoding.ASCII.GetBytes(test5);
      server.Send(data5, data5.Length);

      server.Close();
   }
}

After tweaking the code abit, I was successfully able to write values to X-Plane and even control a plane! Here's the code to send a single sentence to X-Plane:

//////// This program works well so far for sending data to X-Plane

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

namespace UDPTest3_Send
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] data = new byte[1024]; // Array to hold entire data string to be sent to X-Flight

            UdpClient server = new UdpClient("127.0.0.1", 49000);

            

            // --- 11: Flight Controls ---
            // Pitch: 0.20
                //byte[] XFData = { 68, 65, 84, 65, 0, 11, 0, 0, 0, 205, 204, 76, 62, 0, 192, 121, 196, 0, 192, 121, 196, 0, 192, 121, 196, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            // Pitch: 0.00
                byte[] XFData = { 68, 65, 84, 65, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 192, 121, 196, 0, 192, 121, 196, 0, 192, 121, 196, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };


	    float value;


	    // Send the data to X-Plane
            server.Send(XFData, XFData.Length);

	    // Print the data we sent to screen
            Console.WriteLine("Length: {0}", XFData.Length);
            Console.WriteLine("Data Set: {0}", XFData[5]);
            value = BitConverter.ToSingle(XFData, 9);
            Console.WriteLine("XFData1: {0}", value);
            value = BitConverter.ToSingle(XFData, 13);
            Console.WriteLine("XFData2: {0}", value);
            value = BitConverter.ToSingle(XFData, 17);
            Console.WriteLine("XFData3: {0}", value);
            value = BitConverter.ToSingle(XFData, 21);
            Console.WriteLine("XFData4: {0}", value);
            value = BitConverter.ToSingle(XFData, 25);
            Console.WriteLine("XFData5: {0}", value);
            value = BitConverter.ToSingle(XFData, 29);
            Console.WriteLine("XFData6: {0}", value);
            value = BitConverter.ToSingle(XFData, 33);
            Console.WriteLine("XFData7: {0}", value);
            value = BitConverter.ToSingle(XFData, 37);
            Console.WriteLine("XFData8: {0}", value);
  
            server.Close();
            Console.ReadKey(true); // Wait for keypress to close program

        } // End Main

    } // End Program
}

In the above code, many of those lines aren't even needed to actually send the data array. But they're helpful for visualizing the data that was sent. The "value=..." and "Consolue.WriteLines" actually show how easily you can easily 4 bytes from an array into a single-precision floating point number. This function will actually be needed for parsing and processing X-Plane sentences. My last "receiving" example just spit out the raw data, but to make use of it, it will need to be parsed and processed. But that's all something to work on still.

The byte array, XFData, holds the 41 byte values required. I've included a couple example data sets, so only un-comment one at a time. I included two values for pitch adjustments, one sets it to 0.0, the other to 0.20. You can toggle between the two and view the changes in X-Plane. Of course, feel free to experiment with any available sentence.

Perhaps to help out a bit more, I'll explain a little more on my two data arrays under Flight Controls. You'll notice that they are almost identical. The only difference is the first floating point value. The top one is 205, 204, 76, 62, which is equivelent of "0.20". The array below that just has four 0's, a value equal to zero. And finally, because the only value I'm wanting to change at the moment is pitch, I've set the other values 0, 192, 121, 196, which is -999. The 0's at the end of both sentences because the Data Set #11, which we're using, only uses 4 of 8 possible float values. Even though the last 4 values aren't used by this Data Set, we still have to put something there, so 0's are used. That is important to remember. You always need to send a total of 41 bytes. The above code will display on screen the total number of bytes in your array. So that should always show 41, otherwise you've made a mistake in the data array.

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

This will send the pitch value of 0.20 over localnet 127.0.0.1 to port 49000 (X-Plane's receiving port). 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 see a sample application & video...