• 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.

FSX SimConnect does not return any data

Messages
639
Country
panama
I am getting into SimConnect using the default SimConnect (SP2) from Microsoft.

In my code I instantiate an FSXUtil, do an OpenConnection, make sure it is connected and then call GetPosition() . I see the request is sent but the receive event handler, although registered, never gets any data back from FSX/SimConnect.

Here is the FSXUtil class:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;       // Needed for SimConnect
using Microsoft.FlightSimulator.SimConnect; // The SimConnect library (add reference to FSX SDK DLL)

namespace TestSimConnect
{
/// <summary>
    /// The data containers that collect data from the simulator
    /// </summary>
    enum FsxRequestDefinition
    {
        GeoPositionData
    }

    /// <summary>
    /// The types of requests made to FSX
    /// </summary>
    enum FsxRequest
    {
        RequestPosition
    }

    /// <summary>
    /// 
    /// </summary>
    /// <remarks>The StructLayout metatag is used to declara a data structure
    /// so that SimConnect knows how to fill it or read it.
    /// </remarks>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    struct GeoPositionData
    {
        // this is how you declare a fixed size string
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public String Title;
        public double Latitude;
        public double Longitude;
        public double Altitude;
    };

    /// <summary>
    /// Utility class to marshall requests to FSX using SimConnect
    /// </summary>
    internal class FSXUtil
    {
        private string clientName = "CTFSX";
        private IntPtr clientHandle = IntPtr.Zero;
        private SimConnect simConnect = null;
        const int WM_USER_SIMCONNECT = 0x0402;  // user-defined Win32 event

        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="handle">Handle of the GUI form corresponding to client application. i.e. this.Handle</param>
        /// <param name="friendlyName">Friendly name of the application</param>
        internal FSXUtil(IntPtr handle, string friendlyName)
        {
            if (handle == IntPtr.Zero)
                throw new ArgumentNullException("Handle to application was not given");
            else
                clientHandle = handle;
            if (!string.IsNullOrEmpty(friendlyName))
                clientName = friendlyName.Trim();
        }

        /// <summary>
        /// Open a connection to the SimConnect server, in other words the FSX application. It is required
        /// that the FSX application has been configured to enable SimConnect (see FSX documentation)
        /// </summary>
        /// <returns>true on success, false if unable to connect</returns>
        internal bool OpenConnection()
        {
            bool success = true;
            try
            {
                simConnect = new SimConnect(this.clientName, this.clientHandle, WM_USER_SIMCONNECT, null, 0);
                InitDataRequest();
            }
            catch (COMException cex)
            {   // A former HRESULT error results in a COM Exception in managed code
                // TODO: A connection to the SimConnect server could not be established.
                success = false;
                Console.WriteLine(String.Format("SimConnect.Open() : {0}", cex.Message));
            }
            return success;
        }

        /// <summary>
        /// Setup all the SimConnect related data definitions and event handlers
        /// </summary>
        private void InitDataRequest()
        {
            try
            {
                // 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);

                // define a data structure
                simConnect.AddToDataDefinition(FsxRequestDefinition.GeoPositionData, "Title", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(FsxRequestDefinition.GeoPositionData, "A/C Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(FsxRequestDefinition.GeoPositionData, "A/C Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(FsxRequestDefinition.GeoPositionData, "A/C Altitude", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);

                // IMPORTANT: register it with the simconnect managed wrapper marshaller
                // if you skip this step, you will only receive a uint in the .dwData field.
                simConnect.RegisterDataDefineStruct<GeoPositionData>(FsxRequestDefinition.GeoPositionData);

                // catch a simobject data request
                simConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(simConnect_OnRecvSimobjectDataBytype);
            }
            catch (COMException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// Close the connection to the SimConnect server
        /// </summary>
        internal void CloseConnection()
        {
            if (simConnect != null)
            {
                simConnect.Dispose();
                simConnect = null;
            }
        }

        /// <summary>
        /// Send a request to retrieve the current position of the aircraft and
        /// receive the result asynchronously.
        /// </summary>
        internal void GetPosition()
        {
            simConnect.RequestDataOnSimObjectType(
                FsxRequest.RequestPosition, 
                FsxRequestDefinition.GeoPositionData, 
                0, 
                SIMCONNECT_SIMOBJECT_TYPE.USER);

            System.Diagnostics.Debug.WriteLine("Request GeoPosition sent.");
            Console.WriteLine("Request GeoPosition sent.");
        }

        #region SimConnect Event Handlers
        /// <summary>
        /// We are now connected to the SimConnect FSX server
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        void simConnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
        {
            System.Diagnostics.Debug.WriteLine("Connected to FSX");
            Console.WriteLine("Connected to FSX");
        }

        /// <summary>
        /// The user closed/exited Flight Simulator X
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        void simConnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
        {
            System.Diagnostics.Debug.WriteLine("FSX has exited");
            Console.WriteLine("FSX has exited");
            CloseConnection();
        }

        /// <summary>
        /// We received an exception from SymConnect server
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        void simConnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
        {
            System.Diagnostics.Debug.WriteLine("Exception received: " + data.dwException);
            Console.WriteLine("Exception received: " + data.dwException);
        }

        /// <summary>
        /// This event handler receives all data from SimConnect in response to a previous
        /// request.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        void simConnect_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data)
        {
            System.Diagnostics.Debug.WriteLine("Data received type: " + (FsxRequest)data.dwRequestID);
            Console.WriteLine("Data received type: " + (FsxRequest)data.dwRequestID);
            switch ((FsxRequest)data.dwRequestID)
            {
                case FsxRequest.RequestPosition:
                    GeoPositionData s1 = (GeoPositionData)data.dwData[0];

                    Console.WriteLine("Title: " + s1.Title);
                    Console.WriteLine("Lat:   " + s1.Latitude);
                    Console.WriteLine("Lon:   " + s1.Longitude);
                    Console.WriteLine("Alt:   " + s1.Altitude);
                    break;

                default:
                    Console.WriteLine("Unknown request ID: " + data.dwRequestID);
                    break;
            }
        }

        #endregion
    }
}

What am I doing wrong ehre? this is like the SDK says it should be done, yet it doesn't work. Like I said, there is a connection, tried with both FSX and without FSX and when FSX is on then it connects properly but that is about it.
 
Back
Top