语音识别问题

时间:2011-07-11 14:23:50

标签: 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;
    }

现在从这段代码中,当我说红色时,我在消息框中显示红色,因此我想控制电机因此我需要与我的机器人进行通信,因此我从互联网的帮助中制作了一个控制台应用程序,用于向我的伺服控制器发送数据 - 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");这样的串行命令

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

3 个答案:

答案 0 :(得分:2)

使用语音识别控制机器人......一个雄心勃勃的启动项目!这里可能有一百万个问题。

与编写代码的能力同样重要的是调试它的能力。你还能告诉我们什么 - 哪些部分有用,哪些部分没有?您是否已经单步执行代码以查看发生了什么以及何时,以诊断事情何时开始出错?

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

答案 1 :(得分:2)

您似乎需要使用 System.Diagnostics.Process.Start

此页面有一个示例 - how to execute console application from windows form?

// Simple handler for the SpeechRecognized event. private void sr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { System.Diagnostics.Process.Start( @"cmd.exe", @"/k c:\path\my.exe" ); }

确实是一个雄心勃勃的入门项目!

<强>更新

    private bool LaunchApp(String sAppPath, String sArg)
    {
        bool bSuccess = false;

        try
        {
            //create a new process
            Process myApp = new Process();
            myApp.StartInfo.FileName = sAppPath;
            myApp.StartInfo.Arguments = sArg;
            bSuccess = myApp.Start();
        }
        catch (Win32Exception e)
        {
            MessageBox.Show("Error Details: {0}", e.Message);
        }

        return bSuccess;
    }

答案 2 :(得分:1)

如果Now I want like when I speak red instead of giving a text box I want get serial command表示 - 只是_serialPort.Write("#27 P1600 S750\r");而不是显示消息框(即MessageBox.Show(e.Result.Text);),那么任务非常简单。只需复制粘贴该代码即可。并添加using System.IO.Ports;以便您可以使用端口。

如此多,你的代码看起来像这样:

private void sr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            //MessageBox.Show(e.Result.Text);
            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) { }

        }

P.S。 如果您不理解SerialPort Class的工作方式,请转到MSDN