SimVimCockpit Interface Baron-58 sim Blog About/Donate

One of the practical sketch that receives UDP packet from X-Plane and outputs specific data. (2012)
It shows Com1 frequency as separate digits (for 7-segment output) and as number (for LCD output). Auto IP detection




/*
Sample code:
Receiving Data  from X-Plane

Serial monitor shows Com1 frequency as separate digits (for 7-segment output) and as number (for LCD output)

http://simvim.com
 */
 
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h> 

//-----------------------------
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip;                            // Arduino IP address
IPAddress ipx(192, 168, 1, 8);           // X-Plane IP address
unsigned int localPort = 49001;         // port to listen to get UDP packets
unsigned int  XP = 49000;               // port to send UDP packets
EthernetUDP Udp;                        // EthernetUDP instance to send/receive packets over UDP
byte DATA_buf[500];                     // UDP.read buffer [size] /
int Data_in;                           // Incoming Data from X-Plane (ret. size of received packet)

char trans[5];
byte dcom1[5];
int com1; 
//-------Unions allow the same location of memory to be accessed as different data types.
   union { byte bytevalue[4];                         
        float floatvalue; }  bf;
 

//=================  setup  ===========================================
void setup()  { 

  Ethernet.begin(mac);             // start the Ethernet connection:DHCP 
  ip = Ethernet.localIP();         // Set local IP
  Ethernet.begin(mac,ip);           
  Udp.begin(localPort);          
 
//----------------------------------  
         Serial.begin(9600);
   } 
           
//========= Setup End ===========


//------------------------  Program start ------------------------------------------------
void loop() 
{

//========================= RECEIVING   DATA   FROM X-PLANE ====================================

//---------------------------- UDP-Packet Reading )-------------------
                                 
  Data_in = Udp.parsePacket();                 //  Checks for the presence of a UDP packet, and returns its size
//    if (Data_in)//  UDP packet was received and its size is defined
   if (Udp.available())  
    {  
     Udp.read ( DATA_buf, Data_in );   // Reads UDP data from the specified buffer.All incoming DATA stores in DATA_buf 

         // reading Indexes each 36 bytes, begining with byte #5 (following the header "DATA0"):

    for (int i = 5; i < Data_in; i += 36)  
       {  switch (DATA_buf[i])                // find indexes specified below (96)
        {

       case 96:   for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+8+n];       
                   com1 = int(bf.floatvalue);
                 
      itoa(com1, trans, 10);  Serial.print(trans);     // option for LCD output 
               
         for (int n=0; n<5; n++) dcom1[n] = trans[n] & 15;
     
            Serial.println();  
            Serial.print(dcom1[0]); Serial.print(" ");  
            Serial.print(dcom1[1]); Serial.print(" ");         // Print 5 digits of com1  
            Serial.print(dcom1[2]); Serial.print(" ");  
            Serial.print(dcom1[3]); Serial.print(" ");             
            Serial.print(dcom1[4]); Serial.print("      ");     
            
            
           Serial.print("     ");        
                break; 
          } 
        }
    } 
// =================================== END of UDP packed Reading =====================   
}
//------------------------  End ---------------------     
  
   







© Copyright 2012-2016 - SimVim