串口通信(4)-C#串口通信入门实例

本文通过实例讲解C#串口通信。

入门实例设计一个串口助手,能够很好的涵盖串口要点的使用。

目录

一、成品图

 二、界面文件

三、后台代码

四、实例中要点

一、成品图

如下:

实现的过程

创建winform项目,将Form1文件的名称改为MainForm,在主窗体中添加文本控件,选框控件,按钮等控件。以及SerialPort控件,该控件不在窗体中显示。

给控件更改名称

所有控件可以从文件大纲里面查看

设置控件的属性,添加事件。

SerialPort控件的属性如下,把读写的超时时间由默认-1改为1000。

 二、界面文件

 

MainForm.Designer.cs
namespace SerialPortDemo
{partial class MainForm{/// <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();System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));this.groupBox1 = new System.Windows.Forms.GroupBox();this.btnOpen = new System.Windows.Forms.Button();this.cmbParity = new System.Windows.Forms.ComboBox();this.cmbDataBits = new System.Windows.Forms.ComboBox();this.cmbStopBits = new System.Windows.Forms.ComboBox();this.cmbBaudRate = new System.Windows.Forms.ComboBox();this.cmbPortName = new System.Windows.Forms.ComboBox();this.label5 = new System.Windows.Forms.Label();this.label4 = new System.Windows.Forms.Label();this.label3 = new System.Windows.Forms.Label();this.label2 = new System.Windows.Forms.Label();this.label1 = new System.Windows.Forms.Label();this.spt = new System.IO.Ports.SerialPort(this.components);this.txtRead = new System.Windows.Forms.TextBox();this.chk16Read = new System.Windows.Forms.CheckBox();this.btnClearRead = new System.Windows.Forms.Button();this.btnSend = new System.Windows.Forms.Button();this.btnClearWrite = new System.Windows.Forms.Button();this.chk16Write = new System.Windows.Forms.CheckBox();this.txtWrite = new System.Windows.Forms.TextBox();this.groupBox2 = new System.Windows.Forms.GroupBox();this.groupBox3 = new System.Windows.Forms.GroupBox();this.label6 = new System.Windows.Forms.Label();this.chkReadAutoWrap = new System.Windows.Forms.CheckBox();this.chkWriteAutoWrap = new System.Windows.Forms.CheckBox();this.groupBox1.SuspendLayout();this.groupBox2.SuspendLayout();this.groupBox3.SuspendLayout();this.SuspendLayout();// // groupBox1// this.groupBox1.Controls.Add(this.btnOpen);this.groupBox1.Controls.Add(this.cmbParity);this.groupBox1.Controls.Add(this.cmbDataBits);this.groupBox1.Controls.Add(this.cmbStopBits);this.groupBox1.Controls.Add(this.cmbBaudRate);this.groupBox1.Controls.Add(this.cmbPortName);this.groupBox1.Controls.Add(this.label5);this.groupBox1.Controls.Add(this.label4);this.groupBox1.Controls.Add(this.label3);this.groupBox1.Controls.Add(this.label2);this.groupBox1.Controls.Add(this.label1);this.groupBox1.Location = new System.Drawing.Point(10, 13);this.groupBox1.Margin = new System.Windows.Forms.Padding(4);this.groupBox1.Name = "groupBox1";this.groupBox1.Padding = new System.Windows.Forms.Padding(4);this.groupBox1.Size = new System.Drawing.Size(212, 242);this.groupBox1.TabIndex = 0;this.groupBox1.TabStop = false;this.groupBox1.Text = "串口配置";// // btnOpen// this.btnOpen.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.btnOpen.Location = new System.Drawing.Point(11, 203);this.btnOpen.Margin = new System.Windows.Forms.Padding(0);this.btnOpen.Name = "btnOpen";this.btnOpen.Size = new System.Drawing.Size(187, 35);this.btnOpen.TabIndex = 11;this.btnOpen.Text = "打开串口";this.btnOpen.UseVisualStyleBackColor = true;this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);// // cmbParity// this.cmbParity.FormattingEnabled = true;this.cmbParity.Items.AddRange(new object[] {"无","奇校验","偶校验"});this.cmbParity.Location = new System.Drawing.Point(79, 167);this.cmbParity.Margin = new System.Windows.Forms.Padding(4);this.cmbParity.Name = "cmbParity";this.cmbParity.Size = new System.Drawing.Size(117, 23);this.cmbParity.TabIndex = 10;// // cmbDataBits// this.cmbDataBits.FormattingEnabled = true;this.cmbDataBits.Items.AddRange(new object[] {"8","7","6","5"});this.cmbDataBits.Location = new System.Drawing.Point(79, 135);this.cmbDataBits.Margin = new System.Windows.Forms.Padding(4);this.cmbDataBits.Name = "cmbDataBits";this.cmbDataBits.Size = new System.Drawing.Size(117, 23);this.cmbDataBits.TabIndex = 9;// // cmbStopBits// this.cmbStopBits.FormattingEnabled = true;this.cmbStopBits.Items.AddRange(new object[] {"1","1.5","2"});this.cmbStopBits.Location = new System.Drawing.Point(79, 103);this.cmbStopBits.Margin = new System.Windows.Forms.Padding(4);this.cmbStopBits.Name = "cmbStopBits";this.cmbStopBits.Size = new System.Drawing.Size(117, 23);this.cmbStopBits.TabIndex = 8;// // cmbBaudRate// this.cmbBaudRate.FormattingEnabled = true;this.cmbBaudRate.Items.AddRange(new object[] {"1382400","921600","460800","256000","230400","128000","115200","76800","57600","43000","38400","19200","14400","9600","4800","1200"});this.cmbBaudRate.Location = new System.Drawing.Point(79, 69);this.cmbBaudRate.Margin = new System.Windows.Forms.Padding(4);this.cmbBaudRate.Name = "cmbBaudRate";this.cmbBaudRate.Size = new System.Drawing.Size(117, 23);this.cmbBaudRate.TabIndex = 7;// // cmbPortName// this.cmbPortName.FormattingEnabled = true;this.cmbPortName.Location = new System.Drawing.Point(79, 38);this.cmbPortName.Margin = new System.Windows.Forms.Padding(4);this.cmbPortName.Name = "cmbPortName";this.cmbPortName.Size = new System.Drawing.Size(117, 23);this.cmbPortName.TabIndex = 6;// // label5// this.label5.AutoSize = true;this.label5.Location = new System.Drawing.Point(8, 170);this.label5.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);this.label5.Name = "label5";this.label5.Size = new System.Drawing.Size(60, 15);this.label5.TabIndex = 4;this.label5.Text = "校验位:";// // label4// this.label4.AutoSize = true;this.label4.Location = new System.Drawing.Point(8, 106);this.label4.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);this.label4.Name = "label4";this.label4.Size = new System.Drawing.Size(60, 15);this.label4.TabIndex = 3;this.label4.Text = "停止位:";// // label3// this.label3.AutoSize = true;this.label3.Location = new System.Drawing.Point(8, 138);this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);this.label3.Name = "label3";this.label3.Size = new System.Drawing.Size(60, 15);this.label3.TabIndex = 2;this.label3.Text = "数据位:";// // label2// this.label2.AutoSize = true;this.label2.Location = new System.Drawing.Point(8, 72);this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(60, 15);this.label2.TabIndex = 1;this.label2.Text = "波特率:";// // label1// this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(8, 41);this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(60, 15);this.label1.TabIndex = 0;this.label1.Text = "端口号:";// // spt// this.spt.ReadTimeout = 1000;this.spt.WriteTimeout = 1000;this.spt.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.spt_DataReceived);// // txtRead// this.txtRead.BackColor = System.Drawing.Color.White;this.txtRead.ForeColor = System.Drawing.Color.Black;this.txtRead.Location = new System.Drawing.Point(244, 38);this.txtRead.Margin = new System.Windows.Forms.Padding(4);this.txtRead.Multiline = true;this.txtRead.Name = "txtRead";this.txtRead.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;this.txtRead.Size = new System.Drawing.Size(652, 280);this.txtRead.TabIndex = 1;// // chk16Read// this.chk16Read.AutoSize = true;this.chk16Read.Location = new System.Drawing.Point(28, 38);this.chk16Read.Margin = new System.Windows.Forms.Padding(4);this.chk16Read.Name = "chk16Read";this.chk16Read.Size = new System.Drawing.Size(105, 19);this.chk16Read.TabIndex = 2;this.chk16Read.Text = "16进制接收";this.chk16Read.UseVisualStyleBackColor = true;// // btnClearRead// this.btnClearRead.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.btnClearRead.Location = new System.Drawing.Point(244, 324);this.btnClearRead.Margin = new System.Windows.Forms.Padding(0);this.btnClearRead.Name = "btnClearRead";this.btnClearRead.Size = new System.Drawing.Size(120, 35);this.btnClearRead.TabIndex = 3;this.btnClearRead.Text = "清除接收";this.btnClearRead.UseVisualStyleBackColor = true;this.btnClearRead.Click += new System.EventHandler(this.btnClearRead_Click);// // btnSend// this.btnSend.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.btnSend.Location = new System.Drawing.Point(780, 543);this.btnSend.Margin = new System.Windows.Forms.Padding(0);this.btnSend.Name = "btnSend";this.btnSend.Size = new System.Drawing.Size(120, 35);this.btnSend.TabIndex = 5;this.btnSend.Text = "发送";this.btnSend.UseVisualStyleBackColor = true;this.btnSend.Click += new System.EventHandler(this.btnSend_Click);// // btnClearWrite// this.btnClearWrite.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.btnClearWrite.Location = new System.Drawing.Point(776, 328);this.btnClearWrite.Margin = new System.Windows.Forms.Padding(0);this.btnClearWrite.Name = "btnClearWrite";this.btnClearWrite.Size = new System.Drawing.Size(120, 35);this.btnClearWrite.TabIndex = 6;this.btnClearWrite.Text = "清除发送";this.btnClearWrite.UseVisualStyleBackColor = true;this.btnClearWrite.Click += new System.EventHandler(this.btnClearWrite_Click);// // chk16Write// this.chk16Write.AutoSize = true;this.chk16Write.Location = new System.Drawing.Point(28, 50);this.chk16Write.Margin = new System.Windows.Forms.Padding(4);this.chk16Write.Name = "chk16Write";this.chk16Write.Size = new System.Drawing.Size(105, 19);this.chk16Write.TabIndex = 7;this.chk16Write.Text = "16进制发送";this.chk16Write.UseVisualStyleBackColor = true;// // txtWrite// this.txtWrite.Location = new System.Drawing.Point(244, 367);this.txtWrite.Margin = new System.Windows.Forms.Padding(4);this.txtWrite.Multiline = true;this.txtWrite.Name = "txtWrite";this.txtWrite.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;this.txtWrite.Size = new System.Drawing.Size(652, 170);this.txtWrite.TabIndex = 8;// // groupBox2// this.groupBox2.Controls.Add(this.chkWriteAutoWrap);this.groupBox2.Controls.Add(this.chk16Write);this.groupBox2.Location = new System.Drawing.Point(10, 411);this.groupBox2.Margin = new System.Windows.Forms.Padding(4);this.groupBox2.Name = "groupBox2";this.groupBox2.Padding = new System.Windows.Forms.Padding(4);this.groupBox2.Size = new System.Drawing.Size(221, 162);this.groupBox2.TabIndex = 9;this.groupBox2.TabStop = false;this.groupBox2.Text = "发送设置";// // groupBox3// this.groupBox3.Controls.Add(this.chkReadAutoWrap);this.groupBox3.Controls.Add(this.chk16Read);this.groupBox3.Location = new System.Drawing.Point(10, 263);this.groupBox3.Name = "groupBox3";this.groupBox3.Size = new System.Drawing.Size(212, 140);this.groupBox3.TabIndex = 10;this.groupBox3.TabStop = false;this.groupBox3.Text = "接收设置";// // label6// this.label6.AutoSize = true;this.label6.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.label6.Location = new System.Drawing.Point(244, 7);this.label6.Name = "label6";this.label6.Size = new System.Drawing.Size(88, 25);this.label6.TabIndex = 11;this.label6.Text = "数据接收";// // chkReadAutoWrap// this.chkReadAutoWrap.AutoSize = true;this.chkReadAutoWrap.Location = new System.Drawing.Point(28, 71);this.chkReadAutoWrap.Margin = new System.Windows.Forms.Padding(4);this.chkReadAutoWrap.Name = "chkReadAutoWrap";this.chkReadAutoWrap.Size = new System.Drawing.Size(149, 19);this.chkReadAutoWrap.TabIndex = 3;this.chkReadAutoWrap.Text = "自动添加回车换行";this.chkReadAutoWrap.UseVisualStyleBackColor = true;// // chkWriteAutoWrap// this.chkWriteAutoWrap.AutoSize = true;this.chkWriteAutoWrap.Location = new System.Drawing.Point(28, 93);this.chkWriteAutoWrap.Margin = new System.Windows.Forms.Padding(4);this.chkWriteAutoWrap.Name = "chkWriteAutoWrap";this.chkWriteAutoWrap.Size = new System.Drawing.Size(149, 19);this.chkWriteAutoWrap.TabIndex = 8;this.chkWriteAutoWrap.Text = "自动添加回车换行";this.chkWriteAutoWrap.UseVisualStyleBackColor = true;// // MainForm// this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(919, 586);this.Controls.Add(this.label6);this.Controls.Add(this.groupBox3);this.Controls.Add(this.btnClearWrite);this.Controls.Add(this.btnSend);this.Controls.Add(this.groupBox2);this.Controls.Add(this.txtWrite);this.Controls.Add(this.btnClearRead);this.Controls.Add(this.txtRead);this.Controls.Add(this.groupBox1);this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));this.Margin = new System.Windows.Forms.Padding(4);this.MinimumSize = new System.Drawing.Size(841, 559);this.Name = "MainForm";this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;this.Text = "串口助手";this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);this.Load += new System.EventHandler(this.MainForm_Load);this.groupBox1.ResumeLayout(false);this.groupBox1.PerformLayout();this.groupBox2.ResumeLayout(false);this.groupBox2.PerformLayout();this.groupBox3.ResumeLayout(false);this.groupBox3.PerformLayout();this.ResumeLayout(false);this.PerformLayout();}#endregionprivate System.Windows.Forms.GroupBox groupBox1;private System.Windows.Forms.Label label4;private System.Windows.Forms.Label label3;private System.Windows.Forms.Label label2;private System.Windows.Forms.Label label1;private System.Windows.Forms.Label label5;private System.Windows.Forms.Button btnOpen;private System.Windows.Forms.ComboBox cmbParity;private System.Windows.Forms.ComboBox cmbDataBits;private System.Windows.Forms.ComboBox cmbStopBits;private System.Windows.Forms.ComboBox cmbBaudRate;private System.Windows.Forms.ComboBox cmbPortName;private System.IO.Ports.SerialPort spt;private System.Windows.Forms.TextBox txtRead;private System.Windows.Forms.CheckBox chk16Read;private System.Windows.Forms.Button btnClearRead;private System.Windows.Forms.Button btnSend;private System.Windows.Forms.Button btnClearWrite;private System.Windows.Forms.CheckBox chk16Write;private System.Windows.Forms.TextBox txtWrite;private System.Windows.Forms.GroupBox groupBox2;private System.Windows.Forms.GroupBox groupBox3;private System.Windows.Forms.Label label6;private System.Windows.Forms.CheckBox chkReadAutoWrap;private System.Windows.Forms.CheckBox chkWriteAutoWrap;}
}

三、后台代码

using System;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;namespace SerialPortDemo
{public partial class MainForm : Form{string serialPortName;bool bListening = false;//是否没有执行完invoke相关操作 (串口防死锁)bool bClosing = false;//是否正在关闭串口,执行Application.DoEvents,并阻止再次invoke public MainForm(){InitializeComponent();}//窗体加载private void MainForm_Load(object sender, EventArgs e){//获取当前计算机的串行端口名的数组string[] ports = SerialPort.GetPortNames();//使用 System.Array 中每个元素的 System.IComparable`1 泛型接口实现,对整个 System.Array 中的元素进行排序。Array.Sort(ports);//添加到显示列表cmbPortName.Items.AddRange(ports);cmbPortName.SelectedIndex = cmbPortName.Items.Count > 0 ? 0 : -1;       //如果里面有数据,显示第0个//显示默认的显示cmbBaudRate.Text = "9600"; //波特率:9600cmbStopBits.Text = "1";//默认停止位:1cmbDataBits.Text = "8";//默认数据位:8cmbParity.Text = "无";//默认奇偶校验位:无}//处理Windows消息 监听串口硬件的变化protected override void WndProc(ref Message m){//设备改变if (m.Msg == 0x0219){//USB串口拔出if (m.WParam.ToInt32() == 0x8004)           //usb串口{string[] ports = System.IO.Ports.SerialPort.GetPortNames();     //重新获取串口cmbPortName.Items.Clear();                //清除comboBox里面的数据cmbPortName.Items.AddRange(ports);        //给comboBox1添加数据if (btnOpen.Text == "关闭串口")         //用户打开过串口{//如果串行端口已打开,则为 true;否则为 false。 默认值为 false。if (!spt.IsOpen){btnOpen.Text = "打开串口";//释放由 System.ComponentModel.Component 使用的所有资源。spt.Dispose();cmbPortName.SelectedIndex = cmbPortName.Items.Count > 0 ? 0 : -1;   //显示获取的第一个串口号}else{cmbPortName.Text = serialPortName;}}else{cmbPortName.SelectedIndex = cmbPortName.Items.Count > 0 ? 0 : -1;       //显示获取的第一个串口号}}//USB串口连上else if (m.WParam.ToInt32() == 0x8000){string[] ports = System.IO.Ports.SerialPort.GetPortNames();cmbPortName.Items.Clear();cmbPortName.Items.AddRange(ports);if (btnOpen.Text == "关闭串口"){cmbPortName.Text = serialPortName;}else{cmbPortName.SelectedIndex = cmbPortName.Items.Count > 0 ? 0 : -1;   //显示获取的第一个串口号}}}base.WndProc(ref m);}/// <字节数组转16进制字符串>/// <param name="bytes"></param>/// <returns> String 16进制显示形式</returns>public static string byteToHexStr(byte[] bytes){string returnStr = "";try{if (bytes != null){for (int i = 0; i < bytes.Length; i++){returnStr += bytes[i].ToString("X2");returnStr += " ";                       //两个16进制用空格隔开,方便看数据}}return returnStr;}catch (Exception){return returnStr;}}/// <summary>///  字符串转16进制格式,不够自动前面补零/// </summary>/// <param name="hexString"></param>/// <returns></returns>private static byte[] strToToHexByte(String hexString){int i;hexString = hexString.Replace(" ", "");//清除空格         if ((hexString.Length % 2) != 0)//奇数个                {byte[] returnBytes = new byte[(hexString.Length + 1) / 2];try{for (i = 0; i < (hexString.Length - 1) / 2; i++){returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);}returnBytes[returnBytes.Length - 1] = Convert.ToByte(hexString.Substring(hexString.Length - 1, 1).PadLeft(2, '0'), 16);}catch{MessageBox.Show("含有非16进制字符", "提示");return null;}return returnBytes;}else{byte[] returnBytes = new byte[(hexString.Length) / 2];try{for (i = 0; i < returnBytes.Length; i++){returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);}}catch{MessageBox.Show("含有非16进制字符", "提示");return null;}return returnBytes;}}//打开或关闭private void btnOpen_Click(object sender, EventArgs e){if (btnOpen.Text == "打开串口")                             //如果按钮显示的是打开{try                                                     //防止意外错误{spt.PortName = cmbPortName.Text;              //获取comboBox1要打开的串口号serialPortName = cmbPortName.Text;spt.BaudRate = int.Parse(cmbBaudRate.Text);   //获取comboBox2选择的波特率spt.DataBits = int.Parse(cmbDataBits.Text);   //设置数据位/*设置停止位*/if (cmbStopBits.Text == "1"){spt.StopBits = StopBits.One;}else if (cmbStopBits.Text == "1.5"){spt.StopBits = StopBits.OnePointFive;}else if (cmbStopBits.Text == "2"){spt.StopBits = StopBits.Two;}/*设置奇偶校验*/if (cmbParity.Text == "无"){spt.Parity = Parity.None;}else if (cmbParity.Text == "奇校验"){spt.Parity = Parity.Odd;}else if (cmbParity.Text == "偶校验"){spt.Parity = Parity.Even;}//打开串口try{spt.Open();}catch{}btnOpen.Text = "关闭串口";//按钮显示关闭串口cmbPortName.Enabled = false;cmbBaudRate.Enabled = false;cmbStopBits.Enabled = false;cmbDataBits.Enabled = false;cmbParity.Enabled = false;}catch (Exception err){MessageBox.Show("打开失败" + err.ToString(), "提示!");//对话框显示打开失败}}else{//关闭串口try{spt.Close();}catch (Exception){}btnOpen.Text = "打开串口";  //按钮显示打开cmbPortName.Enabled = true;cmbBaudRate.Enabled = true;cmbStopBits.Enabled = true;cmbDataBits.Enabled = true;cmbParity.Enabled = true;}}//串口接收事件private void spt_DataReceived(object sender, SerialDataReceivedEventArgs e){if (bClosing == true){return;}bListening = true;int len = spt.BytesToRead;  //获取可以读取的字节数byte[] buff = new byte[len];        //创建缓存数据数组// 摘要://     从 System.IO.Ports.SerialPort 输入缓冲区读取一些字节并将那些字节写入字节数组中指定的偏移量处。//// 参数://   buffer://     将输入写入到其中的字节数组。////   offset://     要写入字节的 buffer 中的偏移量。////   count://     最多读取的字节数。 如果 count 大于输入缓冲区中的字节数,则读取较少的字节。//// 返回结果://     读取的字节数。spt.Read(buff, 0, len);//C# 3.0以后代替委托的新方法Invoke((new Action(() =>{if (chk16Read.Checked)          //16进制显示{if (chkReadAutoWrap.Checked){txtRead.AppendText(DateTime.Now.ToString() + ":" + byteToHexStr(buff) + "\r\n");}else{txtRead.AppendText(DateTime.Now.ToString() + ":" + byteToHexStr(buff));}}else{if (chkReadAutoWrap.Checked){txtRead.AppendText(DateTime.Now.ToString() + ":" + Encoding.Default.GetString(buff) + "\r\n");}else{txtRead.AppendText(DateTime.Now.ToString() + ":" + Encoding.Default.GetString(buff));}}})));bListening = false;//用完了,主线程可以关闭串口了。 }//清除接收private void btnClearRead_Click(object sender, EventArgs e){txtRead.Clear();}//发送private void btnSend_Click(object sender, EventArgs e){if (spt.IsOpen == false){MessageBox.Show("串口未打开!");return;}string str = txtWrite.Text.ToString();//获取发送文本框里面的数据if (chkWriteAutoWrap.Checked == true){str += "\r\n";}try{if (str.Length > 0){if (chk16Write.Checked)              //16进制发送{byte[] byt = strToToHexByte(str);// 摘要://     使用缓冲区中的数据将指定数量的字节写入串行端口。//// 参数://   buffer://     包含要写入端口的数据的字节数组。////   offset://     buffer 参数中从零开始的字节偏移量,从此处开始将字节复制到端口。////   count://     要写入的字节数。spt.Write(byt, 0, byt.Length);}else{//将指定的字符串写入串行端口。spt.Write(str);}}}catch (Exception){}}//清除发送private void btnClearWrite_Click(object sender, EventArgs e){txtWrite.Clear();}private void MainForm_FormClosing(object sender, FormClosingEventArgs e){bClosing = true; // 根据当前串口对象,来判断操作  while (bListening == true){Application.DoEvents();}try{if (spt.IsOpen){spt.DiscardInBuffer();spt.DiscardOutBuffer();//取消订阅事件  防止又来了数据要处理,造成状态栏显示数据时访问不存在的控件的问题spt.DataReceived -= new SerialDataReceivedEventHandler(spt_DataReceived);spt.Close();}}catch{}}}
}

四、实例中要点

如下:

1、串口的参数设置

2、串口名称的刷新,通过重写消息函数,监听的USB的变化,更新电脑的端口变化。

3、打开和关闭串口尽量判定串口是否打开后在进行操作。

4、串口发送前判定是否已经打开。

5、串口的读取Read和发送Write函数的应用。

6、字符串和16进制的转换以及16进制显示的处理。

7、窗体的关闭与串口处理线程的处理,通过增加标志位进行互斥处理,保证每次关闭都能处理完毕后在关闭,防止卡死。

8、串口接收事件是独立于UI线程的单独线程,因此如果在串口接收线程下刷新UI控件需要用到异步执行,本文使用 Invoke进行处理。

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/220566.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Windows汇编调用printf

VS2022 汇编 项目右键 生成依赖项 生成自定义 勾选masm 链接器 高级 入口点 main X86 .686 .model flat,stdcall option casemap:none includelib ucrt.lib includelib legacy_stdio_definitions.libEXTERN printf:proc.data szFormat db %s,0 szStr db hello,0.code main…

关于职场伪勤奋

前段时间看了一些关于勤奋学习、职场成长类的书籍&#xff0c;就在思考勤奋学习和职场的关系时&#xff0c;结合个人的理解&#xff0c;我定义了一种勤奋叫职场“伪勤奋”。那关于职场“伪勤奋”的定义和理解&#xff0c;与大家分享&#xff1a; 1、选择性任务完成 伪勤奋特征…

vue 图片等比例缩放上传

需求&#xff1a;上传图片之前按比例缩小图片分辨率&#xff0c;宽高不超过1920不处理图片&#xff0c;宽高超过1920则缩小图片分辨率&#xff0c;如果是一张图片请参考这篇博客&#xff1a;js实现图片压缩、分辨率等比例缩放 我根据这篇博主的分享&#xff0c;写下了我的循环上…

HarmonyOS使用Web组件

Web组件的使用 1 概述 相信大家都遇到过这样的场景&#xff0c;有时候我们点击应用的页面&#xff0c;会跳转到一个类似浏览器加载的页面&#xff0c;加载完成后&#xff0c;才显示这个页面的具体内容&#xff0c;这个加载和显示网页的过程通常都是浏览器的任务。 ArkUI为我…

chatGPT 国内版,嵌入midjourney AI创作工具

聊天GPT国内入口,免切网直达,可直接多语言对话,操作简单,无需复杂注册,智能高效,即刻使用.可以用作个人助理,学习助理,智能创作、新媒体文案创作、智能创作等各种应用场景! 地址&#xff1a; https://ai.wboat.cn/

【51单片机系列】直流电机使用

本文是关于直流电机使用的相关介绍。 文章目录 一、直流电机介绍二、ULN2003芯片介绍三、在proteus中仿真实现对电机的驱动 51单片机的应用中&#xff0c;电机控制方面的应用也很多。在学习直流电机(PWM)之前&#xff0c;先使用GPIO控制电机的正反转和停止。但不能直接使用GPIO…

06 python 文件基础操作

6.1 .1文件读取操作 演示对文件的读取 # 打开文件 import timef open(02_word.txt, r, encoding"UTF-8") print(type(f))# #读取文件 - read() # print(f读取10个字节的结果{f.read(10)}) # print(f读取全部字节的结果{f.read()})# #读取文件 - readLines() # lines…

面试官:说说你对 linux 用户管理的理解?相关的命令有哪些?

面试官&#xff1a;说说你对 linux 用户管理的理解&#xff1f;相关的命令有哪些&#xff1f; 一、是什么 Linux是一个多用户的系统&#xff0c;允许使用者在系统上通过规划不同类型、不同层级的用户&#xff0c;并公平地分配系统资源与工作环境 而与 Windows 系统最大的不同…

基于MyBatis二级缓存深入装饰器模式

视频地址 学习文档 文章目录 一、示意代码二、装饰器三、经典案例—MyBatis二级缓存1、Cache 标准定义2、PerpetualCache 基础实现3、增强实现3-1、ScheduledCache3-2、LruCache 先来说说我对装饰器理解&#xff1a;当你有一个基础功能的代码&#xff0c;但你想在不改变原来代…

高效营销系统集成:百度营销的API无代码解决方案,提升电商与广告效率

百度营销API连接&#xff1a;构建无代码开发的高效集成体系 在数字营销的高速发展时代&#xff0c;企业追求的是快速响应市场的能力以及提高用户运营的效率。百度营销API连接正是为此而生&#xff0c;它通过无代码开发的方式&#xff0c;实现了电商平台、营销系统和CRM的一站式…

墒情监测FDS-400 土壤温湿电导率盐分传感器

墒情监测FDS-400 土壤温湿电导率盐分传感器产品概述 土壤温度部分是由精密铂电阻和高精度变送器两部分组成。变送器部分由电源模块、温度传感模块、变送模块、温度补偿模块及数据处理模块等组成&#xff0c;解决铂电阻因自身特点导入的测量误差&#xff0c;变送器内有零漂电路…

Redis队列原理解析:让你的应用程序运行更加稳定!

一、消息队列简介 消息队列&#xff08;Message Queue&#xff09;&#xff0c;字面意思就是存放消息的队列。最简单的消息队列模型包括 3 个角色&#xff1a; 消息队列&#xff1a;存储和管理消息&#xff0c;也被称为消息代理&#xff08;Message Broker&#xff09;生产者…

Turtle绘制菱形-第11届蓝桥杯选拔赛Python真题精选

[导读]&#xff1a;超平老师的Scratch蓝桥杯真题解读系列在推出之后&#xff0c;受到了广大老师和家长的好评&#xff0c;非常感谢各位的认可和厚爱。作为回馈&#xff0c;超平老师计划推出《Python蓝桥杯真题解析100讲》&#xff0c;这是解读系列的第16讲。 Turtle绘制菱形&a…

六.聚合函数

聚合函数 1.什么是聚合函数1.1AVG和SUM函数1.2MIN和MAX函数1.3COUNT函数 2.GROUP BY2.1基本使用2.2使用多个列分组2.3GROUP BY中使用WITH ROLLUP 3.HAVING3.1基本使用3.2WHERE和HAVING的区别 4.SELECT的执行过程4.1查询的结构4.2SELECT执行顺序4.3SQL执行原理 1.什么是聚合函数…

用友 U8总账凭证打印设置

总账--凭证打印——设置 是设置凭证打印显示的格子框&#xff0c;勾上就有框&#xff0c;去掉就没有框。

判断css文字发生了截断,增加悬浮提示

示例&#xff1a; 固定显示宽度&#xff0c;溢出显示...&#xff0c;利用了css的属性&#xff0c;想要实现成下面这样&#xff1a; 针对溢出的文字&#xff0c;hover显示全部。 提示很好加&#xff0c;使用tooltip组件就行了&#xff0c;难点是如何判断是否发生了文字溢出。…

JS数组与它的42个方法

前言 数组在js中作为一个非常重要的类型之一&#xff0c;在我们对数据处理&#xff0c;存储数据&#xff0c;条件渲染的时候经常会用到&#xff0c;所以随着ES的不断更新&#xff0c;数组的方法也是越来越多&#xff0c;也让我们使用数组对数据操作的时候&#xff0c;越来越简…

竞赛保研 python 爬虫与协同过滤的新闻推荐系统

1 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; python 爬虫与协同过滤的新闻推荐系统 &#x1f947;学长这里给一个题目综合评分(每项满分5分) 难度系数&#xff1a;3分工作量&#xff1a;3分创新点&#xff1a;4分 该项目较为新颖&…

Python求小于m的最大10个素数

为了找到小于m的最大10个素数&#xff0c;我们首先需要确定m的值。然后&#xff0c;我们可以使用一个简单的算法来检查每一个小于m的数字是否是素数。 下面是一个Python代码示例&#xff0c;可以找到小于m的最大10个素数&#xff1a; def is_prime(n): if n < 1: …

Conda 使用教程大全来啦

什么是 Conda&#xff1f; Conda 是一款功能强大的软件包管理器和环境管理器&#xff0c;您可以在 Windows 的 Anaconda 提示符或 macOS 或 Linux 的终端窗口中使用命令行命令 Conda 可以快速安装、运行和更新软件包及相关依赖项。Conda 可以在本地计算机上创建、保存、加载和…