• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

pmdg sdk in c#

Messages
8
Country
germany
Helo all,
I want to connect through the PMDG 737 over simconnect in c#. I've created a Project but it doesn't work and i don't know why, i do not get any data.

May be someone can help?

the main things will done in "void testCommunication()" method

Thx all

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


// Add these two statements to all SimConnect clients
using Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;



namespace PMDGTest
{
    public partial class Form1 : Form
    {
        // User-defined win32 event
        const int WM_USER_SIMCONNECT = 0x0402;

        // SimConnect object
        SimConnect simconnect = null;

        enum CLIENT_DATA_IDS
        {
            PMDG_NGX_DATA_ID = 0x4E477831,
            PMDG_NGX_DATA_DEFINITION=0x4E477832,
            PMDG_NGX_CONTROL_ID	=0x4E477833,
            PMDG_NGX_CONTROL_DEFINITION = 0x4E477834,
        };


        enum DATA_REQUEST_ID
        {
            DATA_REQUEST,
            CONTROL_REQUEST,
            AIR_PATH_REQUEST
        };

        enum REQUESTS
        {
            CLIENT_REQUEST
        }

   
        protected override void DefWndProc(ref Message m)
        {
            if (m.Msg == WM_USER_SIMCONNECT)
            {
                if (simconnect != null)
                {
                    simconnect.ReceiveMessage();
                }
            }
            else
            {
                base.DefWndProc(ref m);
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (simconnect == null)
            {
                try
                {
                    // the constructor is similar to SimConnect_Open in the native API
                    simconnect = new SimConnect("PMDG NGX Test", this.Handle, WM_USER_SIMCONNECT, null, 0);

                    testCommunication();

                }
                catch (COMException ex)
                {
                    displayText("Unable to connect to FSX");
                }
            }
            else
            {
                displayText("Error - try again");
                closeConnection();

            }
        }

        void testCommunication()
        {

            // listen to connect and quit msgs
            simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
            simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);

            // listen to exceptions
            simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException);

            simconnect.MapClientDataNameToID(PMDG.PMDG_NGX_DATA_NAME, CLIENT_DATA_IDS.PMDG_NGX_DATA_ID);

    
            // Define the data area structure - this is a required step
            simconnect.AddToClientDataDefinition(CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION, 0,(uint)Marshal.SizeOf(typeof(PMDG.PMDG_NGX_Data)), 0.0f, SimConnect.SIMCONNECT_UNUSED);
            simconnect.RegisterDataDefineStruct<PMDG.PMDG_NGX_Data>(CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION);
            simconnect.OnRecvClientData += new SimConnect.RecvClientDataEventHandler(simconnect_RecvClientDataEvent);

            simconnect.RequestClientData(CLIENT_DATA_IDS.PMDG_NGX_DATA_ID, DATA_REQUEST_ID.DATA_REQUEST, CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION, SIMCONNECT_CLIENT_DATA_PERIOD.ON_SET, SIMCONNECT_CLIENT_DATA_REQUEST_FLAG.CHANGED, 0, 0, 0);

            // listen to events
            //simconnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(simconnect_OnRecvEvent);
            
     
        } // testCommunication

        void simconnect_RecvClientDataEvent(SimConnect sender, SIMCONNECT_RECV_CLIENT_DATA data)
        {
            switch ((REQUESTS)data.dwRequestID)
            {
                case REQUESTS.CLIENT_REQUEST:
                    //object da = data.dwData[0];
                    displayText(data.dwData.Length.ToString());
                    break;

                default:

                    break;
            }
        }

        void simconnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT recEvent)
        {
            displayText("simconnect_OnRecvEvent:" + recEvent.uEventID.ToString());
        }

        void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
        {
            displayText("Connected to FSX");
        }

        // The case where the user closes FSX
        void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
        {
            displayText("FSX has exited");
            closeConnection();
        }

        void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
        {
            displayText("Exception received: " + data.dwException);
        }

        private void closeConnection()
        {
            if (simconnect != null)
            {
                // Dispose serves the same purpose as SimConnect_Close()
                simconnect.Dispose();
                simconnect = null;
                displayText("Connection closed");
            }
        }

        void displayText(string s)
        {
            textBox1.Text += s + "\r\n";
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            closeConnection();
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            closeConnection();
        }

        private void btData_Click(object sender, EventArgs e)
        {
            testCommunication();
        }
    }
}
 
basic question is data transmission enabled in PMDG NGX config? Does any other SDK-dependend application work?
 
Hi,
short answer "yes" and "yes".
I've installed all on my laptop new and getvthe same result as on my development pc.

All fsx sdk c# sample projects are working.

Thx and greetings
 
In the method simconnect_RecvClientDataEvent, you have a case on
REQUESTS.CLIENT_REQUEST but this request Id is not the one you use in the RequestClientData.

The second parameter of RequestClientData must be the request id ( i.e REQUESTS.CLIENT_REQUEST in your program).

Patrick.
 
Hi,
thx for your help. I've changed the entries like your advice but i got the same result. I've changed the entry to DATA_REQUEST_ID.DATA_REQUEST but i get the same result. I think i have to explain what my result is. May be it is correct but I'm too stupid to process the result.
After starting my app it connects succesful to fsx. The app seems to receive data because it enters the simconnect_RecvClientDataEvent event handler.

I cannot convert the received data to
PMDG.PMDG_NGX_Data da = (PMDG.PMDG_NGX_Data)data.dwData[0];

dwData is from type object[]
The debugger show me content of dwdata[0] as object with content "1". Whatever it means.

Here is my last code:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


// Add these two statements to all SimConnect clients
using Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;



namespace PMDGTest
{
    public partial class Form1 : Form
    {
        // User-defined win32 event
        const int WM_USER_SIMCONNECT = 0x0402;

        // SimConnect object
        SimConnect simconnect = null;

        enum CLIENT_DATA_IDS
        {
            PMDG_NGX_DATA_ID = 0x4E477831,
            PMDG_NGX_DATA_DEFINITION = 0x4E477832,
            PMDG_NGX_CONTROL_ID	=0x4E477833,
            PMDG_NGX_CONTROL_DEFINITION = 0x4E477834,
        };


        enum DATA_REQUEST_ID
        {
            DATA_REQUEST,
            CONTROL_REQUEST,
            AIR_PATH_REQUEST
        };

 
   
        protected override void DefWndProc(ref Message m)
        {
            if (m.Msg == WM_USER_SIMCONNECT)
            {
                if (simconnect != null)
                {
                    simconnect.ReceiveMessage();
                }
            }
            else
            {
                base.DefWndProc(ref m);
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (simconnect == null)
            {
                try
                {
                    // the constructor is similar to SimConnect_Open in the native API
                    simconnect = new SimConnect("PMDG NGX Test", this.Handle, WM_USER_SIMCONNECT, null, 0);

                    testCommunication();

                }
                catch (COMException ex)
                {
                    displayText("Unable to connect to FSX");
                }
            }
            else
            {
                displayText("Error - try again");
                closeConnection();

            }
        }

        void testCommunication()
        {

            // listen to connect and quit msgs
            simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
            simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);

            // listen to exceptions
            simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException);

            simconnect.MapClientDataNameToID("PMDG_NGX_Data", CLIENT_DATA_IDS.PMDG_NGX_DATA_ID);

    
            // Define the data area structure - this is a required step
            simconnect.AddToClientDataDefinition(CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION,0,(uint)Marshal.SizeOf(typeof(PMDG.PMDG_NGX_Data)), 0.0f, SimConnect.SIMCONNECT_UNUSED);
            simconnect.RegisterDataDefineStruct<PMDG.PMDG_NGX_Data>(CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION);
   
            simconnect.RequestClientData(CLIENT_DATA_IDS.PMDG_NGX_DATA_ID, DATA_REQUEST_ID.DATA_REQUEST, CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION, SIMCONNECT_CLIENT_DATA_PERIOD.ON_SET, SIMCONNECT_CLIENT_DATA_REQUEST_FLAG.CHANGED, 0, 0, 0);

            simconnect.OnRecvClientData += new SimConnect.RecvClientDataEventHandler(simconnect_RecvClientDataEvent);
        } // testCommunication

        void simconnect_RecvClientDataEvent(SimConnect sender, SIMCONNECT_RECV_CLIENT_DATA data)
        {
      
            switch ((DATA_REQUEST_ID)data.dwRequestID)
            {
                case DATA_REQUEST_ID.DATA_REQUEST:
                    //PMDG.PMDG_NGX_Data da = (PMDG.PMDG_NGX_Data)data.dwData;
                    displayText(data.dwData.Length.ToString());
                    break;

                default:

                    break;
            }
        }

        void simconnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT recEvent)
        {
            displayText("simconnect_OnRecvEvent:" + recEvent.uEventID.ToString());
        }

        void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
        {
            displayText("Connected to FSX");
        }

        // The case where the user closes FSX
        void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
        {
            displayText("FSX has exited");
            closeConnection();
        }

        void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
        {
            displayText("Exception received: " + data.dwException);
        }

        private void closeConnection()
        {
            if (simconnect != null)
            {
                // Dispose serves the same purpose as SimConnect_Close()
                simconnect.Dispose();
                simconnect = null;
                displayText("Connection closed");
            }
        }

        void displayText(string s)
        {
            textBox1.Text += s + "\r\n";
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            closeConnection();
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            closeConnection();
        }

        private void btData_Click(object sender, EventArgs e)
        {
            testCommunication();
        }
    }
}
 
Hi elementarladung,

Could you please post/send me original C# version of you application, that you mention above.

I am trying to read PMDG data, but never receive any client data events and doesn't know why. As I understand you have received event, but only one number arrived. Seems to me I find solution, but have another problem - no event at all.
 
Hi elementarladung,

Could you please post/send me original C# version of you application, that you mention above.

Ok, finally I make it work. There was a problem with sim itself. After restart it start to work.

To those who use .Net and CustomData and still receive only dword, you need to use SimConnect.RegisterStruct:

_simconnect.RegisterStruct<SIMCONNECT_RECV_CLIENT_DATA, PMDG.PMDG_NGX_Data>(CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION);
 
Ok, finally I make it work. There was a problem with sim itself. After restart it start to work.

To those who use .Net and CustomData and still receive only dword, you need to use SimConnect.RegisterStruct:

_simconnect.RegisterStruct<SIMCONNECT_RECV_CLIENT_DATA, PMDG.PMDG_NGX_Data>(CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION);


Hi
can you send your program to me please?
i have some problem
my english is not good
thank you!
my email: liuyang_0804@163.com
 
Hi,

I copied from :"Here is my last code".
There is nothing coming back from FS. simconnect_RecvClientDataEvent() is not being called. There are no errors.

The C++ example that is part of the SDK works as expected. All other simconnect functions to the standard FS work als. What am I doing wrong? I can also send event to PMDG.
But I do not receive any data.
Someone solved this.

elemtarladung, is this realy the code that works for you? Any clues?
The link you posted is no longer active.
 
Back
Top