-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
197 lines (183 loc) · 6.63 KB
/
Form1.cs
File metadata and controls
197 lines (183 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.IO.Ports;
namespace SimpleSpeechReco
{
public partial class Form1 : Form
{
//新建一个语音识别引擎实例
private SpeechRecognitionEngine SRE;
//实例化串口对象(默认:COM1,9600,e,8,1)
SerialPort SPort = new SerialPort();
public Form1()
{
InitializeComponent();
}
//语音识别事件处理函数
void G_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string text = e.Result.Text;
byte[] bytesend = { 0 };
switch (text)
{
case "向左翻":
BackColor = Color.Blue;
bytesend[0] = 0x8F;
WritePort(bytesend);
labelStateShow.Text = "执行" + text + "命令";
break;
case "向右翻":
BackColor = Color.Green;
bytesend[0] = 0x0F;
WritePort(bytesend);
labelStateShow.Text = "执行" + text + "命令";
break;
case "停止翻":
BackColor = Color.Gray;
bytesend[0] = 0;
WritePort(bytesend);
labelStateShow.Text = "执行" + text + "命令";
break;
case "停止翻书":
BackColor = Color.Gray;
bytesend[0] = 0;
WritePort(bytesend);
labelStateShow.Text = "执行" + text + "命令";
break;
/*case "おはようございます":
BackColor = Color.Gray;
bytesend[0] = 0x8F;
WritePort(bytesend);
labelStateShow.Text = "执行" + text + "命令";
break;
case "こんにちは":
BackColor = Color.Gray;
bytesend[0] = 0x0F;
WritePort(bytesend);
labelStateShow.Text = "执行" + text + "命令";
break;
default:
labelStateShow.Text = "您在说什么?";
break;*/
}
}
//写端口函数,把bytesend写到串口
private void WritePort(byte[] bytesend)
{
if (SPort.IsOpen == false)
{
SPort.Open();
}
SPort.Write(bytesend, 0, bytesend.Length);
SPort.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
//初始化串口
InitializeSerialPort();
//初始化语音识别引擎
InitializeSpeechEngine();
}
private void rdoVoiceControl_CheckedChanged(object sender, EventArgs e)
{
btnLeft.Enabled = false;
btnRight.Enabled = false;
btnStop.Enabled = false;
btnVoiceControl.Enabled = true;
btnVoiceControl.Text = "开启语音控制";
//异步调用识别引擎,且允许多次调用
//SRE.RecognizeAsync(RecognizeMode.Multiple);
}
private void rdoHandControl_CheckedChanged(object sender, EventArgs e)
{
btnLeft.Enabled = true;
btnRight.Enabled = true;
btnStop.Enabled = true;
btnVoiceControl.Enabled = false;
btnVoiceControl.Text = "语音控制";
}
private void btnLeft_Click(object sender, EventArgs e)
{
byte[] bytesend = { 0x8F };
if (txtYeShu.Text == "")
{
bytesend[0] = 0x8F;
}
else
{
bytesend[0] = Convert.ToByte(txtYeShu.Text);
}
WritePort(bytesend);
labelStateShow.Text = "执行向左翻命令";
//MessageBox.Show("向左翻");
}
private void btnRight_Click(object sender, EventArgs e)
{
byte[] bytesend = { 0x0F };
if (txtYeShu.Text == "")
{
bytesend[0] = 0x0F;
}
else
{
bytesend[0] = Convert.ToByte(Convert.ToInt32(txtYeShu.Text) + 128);
}
WritePort(bytesend);
labelStateShow.Text = "执行向右翻命令";
//MessageBox.Show("向右翻");
}
private void InitializeSerialPort()
{
//更改参数
SPort.PortName = "COM3";
SPort.BaudRate = 9600;
SPort.Parity = Parity.None;
SPort.DataBits = 8;
SPort.StopBits = StopBits.One;
SPort.ReadBufferSize = 4096;
}
private void InitializeSpeechEngine()
{
SRE = new SpeechRecognitionEngine();
SRE.SetInputToDefaultAudioDevice();
GrammarBuilder GB = new GrammarBuilder();
//GB.Append("向");
GB.Append(new Choices(new string[] { "向左翻", "向右翻", "停止翻", "停止翻书" }));
Grammar G = new Grammar(GB);
G.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(G_SpeechRecognized);
SRE.LoadGrammar(G);
}
private void btnVoiceControl_Click(object sender, EventArgs e)
{
if (rdoVoiceControl.Enabled == true)
{
//异步调用识别引擎,且允许多次调用
SRE.RecognizeAsync(RecognizeMode.Multiple);
btnVoiceControl.Text = "关闭语音控制";
rdoVoiceControl.Enabled = false;
rdoHandControl.Enabled = false;
}
else
{
//停止语音识别
SRE.RecognizeAsyncCancel();
rdoVoiceControl.Enabled = true;
rdoHandControl.Enabled = true;
btnVoiceControl.Text = "开启语音控制";
}
}
private void btnStop_Click(object sender, EventArgs e)
{
byte[] bytesend = { 0 };
WritePort(bytesend);
labelStateShow.Text = "执行停止翻书命令";
}
}
}