void sc_OnRecvEventObjectAddremove(SimConnect sender, SIMCONNECT_RECV_EVENT_OBJECT_ADDREMOVE data) { switch ((EventIDs)data.uEventID) { case EventIDs.AddObject: if (data.dwData != 0 && !Objects.ContainsKey(data.dwData)) { UserPosition pos = new UserPosition(); sc.RequestDataOnSimObject( (RequestID)((int)RequestID.AIDataBase + data.dwData), data.dwData, SIMCONNECT_PERIOD.VISUAL_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.CHANGED, pos); } break; case EventIDs.RemoveObject: if (data.dwData != 0 && Objects.ContainsKey(data.dwData)) { lstViewer.Items.Remove(Objects[data.dwData].ListItem); Objects.Remove(data.dwData); } break; } }
private void buttonStart_Click(object sender, EventArgs e) { simconnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SIM_FRAME, 0, 0, 0, 0); simconnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_2, DEFINITIONS.Struct2, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, 0, 0, 0, 0); setButtons(false, true); }
private void buttonStart(object sender, EventArgs e) { if (simconnect == null) { connect(); } simconnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Control1, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SIM_FRAME, 0, 0, 0, 0); }
void sc_OnRecvEventObjectAddremove(SimConnect sender, SIMCONNECT_RECV_EVENT_OBJECT_ADDREMOVE data) { if ((Events)data.uEventID == Events.ObjectAdded && (data.eObjType == SIMCONNECT_SIMOBJECT_TYPE.AIRCRAFT || data.eObjType == SIMCONNECT_SIMOBJECT_TYPE.HELICOPTER) && (data.dwData != SimConnect.USER_SIMOBJECT)) { sc.RequestDataOnSimObject( (Requests)((uint)Requests.TrafficInfoBase + (uint)data.dwData), data.dwData, SIMCONNECT_PERIOD.SECOND, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, typeof(TrafficInfo)); } }
private void ResetDataAndShutDownRequests() { // Disable requests for data on each individual object foreach (System.Collections.Generic.KeyValuePair <string, AIObjectData> kvp in AIObjects) { // Remove any outstanding requests for data on specific objects simconnect.RequestDataOnSimObject((DATA_REQUESTS)(AIObjects[kvp.Key].dwObjectID), DEFINITIONS.PositionData_Definition, AIObjects[kvp.Key].dwObjectID, SIMCONNECT_PERIOD.NEVER, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); } // Clear the objects from our listview AIObjectListView.Items.Clear(); // Clear the objects from our list container AIObjects.Clear(); }
private void sc_OnRecvEventObjectAddremove(SimConnect sender, SIMCONNECT_RECV_EVENT_OBJECT_ADDREMOVE data) { switch ((EventIDs)data.uEventID) { case EventIDs.AddObject: if (data.dwData != 0 && !Objects.Contains(data.dwData)) { VehiclePosition pos = new VehiclePosition(data.dwData); Objects.Add(pos); sender.RequestDataOnSimObject( (RequestIDs)((int)RequestIDs.AIDataBase + (int)pos.ObjectID), pos.ObjectID, SIMCONNECT_PERIOD.VISUAL_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.CHANGED, pos); } break; case EventIDs.RemoveObject: if (data.dwData != 0 && Objects.Contains(data.dwData)) { Objects.Remove(data.dwData); } break; } }
public void setupSimConnect() { try { p3d = new SimConnect("P3DAutoSave", dummyWindow.Handle, SimConnectDummyWindow.WM_USER_SIMCONNECT, null, 0); } catch (COMException e) { MessageBox.Show(e.Message); MessageBox.Show("Please start P3D before launching this application."); Environment.Exit(1); } // listen to quit msg p3d.OnRecvQuit += new SimConnect.RecvQuitEventHandler(p3d_OnRecvQuit); // listen to events p3d.OnRecvEvent += new SimConnect.RecvEventEventHandler(p3d_OnRecvEvent); // Subscribe to system events p3d.SubscribeToSystemEvent(EVENTS.SIM_START, "SimStart"); p3d.SubscribeToSystemEvent(EVENTS.SIM_STOP, "SimStop"); p3d.SubscribeToSystemEvent(EVENTS.SIM_PAUSE, "Pause"); // Turn events on p3d.SetSystemEventState(EVENTS.SIM_START, SIMCONNECT_STATE.ON); p3d.SetSystemEventState(EVENTS.SIM_STOP, SIMCONNECT_STATE.ON); p3d.SetSystemEventState(EVENTS.SIM_PAUSE, SIMCONNECT_STATE.ON); // SimConnect variables p3d.AddToDataDefinition(VARIABLES.ON_GROUND, "SIM ON GROUND", "Bool", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); p3d.RequestDataOnSimObject(EVENTS.RECEIVE_DATA, VARIABLES.ON_GROUND, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, 0, 0, 0, 0); Console.WriteLine("SimConnect initialized"); }
private void OnConnected(SimConnect sender, SIMCONNECT_RECV_OPEN data) { foreach (var simEvent in Enum.GetValues(typeof(SimEvents)).Cast <SimEvents>()) { _simConnect.MapClientEventToSimEvent(simEvent, simEvent.ToString()); } foreach (var simVar in _simVars) { _simConnect.AddToDataDefinition( simVar.SimVar, simVar.Name, string.Empty, simVar.SimConnectType, 0, SimConnect.SIMCONNECT_UNUSED); _registerDataDefineStructMethodInfo.MakeGenericMethod(simVar.ClrType).Invoke(_simConnect, new object[] { simVar.SimVar }); _simConnect.RequestDataOnSimObject( simVar.SimVar, simVar.SimVar, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.VISUAL_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.CHANGED, 0, 0, 0); } }
/// <summary> /// ウィンドウが表示されたときのイベント /// </summary> private void Window_Loaded(object sender, RoutedEventArgs e) { try { simconnect = new SimConnect("Managed Data Request", this.Handle, WM_USER_SIMCONNECT, null, 0); /// 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); /// Catch a simobject data request simconnect.OnRecvSimobjectData += SimConnect_OnRecvSimobjectData; simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Title", null, SIMCONNECT_DATATYPE.STRING256, 0, SimConnect.SIMCONNECT_UNUSED); simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "ATTITUDE INDICATOR PITCH DEGREES", "Radians", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "ATTITUDE INDICATOR BANK DEGREES", "Radians", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED); simconnect.RegisterDataDefineStruct <Struct1>(DEFINITIONS.Struct1); simconnect.RequestDataOnSimObject(REQUEST.Dummy, DEFINITIONS.Struct1, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SIM_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); } catch (COMException ex) { MessageBox.Show("シミュレーターに接続出来ませんでした。\n終了します。", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); this.Close(); } }
void Simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { Debug.WriteLine("Loaded Simconnect"); simconnect.RequestDataOnSimObject(DATA_REQUESTS.FLIGHT_STATUS, DEFINITIONS.FlightStatus, 0, !SlowMode ? SIMCONNECT_PERIOD.SECOND : SIMCONNECT_PERIOD.SIM_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); }
private void buttonConnect_Click(object sender, EventArgs e) { if (simconnect == null) { try { // the constructor is similar to SimConnect_Open in the native API simconnect = new SimConnect("Managed Data Request", this.Handle, WM_USER_SIMCONNECT, null, 0); setButtons(false, true, true); initDataRequest(); timer1.Start(); // timer for measuring elapsed time between updates lastTicks = Environment.TickCount; simconnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SIM_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0U, 0U, 0U); } catch (COMException ex) { displayText("Unable to connect to FSX"); } } else { displayText("Error - try again"); closeConnection(); setButtons(true, false, false); } }
public void SendRequest(FSDataType requestType) { if (simconnect == null) { return; } //simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_MAPDATA, DEFINITIONS.StructMapData, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); switch (requestType) { case FSDataType.DT_MAP: simconnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_MAPDATA, DEFINITIONS.StructMapData, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, SIMCONNECT_DATA_REQUEST_FLAG.CHANGED, 0, 0, 0); break; case FSDataType.DT_FLIGHTSIM_STATUS: simconnect.RequestSystemState(DATA_REQUESTS.REQUEST_SYSTEMSTATE, "Sim"); break; case FSDataType.DT_FLIGHTPLAN: simconnect.RequestSystemState(DATA_REQUESTS.REQUEST_FLIGHTPLAN, "FlightPlan"); break; default: break; } }
public void stop(SimConnect connection) { startStruct start = new startStruct(registration, 1); connection.SetDataOnSimObject(DATA_DEF.AIRCRAFT_START, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT, start); connection.RequestDataOnSimObject(DATA_REQUESTID.FLIGHT_DATA, DATA_DEF.FLIGHT_DATA, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.NEVER, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); }
/// <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)"); }
private void RequestDataOnConnected() { simconnect.RequestDataOnSimObject( DATA_REQUESTS.AIRCRAFT_POSITION, DEFINITIONS.AircraftPosition, 0, SIMCONNECT_PERIOD.SIM_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); }
void Simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { logger.LogInformation("Connected to Flight Simulator"); simconnect.RequestDataOnSimObject(DATA_REQUESTS.FLIGHT_STATUS, DEFINITIONS.FlightStatus, 0, SlowMode ? SIMCONNECT_PERIOD.SECOND : SIMCONNECT_PERIOD.SIM_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); }
internal static void RequestDataAllDataStructs(SimConnect sc, int userId) { foreach (KeyValuePair <SIMVAR_CATEGORY, bool> category in _registeredDataStructs) { if (category.Value) { sc.RequestDataOnSimObject(category.Key, category.Key, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.VISUAL_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); } } }
void Simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { logger.LogInformation("Connected to Flight Simulator"); simconnect.RequestDataOnSimObject(DATA_REQUESTS.FLIGHT_STATUS, DEFINITIONS.FlightStatus, 0, SlowMode ? SIMCONNECT_PERIOD.SECOND : SIMCONNECT_PERIOD.SIM_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); simconnect.SubscribeToFacilities(SIMCONNECT_FACILITY_LIST_TYPE.AIRPORT, DATA_REQUESTS.SUBSCRIBE_GENERIC); }
private void Simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { LogDebug("Connected to Flight Simulator"); if (_simconnect != null) { _simconnect.RequestDataOnSimObject(DATA_REQUESTS.FLIGHT_STATUS, DEFINITIONS.FlightStatus, 0, SIMCONNECT_PERIOD.SIM_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); } else { LogError("SimConnect gone while opening"); } }
void onRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { this.fsStatusLabel.Text = "Connected"; this.fsStatusLabel.Image = FSEconomy.Properties.Resources.icon_airplane_green; connection.SubscribeToSystemEvent(EVENTS.CRASH, "Crashed"); connection.SubscribeToSystemEvent(EVENTS.JUMP, "PositionChanged"); connection.SubscribeToSystemEvent(EVENTS.SIMSTATE, "Sim"); Aircraft.SetDataDefinition(connection); connection.RequestDataOnSimObject(DATA_REQUESTID.AIRCRAFT_INFO, DATA_DEF.AIRCRAFT_INFO, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); stateManager.connection = connection; connection.MenuAddItem("FS Economy", EVENTS.MENU1, 0); connection.MenuAddSubItem(EVENTS.MENU1, "Start flight", EVENTS.MENU2, 0); }
void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { logger.LogInformation("Connected to Prepar3D"); //simconnect.RequestFlightSegmentCount(DATA_REQUESTS.FLIGHT_PLAN); simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.AIRCRAFT_DATA, DEFINITIONS.PlaneData, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.FLIGHT_PLAN_STATUS, DEFINITIONS.FlightPlan, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); simconnect.RequestDataOnSimObject(DATA_REQUESTS.ENVIRONMENT_DATA, DEFINITIONS.EnvironmentData, 0, SIMCONNECT_PERIOD.SECOND, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); simconnect.SubscribeToFacilities(SIMCONNECT_FACILITY_LIST_TYPE.AIRPORT, DATA_REQUESTS.SUBSCRIBE_GENERIC); simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.FLIGHT_STATUS, DEFINITIONS.FlightStatus, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); }
void AddObject(VehiclePosition pos) { sc.RequestDataOnSimObject( (RequestIDs)((uint)RequestIDs.VehicleDataRequestBase + (uint)pos.ObjectID), pos.ObjectID, SIMCONNECT_PERIOD.VISUAL_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.CHANGED, pos); this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new MyDelegate(delegate() { Objects.Add(pos); })); }
private void OnRecvOpen(SimConnect simConnect, SIMCONNECT_RECV_OPEN data) { SimConnectzmo sc = (simConnect as SimConnectzmo) !; RegisterDataStructs(sc); MapClientEvents(sc); SetGroupPriorities(sc); foreach (IRequestDataOnOpen request in sc.typeToRequest !.Keys .Where(candidate => candidate is IRequestDataOnOpen) .Select(data => (IRequestDataOnOpen)data)) { simConnect.RequestDataOnSimObject(request as IDataListener, request.GetInitialRequestPeriod()); } }
private void sc_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) { if (!Objects.Contains(data.dwObjectID)) { VehiclePosition pos = (VehiclePosition)data.dwData; pos.ObjectID = data.dwObjectID; Objects.Add(pos); sender.RequestDataOnSimObject( (RequestIDs)((int)RequestIDs.AIDataBase + (int)pos.ObjectID), pos.ObjectID, SIMCONNECT_PERIOD.VISUAL_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.CHANGED, pos); } }
private void OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { LogToConnection("Simconnect started receiving data from sim."); SetFlightDataDefinitions(); simconnect.RequestDataOnSimObject(DATA_REQUEST.AircraftStatus, DEFINITIONS.AircraftStatus, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, 0, 0, 0, 0); Task.Run(async() => { while (true) { await Task.Delay(1000); ReceiveSimConnectMessage(); } }); }
public void start(SimConnect connection) { connection.SetDataOnSimObject(DATA_DEF.AIRCRAFT_FUEL, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT, fuelState); int payloadPerStation = (int)(payloadWeight / aircraft.specifications.numPayloadStations); for (int c = 0; c < aircraft.specifications.numPayloadStations; c++) { setPayloadStation(connection, c + 1, payloadPerStation); } startStruct start = new startStruct(registration, 0); connection.SetDataOnSimObject(DATA_DEF.AIRCRAFT_START, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT, start); connection.RequestDataOnSimObject(DATA_REQUESTID.FLIGHT_DATA, DATA_DEF.FLIGHT_DATA, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0); }
public void connect(IntPtr hWnd) { if (simconnect != null) { return; } try { simconnect = new SimConnect("FSX Broadcast", hWnd, WM_USER_SIMCONNECT, null, 0); simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen); simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit); // listen to exceptions simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException); simconnect.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(SimConnect_OnRecvSimobjectData); // define a data structure simconnect.AddToDataDefinition(DEFINITIONS.PositionData_Definition, "Title", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED); simconnect.AddToDataDefinition(DEFINITIONS.PositionData_Definition, "Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); simconnect.AddToDataDefinition(DEFINITIONS.PositionData_Definition, "Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); simconnect.AddToDataDefinition(DEFINITIONS.PositionData_Definition, "Plane Altitude", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); simconnect.AddToDataDefinition(DEFINITIONS.PositionData_Definition, "Airspeed Indicated", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); simconnect.AddToDataDefinition(DEFINITIONS.PositionData_Definition, "Ground Velocity", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); simconnect.AddToDataDefinition(DEFINITIONS.PositionData_Definition, "Plane Heading Degrees Magnetic", "radians", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); simconnect.AddToDataDefinition(DEFINITIONS.PositionData_Definition, "Plane Heading Degrees True", "radians", 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. simconnect.RegisterDataDefineStruct <PositionData>(DEFINITIONS.PositionData_Definition); // catch a simobject data request // simconnect.SubscribeToSystemEvent(EVENTS.EVENT_1S, "1sec"); simconnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1S, DEFINITIONS.PositionData_Definition, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 1, 0); } catch (COMException ex) { Console.WriteLine("Connection to FSX failed: {0}", ex.ToString()); } FSXConnectionChanged(); }
void setupPulling() { sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "Plane Altitude", "kilometers", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "AIRSPEED TRUE", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "ATTITUDE INDICATOR PITCH DEGREES", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "ATTITUDE INDICATOR BANK DEGREES", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "Delta Heading Rate", "degrees per second", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "Turn Coordinator Ball", "number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "Heading Indicator", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "Vertical Speed", "ft/min", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.AddToDataDefinition(SIMCONNECT_DATA_DEFINITION_ID.planeLocation, "GPS GROUND SPEED", "m/s", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); sc.MapClientEventToSimEvent(EVENT_ID.EVENT_PAUSE, "pause_toggle"); sc.MapClientEventToSimEvent(EVENT_ID.EVENT_BRAKES, "brakes"); sc.RegisterDataDefineStruct <PlaneStruct>(SIMCONNECT_DATA_DEFINITION_ID.planeLocation); sc.RequestDataOnSimObject(DATA_REQUEST_ID.REQUEST_1, SIMCONNECT_DATA_DEFINITION_ID.planeLocation, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, 0, 0, 0, 0); }
// May not be the best way to achive regular requests. // See SimConnect.RequestDataOnSimObject private void OnTick(object sender, EventArgs e) { Console.WriteLine("OnTick"); bOddTick = !bOddTick; foreach (SimvarRequest oSimvarRequest in lSimvarRequests) { if (!oSimvarRequest.bPending) { // m_oSimConnect?.RequestDataOnSimObjectType(oSimvarRequest.eRequest, oSimvarRequest.eDef, 0, m_eSimObjectType); oSimvarRequest.bPending = true; } else { oSimvarRequest.bStillPending = true; } if (bConnected && !oSimvarRequest.bSubscribed) { m_oSimConnect?.RequestDataOnSimObject(oSimvarRequest.eRequest, oSimvarRequest.eDef, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SECOND, SIMCONNECT_DATA_REQUEST_FLAG.CHANGED, 0, 0, 0); oSimvarRequest.bSubscribed = true; } } var allSubscribed = true; foreach (SimvarRequest simvarRequest in lSimvarRequests) { if (!simvarRequest.bSubscribed) { allSubscribed = false; break; } } if (allSubscribed) { m_oTimer.Stop(); bOddTick = false; } }
private void HandleOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) { logger.LogDebug("Preparing SimConnect session..."); api.AddToDataDefinition(DATA_DEFINITON_ID.UserPosition, "PLANE LATITUDE", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); api.AddToDataDefinition(DATA_DEFINITON_ID.UserPosition, "PLANE LONGITUDE", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); api.AddToDataDefinition(DATA_DEFINITON_ID.UserPosition, "PLANE HEADING DEGREES TRUE", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); api.AddToDataDefinition(DATA_DEFINITON_ID.UserPosition, "PLANE ALTITUDE", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); api.AddToDataDefinition(DATA_DEFINITON_ID.UserPosition, "VERTICAL SPEED", "feet per second", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); api.AddToDataDefinition(DATA_DEFINITON_ID.UserPosition, "PLANE PITCH DEGREES", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); api.AddToDataDefinition(DATA_DEFINITON_ID.UserPosition, "PLANE BANK DEGREES", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED); api.RequestDataOnSimObject( DATA_DEFINITON_ID.UserPosition, // Definition ID DATA_REQUEST_ID.UserPosition, // Request ID USER_OBJECT_ID, // ObjectID to query SIMCONNECT_PERIOD.SECOND, // Request periodicty SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, // Request mode REQUEST_ORIGIN, // Request initial delay REQUEST_INTERVAL, // Request interval REQUEST_LIMIT // Request count limit ); api.RegisterDataDefineStruct <UserStatus>(DATA_REQUEST_ID.UserPosition); logger.LogInformation("SimConnect session ready."); }
private void buttonConnect_Click(object sender, EventArgs e) { if (simconnect == null) { try { // the constructor is similar to SimConnect_Open in the native API simconnect = new SimConnect("Managed Data Request", this.Handle, WM_USER_SIMCONNECT, null, 0); setButtons(false, true, true); initDataRequest(); timer1.Start(); // timer for measuring elapsed time between updates lastTicks = Environment.TickCount; simconnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SIM_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0U, 0U, 0U); } catch (COMException ex) { displayText("Unable to connect to FSX"); } } else { displayText("Error - try again"); closeConnection(); setButtons(true, false, false); } }
static void Main(string[] args) { try { //Création d'une instance pour acceder à la librairie simconnect //m_scConnection = new Microsoft.FlightSimulator.SimConnect.SimConnect("FFSTracker", base.Handle, WM_USER_SIMCONNECT, null, 0); m_scConnection = new SimConnect("FSXML", IntPtr.Zero, WM_USER_SIMCONNECT, null, 0); Console.WriteLine("La connexion au simulateur a réussie"); } catch { Console.WriteLine("La connexion à FS à échouée."); return; } // On map les event avec notre enumérateur foreach (EventEnum item in Enum.GetValues(typeof(EventEnum))) { m_scConnection.MapClientEventToSimEvent(item, item.ToString()); } //On map les variables avec notre 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); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "APU GENERATOR SWITCH", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "APU_GENERATOR_ACTIVE", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); m_scConnection.AddToDataDefinition(DEFINITIONS.PERIODIQUE, "TOGGLE_MASTER_BATTERY", "", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED); // On associe notre structure à la définition simconnect m_scConnection.RegisterDataDefineStruct<DonneesAvion>(DEFINITIONS.PERIODIQUE); 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); Timer timer = new Timer(); timer.Interval = 1000; timer.AutoReset = true; timer.Elapsed += new ElapsedEventHandler(OnTimer); timer.Start(); listener = new HttpListener(); listener.Prefixes.Add("http://+:8000/"); Console.WriteLine("Listening.."); listener.Start(); while (!quit) { // Note: The GetContext method blocks while waiting for a request. HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a response. if ((request.RawUrl == "/cmd") && (request.HttpMethod == "POST") && request.HasEntityBody && request.ContentType == "application/json") { var sr = new System.IO.StreamReader(request.InputStream, request.ContentEncoding); var code = sr.ReadToEnd(); Console.WriteLine(code); var deserializer = new JavaScriptSerializer(); var json = deserializer.Deserialize<commande>(code); Console.WriteLine(json.cmd + " : " + json.param); json.cmd = json.cmd.ToUpper(); Byte[] Bytes = BitConverter.GetBytes(Convert.ToInt32(json.param)); UInt32 Param = BitConverter.ToUInt32(Bytes, 0); try { m_scConnection.TransmitClientEvent(0, (EventEnum)Enum.Parse(typeof(EventEnum), json.cmd), Param, GROUP.GROUP1, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY); } catch (Exception e) { Console.WriteLine("La requete simconnect a échouée :" + e.Message); } response.Close(); } else if (request.RawUrl == "/getall") { response.ContentType = "application/json"; var serializer = new JavaScriptSerializer(); string json = serializer.Serialize(Donnees); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(json); var output = response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); response.Close(); } else { response.StatusCode = 404; response.StatusDescription = "Not Found"; response.Close(); } } listener.Stop(); }