本帖最后由 robotStudio 于 2014-7-20 12:37 编辑
履带 4WD小车 轮式机器人 电脑鼠标键盘遥控软件
看上去界面布局还可以吧!!通过串口发送鼠标点击按钮对应的字母或按键盘上的9个字母键:Q,W,E,A,S,D,K,L,O。点哪个按钮或按哪个 字母键。其中显示方式选择和数据回显将在下一个版本实现。 开源才是王道,奉上源码供大家参考指正。 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 Control
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} /*************变量区*************/
SerialPort serialPort = null;
int[] baudNames = {2400,4800,9600,115200}; private void Form1_Load(object sender, EventArgs e)
{
serialPort = new SerialPort();//实例化一个serialPort对象
init();
/*---------按钮编辑状态----------*/
Btn_up.Enabled = serialPort.IsOpen;
Btn_right.Enabled = serialPort.IsOpen;
Btn_down.Enabled = serialPort.IsOpen;
Btn_left.Enabled = serialPort.IsOpen;
Btn_stop.Enabled = serialPort.IsOpen;
Street_up.Enabled = serialPort.IsOpen;
Street_down.Enabled = serialPort.IsOpen;
Btn_light.Enabled = serialPort.IsOpen;
Btn_hooter.Enabled = serialPort.IsOpen;
Btn_fire.Enabled = serialPort.IsOpen;
} /*********初始化*********/
private void init()
{
GetPortName();
this.CBx_baud.Text = "9600";//波特率默认值9600
foreach(int name in baudNames)
{
this.CBx_baud.Items.Add(name);
}
} /*********获得串口号*********/
private void GetPortName()
{
string[] portNames = SerialPort.GetPortNames();//得到系统可用串口号
CBx_port.Items.Clear();//清空串口列表框
foreach (string name in portNames)
{
this.CBx_port.Items.Add(name);//将得到的串口号添加到串口列表框
}
if (!CBx_port.Items.Contains(CBx_port.Text))
{
try
{
this.CBx_port.Text = (string)CBx_port.Items[0];
}
catch
{
this.CBx_port.Text ="com1";//默认串口9600
}
}
} /**********选择串口号**********/
private void CBx_port_SelectedIndexChanged(object sender, EventArgs e)
{
serialPort.PortName = this.CBx_port.Text;//赋值串口号
} /**********选择波特率**********/
private void CBx_baud_SelectedIndexChanged(object sender, EventArgs e)
{
serialPort.BaudRate = Convert.ToInt32(this.CBx_baud.Text);//赋值波特率
} /**********端口操作按钮**********/
private void Btn_connect_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen)
{
Btn_connect.Text = "打开";
serialPort.Close();//关闭串口
}
else
{
try
{
serialPort.Open();//开打串口
Btn_connect.Text = "断开";
}
catch
{
MessageBox.Show("串口不存在");
}
}
/*---------按钮编辑状态----------*/
Btn_up.Enabled = serialPort.IsOpen;
Btn_right.Enabled = serialPort.IsOpen;
Btn_down.Enabled = serialPort.IsOpen;
Btn_left.Enabled = serialPort.IsOpen;
Btn_stop.Enabled = serialPort.IsOpen;
Street_up.Enabled = serialPort.IsOpen;
Street_down.Enabled = serialPort.IsOpen;
Btn_light.Enabled = serialPort.IsOpen;
Btn_hooter.Enabled = serialPort.IsOpen;
Btn_fire.Enabled = serialPort.IsOpen;
}
/**********指令发送**********/
private void Btn_up_Click(object sender, EventArgs e)
{
serialPort.Write("W");//前进
}
private void Btn_right_Click(object sender, EventArgs e)
{
serialPort.Write("D");//右转
}
private void Btn_down_Click(object sender, EventArgs e)
{
serialPort.Write("S");//后退
}
private void Btn_left_Click(object sender, EventArgs e)
{
serialPort.Write("A");//左转
}
private void Btn_stop_Click(object sender, EventArgs e)
{
serialPort.Write("stop");//停止
}
private void Street_up_Click(object sender, EventArgs e)
{
serialPort.Write("Q");//舵机上
} private void Street_down_Click(object sender, EventArgs e)
{
serialPort.Write("E");//舵机下
} private void Btn_light_Click(object sender, EventArgs e)
{
if (Btn_light.Text.Equals("开灯(K)"))
{
Btn_light.Text = "关灯(K)";
}
else
{
Btn_light.Text = "开灯(K)";
}
serialPort.Write("K");//开灯
} private void Btn_hooter_Click(object sender, EventArgs e)
{
serialPort.Write("L");//鸣笛
} private void Btn_fire_Click(object sender, EventArgs e)
{
serialPort.Write("O");//开炮
}
/**********键按下**********/
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W) { Btn_up.PerformClick(); Btn_up.BackColor = Color.GreenYellow; }
else if (e.KeyCode == Keys.D) { Btn_right.PerformClick(); Btn_right.BackColor = Color.GreenYellow; }
else if (e.KeyCode == Keys.S) { Btn_down.PerformClick(); Btn_down.BackColor = Color.GreenYellow; }
else if (e.KeyCode == Keys.A) { Btn_left.PerformClick(); Btn_left.BackColor = Color.GreenYellow; }
else if (e.KeyCode == Keys.Q) { Street_up.PerformClick(); Street_up.BackColor = Color.GreenYellow; }
else if (e.KeyCode == Keys.E) { Street_down.PerformClick(); Street_down.BackColor = Color.GreenYellow; }
else if (e.KeyCode == Keys.K) { Btn_light.PerformClick(); Btn_light.BackColor = Color.GreenYellow; }
else if (e.KeyCode == Keys.L) { Btn_hooter.PerformClick(); Btn_hooter.BackColor = Color.GreenYellow; }
else if (e.KeyCode == Keys.O) { Btn_fire.PerformClick(); Btn_fire.BackColor = Color.GreenYellow; }
}
/**********键抬起**********/
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W) { Btn_stop.PerformClick(); Btn_up.BackColor =Color.White; }
else if (e.KeyCode == Keys.D) { Btn_stop.PerformClick(); Btn_right.BackColor = Color.White; }
else if (e.KeyCode == Keys.S) { Btn_stop.PerformClick(); Btn_down.BackColor = Color.White; }
else if (e.KeyCode == Keys.A) { Btn_stop.PerformClick(); Btn_left.BackColor = Color.White; }
else if (e.KeyCode == Keys.Q) { Street_up.BackColor = Color.White; }
else if (e.KeyCode == Keys.E) { Street_down.BackColor = Color.White; }
else if (e.KeyCode == Keys.K) { Btn_light.BackColor = Color.White; }
else if (e.KeyCode == Keys.L) { Btn_hooter.BackColor = Color.White; }
else if (e.KeyCode == Keys.O) { Btn_fire.BackColor = Color.White; }
}
}
} 工程文件: |