public static void CloseConnection() { if (simConnect != null) { // Dispose serves the same purpose as SimConnect_Close() simConnect.Dispose(); simConnect = null; //displayText("Connection closed"); } }
void sc_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) { Console.WriteLine("sc_OnRecvException"); }
private void DataReceived(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) { switch ((Requests)data.dwRequestID) { case Requests.PositionInformationRequest: Data.PositionInformation = (PositionInformation)data.dwData[0]; break; default: Logger.Log("Unknown request ID: " + data.dwRequestID); break; } }
public override void Disconnect() { if (!Connected) return; if (sim != null) { sim.Dispose(); sim = null; } Connected = false; }
public static SimConnect Initialise(IntPtr handle) { try { simConnect = new SimConnect("Motion Simulator", handle, WM_USER_SIMCONNECT, null, 0); SetupEvents(); return simConnect; } catch (COMException ex) { return simConnect; // displayText("Unable to connect to FSX " + ex.Message); } }
private void button1_Click(object sender, EventArgs e) { if (simconnect == null) { try { // the constructor is similar to SimConnect_Open in the native API simconnect = new SimConnect("FSCommunicate", this.Handle, WM_USER_SIMCONNECT, null, 0); initDataRequest(); } catch (COMException ex) { MessageBox.Show("Simulator not available", "FSCommunicate", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("Error, try connect again.", "FSCommunicate", MessageBoxButtons.OK, MessageBoxIcon.Error); closeConnection(); } }
public override void Connect() { if (Connected) return; Connected = true; try { WindowInteropHelper wih = new WindowInteropHelper(Application.Current.MainWindow); sim = new SimConnect( "OpenFSRC", wih.Handle, WM_USER_SIMCONNECT, null, 0 ); HwndSource hs = HwndSource.FromHwnd(wih.Handle); hs.AddHook(new HwndSourceHook(DefWndProcWpf)); InitializeApi(); } catch (Exception ex) { Connected = false; Logger.Log("A connection to the FSX could not be established. Make sure that FSX is running."); MessageBox.Show( "A connection to the FSX could not be established. Make sure that FSX is running.", "Error", MessageBoxButton.OK, MessageBoxImage.Error ); } }
/// <summary> /// Connect to the FSX application. /// </summary> public void Connect() { if (simConnect != null) { logger.Warn("Attempting to connect SimConnect client when connection already exists."); return; } try { // Create the SimConnect object logger.Trace("Connecting SimConnect client. Name: {0}. ClientID: {1}", name, clientID); simConnect = new SimConnect(name, handle, clientID, null, 0); // Throw the connected event if (ConnectEvent != null) ConnectEvent(this); } catch (COMException ex) { logger.ErrorException("Connection to SimConnect server could not be established.", ex); throw new SimException("Connection to SimConnect server could not be established.", ex); } }
private void btnConnect_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); initSystemEvent(); } catch (COMException ex) { DisplayText("Unable to connect to FSX " + ex.Message); } } else { DisplayText("Error - try again"); closeConnection(); setButtons(true, false, false); } }
void sc_OnRecvGroundInfo(SimConnect sender, SIMCONNECT_RECV_GROUND_INFO data) { Console.WriteLine("sc_OnRecvGroundInfo"); }
void sc_OnRecvObserverData(SimConnect sender, SIMCONNECT_RECV_OBSERVER_DATA data) { Console.WriteLine("sc_OnRecvObserverData"); data.PrintDump(); }
void sc_OnRecvEventObjectAddremove(SimConnect sender, SIMCONNECT_RECV_EVENT_OBJECT_ADDREMOVE data) { Console.WriteLine("sc_OnRecvEventObjectAddremove"); }
private void SimClient_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { _simConnectionStatus = true; }
private void SimClient_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data) { _simConnectionStatus = false; SetConnectionText(); }
void sc_OnRecvEventFrame(SimConnect sender, SIMCONNECT_RECV_EVENT_FRAME data) { Console.WriteLine("sc_OnRecvEventFrame"); }
private void Connect() { if (!m_bConnected) { try { m_scConnection = new SimConnect("Fsxml", IntPtr.Zero, 0, m_scEvent, 0); m_scConnection.OnRecvOpen += new SimConnect.RecvOpenEventHandler(OnRecvOpen); Reco.Stop(); } catch (Exception e) { Console.WriteLine("Erreur de connexion au simulateur, celui ci n'est peut être pas lancé"); Console.WriteLine("Nouvel essai dans 10 sec"); Reco.Start(); } } }
void sc_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) { }
void sc_OnRecvSynchronousBlock(SimConnect sender, SIMCONNECT_RECV_SYNCHRONOUS_BLOCK data) { Console.WriteLine("sc_OnRecvSynchronousBlock"); }
public void ConnectToSimConnect() { try { sc = new SimConnect("VE_SC_WPF", (IntPtr)0, 0, scReady, 0); sc.OnRecvClientData += sc_OnRecvClientData; sc.OnRecvCustomAction += sc_OnRecvCustomAction; sc.OnRecvEvent += sc_OnRecvEvent; sc.OnRecvEventFilename += sc_OnRecvEventFilename; sc.OnRecvEventFrame += sc_OnRecvEventFrame; sc.OnRecvEventObjectAddremove += sc_OnRecvEventObjectAddremove; sc.OnRecvException += sc_OnRecvException; sc.OnRecvGroundInfo += sc_OnRecvGroundInfo; sc.OnRecvNull += sc_OnRecvNull; sc.OnRecvObserverData += sc_OnRecvObserverData; sc.OnRecvOpen += sc_OnRecvOpen; sc.OnRecvQuit += sc_OnRecvQuit; sc.OnRecvSimobjectData += sc_OnRecvSimobjectData; sc.OnRecvSimobjectDataBytype += sc_OnRecvSimobjectDataBytype; sc.OnRecvSynchronousBlock += sc_OnRecvSynchronousBlock; sc.OnRecvSystemState += sc_OnRecvSystemState; _udpRecv = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); _udpRecv.Bind(new IPEndPoint(IPAddress.Any, 49000)); SocketAsyncEventArgs e = new SocketAsyncEventArgs(); e.Completed += OnReceive; e.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); e.SetBuffer(new byte[255], 0, 255); if (!_udpRecv.ReceiveFromAsync(e)) OnReceive(_udpRecv, e); } catch (COMException) { Console.WriteLine("Unable to connect to SimConnect"); } catch (Exception) { } finally { } bgThread = new Thread(new ThreadStart(scMessageThread)); bgThread.IsBackground = true; bgThread.Start(); }
void sc_OnRecvSimobjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) { if (data.dwRequestID == (uint)Requests.PlanePosition) { PlanePosition.Struct pos = (PlanePosition.Struct)data.dwData[0]; if (UseMagneticHeading) pos.heading = pos.magheading; SimPosition = new PlanePosition(pos); byte[] packet = P3DHIL.ExtensionMethods.StructExtensionMethods.StructureToByteArray(pos); _udpSend.SendAsync(packet, packet.Length, "127.0.0.1", 49005).ContinueWith((x) => { Console.WriteLine("UDP Send [{0}|{1}] {2}", x.Result, packet.Length, x.Exception.Message); }, TaskContinuationOptions.NotOnRanToCompletion); NotifyOfPropertyChange(() => SimPosition); } else if (data.dwRequestID == (uint)Requests.SurfacesPosition) { SurfacesPosition.Struct pos = (SurfacesPosition.Struct)data.dwData[0]; SimControl = new SurfacesPosition(pos); NotifyOfPropertyChange(() => SimControl); } else { Console.WriteLine("Unknown"); } }
private void SimClient_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) { //lblSimStatus.Content = "ERROR!"; }
void sc_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { sender.RequestClientData(Requests.HostResponse1, SIMCONNECT_CLIENT_DATA_PERIOD.ON_SET, typeof(HostResponse1)); sender.RequestClientData(Requests.HostException1, SIMCONNECT_CLIENT_DATA_PERIOD.ON_SET, typeof(HostException1)); sender.RequestClientData(Requests.HostAPI2, SIMCONNECT_CLIENT_DATA_PERIOD.ON_SET, typeof(HostAPI2)); }
void sc_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { Console.WriteLine("sc_OnRecvOpen"); data.PrintDump(); }
private void OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { Console.WriteLine("FSXml démarré"); m_bConnected = true; // On programme chaque events sur simconnect avec notre enumérateur foreach (EventEnum item in Enum.GetValues(typeof(EventEnum))) { m_scConnection.MapClientEventToSimEvent(item, item.ToString()); } //On programme la collecte des données FS pour notre structure, en prenant soin de garder le même ordre que la structure m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Title", "", SIMCONNECT_DATATYPE.STRING256, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Plane Latitude", "degrees latitude", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Plane Longitude", "degrees longitude", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Plane Altitude", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Airspeed Indicated", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Plane Heading Degrees Magnetic", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Plane Heading Degrees True", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Com Active Frequency:1", "MHz", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Com Active Frequency:2", "MHz", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Com Standby Frequency:1", "MHz", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Com Standby Frequency:2", "MHz", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Nav Active Frequency:1", "MHz", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Nav Active Frequency:2", "MHz", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Nav Standby Frequency:1", "MHz", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Nav Standby Frequency:2", "MHz", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Master", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Approach Hold", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Heading Lock", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Heading Lock Dir", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Altitude Lock", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Altitude Lock Var", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Vertical Hold Var", "feet/minute", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Throttle Arm", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Airspeed Hold", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Airspeed Hold Var", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Autopilot Nav1 Lock", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Gps Drives Nav1", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Number Of Engines", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "General Eng Throttle Lever Position:1", "", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "General Eng Throttle Lever Position:2", "", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "General Eng Throttle Lever Position:3", "", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "General Eng Throttle Lever Position:4", "", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Flaps Handle Percent", "", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Gear Handle Position", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Spoilers Armed", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Spoilers Handle Position", "", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Brake Left Position", "", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "Brake Right Position", "", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); // On associe notre structure à la définition simconnect m_scConnection.RegisterDataDefineStruct<DonneesAvion>(DEFINITIONS.PERIODIQUE); // On définie les fonctions callback pour les différents event de simconnect m_scConnection.OnRecvException += new SimConnect.RecvExceptionEventHandler(OnRecvException); m_scConnection.OnRecvQuit += new SimConnect.RecvQuitEventHandler(OnRecvQuit); m_scConnection.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(OnRecvSimobjectData); //On programme la requete cyclique pour l'obtention des données de l'avion m_scConnection.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1, DEFINITIONS.PERIODIQUE, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, 0, 0, 0, 0); }
void sc_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data) { Console.WriteLine("sc_OnRecvQuit"); }
void sc_OnRecvClientData(SimConnect sender, SIMCONNECT_RECV_CLIENT_DATA data) { switch ((Requests)data.dwRequestID) { case Requests.HostResponse1: { HostResponse1 resp = (HostResponse1)data.dwData; if (resp.RequestorID == guidClient) { // only process replies meant for us switch (resp.RequestedDataIndex) { case 1: txtVal1.Text = resp.RequestedDataValue.ToString(); break; case 2: txtVal2.Text = resp.RequestedDataValue.ToString(); break; case 3: txtVal3.Text = resp.RequestedDataValue.ToString(); break; case 4: txtVal4.Text = resp.RequestedDataValue.ToString(); break; case 5: txtVal5.Text = resp.RequestedDataValue.ToString(); break; } } } break; case Requests.HostException1: { HostException1 exc = (HostException1)data.dwData; if (exc.RequestorID == guidClient) { switch (exc.ReqeustedDataIndex) { case 1: txtVal1.Text = exc.szErrorMessage; break; case 2: txtVal2.Text = exc.szErrorMessage; break; case 3: txtVal3.Text = exc.szErrorMessage; break; case 4: txtVal4.Text = exc.szErrorMessage; break; case 5: txtVal5.Text = exc.szErrorMessage; break; } } } break; case Requests.HostAPI2: { HostAPI2 api = (HostAPI2)data.dwData; txtVal21.Text = api.StringValue; txtVal22.Text = api.BooleanValue ? "True" : "False"; txtVal23.Text = api.IntValue.ToString(); txtVal24.Text = api.DoubleValue.ToString(); } break; } }
void sc_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) { Console.WriteLine("sc_OnRecvSystemState"); data.Dump(); }
void simconnect_OnRecvSimobjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) { if(data.dwRequestID >= (uint)SIMCONNECT_EVENTS.EVENTID_POSITIONREPORT_FOR_AIUPDATE) HandleAIPositionReport(data.dwObjectID, (AIPositionReportStruct)data.dwData[0]); else { switch ((SIMCONNECT_EVENTS)data.dwRequestID) { case SIMCONNECT_EVENTS.EVENTID_POSITIONREPORT: var posreport = (PositionReportStruct)data.dwData[0]; var positionReportMsg = new PositionReportMessage() { TrueAltitude = posreport.truealtitude, PressureAltitude = posreport.pressurealtitude, Longitude = posreport.longitude, Latitude = posreport.latitude, Groundspeed = posreport.groundspeed, Pitch = -posreport.pitch, Bank = -posreport.bank, Heading = posreport.heading, }; broker.Publish(positionReportMsg); break; } } }
void sc_OnRecvSystemState(SimConnect sender, SIMCONNECT_RECV_SYSTEM_STATE data) { Console.WriteLine("sc_OnRecvSystemState"); }
void sc_OnRecvClientData(SimConnect sender, SIMCONNECT_RECV_CLIENT_DATA data) { Console.WriteLine("sc_OnRecvClientData"); }
private void CloseConnection() { m_scThreadRun = false; // stop the second thread if (m_scConnection != null) { m_scConnection.Dispose(); // delete the connection m_scConnection = null; } m_bConnected = false; }
void sc_OnRecvCustomAction(SimConnect sender, SIMCONNECT_RECV_CUSTOM_ACTION data) { Console.WriteLine("sc_OnRecvCustomAction"); }
private void OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) { Console.WriteLine("Erreur SimConnect: " + data.dwException.ToString()); }
void sc_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT data) { Console.WriteLine("sc_OnRecvEvent"); }
private void OnRecvSimobjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) { try { Donnees = (DonneesAvion)data.dwData[0]; } catch (Exception e) { Console.WriteLine("OnRecvSimobjectData a échoué :" + e.Message); } }
internal override void PrepareRequest(ISimConnectPreparator preparator, SimConnect simConnect) { preparator.RegisterDataDefinition(DataDefinition); }
private void OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data) { CloseConnection(); }