public void Connect() { // Connect to FS try { // neue simconnect verbindung aufbauen // event management für open _mySimconnect.OnRecvOpen += simconnect_OnRecvOpen; // event management für close _mySimconnect.OnRecvQuit += simconnect_OnRecvQuit; _mySimconnect.OnRecvAssignedObjectId += mySimconnect_OnRecvAssignedObjectId; // abfragedaten zur verbindung hinzufügen _mySimconnect.AddToDataDefinition(Definitions.StructCoordinates, "Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); _mySimconnect.AddToDataDefinition(Definitions.StructCoordinates, "Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); // IMPORTANT: register it with the simconnect managed wrapper marshaller // if you skip this step, you will only receive a uint in the .dwData field. _mySimconnect.RegisterDataDefineStruct <StructCoordinates>(Definitions.StructCoordinates); // catch a simobject data request _mySimconnect.OnRecvSimobjectDataBytype += simconnect_OnRecvSimobjectDataBytype; _mySimconnect.RequestDataOnSimObjectType(DataRequests.Request_1, Definitions.StructCoordinates, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); _connectionStatus = ConnectionStatus.Connected; } catch (COMException ex) { // A connection to the SimConnect server could not be established _connectionStatus = ConnectionStatus.Pending; Disconnect(); throw new Exception(Resources.SimConnection_Error + ex.Message); } }
void Simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { logger.LogInformation("Connected to Flight Simulator"); cts?.Cancel(); cts = new CancellationTokenSource(); Task.Run(async() => { try { while (true) { await Task.Delay(StatusDelayMilliseconds); cts?.Token.ThrowIfCancellationRequested(); simconnect?.RequestDataOnSimObjectType(DATA_REQUESTS.FLIGHT_STATUS, DEFINITIONS.FlightStatus, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); if (genericValues.Count > 0 && isGenericValueRegistered) { simconnect?.RequestDataOnSimObjectType(DATA_REQUESTS.TOGGLE_VALUE_DATA, DEFINITIONS.GenericData, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); } } } catch (TaskCanceledException) { } }); }
private void OnTickPullData(object sender, EventArgs e) { try { _simConnection?.RequestDataOnSimObjectType( Request.PLANE_ALTITUDE, Definition.PlaneVariables, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); _simConnection?.RequestDataOnSimObjectType( Request.REALISM_CRASH_DETECTION, Definition.SimulationVariables, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); _simConnection?.RequestDataOnSimObjectType( Request.TITLE, Definition.PlaneMetadatas, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); _simConnection?.RequestDataOnSimObjectType( Request.GPS_IS_ACTIVE_FLIGHT_PLAN, Definition.GpsVariables, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); } catch (COMException ex) { Console.WriteLine("Connection to KH failed: " + ex.Message); Connected = false; } }
public void RequestData(RequestsEnum request, DefinitionsEnum definition) { if (!Connected) { return; } simClient.RequestDataOnSimObjectType(request, definition, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); }
private void timer1_Tick(object sender, EventArgs e) { // The following call returns identical information to: //simconnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.ONCE); simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 1, SIMCONNECT_SIMOBJECT_TYPE.USER); //displayText("Request sent..."); }
void UpdatePosition(object sender, EventArgs e) { if (simConnect == null) { return; } simConnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); }
private void DataUpdateTimer_Tick(object sender, EventArgs e) { if (simConnectStatus) { // Запрашиваем небходимую дату у сима. simConn.RequestDataOnSimObjectType(REQUEST_ENUM.planeReq, DATA_STRUCT_ENUM.Plane, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); simConn.RequestDataOnSimObjectType(REQUEST_ENUM.worldReq, DATA_STRUCT_ENUM.World, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); } }
private void btnConnectar_Click(object sender, EventArgs e) { try { // Attempt to open a connection to FSUIPC (running on any version of Flight Sim) FSUIPCConnection.Open(); // Opened OK so disable the Connect button this.btnConnectar.Enabled = false; this.chkEnableAIRadar.Enabled = true; // Start the timer ticking to drive the rest of the application this.timer1.Interval = 200; this.timer1.Enabled = true; // Set the AI object AI = FSUIPCConnection.AITrafficServices; } catch (Exception ex) { // Badness occurred - show the error message MessageBox.Show(ex.Message, AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); FSUIPCConnection.Close(); Debug.WriteLine(ex.Message); } if (simconnect == null) { try { // the constructor is similar to SimConnect_Open in the native API // the ConfigIndex enables you to choose which Simconnect profile to use (in a SimConnect.cfg) if you are // connecting over a network. Leaving it to zero for a local connection here. simconnect = new SimConnect("C# Monitoring AI Objects", this.Handle, WM_USER_SIMCONNECT, null, 0); // setButtons(false, true, true); initDataRequest(); } catch { MessageBox.Show("Make sure Microsoft ESP is running and that you have the SimConnect client installed.", "Unable to connect to ESP"); } } // Store the current object type being monitored monitoredObjectType = SIMCONNECT_SIMOBJECT_TYPE.AIRCRAFT; // Refresh the list of monitored objects for this specific request // first, Clear any current objects and stop all data requests for those objects ResetDataAndShutDownRequests(); // Now, Initiate a request for the list of objects we are interested in monitoring in the given radius simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_AI_OBJECT_ID_BYTYPE, DEFINITIONS.AI_Entity_Definition, MONITOR_RADIUS, monitoredObjectType); }
private void GetData(object sender, EventArgs e) { try { var connection = GetConnection(); if (!_opened) { return; } foreach (var property in PropertiesToWatch) { _simConnect.RequestDataOnSimObjectType(property.Key, property.Key, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); } } catch (Exception ex) { if (_simConnect != null) { _simConnect.Dispose(); _simConnect = null; } OnError?.Invoke(this, ex); } }
public void Connect() { handle = new WindowInteropHelper(Application.Current.MainWindow).Handle; handleSource = HwndSource.FromHwnd(handle); // Get source of handle in order to add event handlers to it handleSource.AddHook(HandleSimConnectEvents); if (sc == null) { try { sc = new SimConnect(appName, handle, WM_USER_SIMCONNECT, null, 0); sc.OnRecvOpen += sc_OnRecvOpen; sc.OnRecvException += sc_OnRecvException; sc.OnRecvQuit += sc_OnRecvQuit; sc.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.RegisterDataDefineStruct<Struct1>(DEFINITIONS.Struct1); sc.OnRecvSimobjectDataBytype += sc_OnRecvSimobjectData; } catch (Exception ex) { Console.WriteLine("Unable to connect to Sim"); } refreshTimer.Elapsed += (sender, e) => { sc.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); }; refreshTimer.Start(); } }
private void Timer_Tick(object sender, EventArgs e) { if (MySim != null) { try { foreach (var toConnect in simConnectProperties) { MySim.RequestDataOnSimObjectType((DUMMYENUM)toConnect.Key, (DUMMYENUM)toConnect.Key, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); } } catch { Disconnect(); } } if (bTimer) { ts = DateTime.Now - takeOffTime; btTimer.Content = ts.ToString(); if (ts.TotalSeconds > 90) { btTimer.Background = Brushes.Red; } } }
private void InitSimConnectField(Field field) { if (!isConnected) { return; } var attrib = FieldDefinition(field); if (attrib is null) { return; } var unitConfig = UnitTypeConfig.GetConfig(attrib.UnitType); // Set up data definition Console.WriteLine($"Def {attrib.DatumName}, {attrib.UnitType}"); simconnect.AddToDataDefinition(field, attrib.DatumName, unitConfig.Units, unitConfig.DataType, 0.0f, SimConnect.SIMCONNECT_UNUSED); var method = typeof(SimConnect).GetMethod("RegisterDataDefineStruct"); var methodRef = method.MakeGenericMethod(unitConfig.Struct); methodRef.Invoke(simconnect, new object[] { field }); // Get initial value simconnect.RequestDataOnSimObjectType(field, field, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); // Create event mapping simconnect.MapClientEventToSimEvent(field, attrib.EventId); simconnect.AddClientEventToNotificationGroup(GROUPS.GroupA, field, false); }
internal override void ExecuteRequest(uint requestId, SimConnect simConnect) { RequestId = requestId; simConnect.RequestDataOnSimObjectType((PrivateDummy)requestId, (PrivateDummy)DataDefinition.DefinitionId, 0, (SIMCONNECT_SIMOBJECT_TYPE)ObjectType); SimConnectNative.SimConnect_GetLastSentPacketID(simConnect.GetSimConnectHandle(), out uint sendId); SendId = sendId; }
private void sc_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { sender.SubscribeToSystemEvent(EventIDs.AddObject, "ObjectAdded"); sender.SubscribeToSystemEvent(EventIDs.RemoveObject, "ObjectRemoved"); sender.RequestDataOnSimObjectType(RequestIDs.AIEnumerate, 200000, SIMCONNECT_SIMOBJECT_TYPE.AIRCRAFT, typeof(VehiclePosition)); }
void sc_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { log.Info(string.Format("Connected to {0}\r\n Simulator Version:\t{1}.{2}.{3}.{4}\r\n SimConnect Version:\t{5}.{6}.{7}.{8}", data.szApplicationName, data.dwApplicationVersionMajor, data.dwApplicationVersionMinor, data.dwApplicationBuildMajor, data.dwApplicationBuildMinor, data.dwSimConnectVersionMajor, data.dwSimConnectVersionMinor, data.dwSimConnectBuildMajor, data.dwSimConnectBuildMinor)); OnRaiseOpenEvent(new OpenEventArgs(data.szApplicationName)); string simIdent = "MSFS"; if (data.szApplicationName.Contains("Flight Simulator X")) { simIdent = "FSX"; } else if (data.szApplicationName.Contains("ESP")) { simIdent = "ESP"; } else if (data.szApplicationName.Contains("Prepar3D")) { simIdent = "P3D"; } ffUdp = ForeFlightUdp.Instance; ffUdp.SetSimulator(simIdent); sc.RequestDataOnUserSimObject(Requests.UserPosition, SIMCONNECT_PERIOD.SECOND, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, typeof(Position)); sc.RequestDataOnSimObjectType(Requests.TrafficEnumerate, 200000, SIMCONNECT_SIMOBJECT_TYPE.AIRCRAFT & SIMCONNECT_SIMOBJECT_TYPE.HELICOPTER, typeof(TrafficInfo)); sc.SubscribeToSystemEvent(Events.ObjectAdded, "ObjectAdded"); sc.SubscribeToSystemEvent(Events.SixHz, "6Hz"); }
public Task <AircraftData> RequestAircraftDataAsync(CancellationToken cancellationToken = default) { if (aircraftDataTcs != null) { logger.LogInformation("Wait for existing aircraft data request..."); return(aircraftDataTcs.Task); } var tcs = new TaskCompletionSource <AircraftData>(); aircraftDataTcs = tcs; cancellationToken.Register(() => { if (tcs.TrySetCanceled()) { logger.LogWarning("Cannot get aircraft data in time limit!"); aircraftDataTcs = null; } }, useSynchronizationContext: false); logger.LogInformation("Requesting aircraft data..."); simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.AIRCRAFT_DATA, DEFINITIONS.AircraftData, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); return(aircraftDataTcs.Task); }
private void getVars() { // The following call returns identical information to: // simconnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.ONCE); simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); }
private void OnTick(object sender, EventArgs e) { foreach (SimVar sv in this.VarList) { if (sv.Subscribed) { try { if (sv.VarName.StartsWith("L:")) { //Console.WriteLine("SKIP:" + oSimvarRequest.VarName); } else { SimConnect?.RequestDataOnSimObjectType((SIMREQUEST)sv.DefID, (SIMREQUEST)sv.DefID, 0, sv.SimObjectType); } sv.Subscribed = true; } catch (Exception ex) { } } else { } } }
public void Connect() { if (sc == null) { try { sc = new SimConnect("FSWebService Controller", form.Handle, WM_USER_SIMCONNECT, null, 0); sc.OnRecvOpen += OnRecvOpen; sc.OnRecvException += OnRecvException; sc.OnRecvQuit += OnRecvQuit; sc.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.RegisterDataDefineStruct <Struct1>(DEFINITIONS.Struct1); sc.OnRecvSimobjectDataBytype += OnRecvSimobjectDataByType; IsConnected = true; } catch (Exception ex) { Console.WriteLine("Unable to connect to Sim"); // form.AddText("Unable to connect to Sim"); return; } pollingTimer.Elapsed += (sender, e) => { sc.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); }; pollingTimer.Start(); } }
/// <summary> /// Request an update for a specific SimVar request /// </summary> /// <param name="requestID">ID returned by SendRequest</param> /// <param name="frequency">SimVar can be requested manually (SimConnectUpdateFrequency.Never) or auto-sent at a pre-defined frequency</param> public static void GetSimVar(int requestID, SimConnectUpdateFrequency frequency) { WriteLog("Start GetSimVar(int, SimConnectUpdateFrequency)"); try { if (IsConnected) { if (frequency == SimConnectUpdateFrequency.Never || (int)frequency > (int)SIMCONNECT_PERIOD.SECOND) { if ((int)frequency > (int)SIMCONNECT_PERIOD.SECOND) { // Register SimVarRequest RegisterSimVar(Requests[requestID], SimConnectUpdateFrequency.Never); AddTimer(requestID, (int)frequency); } simConnect?.RequestDataOnSimObjectType((SIMVARREQUEST)requestID, (SIMVARDEFINITION)requestID, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); } else { SIMCONNECT_PERIOD period = Enum.Parse <SIMCONNECT_PERIOD>(frequency.ToString().ToUpper()); RegisterSimVar(Requests[requestID], SimConnectUpdateFrequency.Never); simConnect?.RequestDataOnSimObject((SIMVARREQUEST)requestID, (SIMVARDEFINITION)requestID, 0, period, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); } } } catch (Exception ex) { WriteLog(string.Format("Get SimVar Error: {0}", ex.Message), EventLogEntryType.Error); // Likely cause, no request for this variable has previously been submitted } WriteLog("End GetSimVar(int, SimConnectUpdateFrequency)"); }
public void Connect() { try { m_oSimConnect = new SimConnect("Arduino", m_hWnd, WM_USER_SIMCONNECT, null, 0); /// Catch a simobject data request m_oSimConnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(OnRecvEventHandler); m_oSimConnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(OnOpenHandler); m_oSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(OnRecvExceptionHandler); m_oSimConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(OnRecvSimobjectDataBytype); m_oSimConnect.AddToDataDefinition(DEFINITION.LedStatus, "AUTOPILOT MASTER", "Bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); m_oSimConnect.AddToDataDefinition(DEFINITION.LedStatus, "AUTOPILOT NAV1 LOCK", "Bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); m_oSimConnect.AddToDataDefinition(DEFINITION.LedStatus, "AUTOPILOT GLIDESLOPE HOLD", "Bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); m_oSimConnect.AddToDataDefinition(DEFINITION.LedStatus, "AUTOPILOT NAV1 LOCK", "Bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); m_oSimConnect.AddToDataDefinition(DEFINITION.LedStatus, "AUTOPILOT APPROACH HOLD", "Bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); m_oSimConnect.AddToDataDefinition(DEFINITION.LedStatus, "AUTOPILOT GLIDESLOPE HOLD", "Bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); m_oSimConnect.RegisterDataDefineStruct <Data>(DEFINITION.LedStatus); m_oSimConnect.RequestDataOnSimObjectType(REQUEST.Dummy, DEFINITION.LedStatus, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); isConnected = true; Log("Connected"); Console.WriteLine("Connected"); } catch (COMException ex) { Console.WriteLine("Connection to KH failed: " + ex.Message); } }
/// <summary> /// Submits a request for data to SimConnect /// </summary> private void SubmitDataRequest() { // Ensure that we are connected if (simconnect != null) { // Try to request the updated data from SimConnect as a user // On failure, disconnect from the SimConnect object try { simconnect.RequestDataOnSimObjectType( FsDataObjects.DATA_REQUESTS.REQUEST_1, FsDataObjects.DEFINITIONS.AircraftDataStruct, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); } catch (COMException e) { WriteToLog(e.Message); btnDisconnect_Click(null, null); btnConnect_Click(null, null); } } else { // Update the connection state displayed if invalid // to update the buttons and the timer setConnectDisconnectState(); } }
private void requestData() { while (true) { simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); Thread.Sleep(200); } }
private void Timer_Tick(object sender, EventArgs e) { if (!_simConnectionStatus) { return; } _simClient.RequestDataOnSimObjectType(RequestsEnum.HeadingRequest, DefinitionsEnum.HeadingDataStruct, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); }
private void Simconnect_OnRecvEventFilename(SimConnect sender, SIMCONNECT_RECV_EVENT_FILENAME data) { logger.LogInformation("OnRecvEventFilename dwID " + data.dwID + " uEventID " + data.uEventID); switch ((EVENTS)data.uEventID) { case EVENTS.AIRCRAFT_LOADED: logger.LogInformation("Aircraft loaded"); simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.AIRCRAFT_DATA, DEFINITIONS.PlaneData, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); break; case EVENTS.FLIGHT_LOADED: { logger.LogInformation("Flight loaded"); //var evt = (SIMCONNECT_RECV_EVENT_FILENAME)data; } break; } }
//Event received. Should trigger a request for the actual data. void simconnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT recEvent) { switch (recEvent.uEventID) { case (uint)EVENTS.AP_MASTER: simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_MCP, DEFINITION.MCP, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); break; } }
public bool RequestDataOnSimObjectType(SimVarItem simVar) { if (_connected) { _simConnect.RequestDataOnSimObjectType(simVar.req, simVar.def, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); } return(false); }
public void UpdateData() { foreach (SimVar s in lSimVars) { if (!s.isEvent) { simconnect.RequestDataOnSimObjectType(s.eRequest, s.eDef, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); } } }
public void GetValue(SimVarRequest request) { try { simConnect?.RequestDataOnSimObjectType(request.ReqID, request.DefID, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); } catch// (Exception ex) { // Likely cause, no request for this variable has been received } }
public void RequestDataOnSimObjectType(uint request, uint definition, uint radiusMeters, SIMCONNECT_SIMOBJECT_TYPE type) { if (simConnect == null) { return; } radiusMeters = Math.Min(200_000, radiusMeters); simConnect.RequestDataOnSimObjectType((REQUEST)request, (DEFINITION)definition, radiusMeters, type); }