plane icon Welcome to Microsoft Flight Simulator’s SDK Q&A Platform!

You have questions regarding the SDK? DevMode Tools? SimConnect? You would like to submit an idea for future improvements, seek help or exchange knowledge? You’re in the right place.


Please take a moment to read the platform’s guidelines before you get started!


question

Clamb avatar image
Clamb asked Clamb commented

Get Airport data in C# using SimConnect_AddToFacilityDefinition

Hi,

I'm desperately trying to get airport information from MSFS using the relatively new SimConnect_AddToFacilityDefinition function.


Sending the request and receiving some kind of data is working fine. However, I'm not able to get more than a single int.

I've made a bare-minimum example for VS2022 in .NET 6 for you to test it yourself easily: https://github.com/Clamb94/SimconnectAirportDataTest


It's obviously very simple. It opens the SimConnect connection and checks every 500ms for a new message.

Once it get's the OSimConnect_OnRecvOpen message, it request the latitude and number of runways in Frankfurt.


```

OSimConnect.AddToFacilityDefinition(sd, "OPEN AIRPORT");

OSimConnect.AddToFacilityDefinition(sd, "LATITUDE");

OSimConnect.AddToFacilityDefinition(sd, "N_RUNWAYS");

OSimConnect.AddToFacilityDefinition(sd, "CLOSE AIRPORT");


OSimConnect.RequestFacilityData(sd, rd, "EDDF", "");

```

Shortly thereafter, I'll receive a response on OSimConnect_OnRecvFacilityData(SimConnect sender, SIMCONNECT_RECV_FACILITY_DATA data).

Now comes the problem: I can't make anything useful out of the data member.


When I'm only requesting a single INT32, like "N_RUNWAYS", the data.Data[0] variable holds a value of 4, which is correct for Frankfurt.


When I'm only requesting a single FLOAT64, like "LATITUDE", the data.Data[0] variable holds an UInt32 with value 1550843904, which doesn't make any sense to me.

I tried casting it to an double, but this throws an exception:


```
double lat = (double)data.Data[0];

throws:

{"Unable to cast object of type 'System.UInt32' to type 'System.Double'."} System.Exception {System.InvalidCastException}

```


Obviously, I'm also not able to request more than one value at a time and get a useful response.

I tried casting it to a struct like this, but this also throws a System.InvalidCastExcpetion.

(That's the version on GitHub)


```
[StructLayout(LayoutKind.Sequential, Pack = 1)
struct airport

{

public double latitude;

public int nRunways;

};


airport a = (airport)data.Data[0];

Console.WriteLine($"Lat: {a.latitude}; Rwys: {a.nRunways}");
```


There is an example C++ included in the SDK samples called FacilityDataDefinition, they also cast the reply in a struct.

But I can't seem to replicate the C++ code in C#.


Any ideas how to solve this?

Thanks


Best regards

Axel

simconnect
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

1 Answer

·
Arzop avatar image
Arzop answered Clamb commented

Hi

C# has some protections, so you can't cast a type to another if the two types are not compatible. In C++, that kind of protection doesn't exist.

In your case, you try to cast an object to airport but the compiler "doesn't" know that object is an airport, so the cast is impossible (That's why you have a System.InvalidCastException).

There are two ways to solve that issue:

- Use RegisterFacilityDataDefineStruct: SimConnect provides some functions which bind a type to an id. So if you receive that id, the object you received is castable to the type you gave.

- Use Marshall: This is a low level method which allows you to cast manually what you want using pointer.... (C style). I don't recommend you to use this method which is more difficult to implement and may lead to bigger issue much harder to solve.


So, if you add the line:

OSimConnect.RegisterFacilityDataDefineStruct<airport>(SIMCONNECT_FACILITY_DATA_TYPE.AIRPORT);

Before requesting data (before RequestFacilityData), the problem should be solved.


Best Regards

Maxime / Asobo

1 comment
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

Clamb avatar image Clamb commented ·

RegisterFacilityDataDefineStruct<airport> is working like a charm.
Thank you, highly appreciated.

Best regards
Axel

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 5 attachments (including images) can be used with a maximum of 19.1 MiB each and 23.8 MiB total.