示例#1
0
        public static void Main(string[] args)
        {
            string hostName = "localhost";
            uint   port     = 500;

            // Also supports "somehostname 1234"
            if (args.Length == 2)
            {
                hostName = args[0];
                port     = uint.Parse(args[1]);
            }

            FsConnect fsConnect = new FsConnect();

            // Specify where the SimConnect.cfg should be written to
            fsConnect.SimConnectFileLocation = SimConnectFileLocation.Local;

            // Creates a SimConnect.cfg and connect to Flight Simulator using this configuration.
            fsConnect.Connect("TestApp", hostName, port, SimConnectProtocol.Ipv4);

            // Other alternatives, use existing SimConfig.cfg and specify config index:
            // fsConnect.Connect(1);
            // or
            // fsConnect.Connect();

            fsConnect.FsDataReceived += HandleReceivedFsData;

            List <SimProperty> definition = new List <SimProperty>();

            // Consult the SDK for valid sim variable names, units and whether they can be written to.
            definition.Add(new SimProperty("Title", null, SIMCONNECT_DATATYPE.STRING256));
            definition.Add(new SimProperty("Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty("Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64));

            // Can also use predefined enums for sim variables and units (incomplete)
            definition.Add(new SimProperty(FsSimVar.PlaneAltitude, FsUnit.Feet, SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty(FsSimVar.PlaneHeadingDegreesTrue, FsUnit.Degrees, SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty(FsSimVar.AirspeedTrue, FsUnit.MeterPerSecond, SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty(FsSimVar.AirspeedTrue, FsUnit.Knot, SIMCONNECT_DATATYPE.FLOAT64));

            fsConnect.RegisterDataDefinition <PlaneInfoResponse>(Requests.PlaneInfo, definition);

            fsConnect.RequestData(Requests.PlaneInfo);
            Console.ReadKey();
            fsConnect.Disconnect();
        }
示例#2
0
        public static void Main(string[] args)
        {
            string hostName = "localhost";
            uint   port     = 500;

            // Also supports "somehostname 1234"
            if (args.Length == 2)
            {
                hostName = args[0];
                port     = uint.Parse(args[1]);
            }

            FsConnect fsConnect = new FsConnect();

            // Specify where the SimConnect.cfg should be written to
            fsConnect.SimConnectFileLocation = SimConnectFileLocation.Local;

            // Creates a SimConnect.cfg and connect to Flight Simulator using this configuration.
            fsConnect.Connect("TestApp", hostName, port, SimConnectProtocol.Ipv4);

            // Other alternatives, use existing SimConfig.cfg and specify config index:
            // fsConnect.Connect(1);
            // or
            // fsConnect.Connect();

            fsConnect.FsDataReceived += HandleReceivedFsData;

            int planeInfoDefinitionId = fsConnect.RegisterDataDefinition <PlaneInfoResponse>();

            ConsoleKeyInfo cki;

            do
            {
                fsConnect.RequestData((int)Requests.PlaneInfoRequest, planeInfoDefinitionId);
                cki = Console.ReadKey();
            } while (cki.Key != ConsoleKey.Escape);

            fsConnect.Disconnect();
        }
示例#3
0
文件: Program.cs 项目: qery/FsConnect
        private static void Run(Options commandLineOptions)
        {
            _levelSwitch.MinimumLevel = commandLineOptions.LogLevel;

            try
            {
                _fsConnect = new FsConnect();

                try
                {
                    if (string.IsNullOrEmpty(commandLineOptions.Hostname))
                    {
                        Console.WriteLine($"Connecting to Flight Simulator using index {commandLineOptions.ConfigIndex}");
                        _fsConnect.Connect("FsConnectTestConsole", commandLineOptions.ConfigIndex);
                    }
                    else
                    {
                        Console.WriteLine($"Connecting to Flight Simulator on {commandLineOptions.Hostname}:{commandLineOptions.Port}");
                        _fsConnect.Connect("FsConnectTestConsole", commandLineOptions.Hostname, commandLineOptions.Port, SimConnectProtocol.Ipv4);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred while connection to Microsoft Flight Simulator: " + e.Message);
                    return;
                }

                //
                // Register event handlers
                //
                _fsConnect.ConnectionChanged += OnFsConnectOnConnectionChanged;

                //
                // Wait for connection to Flight Simulator before using API
                //

                bool receivedEvent = _connectedResetEvent.WaitOne(2000);

                if (receivedEvent == false)
                {
                    Console.WriteLine("Could not connect to Flight Simulator. Timed out waiting for connection");
                    return;
                }

                //
                // Post connection initialization
                //

                Console.WriteLine("Initializing data definitions");
                InitializeDataDefinitions(_fsConnect);

                _fsConnect.SetText("Test Console connected", 2);

                //
                // Show menu
                //

                MainMenu mainMenu = new MainMenu(_fsConnect);
                mainMenu.Run();

                //
                // Tear down
                //

                if (_fsConnect.Connected)
                {
                    _fsConnect.Disconnect();
                }

                _fsConnect.Dispose();
                _fsConnect = null;

                Console.WriteLine("Done");
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e);
            }
        }
示例#4
0
        private static void Run(Options commandLineOptions)
        {
            try
            {
                _fsConnect = new FsConnect();

                try
                {
                    if (string.IsNullOrEmpty(commandLineOptions.Hostname))
                    {
                        Console.WriteLine($"Connecting to Flight Simulator using index {commandLineOptions.ConfigIndex}");
                        _fsConnect.Connect("FsConnectTestConsole", commandLineOptions.ConfigIndex);
                    }
                    else
                    {
                        Console.WriteLine($"Connecting to Flight Simulator on {commandLineOptions.Hostname}:{commandLineOptions.Port}");
                        _fsConnect.Connect("FsConnectTestConsole", commandLineOptions.Hostname, commandLineOptions.Port, SimConnectProtocol.Ipv4);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return;
                }

                _fsConnect.FsDataReceived += HandleReceivedFsData;

                Console.WriteLine("Initializing data definitions");
                InitializeDataDefinitions(_fsConnect);

                _keyHandlers.Add(ConsoleKey.P, PollFlightSimulator);
                _keyHandlers.Add(ConsoleKey.W, MoveForward);
                _keyHandlers.Add(ConsoleKey.S, MoveBackward);
                _keyHandlers.Add(ConsoleKey.A, MoveLeft);
                _keyHandlers.Add(ConsoleKey.D, MoveRight);
                _keyHandlers.Add(ConsoleKey.Q, RotateLeft);
                _keyHandlers.Add(ConsoleKey.E, RotateRight);
                _keyHandlers.Add(ConsoleKey.R, IncreaseAltitude);
                _keyHandlers.Add(ConsoleKey.F, DecreaseAltitude);

                Console.WriteLine("Press any key to request data from Flight Simulator or ESC to quit.");
                Console.WriteLine("Press WASD keys to move, Q and E to rotate, R and F to change altitude.");
                ConsoleKeyInfo cki = Console.ReadKey(true);

                _fsConnect.SetText("Test Console connected", 2);

                _fsConnect.RequestData(Requests.PlaneInfo);

                while (cki.Key != ConsoleKey.Escape)
                {
                    if (_keyHandlers.ContainsKey(cki.Key))
                    {
                        _keyHandlers[cki.Key].Invoke();
                    }
                    else
                    {
                        PollFlightSimulator();
                    }

                    cki = Console.ReadKey(true);
                }

                Console.ReadKey(true);

                Console.WriteLine("Disconnecting from Flight Simulator");
                _fsConnect.SetText("Test Console disconnecting", 1);
                _fsConnect.Disconnect();
                _fsConnect.Dispose();
                _fsConnect = null;

                Console.WriteLine("Done");
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e);
            }
        }
        public void TransmitClientEvent_HeadingBugDirectionSet_SetsHeadingBugDirection()
        {
            // Arrange
            AutoResetEvent resetEvent = new AutoResetEvent(false);
            int            errorCount = 0;

            FsConnect fsConnect = new FsConnect();

            fsConnect.ConnectionChanged += (sender, b) =>
            {
                if (b)
                {
                    resetEvent.Set();
                }
            };

            fsConnect.FsError += (sender, args) =>
            {
                errorCount++;
                Console.WriteLine($"Error: {args.ExceptionDescription}");
            };

            fsConnect.Connect("FsConnectIntegrationTest", 0);
            bool res = resetEvent.WaitOne(2000);

            if (!res)
            {
                Assert.Fail("Not connected to MSFS within timeout");
            }

            var            hbDef          = fsConnect.RegisterDataDefinition <HeadingBugTest>();
            HeadingBugTest headingBugData = default;

            // Act
            fsConnect.MapClientEventToSimEvent(TestEnums.GroupId, TestEnums.EventId, FsEventNameId.HeadingBugSet);
            fsConnect.SetNotificationGroupPriority(TestEnums.GroupId);
            uint headingValue = (uint)DateTime.Now.Second * 6;

            fsConnect.TransmitClientEvent(TestEnums.EventId, (uint)headingValue, TestEnums.GroupId);

            // Assert
            Assert.That(errorCount, Is.Zero, "MSFS returned errors. Check console output.");

            fsConnect.FsDataReceived += (sender, args) =>
            {
                var data = args.Data.FirstOrDefault();

                headingBugData = data is HeadingBugTest ? (HeadingBugTest)data : default;

                resetEvent.Set();
            };

            fsConnect.RequestData((int)TestEnums.RequestId, hbDef);
            res = resetEvent.WaitOne(2000);
            if (!res)
            {
                Assert.Fail("Data not returned from MSFS within timeout");
            }

            Assert.That(headingBugData, Is.Not.Null);
            Assert.That(headingBugData.HeadingBug, Is.EqualTo(headingValue));

            // Teardown
            fsConnect?.Disconnect();
        }