0

I'm developing an XPlane 11 ground control station in C# .net framework winforms. I am successfully pulling data from XPlane. However, I have a problem. Data comes via UDP. After selecting the data packets that I want to send in the Xplane interface, I start the program and only the packet with the smallest index number is transmitted from the selected packets. If I just pick a random package I can successfully see it in the c# interface. What is the reason of this?

Note: My data retrieval code is communicating with the UAV. So I don't think there is a problem in my code.

Only one packet comes from Xplane 11 data packets to C#

private UdpClient udpClient;        
private IPEndPoint endPoint;
readonly byte[] ExpectedSignature = { 68, 65, 84, 65, 42 }; 
bool IsValidSignature = true;
byte[] receivedData;

public Form8()  
{ 
    InitializeComponent();  
    InitializeUdpClient();  
}

private void InitializeUdpClient()
{   
    udpClient = new UdpClient();  
    endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 49001);  
    udpClient.Client.Bind(endPoint);
}

private void BtnGetData_Click(object sender, EventArgs e)    
{   
    timer1.Start(); 
}

private void Timer1_Tick(object sender, EventArgs e)
{   
    byte[] data = GetData();  
    ShowData(data); 
    ResetData(); 
}

private byte[]  GetData()
{ 
    receivedData = udpClient.Receive(ref endPoint);  
    return receivedData;  
}

private void ResetData()
{   
    receivedData = null;    
}

private void ShowData(byte[] data)
{
    for (int i = 0; i < ExpectedSignature.Length; i++) 
    { 
        if (data[i] != ExpectedSignature[i]) 
        { 
            IsValidSignature = false; 
            break; 
        } 
    }
    if (IsValidSignature) 
    {
        for (int i = 0; i < data.Length; i++) 
        { 
            if (i == (data.Length - 1)) 
            { 
                richTextBox1.AppendText(data[i] + "\n"); 
            } 
            else 
            { 
                richTextBox1.AppendText(data[i] + ", "); 
            } 
        }
        
        switch (data[5])//https://www.x-plane.com/kb/data-set-output-table/
        {

            case 1:
                double realtime_value = BitConverter.ToSingle(data, 9); 
                Lbl_1_RealTime.Text = realtime_value.ToString();
                
                double totaltime_value = BitConverter.ToSingle(data, 13); 
                Lbl_1_TotalTime.Text = totaltime_value.ToString();
                
                double missiontime_value = BitConverter.ToSingle(data, 17); 
                Lbl_1_MissionTime.Text = missiontime_value.ToString();
                
                double timertime_value = BitConverter.ToSingle(data, 21); 
                Lbl_1_TimerTime.Text = timertime_value.ToString();
                
                double zulutime_value = BitConverter.ToSingle(data, 29); 
                Lbl_1_ZuluTime.Text = zulutime_value.ToString();
                
                double localtime_value = BitConverter.ToSingle(data, 33); 
                Lbl_1_LocalTime.Text = localtime_value.ToString();
                
                double hobbstime_value = BitConverter.ToSingle(data, 37); 
                Lbl_1_HobbsTime.Text = hobbstime_value.ToString();
                break;
                
            case 3:
                double VindKias_value = BitConverter.ToSingle(data, 9); 
                Lbl_3_VindKias.Text = VindKias_value.ToString();
                
                double VindKeas_value = BitConverter.ToSingle(data, 13); 
                Lbl_3_VindKeas.Text = VindKeas_value.ToString();
                
                double VtrueKtas_value = BitConverter.ToSingle(data, 17); 
                Lbl_3_VtrueKtas.Text = VtrueKtas_value.ToString();
                
                double VtrueKtgs_value = BitConverter.ToSingle(data, 21); 
                Lbl_3_VtrueKtgs.Text = VtrueKtgs_value.ToString();
                
                double VindMph_value = BitConverter.ToSingle(data, 29); 
                Lbl_3_VindMph.Text = VindMph_value.ToString();
                
                double VtrueMphas_value = BitConverter.ToSingle(data, 33); 
                Lbl_3_VtrueMphas.Text = VtrueMphas_value.ToString();
                
                double VtrueMphgs_value = BitConverter.ToSingle(data, 37); 
                Lbl_3_VtrueMphgs.Text = VtrueMphgs_value.ToString();
                break;
        }
    }
    else 
    { 
        IsValidSignature = true; 
    }
    
}

enter image description here

enter image description here

  • The first number you may not see because it is at the edge of the graph. The graph is probably auto scaling so the min and max points may be at the edge of the graph. – jdweng Jun 13 '23 at 13:01
  • I didn't understand anything from your message. I am sorry. Can you explain a little more? My problem is not with the graphics. The raw data coming to C# contains only one package. But I know I am sending multiple packets in Xplane interface. – yardimcioyuncu Jun 13 '23 at 13:12
  • What makes you think you are sending multiple packets? Single Packets are working. Multiple Packets are not working. Often this happens if you forget to terminate each packet. Terminate may just mean adding a return character to each message. – jdweng Jun 13 '23 at 13:16
  • I think it will be helpful if I share my code. I updated my main post. I reset the incoming package. – yardimcioyuncu Jun 13 '23 at 14:22
  • Did you debug code? Is IsValidSignature valid? – jdweng Jun 13 '23 at 14:43
  • " IsValidSignature " has never had a value of " false " – yardimcioyuncu Jun 14 '23 at 09:42
  • So you put a break point on the line and you never get there? What is Form8 code? – jdweng Jun 14 '23 at 12:16
  • The code I shared in the main message belongs to Form8. I use "break" in Switch-Case. I have shared index numbers 1 and 3 as an example. With this code, I have successfully retrieved data from pixhawk, but for Xplane it only sends only one of the packets continuously. For example; When I want to send the 1,3 and 4 packets, it only sends the 1st packet. If I want to send 3rd and 4th packets, it only sends 3rd packet. – yardimcioyuncu Jun 14 '23 at 13:22

2 Answers2

0
  1. Remove the ResetData method as it is not needed.

  2. Add a variable called receivedPackets to store multiple received packets. We'll use a list to hold these packets. Place the following line at the beginning of your Form8 class:

private List<byte[]> receivedPackets = new List<byte[]>();
  1. Update the GetData method to store each received packet in the receivedPackets list:
private byte[] GetData()
{
    byte[] receivedData = udpClient.Receive(ref endPoint);
    receivedPackets.Add(receivedData);
    return receivedData;
}
  1. Modify the Timer1_Tick method to process each packet in the receivedPackets list:
private void Timer1_Tick(object sender, EventArgs e)
{
    // Iterate through each packet in the receivedPackets list
    foreach (byte[] data in receivedPackets)
    {
        // Process the packet by calling the ShowData method
        ShowData(data);
    }

    // After processing all packets, clear the receivedPackets list
    receivedPackets.Clear();
}

By making these changes, you will now store each received packet in the receivedPackets list. During each tick of the timer, you iterate through the list and process each packet using the ShowData method. After processing, the list is cleared to prepare for the next batch of received packets.

  • I tried your code but I didn't get any output. I added the output I got with my own code to the main message as a screenshot. – yardimcioyuncu Jun 14 '23 at 05:46
0

I solved the problem. Xplane is sending the data packet in one go. That is, after the initial identification bytes, 36 bytes of data are the data of the first index. The next 36-byte index is added to the end of the existing 41-byte packet. Each time an index is selected, the packet extends by 36 bytes. My mistake was that I thought of each index as a 41-byte packet. Thanks to everyone who replied. :)