基本信息
源码名称:C#串口调试助手(源码)
源码大小:0.26M
文件格式:.zip
开发语言:C#
更新时间:2018-09-16
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
串口调试助手 VS开发,C#串口编程实例
串口调试助手 VS开发,C#串口编程实例
public partial class Form1 : Form
{
private StreamReader sRead;
public int iTextbox2 = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] str = SerialPort.GetPortNames();
if (str == null)
{
MessageBox.Show("本机没有串口!", "Error");
return;
}
comboBox1.Items.AddRange(str);
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 5;
comboBox3.SelectedIndex = 3;
comboBox4.SelectedIndex = 0;
this.toolStripStatusLabel1.Text = "端口号:端口未打开";
this.toolStripStatusLabel2.Text = "波特率:端口未打开";
this.toolStripStatusLabel3.Text = "数据位:端口未打开";
this.toolStripStatusLabel4.Text = "停止位:端口未打开";
}
private void button1_Click(object sender, EventArgs e)
{
String str1 = comboBox1.Text;
String str2 = comboBox2.Text;
String str3 = comboBox3.Text;
String str4 = comboBox4.Text;
Int32 int2 = Convert.ToInt32(str2);
Int32 int3 = Convert.ToInt32(str3);
try
{
if (str1 == null)
{
MessageBox.Show("请先选择串口!", "Error");
return;
}
serialPort1.PortName = str1;
serialPort1.BaudRate = int2;
serialPort1.DataBits = int3;
switch (comboBox4.Text)
{
case "1":
serialPort1.StopBits = StopBits.One;
break;
case "1.5":
serialPort1.StopBits = StopBits.OnePointFive;
break;
case "2":
serialPort1.StopBits = StopBits.Two;
break;
default:
MessageBox.Show("Error:参数不正确", "Error");
break;
}
if (serialPort1.IsOpen == true)
{
serialPort1.Close();
}
serialPort1.Open();
MessageBox.Show("串口打开成功!", str1);
this.toolStripStatusLabel1.Text = "端口号:" serialPort1.PortName " ";
this.toolStripStatusLabel2.Text = "波特率:" serialPort1.BaudRate " ";
this.toolStripStatusLabel3.Text = "数据位:" serialPort1.DataBits " ";
this.toolStripStatusLabel4.Text = "停止位:" serialPort1.StopBits " ";
button1.Enabled = false;
comboBox1.Enabled = false;
comboBox2.Enabled = false;
comboBox3.Enabled = false;
comboBox4.Enabled = false;
}
catch(Exception er)
{
MessageBox.Show("Error:" er.Message, "Error");
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
comboBox1.Enabled = true;
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
serialPort1.Close();
this.toolStripStatusLabel1.Text = "端口号:端口未打开";
this.toolStripStatusLabel2.Text = "波特率:端口未打开";
this.toolStripStatusLabel3.Text = "数据位:端口未打开";
this.toolStripStatusLabel4.Text = "停止位:端口未打开";
}