前面已经对串口编程进行了简单的介绍
下面是一个串口调试程序,主要功能:
a. 串口参数可以自动选择,并可以保存配置下次自动匹配(.ini文件存储)
b.可以手动开关串口
c.可以选择发送数据的格式
d.可以循环发送数据
e.发送数据可以选择是单字符串、多字符串发送,甚至可以导入发送的命令
f.接收数据
...
界面如下:
在串口配置区域 参数都已经输入进去了,只需要在通信的时候自行选择
代码展示:
// 窗体加载
private void FrmMain_Load(object sender, EventArgs e)
{
//如果有配置 则读取配置文件
if (File.Exists(Application.StartupPath + @"\SerConfig.ini"))
{
using (StreamReader sr = new StreamReader(Application.StartupPath + @"\SerConfig.ini", Encoding.UTF8))
{
comboBox1.SelectedItem = sr.ReadLine();
comboBox2.SelectedItem = sr.ReadLine();
comboBox3.SelectedItem = sr.ReadLine();
comboBox4.SelectedItem = sr.ReadLine();
comboBox5.SelectedItem = sr.ReadLine();
}
}
else
{
comboBox1.SelectedIndex = 2;
comboBox2.SelectedIndex = 6;
comboBox3.SelectedIndex = 0;
comboBox4.SelectedIndex = 0;
comboBox5.SelectedIndex = 0;
}
FrmMain.CheckForIllegalCrossThreadCalls = false;
}
// 打开串口
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "打开串口")
{
//打开串口
try
{
serialPort1.PortName = comboBox1.SelectedItem.ToString();
serialPort1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem.ToString());
switch (comboBox3.SelectedIndex)
{
case 0:
serialPort1.Parity = Parity.None;
break;
case 1:
serialPort1.Parity = Parity.Odd;
break;
case 2:
serialPort1.Parity = Parity.Even;
break;
case 3:
serialPort1.Parity = Parity.Mark;
break;
case 4:
serialPort1.Parity = Parity.Space;
break;
}
serialPort1.DataBits = Convert.ToInt32(comboBox4.SelectedItem.ToString());
switch (comboBox5.SelectedIndex)
{
case 0:
serialPort1.StopBits = StopBits.One;
break;
case 1:
serialPort1.StopBits = StopBits.Two;
break;
case 2:
serialPort1.StopBits = StopBits.OnePointFive;
break;
}
serialPort1.Open();
panel1.BackColor = Color.LawnGreen;
comboBox1.Enabled = false;
comboBox2.Enabled = false;
comboBox3.Enabled = false;
comboBox4.Enabled = false;
comboBox5.Enabled = false;
tssl01.Text = "串口" + comboBox1.SelectedItem.ToString() + "已经打开";
button1.Text = "关闭串口";
}
catch (Exception)
{
MessageBox.Show("串口打开失败" + "\n" + "1.请检查配置的参数是否正确" + "\n" + "2.外围设备是否没有连接" + "\n" + "3.串口是否已经打开或被占用" + "\n" + "4.串口驱动是否没有安装");
}
}
else
{
serialPort1.Close();
panel1.BackColor = Color.Red;
comboBox1.Enabled = true;
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
comboBox5.Enabled = true;
tssl01.Text = "串口" + comboBox1.SelectedItem.ToString() + "已经关闭";
button1.Text = "打开串口";
}
}
// 发送数据
private void button5_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
if (button5.Text == "开始发送")
{
if (checkBox1.Checked == true)
{
type = 1;
timer1.Interval = Convert.ToInt32(numericUpDown1.Value) * 1000;
timer1.Enabled = true;
button1.Enabled = false;
}
else
{
TimerAction.timerAction(this, type);
}
}
else if (button5.Text == "停止发送")
{
timer1.Enabled = false;
button1.Enabled = true;
textBox1.Text = "";
groupBox2.Enabled = true;
button5.Text = "开始发送";
}
}
else
{
timer1.Enabled = false;
groupBox2.Enabled = true;
textBox1.Text = "";
button5.Text = "开始发送";
MessageBox.Show("请先打开串口 再发送数据");
}
}
// 接收数据
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
string text = string.Empty;
byte[] result = new byte[serialPort1.BytesToRead];
receivedNum += serialPort1.Read(result, 0, serialPort1.BytesToRead);
// 判断是否显示为十六进制
if (this.checkBox2.CheckState == CheckState.Checked)
{
foreach (byte b in result)
{
text = text + Convert.ToString(b, 16);
}
textBox1.Text += text + " ";
}
else
{
textBox1.Text += Encoding.ASCII.GetString(result);
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
主要功能代码已展示
程序源码下载
谢谢耐心看完
下一篇简单介绍二进制协议解析