The MobiFlight Module and Custom SimVars

I needed to be able to access some local variables that are not exposed by SimConnect for a skunkworks project that I’m working on. After a lot of research I finally managed to get something working.

I already use the excellent MobiFlight WASM module to be able to send non-standard events and when I realised that the code had been open sourced I decided to add this functionality to that module.

This is the result of my efforts. It is based on a method used in this repository.

You can access the fork here MobiFlightWASM-Elephant42. I have also included some basic C++, C# and VB client code to exercise the new functions.

I have sent a pull request to the @MobiFlight author so if he likes what I’ve done it might make it’s way to the main branch.

4 Likes

Thank you for this. Can you briefly explain how to use your files.

Does your work replace the standard Mobiflight ?

How do I install it ?

It is an extension of the current MobiFlight module so yes it does replace it. There is a zip file in the root of the repository that contains the new WASM module and compiled binaries of the sample clients.

However this is not really meant for end-users. It’s more for developers and/or cockpit builders who know their way around code and SimConnect. It doesn’t provide anything usefully new unless the SimConnect clients are coded to use the new functions.

Thanks. Well I hope the added functionality finds its way into the main branch.

This looks very promising! Thanks a lot!

I’m the developer of the open-source MSFS Mobile Companion App and I’m already using the Mobiflight WASM module to change L-Vars. Do I understand it correctly, that with your module it’s now possible to read L-vars? And do you know if it works with the Python SimConnect wrapper (the regular Mobiflight WASM module works just fine)?

Thanks!

Yes it does read L-Vars. It should work with any sim connect client as long as the appropriate code is added to the client - see the included sample clients for details - sorry, no python sample but it should be fairly easy to code in the necessary logic - it’s only a few lines to set up a ClientData area and query it.

The current MobiFlight functionality is exactly the same and the new module will continue to work anywhere the old one will, but the new read ability requires additions to the SimConnect client.

1 Like

This is perfect news! I’ve tried to make it work with the Simconnect Python wrapper, but I couldn’t solve it. I kept getting the error massage that the var I’m trying to read can’t be found. I’ve tested this on the existing DA62 L-vars and added them into the list of known vars (the python wrapper uses a list for vars and events), but still nothing.

Unfortunately, I don’t know C/++/# and I couldn’t figure out what to do to make it work. The procedure with Mobiflight events is simple in the Python Simconnect wrapper. All you have to do is add the Mobiflight.EVENTNAME into the list of known events and it works. I’d be very grateful if you could give me additional tips on how to make your Mobiflight module work with the Python wrapper. The community would also appreciate it since quite a lot of simmers use it for their own projects. Thanks again!

The new module is forked off the latest code committed to the parent MobiFlight GitHub repository. The format of events.txt has changed quite a bit in that version. You need to specify the name of the variable/event and also the internal Sim name separated by a # character. If you have a look at the events.txt and wasm_vars.txt files included in the module you will see what is required. The version of the module currently distributed with MobiFlight is still an earlier one and uses the much simplified original format for events.txt.

L Vars can not be read via SimConnect via the same method used to read normal A Vars, hence the need for this additional WASM code. This means that any SimConnect client, including the python wrapper, will not be able to read L Vars until the new method exposed by this WASM code is added to them.

Somethings to note about L Vars -
They are not allways visble, some are only visible/valid while the particular aircraft they belong to is loaded. If a var is not visible in the LocalVariables tab in the ModelBehaviourDebug window, it will not return any valid data via this method (it will always return 0).

So to cut a long story short, you can’t use the Python wrapper to read L vars using this WASM module unless you can modify it yourself, or persuade the author to do it - it’s only a few lines of code and it should be pretty obvious by examining the sample clients that I’ve provided.

Thanks for the clarification. I’m already using the latest WASM module committed to the repository (the one which uses # separated events) so I can create my own events. I check the commands via the ModelBehaviourDebug and write them down accordingly into the events.txt file. The WASM module is extremely useful for my app because, as you’ve mentioned, it opens up the possibility to manipulate L-vars that wouldn’t be possible with a regular Simconnect client.

WASM events work great. All I need to do is add the name of the event into a list of existing events in the python wrapper (which includes the common K-events). But there is obviously more to reading L-vars than simply manipulating them via events.

I’m willing to modify the python wrapper myself and share it with the community, but I honestly don’t know where to begin. If it’s just a few lines of code, I’d appreciate a little guidance. I assume it has to do something with the RegisterVars method (looking at the Form1.cs file), but I’m not sure.

Ok then here we go…

The way the WASM implements reading L Vars is to read in a list of them from the wasm_vars.txt file and build a 0 based array with the exact name of the L Var (the second field in the # separated value). It then registers a client data area big enough to hold all the read in vars and registers each one with SimConnect in the client area at its’ indexed offset. It then registers to listen for a special Event which is named exactly “MobiFlight.CUST_VAR”.

When the WASM receives that event it examines the evData parameter and converts it into an index into the array of SimVars. It then retrieves the actual var name from the array and feeds it to the Sim to get the current value. It then uses SimConnect to store the value in the CLientData area at the correct offset, ready to be queried by the calling client.

So any SimConnect Client that wants to query L Vars must do four things.

  1. Build a list of L Vars to query that exactly matches the same list loaded by the WASM module - the easiest way to do this is to parse the exact same file that the module does and this is what the sample clients do.
  2. Map an ID to the ClientData area with simconnect.MapClientDataNameToID using exactly the same name as the WASM module used to create it, then map each var in the list with simconnect.MapClientDataNameToID.
  3. Register a handler for the simconnect.OnRecvClientData callback.
  4. Register an extra event with simconnect.MapClientEventToSimEvent using exactly the same event name as registered in the WASM module viz. “MobiFlight.CUST_VAR”.

Once this has been done the client requests data for an L Var by first sending the “MobiFlight.CUST_VAR” event with the evData parameter set to the index of the var that needs to be read - this is the index that was loaded in as described above. Immediately after sending the event a call to simconnect.RequestClientData is made with the DefineID paramater set to the exact same index as was sent with the event. The value will then be received in the OnRecvClientData callback with the data.dwDefineID field holding the index as sent in the request so that the value can be correctly matched with the simvar.

1 Like

Thanks a lot for the detailed description! I’ll try to implement it into the Python wrapper and will post a solution here once I have it.

Hi guys, I have released a new version of the MobiFlight WASM Module. We are now able to access LVars.

In contrast to how it was explained ealier by @E1ephant42 , The MobiFlight WASM Module expects that the client registers dynamically the Variables it wants to access. Anything is possible, LVARS, Airplane Variables and even expressions (anything that can be evaluated in execute_calculator_code). This is really powerful.

Take a look at the code here: Bitbucket if you like.

MobiFlight Connector already interfaces with these new possibilities and allows for reading these sim variables and sending the values to your Displays, like LEDs, 7 Segment Displays, Steppers, Servos, etc.

2 Likes