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

P3D v3 Retrieving Correct Altitude Value - C#

Messages
6
Country
unitedstates
Hey everyone. I've been building a large simconnect application, and this is my first time working with prepar3d so it's been quite a learning experience.

The issue that I'm currently facing is that I can't seem to find the correct way to grab the altitude in feet when I pull position data from the sim. It appears that the title, latitude, and longitude return back to the simconnect correctly, but the altitude is always a small decimal value with an exponent.

If there's anything that immediately appears wrong, please let me know. I've looked through hundreds of forum posts online already.

In my InitDataRequest method:

C#:
                simConnect.AddToDataDefinition(DEFINITIONS.PositionStruct, "title", null, SIMCONNECT_DATATYPE.STRING256,
                    0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DEFINITIONS.PositionStruct, "Plane Latitude", "degrees",
                    SIMCONNECT_DATATYPE.FLOAT32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DEFINITIONS.PositionStruct, "Plane Longitude", "degrees",
                    SIMCONNECT_DATATYPE.FLOAT32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DEFINITIONS.PositionStruct, "Plane Altitude", "feet",
                    SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);

                simConnect.RegisterDataDefineStruct<PositionStruct>(DEFINITIONS.PositionStruct);

A defined struct to map the data
C#:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
        struct PositionStruct
        {
            // fixed size string
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string title;
            public double latitude;
            public double longitude;
            public double altitude;
        };

Requesting the data
C#:
            simConnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.PositionStruct, 0,
                SIMCONNECT_SIMOBJECT_TYPE.USER);

Handling the data request

C#:
case DATA_REQUESTS.REQUEST_1:
                    PositionStruct s1 = (PositionStruct) data.dwData[0];

                    titleLabel.Text = "Title: " + s1.title;
                    latitudeLabel.Text = "Latitude: " + s1.latitude;
                    longitudeLabel.Text = "Longitude: " + s1.longitude;
                    currentAltitude = (int) (s1.altitude);
                    altitudeTextBox.Text = $"{s1.altitude:N0}";
                    break;

From what I've seen online, there's only been 1 other person who's come across this issue while attempting to write the c# simconnect application. I'm not sure what I can do to fix this, so I'm reaching out to potentially any more experienced developers with prepar3d. Would it be the data type I'm requesting the data into that I have to change? Or do I have to process this value somehow? From what I've seen online, and from the way I've requested other integer values onto my application, it shouldn't take more than just requesting the data and mapping it to the value in the structure. Altitude has been the only value causing me issues, and unforurtunately it's necessary for me to complete this application that I have that value ready.

Any tips or feedback would be greatly appreciated! Thanks.
 
just guessing as your example code looks like the P3D example code.

You have a struct with 3 doubles but you are using FLOAT32 for two of them and FLOAT64 for the last in the addtodatadefition function. I don't think the struct is aligning properly. So change the FLOAT32's to FLOAT64s
 
C#:
            this.SimConnect.AddToDataDefinition(DataIdentifier.RecieveAIObjectQuery,
                                                "PLANE LATITUDE",       // Simulation Variable
                                                "degrees",                           // Units - for strings put 'null'
                                                SIMCONNECT_DATATYPE.FLOAT64,  // Data type
                                                0.0f,
                                                SimConnect.SIMCONNECT_UNUSED);

            this.SimConnect.AddToDataDefinition(DataIdentifier.RecieveAIObjectQuery,
                                                "PLANE LONGITUDE",       // Simulation Variable
                                                "degrees",                           // Units - for strings put 'null'
                                                SIMCONNECT_DATATYPE.FLOAT64,  // Data type
                                                0.0f,
                                                SimConnect.SIMCONNECT_UNUSED);
            this.SimConnect.AddToDataDefinition(DataIdentifier.RecieveAIObjectQuery,
                                                "PLANE ALTITUDE",       // Simulation Variable
                                                "feet",                           // Units - for strings put 'null'
                                                SIMCONNECT_DATATYPE.INT32,  // Data type
                                                0.0f,
                                                SimConnect.SIMCONNECT_UNUSED);

C#:
        public int Altitude;

        public double Latitude;

        public double Longitude;


some parts of the code I use maybe it will help you
 
just guessing as your example code looks like the P3D example code.

You have a struct with 3 doubles but you are using FLOAT32 for two of them and FLOAT64 for the last in the addtodatadefition function. I don't think the struct is aligning properly. So change the FLOAT32's to FLOAT64s

This worked! Appreciate it man.
 
Back
Top