• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

simconnect.TransmitClientEvent Help

Messages
85
Country
australia
I have this C# code taken from the P3d sdk and adapted to run in FSX with Simconnect.

The code works for the Pause/Unpause and I am able to use it to Pause / Unpause FSX with the status output to the console.
However with the code to set the GearUp/Gear Down does not work. The program recognises the words "GEAR UP" and "GEAR DOWN" and prints them to the console but the gear of the a/c in FSX do not go up or down.

I an not very familiar with C# so if someone can tell me what the problem is and how to fix it that would be great.

Thanks
Here is the code:

C#:
// Copyright (c) 2010-2019 Lockheed Martin Corporation. All rights reserved.
// Use of this file is bound by the PREPAR3D® SOFTWARE DEVELOPER KIT END USER LICENSE AGREEMENT

// Managed Voice Controls
/*
Use T to toggle the Voice control OFF/ON while console has focus
Say PAUSE to pause FSX
Say UNPAUSE to unpause FSX
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Windows.Forms;

using Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;

namespace Managed_Voice_Controls
{
    class ManagedVoiceControls
    {
        enum EVENTS
        {
            UNPAUSE = 0,
            PAUSE,
            GEARUP,
            GEARDOWN
        };

        Dictionary<string, EVENTS> phrases = new Dictionary<string, EVENTS>()
        {
            //{ "pause prepared", EVENTS.PAUSE },
           // { "unpause prepared", EVENTS.UNPAUSE }

            { "pause", EVENTS.PAUSE },
            { "unpause", EVENTS.UNPAUSE },
            { "gear up", EVENTS.GEARUP },
            { "gear down", EVENTS.GEARDOWN }
        };

        enum GROUPID
        {
            FLAG = 2000000000,
        };

        SimConnect simconnect = null;
        GrammarBuilder grammarBuilder = null;
        SpeechRecognitionEngine recognitionEngine = null;
        bool bVoiceRecognitionEnabled = true;
        bool done = false;
        const int WM_USER_SIMCONNECT = 0x0402;

        public IntPtr Handle { get; private set; }

        public ManagedVoiceControls()
        {
        }

        public void Run()
        {
            Connect();
            BuildGrammar();
            Listen();
        }

        public void Connect()
        {
            try
            {
                simconnect = new SimConnect("Managed Voice Control", this.Handle, WM_USER_SIMCONNECT, null, 0);
                simconnect.MapClientEventToSimEvent(EVENTS.PAUSE, "PAUSE_ON");
                simconnect.MapClientEventToSimEvent(EVENTS.UNPAUSE, "PAUSE_OFF");

                simconnect.MapClientEventToSimEvent(EVENTS.GEARUP, "GEAR_UP");
                simconnect.MapClientEventToSimEvent(EVENTS.GEARDOWN, "GEAR_DOWN");
                         }
            catch (Exception ex)
            {
                Console.WriteLine("Sim Connect unable to connect to FSX!\n\n{0}\n\n{1}", ex.Message, ex.StackTrace);
            }
        }

        public void BuildGrammar()
        {
            grammarBuilder = new GrammarBuilder();
            recognitionEngine = new SpeechRecognitionEngine();
            Choices phraseChoices = new Choices();

            foreach (KeyValuePair<string, EVENTS> i in phrases)
            {
                phraseChoices.Add(i.Key);
            }

            grammarBuilder.Append(phraseChoices);
            Grammar grammar = new Grammar(grammarBuilder);

            recognitionEngine.SetInputToDefaultAudioDevice();
            recognitionEngine.LoadGrammar(grammar);
        }

        public void Listen()
        {
            recognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(OnSpeechRecognized);
            recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

            while (!done)
            {
                string key = Console.ReadKey(true).Key.ToString();

                if (key.ToUpper() == "T")
                {
                    ToggleVoiceRecognition();
                }
            }
        }

        public void ToggleVoiceRecognition()
        {
            if (bVoiceRecognitionEnabled)
            {
                recognitionEngine.RecognizeAsyncStop();
                bVoiceRecognitionEnabled = false;
                Console.WriteLine("Voice recognition disabled\n");
            }
            else
            {
                recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
                bVoiceRecognitionEnabled = true;
                Console.WriteLine("Voice recognition enabled\n");
            }
        }

        public void OnSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Confidence < .90 || !bVoiceRecognitionEnabled)
            {
                return;
            }

            switch (phrases[e.Result.Text])
            {
                case EVENTS.PAUSE:
                    simconnect.TransmitClientEvent((uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.PAUSE, (uint)0, GROUPID.FLAG, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
                    Console.WriteLine("Pausing FSX\n");
                    break;

                case EVENTS.UNPAUSE:
                    simconnect.TransmitClientEvent((uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.UNPAUSE, (uint)0, GROUPID.FLAG, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
                    Console.WriteLine("Unpausing FSX\n");
                    break;

                case EVENTS.GEARUP:
                    simconnect.TransmitClientEvent((uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.GEARUP, (uint)0, GROUPID.FLAG, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
                    Console.WriteLine("Gear Up FSX\n");
                    break;
                  
                case EVENTS.GEARDOWN:
                    simconnect.TransmitClientEvent((uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.GEARDOWN, (uint)0, GROUPID.FLAG, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
                    Console.WriteLine("Gear Down FSX\n");
                    break;

                default:
                    break;
            }
        }

        public void clearGrammar()
        {
            recognitionEngine.UnloadAllGrammars();
        }
    }
}
 
OK an update

I added this to the code after the other enum declaration:
C#:
 public enum hSimconnect : int
        {
            group1
        }

and changed the simconnect.TransmitClientEvent to this:
Code:
                case EVENTS.GEARUP:
                    simconnect.TransmitClientEvent(SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.GEARUP, 0, hSimconnect.group1 , SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
                    Console.WriteLine("Gear Up FSX\n");
                    break;
                  
                case EVENTS.GEARDOWN:
                    simconnect.TransmitClientEvent((uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.GEARDOWN, (uint)0, hSimconnect.group1, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
                    Console.WriteLine("Gear Down FSX\n");
                    break;

and now the voice commands work - prints to the console and the gears actually go up/down, So can anyone explain why this works and the initial code did not?
 
Back
Top