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

Nearest Airport

Messages
533
Country
france
I am in a C gauge (or a DLL module loaded when FS starts) and I am trying to find the airport on which I am parked.
Obviously, the first idea that came into my mind was to use the GPS to read the NearestAirportICAO variable, but I couldn't get it to work. I read MANY posts on this subject but it seems that nobody could make it work. I think I tried everything but... no success.

I someone could make this work, I would be happy to know about it :D

Thanks,
Eric
 
Hi,
Did you try using execute_calculator_code ?
You could do something like:
First set NearestAirportCurrentLatitude to your a/c latitude, NearestAirportCurrentLongitude to your a/c longitude, NearestAirportMaximumItems to 1 and NearestAirportMaximumDistance to 2000 meters. When NearestAirportItemsNumber is greater than 0, set NearestAirportCurrentLine to 1 (or 0 if it starts in position #0) and read NearestAirportCurrentICAO.
As the search is an asynchronious process, should be called in the ::update() function within the class.

Tom
 
Yes, this is what I did, except that I didn't wait for the NearestAirportItemsNumber to be higher than 0. In fact, I read the result right after sending the request, which is probably why it doesn't work. If I understand you correctly, I should send the request, then watch regulary for NearestAirportItemsNumber until it is not null, then read the results. Is that right?

I can't show my code here because I am not currently at home, but I will change it and let you know if it works better.

Thanks for your help,
Eric
 
If I understand you correctly, I should send the request, then watch regulary for NearestAirportItemsNumber until it is not null, then read the results. Is that right?

Yes, that would be the way. I guess you're using IGaugeCCallback base class and some constant identifiers to access the module from an XML gauge?

Tom
 
No, I don't use the IGaugeCCallback interface. Should I?
Today, I use a standard gauge and I use execute_calculator_code in the gauge callback, when service_id is PANEL_SERVICE_PRE_UPDATE. Do you think this is wrong?

I also tried to use execute_calculator_code in the callback of a timer, within a DLL module that is loaded when FS starts. It didn't work any better...

Eric
 
No, I don't use the IGaugeCCallback interface. Should I?
Today, I use a standard gauge and I use execute_calculator_code in the gauge callback, when service_id is PANEL_SERVICE_PRE_UPDATE. Do you think this is wrong?

No, it's ok that way. I think I misunderstood your statement "I am in a C gauge (or a DLL module loaded when FS starts)" and believe you were actually meaning the DLL module as a "C gauge", and a common C gauge and a DLL module from those loaded at startup are quite different things.

Tom
 
Here is the code I have now, called regularly by PANEL_SERVICE_PRE_UPDATE callback:

Code:
if (!requestSent)
{
	execute_calculator_code("(A:GPS POSITION LAT, Radians) (>C:fs9gps:NearestAirportCurrentLatitude, Radians)", NULL, NULL, NULL);
	execute_calculator_code("(A:GPS POSITION LON, Radians) (>C:fs9gps:NearestAirportCurrentLongitude, Radians)", NULL, NULL, NULL);
	execute_calculator_code("5 (>C:fs9gps:NearestAirportMaximumItems)", NULL, NULL, NULL);
	execute_calculator_code("2 (>C:fs9gps:NearestAirportMaximumDistance, NMiles)", NULL, NULL, NULL);
	requestSent = true;
	requestReply = false;
}

if (!requestReply)
{
	FLOAT64 fValue;
	SINT32 intValue;
	execute_calculator_code("(C:fs9gps:NearestAirportItemsNumber, number)", &fValue, &intValue, NULL);
	Debug("Result = %f / %d", fValue, intValue);
	if (intValue > 0)
	{
		Debug("%d airports found", intValue);
		requestReply = true;
		execute_calculator_code("0 (>C:fs9gps:NearestAirportCurrentLine, number)", NULL, NULL, NULL);
		execute_calculator_code("(C:fs9gps:NearestAirportCurrentICAO, string)", NULL, NULL, &icao);
		Debug("Airport #1: %s", icao);
	}
}
As you can see, I form a request first that sends the aircraft latitude and longitude, the max items and the max distance. Then I read the number of items and read the request result only if the number of items is greater than 0. Unfortunately, it is always 0 so I never get my result...

Taguilo, I read a thread on the "other" website forum and saw that you succeeded in including your code in a class derived from IGaugeCCallback. Does it mean that using such a class is mandatory to have this working?

Thanks again for your help,
Eric
 
Hi Eric, yes you're right. I forgot that calling for a GPS var value is different if done within a C gauge than from a custom DLL. I've worked on these issues long time ago, so I'm going to collect some of those old examples and give you a clear example on how to code for the different cases.

Tom
 
Hi Eric,

This is the code that you can use to retrieve fs9gps data, as well as any data from other(s) custom module(s):

1) Global declare the three types of pointers that you need to access Aircraft, Panel and Gauge callbacks.
Code:
[FONT="Courier New"]IAircraftCCallback *pGPSAircraftCallBack = NULL;
IPanelCCallback *pGPSPanelCallBack = NULL;
IGaugeCCallback *pGPSdata = NULL;[/FONT]

2)You will need to access the functions encapsulated in IPanelCCallBack and IGaugeCCallBack classes. So a template is a good option here. You need one for storing and another for loading data:


Code:
template <class T_in> void tGPS_varset(PCSTRINGZ szProperty, T_in tValue )
{
	SINT32 pI32;
	b = pGPSPanelCallBack->ConvertStringToProperty(szProperty, &pI32);
	b = pGPSdata->SetPropertyValue(pI32,tValue);
}
template <class T_out> T_out tGPS_varget(PCSTRINGZ szProperty,T_out tParam)
{
	SINT32 pI32;
	T_out tValue;
	b = pGPSPanelCallBack->ConvertStringToProperty(szProperty, &pI32);
	b = pGPSdata->GetPropertyValue(pI32,&tValue);
	return tValue;
}

3) You must register "fs9gps" in Panel and Aircraft classes, then create a new instance of IGaugeCCallBack and obtain a pointer to this new class:

Code:
case PANEL_SERVICE_CONNECT_TO_WINDOW:
{	
      pGPSPanelCallBack = panel_get_registered_c_callback("fs9gps");
      pGPSAircraftCallBack = panel_get_aircraft_c_callback("fs9gps");
      pGPSdata = pGPSAircraftCallBack->CreateGaugeCCallback();
}
break;

4) Now you're ready to save and retrieve data from gps variables:

Code:
case PANEL_SERVICE_PRE_UPDATE:
{
	FLOAT64 fGPSVal;				
	PCSTRINGZ szGPSVal;			
        fGPSVal = tGPS_varget<FLOAT64>("FlightPlanIsActiveFlightPlan",0);
	szGPSVal = tGPS_varget<PCSTRINGZ>("FlightPlanDepartureAirportIdent","s");

	FLOAT64 fWpt=2;
	tGPS_varset("FlightPlanIsActiveWaypoint",fWpt);

	PCSTRINGZ szICAO = "KORD";
	tGPS_varset("FlightPlanWaypoint",szICAO);

}
break;

Naturally, you can #define different IDs to make things simpler :D, for example

Code:
#define GPS_LOADN	       tGPS_varget<FLOAT64>
#define GPS_LOADS             tGPS_varget<PCSTRINGZ>
#define NUMBER                  0 
#define STRING                   "s"

and use:

Code:
fGPSVal   =  GPS_LOADN("FlightPlanIsActiveFlightPlan",NUMBER);
szGPSVal =   GPS_LOADS("FlightPlanDepartureAirportIdent",STRING);

Hope this helps. Notice that with this method you can access any other module, like XMLVars.dll; Logger.dll, etc.

Tom
 
Last edited:
Thanks to your code sample, I almost got it to work. I don't know why FS crashes when I try to read NearestAirportItemsNumber.

Here is my code (in the PANEL_SERVICE_PRE_UPDATE callback) :

Code:
if (!request)
{
	lookup_var(&latVar);
	tGPS_varset("NearestAirportCurrentLatitude", latVar.var_value.n);
	lookup_var(&lonVar);
	tGPS_varset("NearestAirportCurrentLongitude", lonVar.var_value.n);
	FLOAT64 maxItems = 5;
	tGPS_varset("NearestAirportMaximumItems", maxItems);
	FLOAT64 maxDistance = 2;
	tGPS_varset("NearestAirportMaximumItems", maxDistance);
	request = true;
}
FLOAT64 nbItems;
PCSTRINGZ icao;
nbItems = tGPS_varget<FLOAT64>("NearestAirportItemsNumber", 0);
if (nbItems > 0)
	icao = tGPS_varget<PCSTRINGZ>("NearestAirportCurrentICAO","s");
Debug("Items = %.1f / ICAO = %s", nbItems, icao);

I put a 'request' boolean so that the request input is done only once, but it crashes when I read the number of items. If I remove this line, it works but my 'icao' string is always empty. I feel I am close to the goal but still not there...

FYI, your code that reads the flight plan departure airport worked perfect (even if there are some little errors in the code you attached :)), so my code should work as well :confused:

Thank you,
Eric
 
FYI, your code that reads the flight plan departure airport worked perfect (even if there are some little errors in the code you attached :)), so my code should work as well :confused:

Thank you,
Eric

Oh yes, sorry; I forgot to mention the destructor, but I guess you've figured out already:

Code:
case PANEL_SERVICE_PRE_KILL:
{	
	pGPSdata->Release();
}
break;

Appart from that or any typo, the code attached is actually working in a gauge.

Now, your code, with a bunch of modifications, will work as well:

Code:
if (!request)
{
    
     ....lookup latVar and lonVar then....

     FLOAT64 fLat = (latVar.var_value.n / 40007000.0) * 360,
                  fLon = (lonVar.var_value.n / 281474976710656.0) * 360,
  		  fMaxItems = 5,
		  fMaxDistance = 2000;

     tGPS_varset("NearestAirportCurrentLatitude", fLat);
     tGPS_varset("NearestAirportCurrentLongitude", fLon);
     tGPS_varset("NearestAirportMaximumItems", fMaxItems);
     tGPS_varset("NearestAirportMaximumDistance", fMaxDistance);
     request = true;
}
   
FLOAT64 fNItems;    
fNItems = tGPS_varget<FLOAT64>("NearestAirportItemsNumber", 0);

I believe you have declared request as global outside the callback function otherwise you are reseting it to false on each cycle.


Tom
 
I understand latitude and longitude were not computed the right way, thank you for the correction, but it still doesn't work. I still have the same problem: crash when reading number of items (NearestAirportItemsNumber). When I remove this line, it doesn't crash but I have no result...

Yes, the request bool is defined as a static so it is not re-intialized at each cycle.

I wonder why reading the items number still crashes FS...

Eric
 
So far I can't help you on this, it works ok for me. It seems "NearestAiportItemsNumber" is returning an invalid value. Did you try tGPS_varget calls for reading other GPS variables?

Tom
 
Yes, indeed, it seems an invalid value is returned. I didn't try many other variables, but I can tell you the variables used in your sample code were working fine. Anyway, you already helped me much and I thank you for this, I will try to work this out and let you know if I find the solution.

Just a quick question: in the tGPS_varget function, the tParam input parameter is not used. Is this normal?

Eric
 
Last edited:
Just a quick question: in the tGPS_varget function, the tParam input parameter is not used. Is this normal?

Eric

That's correct, it's not used in this case so it can be removed. Actually is a carry from some tests I once made with the source gauge :D

Tom
 
Just to let you know about the work in progress...
I've just realized it was not crashing because of reading the number of items, but because I was trying to display the icao string that was not initialized and was probably a pointer to... nowhere. I also realized I was initializing NearestAirportMaximumItems twice and didn't initalize NearestAirportMaximumDistance. A copy/paste error again...

My code now looks like this:

Code:
if (!request)
{
	lookup_var(&latVar);
	FLOAT64 fLat = (latVar.var_value.n / 40007000.0) * 360;
	tGPS_varset("NearestAirportCurrentLatitude", fLat);
	lookup_var(&lonVar);
	FLOAT64 fLon = (lonVar.var_value.n / 281474976710656.0) * 360;
	tGPS_varset("NearestAirportCurrentLongitude", fLon);
	FLOAT64 maxItems = 5;
	tGPS_varset("NearestAirportMaximumItems", maxItems);
	FLOAT64 maxDistance = 2000;
	tGPS_varset("NearestAirportMaximumDistance", maxDistance);
	request = true;
}
FLOAT64 nbItems = 0.0;
PCSTRINGZ icao = 0;
SINT32 pI32 = 0;
nbItems = tGPS_varget<FLOAT64>("NearestAirportItemsNumber");
Debug("Items = %.1f", nbItems);
if (nbItems > 0)
{
	icao = tGPS_varget<PCSTRINGZ>("NearestAirportCurrentICAO");
	if (icao)
		Debug("Items = %.1f / ICAO = %s", nbItems, icao);
}

Now it doesn't crash any more, and NearestAirportItemsNumber is now 1.0 (good news), but I still can't read the ICAO from the GPS...
I keep searching.

Eric
 
Last edited:
Have you tried "NearestAirportCurrentIdent" ? If you want to identify the airport where you're sitting at, like "KORD", "LFPO", etc, that is the property to read.

Now, I'm rather confused about your FS crashes. The pointer to a PCSTRINGZ property that is correct called for inspection but still has no value defined returns an empty string, but should not make FS crash. However, if you do use a bad property name, one that is not defined in the module, the pointer will point to elsewhere, and you could obtain garbage or a FS crash. Maybe it's the debug function that you are using what causes the trouble? I've never tried debug() inside FS, always use wsprintf() for gauge debugging outputs.

Tom
 
Last edited:
I finally got it to work and I can use NearestAirportCurrentIdent or NearestAirportCurrentICAO, they both work. As you said, NearestAirportCurrentIdent corresponds to what I need so I will use this one. In fact, it works because I have created a loop with NearestAirportItemsNumber, using NearestAirportCurrentLine for each iteration.

Now to answer your remark, Debug is a function I have developed, which writes in a file using fprintf or to the debug output using OutputDebugString. I can tell you this function works fine, I use it for years... The problem was not here. The problem was that PCSTRINGZ icao was not initialized until NearestAirportItemsNumber is greater than 0. In my previous code, this value was always 0 so icao was never initialized and I was trying to display it using Debug. Doing this with an unitialized pointer is always at risk, and in this case it was crashing FS, which is normal.

Anyway, the code now works fine. I thank you for your help, I couldn't make it without you.

Eric
 
Ah ok, I read your offending code again and see what you mean.
I'm glad that everything's working fine for you now :)


Tom
 
Back
Top