语音识别问题

发布于 2024-11-19 14:26:49 字数 2898 浏览 2 评论 0原文

我是一个初学者,最初我的主要目标是使用语音控制机器人。最初,我开始用这段代码为我的演讲制作语法,我什至成功了,我的代码是这样的,我在 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"); 这样的串行命令;

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

I am a starter who is stuck very badly on this initially my main aim is to control robots using speech. Initially I started with making grammar for my speech with this code I was even successful my code is this I made this in windows form application:

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;
    }

Now from this code when I speak red , I get red in message box now I want to control motors therefore i need to communicate with my robots therefore i MADE ONE CONSOLE APPLICATION from help from internet FOR SENDING DATA TO MY SERVO CONTROLLER -SSC 32 THE CODE FOR ABOVE IS:

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) { }
        }
    }
}

Now I want like when I speak red instead of giving a text box I want get serial command like _serialPort.Write("#27 P1600 S750\r");

Please help I have tried but I was not successful , it is my humble request please answer in more detailed manner , I am a just starter so it will be easy for me thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轻拂→两袖风尘 2024-11-26 14:26:49

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

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

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

Controlling a robot using voice recognition... an ambitious project for a starter! There could be a million things going wrong here.

Just as important as the ability to write code is the ability to debug it. What can you tell us further - which parts work, which parts don't? Have you single-stepped through the code to see what happens and when, to diagnose where things start to go wrong?

You could also try some debugging output - Console.WriteLine for example - so we you can see the state of variables and flow of the code as it's running.

寄居人 2024-11-26 14:26:49

看来您需要使用 System.Diagnostics.Process.Start

此页面有一个示例 - 如何从 Windows 窗体执行控制台应用程序?

// SpeechRecognized 事件的简单处理程序。
私人无效 sr_SpeechRecognized(对象发送者,SpeechRecognizedEventArgs e)
{
System.Diagnostics.Process.Start( @"cmd.exe", @"/kc:\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;
    }

It looks like you need to use System.Diagnostics.Process.Start

This page has an example - 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" );
}

An ambitious starter project indeed!

Update

    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;
    }
dawn曙光 2024-11-26 14:26:49

如果现在我想要像当我说红色而不是给出一个文本框时我想要获取串行命令意味着 - 只是_serialPort.Write("#27 P1600 S750\r"); code> 而不是显示消息框(即 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) { }

        }

ps
如果您不明白 SerialPort Class 的工作原理,请转到 MSDN

if Now I want like when I speak red instead of giving a text box I want get serial command means - just to _serialPort.Write("#27 P1600 S750\r"); instead of showing messagebox (i.e. MessageBox.Show(e.Result.Text);) then task is really simple. just copy-paste that code. and add using System.IO.Ports; so that u can work with ports.

so prolly ur code will look like this:

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.
if you don't understand how SerialPort Class works go to MSDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文