c# - 语音识别问题

标签 c# winforms speech-recognition

我是一个初学者,最初我的主要目标是使用语音控制机器人。最初,我开始用这段代码为我的演讲制作语法,我什至成功了,我的代码是这样的,我在 Windows 窗体应用程序中制作了这个:

using System.Speech.Recognition;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create a new SpeechRecognizer instance.
            sr = new SpeechRecognizer();

            // Create a simple grammar that recognizes "red", "green", or "blue".
            Choices colors = new Choices();
            colors.Add("red");
            colors.Add("green");
            colors.Add("blue");
            colors.Add("white");

            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(colors);

            // Create the actual Grammar instance, and then load it into the speech recognizer.
            Grammar g = new Grammar(gb);
            sr.LoadGrammar(g);

            // Register a handler for the SpeechRecognized event.
            sr.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sr_SpeechRecognized);
        }


        // Simple handler for the SpeechRecognized event.
        private void sr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            MessageBox.Show(e.Result.Text);
        }    

        private SpeechRecognizer sr;
    }

现在,从这段代码中,当我说红色时,我在消息框中变成红色,现在我想控制电机,因此我需要与我的机器人通信,因此我在互联网的帮助下制作了一个控制台应用程序,用于将数据发送到我的伺服 Controller - SSC 32 上述代码是:

using System.IO.Ports;
using System.Threading;

namespace cConsoleAppMonitorServoCompletion
{
    class Program
    {
        static SerialPort _serialPort;

        static void Main(string[] args)
        {
            try
            {
                _serialPort = new SerialPort();
                _serialPort.PortName = "COM3";
                _serialPort.Open();
                _serialPort.Write("#27 P1600 S750\r");
                Console.WriteLine("#27 P1500 S750\r");
                string output;
                output = "";
                //Example: "Q <cr>" 
                //This will return a "." if the previous move is complete, or a "+" if it is still in progress. 
                while (!(output == ".")) //loop until you get back a period 
                {
                    _serialPort.Write("Q  \r");
                    output = _serialPort.ReadExisting();
                    Console.WriteLine(output);
                    Thread.Sleep(10);
                }
                _serialPort.Close();
            }
            catch (TimeoutException) { }
        }
    }
}

现在我想要像当我说红色而不是给出一个文本框时我想要得到串行命令,如 _serialPort.Write("#27 P1600 S750\r");

请帮助我尝试过,但没有成功,这是我的谦卑请求,请更详细地回答,我是一个刚刚起步的人,所以这对我来说很容易,提前谢谢。

最佳答案

使用语音识别控制机器人...对于初学者来说这是一个雄心勃勃的项目!这里可能有一百万个地方出了问题。

与编写代码的能力同样重要的是调试代码的能力。您能进一步告诉我们什么吗?哪些部分有效,哪些部分无效?您是否单步执行代码来查看发生了什么以及何时发生,以诊断哪里开始出错?

您还可以尝试一些调试输出 - 例如 Console.WriteLine - 这样我们就可以看到变量的状态和代码运行时的流程。

关于c# - 语音识别问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6651579/

相关文章:

c# - 是否可以在 C# 类库中创建 Windows 窗体?

java - 接口(interface)泛型重载

c# - 调用委托(delegate)时出现 NotImplementedException

android - 如何处理 ERROR_RECOGNIZER_BUSY

swift - 将 Microsoft Cognitive SpeechSDK 框架集成到 Swift 应用程序中

c# - String.StartsWith 在下一个字符是素数符号时不起作用 (char)697

c# - ColorAnimation 为滑动时的 listviewItem 颜色设置动画 - WP8.1

vb.net - 有哪些方法可以更新 Windows 窗体应用程序?

c# - 如何创建一个简单的 Windows 窗体来访问 SQL Server 数据库?

c# - 当默认音频输入更改时,.NET 中是否有事件?