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

MSFS problems accessing aircraft nav data

Messages
9
Country
us-oregon
Hi there,
I wrote a program in C++ that successfully accesses aircraft instrument data, but when I try to access any other data, all I get is 0. Here is a snippet of the code:
C++:
enum DATA_DEFINE_ID{
    DEFINITION_1,
    DEFINITION_2,
};

enum DATA_REQUEST_ID{
    REQUEST_1,
    REQUEST_2,
    REQUEST_3
};

enum EVENT_ID{
    EVENT_SIM_START,
};
    
struct instruments{
    double airspeed;
    double bank;
    double pitch;
    double altitude;
    double kholsman;
    double turn;
    double slip;
    double heading;
    double v_speed;
    double glide_slope;
    double obs_sel;
};

void CALLBACK MyDispatchProcRD(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext){
    HRESULT hr;
    
    SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
    
    hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1,
    DEFINITION_1, SIMCONNECT_DATA_REQUEST_FLAG_CHANGED, SIMCONNECT_SIMOBJECT_TYPE_USER);
    
    SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE *pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE*)pData;
    
    
    
    airspeed = pS->airspeed;
    bank = pS->bank;
    pitch = pS->pitch;
    altitude = pS->altitude;
    kholsman = pS->kholsman;
    turn = pS->turn;
    slip = pS->slip;
    heading = pS->heading;
    v_speed = pS->v_speed;
    glide_slope = pS->glide_slope;
    obs_sel = pS->obs_sel;
}

void h_Result(){
    HRESULT hr;
    if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Request Data", NULL, 0, 0, 0))){
        printf("Connected\n");
        
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "airspeed indicated", "knots");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "ATTITUDE INDICATOR BANK DEGREES", "Radians");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "ATTITUDE INDICATOR PITCH DEGREES", "Radians");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "INDICATED ALTITUDE", "Feet");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "KOHLSMAN SETTING HG", "inHg");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "DELTA HEADING RATE", "Radians per second");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "TURN COORDINATOR BALL", "Position 128");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "PLANE HEADING DEGREES GYRO", "Radians");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "VERTICAL SPEED", "Feet per second");
        
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "NAV_GSI", "Number");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "NAV OBS : 1", "Degrees");
        
    }else{
        printf("WARNING! Not Connected!\nPlease restart after opening MSFS\n");
    }
}

All of the instrument related variables give me the expected values, but anything else just gives me 0.
Can anyone advise what I'm doing wrong?
Thanks!
 
The stupid question is; are you tuned to a glideslope and are you on the glideslope? I'd also change this:

SIMCONNECT_DATA_REQUEST_FLAG_CHANGED

to

SIMCONNECT_PERIOD_SECOND

[Edit] Don't use SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE. Use SIMCONNECT_RECV_SIMOBJECT_DATA instead.
 
Last edited:
Thanks for the reply!
Yes, I am tuned to a glideslope and I can see the needles active in the sim.
I have played arround with the flags, but I will try the SIMCONNECT_PERIOD_SECOND a little later today, and see if it works.
 
Well, I was going to implement DragonflightDesign's suggestion, but MSFS had a mandatory update. So I'll have to try it tomorrow.
 
😄 Don't forget to change to SIMCONNECT_RECV_SIMOBJECT_DATA too. That's likely to have the biggest effect.

-Dai
 
Once this update is done, I will definitely give it a go.
I also noticed in the release notes that they are adding some new simvars as well as fixing some old ones.
 
I tried your suggestion but it still does not seem to work. Here is my new code:
Code:
void CALLBACK MyDispatchProcRD(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext){
    HRESULT hr;
   
    SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
   
    hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1,
    DEFINITION_1, SIMCONNECT_PERIOD_SECOND, SIMCONNECT_SIMOBJECT_TYPE_USER);
    //-----------------------^
    SIMCONNECT_RECV_SIMOBJECT_DATA *pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA*)pData;
    //---------------------------------^                                           ^

    instruments *pS = (instruments*)&pObjData->dwData;
   
    airspeed = pS->airspeed;
    bank = pS->bank;
    pitch = pS->pitch;
    altitude = pS->altitude;
    kholsman = pS->kholsman;
    turn = pS->turn;
    slip = pS->slip;
    heading = pS->heading;
    v_speed = pS->v_speed;
    glide_slope = pS->glide_slope;
    obs_sel = pS->obs_sel;
}

void h_Result(){
    HRESULT hr;
    if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Request Data", NULL, 0, 0, 0))){
        printf("Connected\n");
       
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "airspeed indicated", "knots");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "ATTITUDE INDICATOR BANK DEGREES", "Radians");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "ATTITUDE INDICATOR PITCH DEGREES", "Radians");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "INDICATED ALTITUDE", "Feet");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "KOHLSMAN SETTING HG", "inHg");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "DELTA HEADING RATE", "Radians per second");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "TURN COORDINATOR BALL", "Position 128");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "PLANE HEADING DEGREES GYRO", "Radians");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "VERTICAL SPEED", "Feet per second");
       
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "NAV_GSI", "Number");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "NAV OBS : 1", "Degrees");
       

    }else{
        printf("WARNING! Not Connected!\nPlease restart after opening MSFS\n");
    }
}
This is how I'm printing the data:
Code:
if(glide_slope != 0 && glide_slope != -0){
        printf("hsi_glide = %f\n", hsi_glide);
    }
But I it still prints out lots of zeros!
0.jpg


It is also worth mentioning that every once in a while, it spits out an impossibly large number on a totally unpredictable basis.
 
Typo for the glideslope? 'hsi_glide' when it should be 'glide_slope'?
Code:
if(glide_slope != 0){
        printf("hsi_glide = %f\n", glide_slope);
    }
Those 'impossibly large numbers' are most likely just crap in the buffer as you seem to be trying to read from a non-initialised area.
 
I am wondering why you are executing this line of code every time you receive data:
hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1,
DEFINITION_1, SIMCONNECT_PERIOD_SECOND, SIMCONNECT_SIMOBJECT_TYPE_USER);

It should only be executed once.
 
It shouldn't be called in the callback routine at all. It should only get called once.
 
Ah - just caught up with you fully. Yes. It shouldn't be there. I see what's happened; I'm guessing he used the RequestData SDK example but missed out the Switch calls.
 
Last edited:
Thanks for all the replies!
You are right about the SDK example.
After a while of trying to implement the switch() thing, I realized that their example replaced the first switch() with "...." Because I had no idea what went there, I tried other ways of doing it till I finally got the first code to give me instrumentation data. That led me down the rosy path of thinking that was the only way to do it till I couldn't get any nav data.
So my (dumb?) question is, what expression goes in the mysterious switch() statement?
 
I see this for the switch statement:
C++:
void CALLBACK MyDispatchProcRD(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
{
    HRESULT hr;
    
    switch(pData->dwID)
    {
        case SIMCONNECT_RECV_ID_EVENT:
        {
            SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;

            switch(evt->uEventID)
            {
                case EVENT_SIM_START:
                    
                    // Now the sim is running, request information on the user aircraft
                    hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1, DEFINITION_1, 0, SIMCONNECT_SIMOBJECT_TYPE_USER);
                    
                    break;

                default:
                   break;
            }
            break;
        }

        case SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE:
        {
            SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE *pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE*)pData;
            
            switch(pObjData->dwRequestID)
            {
                case REQUEST_1:
                {
                    DWORD ObjectID = pObjData->dwObjectID;
                    Struct1 *pS = (Struct1*)&pObjData->dwData;
                    if (SUCCEEDED(StringCbLengthA(&pS->title[0], sizeof(pS->title), NULL))) // security check
                    {
                        printf("\nObjectID=%d  Title=\"%s\"\nLat=%f  Lon=%f  Alt=%f  Kohlsman=%.2f", ObjectID, pS->title, pS->latitude, pS->longitude, pS->altitude, pS->kohlsmann );
                    } 
                    break;
                }

                default:
                   break;
            }
            break;
        }


        case SIMCONNECT_RECV_ID_QUIT:
        {
            quit = 1;
            break;
        }

        default:
            printf("\nReceived:%d",pData->dwID);
            break;
    }
}
 
The best thing you can do is to start again with the RequestData example. It sends a request for aircarft data once a second in testDataRequest():
Code:
        while( 0 == quit )
        {
            SimConnect_CallDispatch(hSimConnect, MyDispatchProcRD, NULL);
            Sleep(1);
        }
 
I see I made a mistake, I had been looking at the API reference online, and failed to take the SDK example into consideration. The SDK example is complete, the API reference has the "....".

The SDK example successfully gets the data I had been getting before, but when I try to get the glideslope data (I am tuned to the ILS and the needles are alive), I still get zeros.
Again, I can get all other aircraft instrumentation data, but no nav data.

Here is my current code:
Code:
void CALLBACK MyDispatchProcRD(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
{
    HRESULT hr;
    printf("DBUG 01\n");
    switch(pData->dwID)
    {
        case SIMCONNECT_RECV_ID_EVENT:
        {
            SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
            printf("DBUG 02\n");

            switch(evt->uEventID)
            {
                case EVENT_SIM_START:
                    printf("DBUG 03\n");
                    // Now the sim is running, request information on the user aircraft
                    hr = SimConnect_RequestDataOnSimObject(hSimConnect, REQUEST_1, DEFINITION_1, SIMCONNECT_SIMOBJECT_TYPE_USER, SIMCONNECT_PERIOD_SECOND);
                    
                    break;

                default:
                printf("DBUG 04\n");
                   break;
            }
            break;
        }

        case SIMCONNECT_RECV_ID_SIMOBJECT_DATA:
        {
            SIMCONNECT_RECV_SIMOBJECT_DATA *pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA*)pData;
            printf("DBUG 05\n");
            switch(pObjData->dwRequestID)
            {
                case REQUEST_1:
                {
                    DWORD ObjectID = pObjData->dwObjectID;
                    Struct1 *pS = (Struct1*)&pObjData->dwData;
                    printf("DBUG 06\n");
                    if (SUCCEEDED(StringCbLengthA(&pS->title[0], sizeof(pS->title), NULL))) // security check
                    {
                        printf("\nObjectID=%d  Title=\"%s\"\nLat=%f  Lon=%f  Alt=%f  Kohlsman=%.2f", ObjectID, pS->title, pS->latitude, pS->longitude, pS->altitude, pS->kohlsmann );
                    } 
                    break;
                }

                default:
                printf("DBUG 07\n");
                   break;
            }
            break;
        }


        case SIMCONNECT_RECV_ID_QUIT:
        {
            printf("DBUG 08\n");
            quit = 1;
            
            break;
        }

        default:
            printf("\nReceived:%d",pData->dwID);
            break;
    }
}

void testDataRequest()
{
    HRESULT hr;
    printf("DBUG 09\n");
    if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Request Data", NULL, 0, 0, 0)))
    {
        printf("\nConnected to Flight Simulator!");   

        // Set up the data definition, but do not yet do anything with it
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Title", NULL, SIMCONNECT_DATATYPE_STRING256);
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "AIRSPEED INDICATED", "Knots");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Altitude", "feet");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "NAV GLIDE SLOPE ERROR", "degrees");
        hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Longitude", "degrees");

        // Request an event when the simulation starts
        hr = SimConnect_RequestDataOnSimObject(hSimConnect, REQUEST_1, DEFINITION_1, SIMCONNECT_SIMOBJECT_TYPE_USER, SIMCONNECT_PERIOD_SECOND);

        
        
        printf("\nLaunch a flight.");

        while( 0 == quit )
        {
            SimConnect_CallDispatch(hSimConnect, MyDispatchProcRD, NULL);
            Sleep(1);
        } 

        hr = SimConnect_Close(hSimConnect);
    }
}
Note that I have a SimConnect_RequestDataOnSimObject in the testDataRequest() function so I can start the program after a flight is already running.

Thanks for the helpful replies!
 
NAV GLIDE SLOPE ERROR requires an index value... NAV GLIDE SLOPE ERROR: index is how the SDK defines it.
 
Thanks for pointing that out!
I finally got success with "HSI GSI NEEDLE", but I wasn't able to get anything from "NAV GLIDE SLOPE ERROR : 1"
 
Back
Top