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

MSFS MSFS2020 SimConnect in Winforms && Wpf WORKING, but some help needed. CODE EXAMPLES INCLUDED

Messages
31
Country
unitedstates
I have a partially working CSharp wrapper that seems to be communicating with MSFS2020 via SimConnect failry well. I am however, running into some issues with a few of the SimVars and was hoping someone could help me out. Specifically, I am unable to get the following values from SimConnect that I would like to:

[The current % setting of the throttle handle]
I cannot figure out what is the proper value for the SimVar name. I am currently using "GENERAL ENG THROTTLE LEVER POSITION:index", but that doesn't appear to be working. Is the ":index" meant to represent something like ":(index of the engine)"? So, in a twin engine plane I would do "GENERAL ENG THROTTLE LEVER POSITION:1" for the second engine? I'm able to parse the "FLAPS HANDLE PERCENT" SimVar, so if I can just get the throttle data to come across, I should be able to handle it.

[ATC information, such as ATC ID, ATC TYPE etc]
I have had no luck capturing ANY string information. I've tried every variation of STRING8, STRING32, STRING54, STRING128, STRING256, STRINGV to get the proper data on string types. I am also not sure what should be passed as the unit when getting strings. I've seen "null" passed elsewhere for unit type on string values, but still have had no success. For strings, I need help ensuring that I'm using the proper SimVar names, as well as DataTypes, and also that my data struct has been properly defined.

I have built a polling request system like you see present in the SDK samples. I am going to include some code snippets so you may be able to help me diagnose the issue. Long story short, I need to make sure that I'm registering my calls via SimConnect.AddToDataDefinition() correctly and that my struct has the fields defined properly, because that is likely where the issue is.


Here is my return data STRUCT that should contain the requested data from SimConnect:


C#:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ATCInfo
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
    public string Type_Name;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
    public string Tail_Number;
}

Here are the calls to register my request:

C#:
                                  *this is correct       *??       *??      *??
simConnector.AddToDataDefinition(DEFINITIONS.ATCInfo, "ATC TYPE", null, SIMCONNECT_DATATYPE.STRING64, 0, SimConnect.SIMCONNECT_UNUSED);
simConnector.AddToDataDefinition(DEFINITIONS.ATCInfo, "ATC FLIGHT NUMBER", null, SIMCONNECT_DATATYPE.STRING8, 0, SimConnect.SIMCONNECT_UNUSED);
simConnector.RegisterDataDefineStruct<ATCInfo>(DEFINITIONS.ATCInfo);

I am getting exceptions number 7 & 3 from the SimConnect library when trying to get these string values.

Can anyone provide some direction?


P.S. I'll be happy to post my working WinForms & WPF implementation if anyone would like to see it.
 
Last edited:
Messages
31
Country
unitedstates
@David McClanahan it appears that you are working with the managed library as well. I haven't tried setting any variables yet, but are you able to read string type variables from SimConnect? Are you GETTING the throttle variable? If so, are you using the event system, or polling for it?
 
Messages
2,046
Country
us-ohio
The index is engine number... and there is no engine number 0. It is a 1-based index for all of the SimConnect variables that have an index value.

At what point in your code are you getting the exceptions? Also, exceptions usually have a name associated with them, not strictly a number. The only thing I see different between your code and example(s) in the SDK are that they have (uint) in front of the DEFINITIONS.ATCInfo in the AddToDataDefinition statement.
 
Messages
116
Country
unitedstates
I have a partially working CSharp wrapper that seems to be communicating with MSFS2020 via SimConnect failry well.

P.S. I'll be happy to post my working WinForms & WPF implementation if anyone would like to see it.

I'd really appreciate seeing the code, I have the beginning of an C# app working to collect in one display somethings I find useful, throttle, ground speed (for taxi), parking brake state, AutoThrust state etc. I intended to add a governor (speed control) for taxiing .. and co-pilot call outs for speed and V1 and any reminders I want (maybe gear up, flaps up .. etc.) Just doing it for myself to learn but I haven't set any states yet
 
Messages
31
Country
unitedstates
@David McClanahan Here is the code along with a compiled library. It is a very simple wrapper. Here is simple Winforms implementation. Just need to pass the window handle on constructor and subscribe to the AircraftStatusUpdated event. Calling Start() method begins the polling. You can set a couple parameters. SetRefreshInterval(int miliseconds) will set the update period. SetRequestVerbosity(typeof(RequestVerbosity)) will change the quantity of data returned in the event, but it defaults to the highest level on initialization.

C#:
using MSFSClient;

client = new MSFSClient.MSFSClient(this.Handle);
client.AircraftStatusUpdated += Client_AircraftStatusUpdated;
client.Start();


private void Client_AircraftStatusUpdated(AircraftStatus status)
{
    UpdateAircraftStatus(status); //Do whatever you want with the AircraftStatus object here. It contains the real-time info from MSFS
}

It is also imperative that you have the following code in your Form (this will capture the windows messages for SimConnect):

C#:
protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
     if (m.Msg == 0x0402)
     {
         client.ReceiveMessage();
     }
}
 

Attachments

  • MSFSClient.zip
    99.7 KB · Views: 372
Last edited:
Messages
31
Country
unitedstates
As an update, I am no longer getting the exceptions and have registered a request properly for the string variables. It is returning data now, but when I parse it I am ending up with sometimes readable characters and sometimes garbage. I'm fairly certain it has to do with the defined length of the string type on my data STRUCT.
 
Messages
31
Country
unitedstates
It appears that I have the strings working relatively correctly now. The only one that is still a little suspect is the variable length string. It appears that not specifying a constant length string field on the DATA struct causes some issue. I'm using a length of 64 to get the "TITLE" SimVar and I have yet to see any negative side effects...

P.S. I should also add that I've put the variable length string as the last field in my struct. That way a pre-defined string length should be truncated at worst (so far haven't run into any index out of bounds issues).
 
Last edited:
Messages
31
Country
unitedstates
Here is a snapshot of the data I'm able to get updates on at the moment. This is not exhaustive by any stretch of the imagination. Just a sample of some things I wanted to see.
workingSimVars.PNG
 
Messages
116
Country
unitedstates
do you have some insight into the Waypoint array? I can see that they are indexed but I can't get any descriptive text about them.

Is there a variable or function to give me current distance from the destination?
 
Last edited:
Messages
31
Country
unitedstates
I'm happy to announce a little mini-component to satiate your flight sim appetites. Check out my sample MSFS 2020 Mission addon here:

 
Messages
6
Country
france
@jpdise I have the same issue with the string. I would like to get the title, how finally did you manage to do that ? I download your example but the atc string part seems commented. Thanks
 
Messages
31
Country
unitedstates
Hi @maugiroe, here is a sample request for a string. The IMPORTANT part is that you must define your string length properly in your data struct. My newest iteration of the SimConnect Wrapper has done away with multi-variable data structures in favor or single entity requests. This way I don't run into string length errors when parsing the returned data structures. (If one of the casts fails in the Data Struct, then the rest of the struct doesn't get parsed). So, here is an example of how I register a string call for the "TITLE" SimVar:

C#:
 public void RegisterRequest(DataRequest request)
        {
            if(!Requests.Any((item) => item.RequestName == request.RequestName))
            {
                try
                {
                    request.DefinitionID = (RequestDefinition)GetRequestID();
                    _connector.AddToDataDefinition(request.DefinitionID, request.RequestName, null, request.DataType, 0f, (uint)0);
                    
                    switch (request.DataType)
                    {
                        case SIMCONNECT_DATATYPE.STRING64:
                            _connector.RegisterDataDefineStruct<StringVal64>(request.DefinitionID); //To properly parse the string it is IMPERATIVE that you use the proper struct type as your generic here
                            break;

                        default:
                            _connector.RegisterDataDefineStruct<double>(request.DefinitionID);
                            break;
                    }
                    Requests.Add(request);
                }
                catch(Exception excep)
                {
                    Console.WriteLine(excep.Message);
                }
            }
        }

public static DataRequest AircraftTitle
        {
            get
            {
                return new DataRequest()
                {
                    RequestName = SimvarNames.AircraftModelName, //This is a static string equal to "TITLE"
                    DataType = Microsoft.FlightSimulator.SimConnect.SIMCONNECT_DATATYPE.STRING64,
                    UnitLabel = null,
                    Value = string.Empty
                };
            }
        }

struct StringVal64 {[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] public string Value; }

The BIG change to my approach was allowing my RequestHandler to programmatically assign and manage the RequestDefinitionID as opposed to statically defining them.
Feel free to reach out with any questions.
-jpdise
 
Last edited:
Messages
6
Country
france
@jpdise Hi
I wonder if you found a simvar who tell you on witch icao the plane is ? Currently i calculate the distance between the plane and the lat lon airport. I’m pretty sure there is another way.
 
Top