接线图就不需要了吧,哈很简单
直接上代码吧
上位机: - 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.IO.Ports;
- namespace pc
- {
- public partial class Form1 : Form
- {
- private StringBuilder builder = new StringBuilder();
- private SerialPort comm = new SerialPort();
- private string[] commarr = null;
- private int commmaxlen = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void SerialInit()
- {
- string[] ports = SerialPort.GetPortNames();
- Array.Sort(ports);
- comboPortName.Items.AddRange(ports);
- comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;
- comboBaudrate.SelectedIndex = comboBaudrate.Items.IndexOf("9600");
- comm.RtsEnable = true;
- comm.DataReceived += comm_DataReceived;
- }
- void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- int n = comm.BytesToRead;
- byte[] buf = new byte[n];
- comm.Read(buf, 0, n);
- builder.Remove(0, builder.Length);
- builder.Append(Encoding.ASCII.GetString(buf));
- this.Invoke((EventHandler)(delegate
- {
- txGet.AppendText(builder.ToString());
- txGet.ScrollToCaret();
- }));
- }
- private void buttonSend_Click(object sender, EventArgs e)
- {
- if (comm.IsOpen)
- {
- int n = 0;
- txGet.AppendText("-->" + txSend.Text + "\r\n");
- txGet.ScrollToCaret();
- if (newline_cb.Checked)
- {
- comm.Write(txSend.Text + "\r\n");
- }
- else
- {
- comm.Write(txSend.Text);
- }
- n = txSend.Text.Length;
- txSend.Clear();
- }
- else
- {
- MessageBox.Show("请先打开端口");
- }
- }
- private void buttonOpenClose_Click(object sender, EventArgs e)
- {
- if (comm.IsOpen)
- {
- comm.Close();
- }
- else
- {
- commarr = comm_tb.Lines;
- if (commarr.Length == 0) { MessageBox.Show("请先设置语音指令"); return; }
- foreach (string tstr in commarr)
- {
- if (tstr.IndexOf('@') > commmaxlen) commmaxlen = tstr.IndexOf('@');
- }
- try
- {
- comm.PortName = comboPortName.Text;
- comm.BaudRate = int.Parse(comboBaudrate.Text);
- comm.Open();
- }
- catch (Exception ex)
- {
- comm = new SerialPort();
- MessageBox.Show(ex.Message);
- return;
- }
- }
- buttonOpenClose.Text = comm.IsOpen ? "关闭" : "打开";
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- SerialInit();
- timer1.Enabled = true;
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (comm.IsOpen)
- {
- comm.Close();
- }
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (comm.IsOpen)
- {
- portstatus_lbl.Text = "已连接...";
- portstatus_lbl.ForeColor = Color.Blue;
- txSend.Focus();
- }
- else
- {
- portstatus_lbl.Text = "未连接";
- portstatus_lbl.ForeColor = Color.Red;
- }
- // movesend();
- }
- private void txSend_TextChanged(object sender, EventArgs e)
- {
- if (comm.IsOpen)
- {
- string commstr = txSend.Text.Trim();
- if (string.IsNullOrEmpty(commstr)) return;
- if (commarr.Length == 0) { MessageBox.Show("请先设置语音指令"); return; }
- commstr = commstr + "@";
- string sendtxt = "";
- foreach (string tstr in commarr)
- {
- if (tstr.StartsWith(commstr))
- {
- sendtxt = tstr.Replace(commstr, "").Trim();
- break;
- }
- }
- if (sendtxt.Length>0)
- {
- comm.Write(sendtxt);// + "\r\n"
- txGet.AppendText("-->" + sendtxt + "\r\n");
- txGet.ScrollToCaret();
- sendtxt = "";
- txSend.Clear();
- }
- else
- {
- if (commstr.Length-1 > commmaxlen)
- {
- txSend.Clear();
- }
- }
- }
- else
- {
- MessageBox.Show("请先打开端口");
- }
- }
- private void comm_tb_TextChanged(object sender, EventArgs e)
- {
- commarr = comm_tb.Lines;
- foreach (string tstr in commarr)
- {
- if (tstr.IndexOf('@') - 1 > commmaxlen) commmaxlen = tstr.IndexOf('@') - 1;
- }
- }
- }
- }
复制代码
Arduino代码 - String sdata="";
- String mode="out";
- void setup()
- {
- Serial.begin(9600);
- pinMode(12,OUTPUT);
- digitalWrite(12,LOW);
- Serial.println("start...");
- }
- void loop()
- {
- while (Serial.available() > 0)
- {
- sdata += char(Serial.read());
- delay(2);
- }
- if(sdata.length()>0)
- {
- mode=sdata;
- sdata="";
- Serial.print(">--");
- Serial.println(mode);
- }
- modeshow();
- }
- void modeshow()
- {
- if(mode=="light")
- {
- digitalWrite(12,HIGH);
- }
- else if(mode=="blink")
- {
- digitalWrite(12,HIGH);
- delay(100);
- digitalWrite(12,LOW);
- delay(100);
- }
- else if(mode=="out")
- {
- digitalWrite(12,LOW);
- }
- }
复制代码
在控制面板里,打开win7的语音识别功能,就可以啦,右边编辑框里输入好语音指令就可以测试了。 |