用C# 编写一个串口调试助手,这里借鉴学习了B站杜洋工作室的学习视频,现将代码拿出来分享学习,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 串口调试助手0519
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
for (int i = 1; i < 20; i++)
{
comboBox1.Items.Add("COM" + i.ToString());
}
comboBox1.Text = "COM15";// 串口号默认值
comboBox2.Text = "115200";//波特率默认值
/*************重要****************/
serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);//委托一个串口接收方法
//上面这句也可以写到From1.Designer.cs里
//
//serialPort1
//
//this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
}
private void button1_Click(object sender, EventArgs e)
{
try //防止出错
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text, 10);//十进制数据转换,即十进制字符串转换为十进制数
serialPort1.Open();
button1.Enabled = false;//打开串口不可用
button2.Enabled = true;//关闭串口;
}
catch
{
MessageBox.Show("端口错误,请检查串口", "错误");
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
serialPort1.Close();//关闭串口
button1.Enabled = true;//打开串口按钮可用
button2.Enabled = false;//关闭串口按钮不可用
}
catch (Exception err)//一般情况下关闭串口不会出错,所以不需要加处理程序
{
}
}
private void button3_Click(object sender, EventArgs e)
{
byte[] Data = new byte[1];//只存储一个字节,单字节发送
if (serialPort1.IsOpen)//判断串口是否打开,如果打开执行下一步操作
{
if (textBox2.Text != "")
{
if (!radioButton1.Checked)//如果发送模式是字符模式
{
try
{
serialPort1.WriteLine(textBox2.Text);//写数据
}
catch
{
MessageBox.Show("串口数据写入错误", "错误");//出错提示
serialPort1.Close();
button1.Enabled = true;//打开串口按钮可用
button2.Enabled = false;//关闭串口按钮不可用
}
}
else
{
for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)//取余3运算作用是防止用户输入的字符为奇数个
{
Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);
serialPort1.Write(Data, 0, 1);//循环发送(如果输入字符为0A0BB,则只发送0A,0B)
}
if (textBox2.Text.Length % 2 != 0)//剩下一位单独处理
{
Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - 1, 1), 16);//单独发送B(0B)
serialPort1.Write(Data, 0, 1);//发送
}
}
}
}
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口数据接收事件,手动添加一个串口处理函数
{
if (!radioButton3.Checked)//如果接受模式为字符模式
{
string str = serialPort1.ReadExisting();//字符串方式读
textBox1.AppendText(str);//添加内容,等价于textBox1.Text += str
}
else
{ //如果接收模式为数据接收
byte data;
data = (byte)serialPort1.ReadByte();//此处需要强制转换类型,将(int)类型数据转换为(byte类型数据)
string str = Convert.ToString(data, 16).ToUpper();//转换为大写十六进制字符串
textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位补"0"
//上一句等同为:if(str.length==1)
// str="0"+str;
// else
// str=str;
// textBox1.AppendText("0x"+str);
}
}
}
}
为了方便对应其中一些控件的名称和属性,我把Form.Designer.cs拿出来,方便读者自己做的时候更好对应。
namespace 串口调试助手0519
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button3 = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.label4 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.label3 = new System.Windows.Forms.Label();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
this.panel2.SuspendLayout();
this.flowLayoutPanel1.SuspendLayout();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// button3
//
this.button3.Location = new System.Drawing.Point(440, 216);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(100, 43);
this.button3.TabIndex = 19;
this.button3.Text = "发送";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// panel2
//
this.panel2.Controls.Add(this.comboBox1);
this.panel2.Controls.Add(this.label1);
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.label2);
this.panel2.Controls.Add(this.button1);
this.panel2.Controls.Add(this.flowLayoutPanel1);
this.panel2.Controls.Add(this.label4);
this.panel2.Controls.Add(this.panel1);
this.panel2.Controls.Add(this.label3);
this.panel2.Controls.Add(this.comboBox2);
this.panel2.Location = new System.Drawing.Point(3, 12);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(222, 197);
this.panel2.TabIndex = 18;
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(83, 13);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 7;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(27, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 0;
this.label1.Text = "端口";
//
// button2
//
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(129, 152);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 35);
this.button2.TabIndex = 12;
this.button2.Text = "关闭端口";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(27, 51);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 1;
this.label2.Text = "波特率";
//
// button1
//
this.button1.Location = new System.Drawing.Point(26, 152);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 35);
this.button1.TabIndex = 11;
this.button1.Text = "打开端口";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Controls.Add(this.radioButton1);
this.flowLayoutPanel1.Controls.Add(this.radioButton2);
this.flowLayoutPanel1.Location = new System.Drawing.Point(83, 92);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(121, 20);
this.flowLayoutPanel1.TabIndex = 4;
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(3, 3);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(47, 16);
this.radioButton1.TabIndex = 2;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "数值";
this.radioButton1.UseVisualStyleBackColor = true;
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(56, 3);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(47, 16);
this.radioButton2.TabIndex = 3;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "字符";
this.radioButton2.UseVisualStyleBackColor = true;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(24, 129);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(53, 12);
this.label4.TabIndex = 10;
this.label4.Text = "接收模式";
//
// panel1
//
this.panel1.Controls.Add(this.radioButton4);
this.panel1.Controls.Add(this.radioButton3);
this.panel1.Location = new System.Drawing.Point(83, 118);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(121, 28);
this.panel1.TabIndex = 6;
//
// radioButton4
//
this.radioButton4.AutoSize = true;
this.radioButton4.Location = new System.Drawing.Point(56, 9);
this.radioButton4.Name = "radioButton4";
this.radioButton4.Size = new System.Drawing.Size(47, 16);
this.radioButton4.TabIndex = 1;
this.radioButton4.TabStop = true;
this.radioButton4.Text = "字符";
this.radioButton4.UseVisualStyleBackColor = true;
//
// radioButton3
//
this.radioButton3.AutoSize = true;
this.radioButton3.Location = new System.Drawing.Point(3, 9);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(47, 16);
this.radioButton3.TabIndex = 0;
this.radioButton3.TabStop = true;
this.radioButton3.Text = "数值";
this.radioButton3.UseVisualStyleBackColor = true;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(24, 95);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(53, 12);
this.label3.TabIndex = 9;
this.label3.Text = "发送模式";
//
// comboBox2
//
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Items.AddRange(new object[] {
"600",
"1200",
"2400",
"4800",
"9600",
"14400",
"19200",
"115200"});
this.comboBox2.Location = new System.Drawing.Point(83, 48);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(121, 20);
this.comboBox2.TabIndex = 8;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(3, 216);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox2.Size = new System.Drawing.Size(413, 43);
this.textBox2.TabIndex = 17;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(231, 11);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(309, 198);
this.textBox1.TabIndex = 16;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(562, 272);
this.Controls.Add(this.button3);
this.Controls.Add(this.panel2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = " 串口调试助手";
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.flowLayoutPanel1.ResumeLayout(false);
this.flowLayoutPanel1.PerformLayout();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.RadioButton radioButton4;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox comboBox2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
private System.IO.Ports.SerialPort serialPort1;
}
}