SimVimCockpit Interface Baron-58 sim Blog About/Donate

One of the first tests to analize UDP packets from X-Plane (May 2012).
It receives UDP packets from X-Plane, splits them into the groups and shows all 8 4-byte parameters in the group and their values.
Check this groups in the Data Input & Output Screen of the X-Plane for sending.


/*
Test sketch that receives UDP packets from X-Plane
and prints to Serial terminal  data by the group index.

Shows - packet size, prefix, index (number) of the group
and  eight 4-byte parameters:

********************************
Packet size: 41
Xplane Data:
68-65-84-65-64- - prefix DATA
67 - Index of the Group

72-122-74-62-
 Dataref_Value = 0.20

152-38-184-62-
 Dataref_Value = 0.36

112-34-224-62-
 Dataref_Value = 0.44

0-0-0-0-
 Dataref_Value = 0.00

0-0-0-0-
 Dataref_Value = 0.00

0-0-0-0-
 Dataref_Value = 0.00

0-0-0-0-
 Dataref_Value = 0.00

0-0-128-63-
 Dataref_Value = 1.00
*************************************

Check this groups in the Data Input & Output Screen of the X-Plane for sending.



May 2012
Vlad S
http://simvim.com
 */

#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 4);    // local IP
unsigned int localPort = 49001;      // local port to listen
byte buf = B10010;   // 
int ByteGroup;
int ByteVar;
long X[4]; // Byte array
float Dataref_Value; // Float result
int Index;


// Translate 4-byte to float
union u_tag {
byte b[4];
float fval;
} u;

// 

EthernetUDP Udp; // An EthernetUDP instance to let us send and receive packets over UDP

//-------------------------------------------------------------------------------

void setup() 
{
  
  Ethernet.begin(mac,ip);   // start the Ethernet
  Udp.begin(localPort);     //..and UDP:
 Serial.begin(9600);
}

void loop() {
        

        
  delay(50);
  

  int packetSize = Udp.parsePacket();   //  read a packet
  if(packetSize)
  {
    Serial.println(); 
    Serial.println();
    Serial.print("Packet size: ");
    Serial.println(packetSize);

 // If using Udp.read without arguments, it returns next char (byte in this case) :
 // in cycle - number of bytes, got from paketSize:
      

      Serial.println("Xplane Data:");
    ByteGroup = 0;  
    ByteVar = 0;
    Index = 0;

    // allocate DATA
    for (int i =0; i < 5; i++)
    {
        buf = Udp.read();
        Serial.print(buf);
        Serial.print("-");      
    }
          Serial.print(" - prefix DATA");   
          Serial.println();
  
  // data reding cycle        
    for (int i = 5; i < packetSize; i++)
    { 
        buf = Udp.read();
        
                    // data not index
        if (ByteGroup > 3)
        {
          Serial.print(buf);
          Serial.print("-");
          X[ByteVar] = buf;
        }
        
        // index number in the group
        if (ByteGroup == 0)
        {
          Index = buf;
        }
        
        ByteGroup++;
        ByteVar++;
        
        if (ByteGroup == 4)
        {
          Serial.print(Index);
          Serial.print(" - Index of the Group");  
          Serial.println(); 
        }
        
        // End of Data group
        if (ByteGroup == 36)
        {
          Serial.println(); 
          ByteGroup = 0;
        }      
       
       // End of variable
        if (ByteVar == 4)
        {
          Serial.println();
          ByteVar = 0;
          if (ByteGroup > 4 || ByteGroup == 0)
          {

            u.b[0] = X[0];
            u.b[1] = X[1];
            u.b[2] = X[2];
            u.b[3] = X[3];
            Dataref_Value = u.fval;
            Serial.print(" Dataref_Value = ");
            Serial.print(Dataref_Value);
            Serial.println();
            Serial.println();

            
          }
        } 
          
    }
  
  }
  
}


   







© Copyright 2012-2016 - SimVim