void connect() { const int WM_USER_SIMCONNECT = 0x0402; connection = new SimConnect("simmaps", IntPtr.Zero, WM_USER_SIMCONNECT, null, 0); connection.AddToDataDefinition(DEFINITIONS.SimData, "Plane Altitude", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); connection.AddToDataDefinition(DEFINITIONS.SimData, "Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); connection.AddToDataDefinition(DEFINITIONS.SimData, "Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); connection.AddToDataDefinition(DEFINITIONS.SimData, "Plane Heading Degrees True", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); connection.RegisterDataDefineStruct <SimData>(DEFINITIONS.SimData); connection.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(simconnect_OnRecvSimobjectData); connection.OnRecvEvent += new SimConnect.RecvEventEventHandler(simconnect_OnRecvEvent); connection.SubscribeToSystemEvent(DEFINITIONS.SimStart, "SimStart"); requestData(); Terminal.WriteLine("Connected to Flight Simulator", ConsoleColor.Green); while (true) { connection.ReceiveMessage(); Thread.Sleep(10); //not a bottleneck } }
private void Tick(object sender, ElapsedEventArgs e) { if (ConnectionStatus == SimConnectionStatus.Tentative || IsConnected) { simConnection.ReceiveMessage(); } }
public IntPtr HandleSimConnectEvents(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam, ref bool isHandled) { if (!isSetUp) { return(IntPtr.Zero); } isHandled = false; if (message == WM_USER_SIMCONNECT) // for us { if (simConnect != null) // process it { try { simConnect.ReceiveMessage(); isHandled = true; Console.ForegroundColor = ConsoleColor.Green; Console.Write("\nSimConnect: "); Console.ResetColor(); Console.Write($"SimConnect.ReceiveMessage : received & handled \n"); } catch (COMException x) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("\nSimConnect: "); Console.ResetColor(); Console.Write($"SimConnect.ReceiveMessage : EXCEPTION : {x} \n"); } } } return(IntPtr.Zero); }
// events raised from options window or 'dummy' P3D event handler window public void receiveMessage() { if (p3d != null) { p3d.ReceiveMessage(); } }
public void ReadMessages() { if (null != simConnect) { simConnect.ReceiveMessage(); } }
/// <summary> /// Creates the SimConnect connection with Prepar3D and /// continuously requests to receive messages. /// </summary> private void StartSimConnectThread() { Thread.Sleep(2500); if (PollForConnection()) { if (SimConnect != null) { while (!mQuit) { try { SimConnect.ReceiveMessage(); } catch { } Thread.Sleep(500); } if (mQuit) { SimConnect.Dispose(); SimConnect = null; } } } else { System.Console.WriteLine("ERROR: SimConnect failed to connect (timed out)."); } }
public bool Connect() { Disconnect(); _simVarRequests = new List <SimVarRequest>(); try { _simConnect = new SimConnect("Simconnect - Var Test", new IntPtr(0), WM_USER_SIMCONNECT, null, 0); } catch (Exception ex) { Console.WriteLine("An error has occurred while trying to connect to SimConnect. Error: " + ex.Message); return(false); } _simConnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(SimConnect_OnRecvOpen); _simConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit); _simConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvSimobjectDataBytype); var startTime = DateTime.Now; while (!_isConnected && (DateTime.Now - startTime).TotalSeconds <= 10) { System.Threading.Thread.Sleep(100); _simConnect.ReceiveMessage(); } return(_isConnected); }
// Simconnect client will send a win32 message when there is // a packet to process. ReceiveMessage must be called to // trigger the events. This model keeps simconnect processing on the main thread. protected override void DefWndProc(ref Message m) { if (m.Msg == WM_USER_SIMCONNECT) { if (simconnect != null) { try { simconnect.ReceiveMessage(); } catch (COMException ce) { displayText("COM EXCEPTION"); // if this happens, try to re-connect buttonDisconnect_Click(new object(), new EventArgs()); Thread.Sleep(10); buttonConnect_Click(new object(), new EventArgs()); } } } else { base.DefWndProc(ref m); } }
public void ListenToMessage() { if (_mySimconnect == null) { return; } _mySimconnect.ReceiveMessage(); }
private IntPtr WndProc(IntPtr hWnd, int iMsg, IntPtr hWParam, IntPtr hLParam, ref bool bHandled) { if (iMsg == WM_USER_SIMCONNECT) { simconnect?.ReceiveMessage(); } return(IntPtr.Zero); }
public IntPtr WndProc(IntPtr hWnd, int iMsg, IntPtr hWParam, IntPtr hLParam, ref bool bHandled) { if (iMsg == 0x0402) { _simConnect?.ReceiveMessage(); } return(IntPtr.Zero); }
private void RecieveSimConnectMessage(object sender, EventArgs e) { try { simConnect?.ReceiveMessage(); } catch (COMException) { Disconnect("Lost Connection to SimConnect", true); } }
protected override void DefWndProc(ref Message m) { if (m.Msg != WM_USER_SIMCONNECT) { base.DefWndProc(ref m); } else if (connection != null) { connection.ReceiveMessage(); } }
public void ReceiveSimConnectMessage() { try { SimConnect?.ReceiveMessage(); } catch (Exception e) { Debug.Write(e.Message); } }
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { handled = false; // if message is coming from simconnect and the connection is not null; // continue and receive message if (msg == 0x402 && _simClient != null) { _simClient.ReceiveMessage(); handled = true; } return((IntPtr)0); }
public Task WaitForMessage(CancellationToken cancellationToken) { while (_connected && !cancellationToken.IsCancellationRequested) { if (_scReady.WaitOne(TimeSpan.FromSeconds(5))) { _simConnect?.ReceiveMessage(); } } return(Task.CompletedTask); }
public bool HandleWindowMessage(ref Message m) { if (m.Msg == SimConnectFlightDataConnector.WM_USER_SIMCONNECT) { if (simConnect != null) { simConnect.ReceiveMessage(); } return(true); } return(false); }
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_USER_SIMCONNECT) { if (simconnect != null) { simconnect.ReceiveMessage(); handled = true; } } return(IntPtr.Zero); }
private void MessageReceived(object sender, Message msg) { if (msg.Msg == WM_USER_SIMCONNECT && simConnect != null) { try { simConnect.ReceiveMessage(); } catch// (Exception ex) { // Seen to happen if FS is shutting down } } }
protected override void DefWndProc(ref Message m) { if (m.Msg == WM_USER_SIMCONNECT) { if (simconnect != null) { simconnect.ReceiveMessage(); } } else { base.DefWndProc(ref m); } }
public void HandleWndProc(int msg) { if (msg == WM_USER_SIMCONNECT && _simConnect != null) { try { _simConnect.ReceiveMessage(); } catch (Exception ex) { OnError?.Invoke(this, ex); } } }
public Task WaitForMessage(CancellationToken cancellationToken) { while (_connected && !cancellationToken.IsCancellationRequested) { if (_scReady.WaitOne(TimeSpan.FromSeconds(5))) { // TODO: Exception on quit _simConnect?.ReceiveMessage(); //simconnect.RequestDataOnSimObjectType(Events.Test, Group.Test, 0, SIMCONNECT_SIMOBJECT_TYPE.AIRCRAFT); } } return(Task.CompletedTask); }
public void GetFSMessages() { if (simconnect != null) { try { simconnect.ReceiveMessage(); } catch (COMException ex) { // Exception means connection is lost, so the client side has to close DisconnectSim(); OnError(this, ex); } } }
private void scMessageProcess() { bool ok = false; while (!ok) { try { sc.ReceiveMessage(); ok = true; } catch (Exception) { ok = false; } } }
private IntPtr HandleSimConnectEvents(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam, ref bool isHandled) { isHandled = false; switch (message) { case WM_USER_SIMCONNECT: { if (sc != null) { sc.ReceiveMessage(); isHandled = true; } } break; } return IntPtr.Zero; }
protected override void DefWndProc(ref Message m) { if (simconnect != null) { try { simconnect.ReceiveMessage(); } catch (COMException ex) { throw ex; } } else { base.DefWndProc(ref m); } }
// override DefWndProc so we can grab the SimConnect messages protected override void DefWndProc(ref Message m) { if (m.Msg == WM_USER_SIMCONNECT) { if (simconnect != null) { try // the Sim Connection may no longer be valid { simconnect.ReceiveMessage(); } catch // do nothing here we will not be receiving any more simconnect message if the Prepar3D runtime has closed down {} } } else { base.DefWndProc(ref m); } }
public void Poll() { if (api != null) { // We are connected, attempt fetching a message. try { api.ReceiveMessage(); } catch (COMException) { // COMException on ReceiveMessage () means connection was lost. // Clear connection object and let next Tick() will attempt reconnection. logger.LogError("Connection error, will try to reconnect."); Stop(); } } else { // We are not connected, attempt connection. Connect(); } }
/// <summary> /// Every Windows Message is captured here, we check for SimConnect messages and process them, else we ignore it /// </summary> /// <param name="sender">Windows Object generating the message</param> /// <param name="msg">Message from sender</param> private static void MessageReceived(object sender, Message msg) { //WriteLog("Start MessageReceived(object, Message)"); if (msg.Msg == WM_USER_SIMCONNECT && simConnect != null) { try { // SimConnect has something to tell us - ask it to raise the relevant event if (simConnect != null) { simConnect.ReceiveMessage(); } } catch// (Exception ex) { // Seems to happen if FS is shutting down or when we disconnect } } //WriteLog("End MessageReceived(object, Message)"); }
protected override void DefWndProc(ref Message m) { if (m.Msg == WM_USER_SIMCONNECT) { if (simconnect != null) { try { simconnect.ReceiveMessage(); } catch (Exception e) { WriteToLog(e.Message); } } } else { base.DefWndProc(ref m); } }