Jump to content
Sign in to follow this  
Guest shawnpwilson

events

Recommended Posts

Guest shawnpwilson

Hello.I'm trying to learn the jist of events in simconnect and am confused a bit with what I expect the code to do versus what it actually does. I've put this together (mostly a copy) from the managed code samples for c#. (code is below)The confusion I'm seeing is:1. when I click on the pitot switch in FSX, PITOT_HEAT_TOGGLE event fires fine. What I don't see is the PITOT_HEAT_ON event firing when the heat acutally gets turned on.2. I have a command button that turns on the pitot heat ok but fires neither event. I know the pitot heat gets turned on because I see the switch to the ON position in FS. I expected to see 2 events fired.Can someone who is more experienced please show me why I don't see what I expect to see. I'm sure obviously I'm doing something incorrect and am not seeing it.Thank you to anyone who can help with this.using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Timers;// Add these two statements to all SimConnect clientsusing Microsoft.FlightSimulator.SimConnect;using System.Runtime.InteropServices;namespace Managed_Data_Request{ public partial class Form1 : Form { const int WM_USER_SIMCONNECT = 0x0402; SimConnect simconnect = null; enum EVENTS { PITOT_TOGGLE, PITOT_HEAT_ON, } enum NOTIFICATION_GROUPS { GROUP0, } public Form1() { InitializeComponent(); setButtons(true, false, false); } protected override void DefWndProc(ref Message m) { if (m.Msg == WM_USER_SIMCONNECT) { if (simconnect != null) { simconnect.ReceiveMessage(); } } else { base.DefWndProc(ref m); } } private void setButtons(bool bConnect, bool bGet, bool bDisconnect) { buttonConnect.Enabled = bConnect; buttonRequestData.Enabled = bGet; buttonDisconnect.Enabled = bDisconnect; } private void closeConnection() { if (simconnect != null) { simconnect.Dispose(); simconnect = null; displayText("Connection closed"); } } private void initDataRequest() { try { simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException); } catch (COMException ex) { displayText(ex.Message); } } private void initClientEvent() { try { // listen to connect and quit msgs simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen); simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit); // listen to exceptions simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException); // listen to events simconnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(simconnect_OnRecvEvent); // subscribe to pitot heat switch toggle simconnect.MapClientEventToSimEvent(EVENTS.PITOT_TOGGLE, "PITOT_HEAT_TOGGLE"); simconnect.AddClientEventToNotificationGroup(NOTIFICATION_GROUPS.GROUP0, EVENTS.PITOT_TOGGLE, false); simconnect.MapClientEventToSimEvent(EVENTS.PITOT_HEAT_ON, "PITOT_HEAT_ON"); simconnect.AddClientEventToNotificationGroup(NOTIFICATION_GROUPS.GROUP0, EVENTS.PITOT_HEAT_ON, false); simconnect.SetNotificationGroupPriority(NOTIFICATION_GROUPS.GROUP0, SimConnect.SIMCONNECT_GROUP_PRIORITY_HIGHEST); } catch (COMException ex) { displayText(ex.Message); } } void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { displayText("Connected to FSX"); } void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data) { displayText("FSX has exited"); closeConnection(); } void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) { displayText("Exception received: " + data.dwException); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { closeConnection(); } void simconnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT recEvent) { //displayText(recEvent.dwData.ToString()); switch (recEvent.uEventID) { case (uint)EVENTS.PITOT_HEAT_ON: displayText("PITOT ON"); break; case (uint)EVENTS.PITOT_TOGGLE: displayText("PITOT switched"); break; default: displayText("Unknown Event"); break; } } private void buttonConnect_Click(object sender, EventArgs e) { if (simconnect == null) { try { simconnect = new SimConnect("Managed Data Request", this.Handle, WM_USER_SIMCONNECT, null, 0); setButtons(false, true, true); initDataRequest(); initClientEvent(); } catch (COMException ex) { displayText("Unable to connect to FSX"); } } else { displayText("Error - try again"); closeConnection(); setButtons(true, false, false); } } private void buttonDisconnect_Click(object sender, EventArgs e) { closeConnection(); setButtons(true, false, false); } int response = 1; string output = "nnnnnnnnnn"; void displayText(string s) { output = output.Substring(output.IndexOf("n") + 1); output += "n" + response++ + ": " + s; richResponse.Text = output; } private void cmdTogglePitot_Click(object sender, EventArgs e) { simconnect.TransmitClientEvent(1, EVENTS.PITOT_HEAT_ON, 0, NOTIFICATION_GROUPS.GROUP0, 0); } }}

Share this post


Link to post
Share on other sites

> when I click on the pitot switch in FSX, PITOT_HEAT_TOGGLE> event fires fine. What I don't see is the PITOT_HEAT_ON event> firing when the heat acutally gets turned on. The TOGGLE control turns the heat on or off. The "ON" and "OFF" controls are simply alternatives. One even doesn't cause another. The same applies to GEAR (toggle, up or down) and so on. There are, annoyingly for cockpit builders, some toggle controls for which there are no on/off equivalent.Incidentally, I call events "controls" because they are tabulated in the CONTROLS.DLL and are the values assigned to buttons and keys via FS's Assignments dialogue. They were called "Key Events" in the Gauges SDK, and now Sim Events in SimConnect.> I have a command button that turns on the pitot heat ok but> fires neither event. I know the pitot heat gets turned on> because I see the switch to the ON position in FS. I expected> to see 2 events fired. No, you'd only ever likely see one, at most -- the one to operate the heat. But whether you see an event will probably depend on relative priorities. If you install FSUIPC4 and turn on Event logging (you don't need a registered copy to use the Logging), then operate your button, the log wil show you what control was used.However, I fear your method for determining things happening inside FSX is doomed to fail for many things which can get switched without any controls or events being sent to the Sim Engine. Even external applications can, in many cases, write direct to Sim Vars instead. You should be using Events (Controls) to make things happen, but reading Sim Vars to see the state. In the case of the Pitot Heat, read the Sim Var of that name.RegardsPeteP.S. Sorry, I am away now till the 24th.


Win10: 22H2 19045.2728
CPU: 9900KS at 5.5GHz
Memory: 32Gb at 3800 MHz.
GPU:  RTX 24Gb Titan
2 x 2160p projectors at 25Hz onto 200 FOV curved screen

Share this post


Link to post
Share on other sites
Guest AVN8tr

Actually I think if he went inside his FSX client and assigned a key to that Pitot Heat On event then it might actually be triggered. :)Also remember events relate to any .DLL that is actually a COM object. So for all control events they go through the CONTROL.DLLThink of it as Microsoft's perfered way of accessing objects that provide services.The traditional object model would implement methods to be performed on the object or by the object once you create it. Well with COM Objects you don't actually create the object its created when the program starts and what you do create is an interface to the COM object instead. This way methods are triggered when events actually happen otherwise the COM or Control.DLL stays idle consuming very little resources :) So with the case of the CONTROL.DLL your client program knows about it through the SimConnect.DLL, but Flight SImulator X is the one that actually creates the particular COM object long before even you client can use it.Anyways theres some incite.. hopefully it helps others as well. But hopefully you see how this is useful when you want any number of processes to be able to acess the Control.dll not just your client ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Tom Allensworth,
    Founder of AVSIM Online


  • Flight Simulation's Premier Resource!

    AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!

    Click here for more information and to see all donations year to date.
×
×
  • Create New...