Page 1 of 1

How to use execute_calculator_code function to read GPS Variables (C:fs9gps) in any aircraft?

Posted: Mon Mar 20, 2017 4:34 am
by laiyokabc
Hi,
I hope that use c++ language to read GPS Variables (C:fs9gps) in any aircraft,My code is based on the example of P3D C++/XML Mixed Mode Sample 'CabinComfort' modification,I met two problems:

1.The Sample code only used to one aircraft(The sample is Mooney_Bravo),I hope that in any aircraft.

2.I try to create a separate thread to call execute_calculator_code function, P3D(V3.1 version) after completion of loading always crash. This is my code snippet:

  • bool bStart = false;
    HANDLE hMyMainThread = NULL;
    void My_main(void* pArguments);
    void FSAPI module_init(void)
    {
    if (NULL != Panels)
    {
    ImportTable.PANELSentry.fnptr = (PPANELS)Panels;
    CABINPanelCallbackInit();
    bStart = true;
    hMyMainThread = (HANDLE)_beginthread(My_main, 0, NULL); //Create a separate thread to call execute_calculator_code function
    }
    }

    void FSAPI module_deinit(void)
    {
    bStart = false;
    WaitForSingleObject(hMyMainThread, 1000);
    CABINPanelCallbackDeInit();
    }

    void My_main(void* pArguments)
    {
    while (bStart)
    {
    FLOAT64 att_pitch = 0;
    FLOAT64 att_bank = 0;
    PCSTRINGZ WaypointAirportICAO;
    execute_calculator_code ("(A:ATTITUDE INDICATOR PITCH DEGREES:2, degrees)", &att_pitch, NULL, NULL); //P3D(V3.1 version) after completion of loading always crash
    execute_calculator_code ("(A:ATTITUDE INDICATOR BANK DEGREES:2, degrees)", &att_bank, NULL, NULL); //P3D(V3.1 version) after completion of loading always crash
    execute_calculator_code("(C:fs9gps:WaypointAirportICAO, String)",NULL,NULL,&WaypointAirportICAO); //P3D(V3.1 version) after completion of loading always crash
    Sleep(10);
    }
    }


Is there a better way, only need .DLL file, without the XML file?
If there is a use of c++ execute_calculator_code function to get GPS Variables (C: fs9gps) example is the best!

thanks,
Yorke

Re: How to use execute_calculator_code function to read GPS Variables (C:fs9gps) in any aircraft?

Posted: Mon Mar 20, 2017 10:25 am
by WarpD
Your entire approach is flawed and has nothing to do with execute_calculator_code itself.

I'm not certain what you're trying to create, but the DLL for CabinComfort is a module that is loaded by the sim whenever the sim is started. The variables you are wanting to read are only valid during an actual flight, yet your approach attempts to read them every 10 milliseconds. That's a crash source.

There is no legitimate reason for your DLL to constantly use execute_calculator_code to begin with, but even more importantly you would be far better off learning how to set up a SimConnect interface to get the data you seek, and then only when in a flight and only at a rate that doesn't tax the core sim.

You have a great deal of learning ahead of you.

Re: How to use execute_calculator_code function to read GPS Variables (C:fs9gps) in any aircraft?

Posted: Mon Mar 20, 2017 2:16 pm
by laiyokabc
WarpD wrote: Mon Mar 20, 2017 10:25 am Your entire approach is flawed and has nothing to do with execute_calculator_code itself.

I'm not certain what you're trying to create, but the DLL for CabinComfort is a module that is loaded by the sim whenever the sim is started. The variables you are wanting to read are only valid during an actual flight, yet your approach attempts to read them every 10 milliseconds. That's a crash source.

There is no legitimate reason for your DLL to constantly use execute_calculator_code to begin with, but even more importantly you would be far better off learning how to set up a SimConnect interface to get the data you seek, and then only when in a flight and only at a rate that doesn't tax the core sim.

You have a great deal of learning ahead of you.
Thanks for your reply,I am a beginner P3D plugin development.

Use SimConnect interface can't read/write GPS Variables(C:fs9gps), So choosing the way of the module,But not yet understand the callback function principle of it way.

Could you please send me a example of Get/Set GPS variables?

Thank you very much

Re: How to use execute_calculator_code function to read GPS Variables (C:fs9gps) in any aircraft?

Posted: Mon Mar 20, 2017 3:06 pm
by WarpD
I have never had to make a module that accesses GPS via execute_calculator_code... and it really doesn't matter a great deal as to what you're using the function for.

A module should remain dormant except for when it actually has a purpose. Since there is no GPS information except for when there is a flight that's active, your module should be completely silent and doing absolutely nothing until there is a flight that is active.

A DLL must be minimally intrusive. If it isn't... things can go horribly wrong performance wise.

The function execute_calculator_code only exists when there is a panel active in an aircraft. Any attempts to use it will fail if there is not an aircraft with a panel loaded into a flight.

Re: How to use execute_calculator_code function to read GPS Variables (C:fs9gps) in any aircraft?

Posted: Mon Mar 20, 2017 3:23 pm
by laiyokabc
WarpD wrote: Mon Mar 20, 2017 3:06 pm I have never had to make a module that accesses GPS via execute_calculator_code... and it really doesn't matter a great deal as to what you're using the function for.

A module should remain dormant except for when it actually has a purpose. Since there is no GPS information except for when there is a flight that's active, your module should be completely silent and doing absolutely nothing until there is a flight that is active.

A DLL must be minimally intrusive. If it isn't... things can go horribly wrong performance wise.

The function execute_calculator_code only exists when there is a panel active in an aircraft. Any attempts to use it will fail if there is not an aircraft with a panel loaded into a flight.
Thank you for your advice, I have to reconsider my code, do you have a better method for get GPS variables in any aircraft ?

Re: How to use execute_calculator_code function to read GPS Variables (C:fs9gps) in any aircraft?

Posted: Mon Mar 20, 2017 6:11 pm
by JB3DG
Use the IAircraftCCallback, IPanelCCallback, and IGaugeCCallback interfaces.

https://forums.vrsimulations.com/suppor ... ia_C.2B.2B

As WarpD pointed out, trying to access the panel functions in your own separate thread from the system is asking for a crash. Rather create a SimConnect callback, and monitor the SimStart event. When it occurs, do a SimConnect_RequestDataOnSimObject using some A var you don't need, accessed via SimConnect_AddToDataDefinition, and set the SIMCONNECT_PERIOD to SIMCONNECT_PERIOD_SIMFRAME.

You can then monitor that request event and retrieve the data using aircraft_varget (see SDK for how this function works) for A vars and the IGaugeCCallback interface for fs9gps.

Re: How to use execute_calculator_code function to read GPS Variables (C:fs9gps) in any aircraft?

Posted: Tue Mar 21, 2017 3:05 am
by laiyokabc
Naruto-kun wrote: Mon Mar 20, 2017 6:11 pm Use the IAircraftCCallback, IPanelCCallback, and IGaugeCCallback interfaces.

https://forums.vrsimulations.com/suppor ... ia_C.2B.2B

As WarpD pointed out, trying to access the panel functions in your own separate thread from the system is asking for a crash. Rather create a SimConnect callback, and monitor the SimStart event. When it occurs, do a SimConnect_RequestDataOnSimObject using some A var you don't need, accessed via SimConnect_AddToDataDefinition, and set the SIMCONNECT_PERIOD to SIMCONNECT_PERIOD_SIMFRAME.

You can then monitor that request event and retrieve the data using aircraft_varget (see SDK for how this function works) for A vars and the IGaugeCCallback interface for fs9gps.
You give I pointed out the direction, I try to do so.
Thank you very much