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

FSX Proper way to define SimConnect data request Structs

Messages
639
Country
panama
When one creates structs for data requests to SimConnect one most create entries in DEFINITIONS and DATA_REQUESTS enumerations (I am using managed code, C#). In managed code one must also decorate the struct with InteropServices.StructLayout and some parameters to indicate it is sequential.

In registering the request one must also use AddToDataDefinition() prior to RegisterDefinition(). I know that the order of the data definition additions must match the order of the fields defined in the struct.

However, it is unclear to me how the definition of the member variables (private, internal, public, protected) actually affect whether they are taken into consideration or not. I also don't know if there is a way to skip some fields or properties, for example when one has a computed property that converts a raw value to something else.

Code:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
struct TestDR
{
    internal int GaugeStatus;      // THIS member should correspond to the AddToDataDefinition item 1
    internal GaugeStateMode TestGaugeStatus
    {
        get { return (GaugeStateMode)GaugeStatus; }
        set { GaugeStatus = (int)value; }
    }

    private SimConnect simconnect;


    public TestDR(SimConnect simConnect) : this()
    {
        this.simconnect = simConnect;
        this.TestGaugeStatus = GaugeStateMode.OK;
    }

    public void Register()
    {
        this.simconnect.AddToDataDefinition(DEFINITIONS.TestDR, "PARTIAL PANEL HEADING:0", SimUnits.ENUM, SIMCONNECT_DATATYPE.INT32, 0.0f, SimConnect.SIMCONNECT_UNUSED);

        this.simconnect.RegisterDataDefineStruct<TestDR>(DEFINITIONS.TestDR);
    }

    public void RequestData()
    {
        this.simconnect.RequestDataOnSimObject(DATA_REQUESTS.TestRequest, DEFINITIONS.TestDR, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.ONCE, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 1, 1);
    }

    public void Receive(TestDR receivedData)
    {
        this.TestGaugeStatus = receivedData.TestGaugeStatus;
    }

    public void SendData()
    {
        this.simconnect.SetDataOnSimObject(DEFINITIONS.TestDR, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT, this);
    }
}

In the above struct, only the first member variable should be used, that is GaugeStatus declared as int which is an Int32 in SimConnect. I don't know if putting the SimConnect private instance before it will skip it from the definition or will use that instead of my intended member variable.

Also, with the above definition the value which is an enum of 0..2 always gives me a SIMCONNECT_EXCEPTION_INVALID_DATA_SIZE.
 
Back
Top