c#winform串口助手源码

         c#串口助手的源码,使用visual studio 2010编写,c#winform类。具体的功能实现原理后续再发,今天提供串口的源码复制粘贴就可以运行(要粘贴到正确的位置,如果不明白粘哪里留言直接问就可)。winform的源代码由三部分组成designer.cs负责生成控件的代码,form1.cs功能实现功能代码(接收消息、发送消息、打开串口、实时刷新串口、定时发送、十六进制发送、十六进制接收)、program.cs负责运行这个串口助手。后续有具体问题直接留言看到了就回复你。我是个准研究生实习中,使用c#学习编程每天会使用csdn欢迎来交流学习心得。

 

第一段代码(program.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WFporthander
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new wfport());
        }
    }
}

第二段代码(designer.cs):

namespace WFporthander
{
    partial class wfport
    {
        /// <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(wfport));
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btnclearrece = new System.Windows.Forms.Button();
            this.btnopen = new System.Windows.Forms.Button();
            this.cbcheck = new System.Windows.Forms.ComboBox();
            this.cbstop = new System.Windows.Forms.ComboBox();
            this.cbdata = new System.Windows.Forms.ComboBox();
            this.cbbaute = new System.Windows.Forms.ComboBox();
            this.cbport = 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.btnsend = new System.Windows.Forms.Button();
            this.btnclerarsend = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.txtrece = new System.Windows.Forms.TextBox();
            this.groupBox4 = new System.Windows.Forms.GroupBox();
            this.txtsend = new System.Windows.Forms.TextBox();
            this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.label8 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.btnhexsend = new System.Windows.Forms.Button();
            this.btnhexrece = new System.Windows.Forms.Button();
            this.cbtimeintervial = new System.Windows.Forms.ComboBox();
            this.label6 = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox4.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.btnclearrece);
            this.groupBox1.Controls.Add(this.btnopen);
            this.groupBox1.Controls.Add(this.cbcheck);
            this.groupBox1.Controls.Add(this.cbstop);
            this.groupBox1.Controls.Add(this.cbdata);
            this.groupBox1.Controls.Add(this.cbbaute);
            this.groupBox1.Controls.Add(this.cbport);
            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(12, 5);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(278, 259);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "串口属性与操作区";
            // 
            // btnclearrece
            // 
            this.btnclearrece.Location = new System.Drawing.Point(23, 218);
            this.btnclearrece.Name = "btnclearrece";
            this.btnclearrece.Size = new System.Drawing.Size(75, 23);
            this.btnclearrece.TabIndex = 12;
            this.btnclearrece.Text = "清除接收";
            this.btnclearrece.UseVisualStyleBackColor = true;
            this.btnclearrece.Click += new System.EventHandler(this.btnclearrece_Click);
            // 
            // btnopen
            // 
            this.btnopen.Location = new System.Drawing.Point(138, 218);
            this.btnopen.Name = "btnopen";
            this.btnopen.Size = new System.Drawing.Size(75, 23);
            this.btnopen.TabIndex = 10;
            this.btnopen.Text = "打开串口";
            this.btnopen.UseVisualStyleBackColor = true;
            this.btnopen.Click += new System.EventHandler(this.btnopen_Click);
            // 
            // cbcheck
            // 
            this.cbcheck.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbcheck.FormattingEnabled = true;
            this.cbcheck.Items.AddRange(new object[] {
            "无",
            "奇校验",
            "偶校验",
            "",
            " "});
            this.cbcheck.Location = new System.Drawing.Point(92, 177);
            this.cbcheck.Name = "cbcheck";
            this.cbcheck.Size = new System.Drawing.Size(121, 20);
            this.cbcheck.TabIndex = 9;
            // 
            // cbstop
            // 
            this.cbstop.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbstop.FormattingEnabled = true;
            this.cbstop.Items.AddRange(new object[] {
            "1",
            "1.5",
            "2"});
            this.cbstop.Location = new System.Drawing.Point(92, 141);
            this.cbstop.Name = "cbstop";
            this.cbstop.Size = new System.Drawing.Size(121, 20);
            this.cbstop.TabIndex = 8;
            // 
            // cbdata
            // 
            this.cbdata.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbdata.FormattingEnabled = true;
            this.cbdata.Items.AddRange(new object[] {
            "8",
            "7",
            "6",
            "5"});
            this.cbdata.Location = new System.Drawing.Point(92, 105);
            this.cbdata.Name = "cbdata";
            this.cbdata.Size = new System.Drawing.Size(121, 20);
            this.cbdata.TabIndex = 7;
            // 
            // cbbaute
            // 
            this.cbbaute.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbbaute.FormattingEnabled = true;
            this.cbbaute.Items.AddRange(new object[] {
            "1382400",
            "921600",
            "460800",
            "256000",
            "230400",
            "128000",
            "115200",
            "76800",
            "57600",
            "43000",
            "38400",
            "19200",
            "14400",
            "9600",
            "4800",
            "1200"});
            this.cbbaute.Location = new System.Drawing.Point(92, 66);
            this.cbbaute.Name = "cbbaute";
            this.cbbaute.Size = new System.Drawing.Size(121, 20);
            this.cbbaute.TabIndex = 6;
            // 
            // cbport
            // 
            this.cbport.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbport.FormattingEnabled = true;
            this.cbport.Location = new System.Drawing.Point(92, 32);
            this.cbport.Name = "cbport";
            this.cbport.Size = new System.Drawing.Size(121, 20);
            this.cbport.TabIndex = 5;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(21, 180);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(41, 12);
            this.label5.TabIndex = 4;
            this.label5.Text = "校检位";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(21, 144);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(41, 12);
            this.label4.TabIndex = 3;
            this.label4.Text = "停止位";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(21, 105);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 12);
            this.label3.TabIndex = 2;
            this.label3.Text = "数据位";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(21, 69);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "波特率";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(21, 35);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "串口号";
            // 
            // btnsend
            // 
            this.btnsend.Location = new System.Drawing.Point(126, 26);
            this.btnsend.Name = "btnsend";
            this.btnsend.Size = new System.Drawing.Size(102, 23);
            this.btnsend.TabIndex = 13;
            this.btnsend.Text = "发送";
            this.btnsend.UseVisualStyleBackColor = true;
            this.btnsend.Click += new System.EventHandler(this.btnsend_Click);
            // 
            // btnclerarsend
            // 
            this.btnclerarsend.Location = new System.Drawing.Point(22, 26);
            this.btnclerarsend.Name = "btnclerarsend";
            this.btnclerarsend.Size = new System.Drawing.Size(75, 23);
            this.btnclerarsend.TabIndex = 11;
            this.btnclerarsend.Text = "清除发送";
            this.btnclerarsend.UseVisualStyleBackColor = true;
            this.btnclerarsend.Click += new System.EventHandler(this.btnclerarsend_Click);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.txtrece);
            this.groupBox2.Location = new System.Drawing.Point(292, 12);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(539, 252);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "消息接收区";
            // 
            // txtrece
            // 
            this.txtrece.Font = new System.Drawing.Font("华文楷体", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.txtrece.ForeColor = System.Drawing.Color.Red;
            this.txtrece.Location = new System.Drawing.Point(10, 20);
            this.txtrece.Multiline = true;
            this.txtrece.Name = "txtrece";
            this.txtrece.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtrece.Size = new System.Drawing.Size(523, 235);
            this.txtrece.TabIndex = 0;
            // 
            // groupBox4
            // 
            this.groupBox4.Controls.Add(this.txtsend);
            this.groupBox4.Location = new System.Drawing.Point(296, 270);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new System.Drawing.Size(535, 220);
            this.groupBox4.TabIndex = 3;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "消息发送区域";
            // 
            // txtsend
            // 
            this.txtsend.Font = new System.Drawing.Font("华文楷体", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.txtsend.ForeColor = System.Drawing.Color.Blue;
            this.txtsend.Location = new System.Drawing.Point(6, 20);
            this.txtsend.Multiline = true;
            this.txtsend.Name = "txtsend";
            this.txtsend.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtsend.Size = new System.Drawing.Size(510, 189);
            this.txtsend.TabIndex = 0;
            this.txtsend.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtsend_KeyDown);
            this.txtsend.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtsend_KeyUp);
            // 
            // serialPort1
            // 
            this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.label8);
            this.groupBox3.Controls.Add(this.label7);
            this.groupBox3.Controls.Add(this.btnhexsend);
            this.groupBox3.Controls.Add(this.btnhexrece);
            this.groupBox3.Controls.Add(this.cbtimeintervial);
            this.groupBox3.Controls.Add(this.label6);
            this.groupBox3.Controls.Add(this.btnclerarsend);
            this.groupBox3.Controls.Add(this.btnsend);
            this.groupBox3.Location = new System.Drawing.Point(12, 270);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(278, 220);
            this.groupBox3.TabIndex = 4;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "发送区域";
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(20, 179);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(89, 12);
            this.label8.TabIndex = 19;
            this.label8.Text = "十六进制发送:";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(20, 135);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(89, 12);
            this.label7.TabIndex = 18;
            this.label7.Text = "十六进制接收:";
            // 
            // btnhexsend
            // 
            this.btnhexsend.Location = new System.Drawing.Point(137, 174);
            this.btnhexsend.Name = "btnhexsend";
            this.btnhexsend.Size = new System.Drawing.Size(75, 23);
            this.btnhexsend.TabIndex = 17;
            this.btnhexsend.Text = "打开";
            this.btnhexsend.UseVisualStyleBackColor = true;
            this.btnhexsend.Click += new System.EventHandler(this.btnhexsend_Click);
            // 
            // btnhexrece
            // 
            this.btnhexrece.Location = new System.Drawing.Point(137, 130);
            this.btnhexrece.Name = "btnhexrece";
            this.btnhexrece.Size = new System.Drawing.Size(75, 23);
            this.btnhexrece.TabIndex = 16;
            this.btnhexrece.Text = "打开";
            this.btnhexrece.UseVisualStyleBackColor = true;
            this.btnhexrece.Click += new System.EventHandler(this.btnhexrece_Click);
            // 
            // cbtimeintervial
            // 
            this.cbtimeintervial.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbtimeintervial.FormattingEnabled = true;
            this.cbtimeintervial.Items.AddRange(new object[] {
            "正常模式",
            "300",
            "500",
            "600",
            "700",
            "800",
            "900",
            "1000",
            "1500",
            "2000",
            "2500",
            "3000"});
            this.cbtimeintervial.Location = new System.Drawing.Point(126, 79);
            this.cbtimeintervial.Name = "cbtimeintervial";
            this.cbtimeintervial.Size = new System.Drawing.Size(102, 20);
            this.cbtimeintervial.TabIndex = 15;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(8, 82);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(101, 12);
            this.label6.TabIndex = 14;
            this.label6.Text = "时间间隔(毫秒)";
            // 
            // wfport
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(843, 491);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox4);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "wfport";
            this.Text = "串口通信助手";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox4.ResumeLayout(false);
            this.groupBox4.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Button btnsend;
        private System.Windows.Forms.Button btnclearrece;
        private System.Windows.Forms.Button btnclerarsend;
        private System.Windows.Forms.Button btnopen;
        private System.Windows.Forms.ComboBox cbcheck;
        private System.Windows.Forms.ComboBox cbstop;
        private System.Windows.Forms.ComboBox cbdata;
        private System.Windows.Forms.ComboBox cbbaute;
        private System.Windows.Forms.ComboBox cbport;
        private System.Windows.Forms.Label label5;
        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.GroupBox groupBox2;
        private System.Windows.Forms.TextBox txtrece;
        private System.Windows.Forms.GroupBox groupBox4;
        private System.Windows.Forms.TextBox txtsend;
        private System.IO.Ports.SerialPort serialPort1;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.ComboBox cbtimeintervial;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Button btnhexsend;
        private System.Windows.Forms.Button btnhexrece;
    }
}

第三段代码(form1.cs) 

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.Net;

namespace WFporthander
{
    public partial class wfport : Form
    {
        string serialportname;
        string timenow;
        
        public wfport()
        {
            InitializeComponent();
        }

      

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                string[] ports = System.IO.Ports.SerialPort.GetPortNames();
                cbport.Items.AddRange(ports);
                cbport.SelectedIndex = cbport.Items.Count > 0 ? 0 : -1;
                serialportname = cbport.Text;
                cbbaute.Text = "115200";
                cbdata.Text = "8";
                cbstop.Text = "1";
                cbcheck.Text = "无";
                txtsend.Text = "你好,开始进行通信测试!";
                cbtimeintervial.SelectedIndex = cbtimeintervial.Items.Count > 0 ? 0 : -1;
            }
            catch (Exception errlad)
            {

                MessageBox.Show(errlad.ToString());
            }
           
        }
        private void datasend()
        {
            

            try
            {
                serialPort1.Encoding = Encoding.GetEncoding("GB2312");
                string strsend = txtsend.Text;
             
             
                if (btnhexsend.Text=="打开")
                {
                    byte[] bytesend = Encoding.Default.GetBytes(strsend);
                    if (bytesend.Length > 0)
                    {

                        serialPort1.Write(bytesend, 0, bytesend.Length);
                        txtadd(strsend);

                        txtsend.Focus();
                    } 
                }
                else
                {
                    byte[] bytehexsend = strhextobyte(strsend);
                    if (bytehexsend.Length > 0)
                    {
                        serialPort1.Write(bytehexsend, 0, bytehexsend.Length);
                        txtadd(strsend);

                        txtsend.Focus();
                    }



                    else
                    {
                        MessageBox.Show("please input the content in the textbox!");
                        txtsend.Text = "";
                    }
                }
               
               
               
                
            }
            catch (Exception errs)
            {

                MessageBox.Show(errs.ToString());
            }
        }
        private byte[] strhextobyte(string s)
        {
            
                    s = s.Replace(" ", "");
                
              
                    byte[] bytehex;
                    bytehex = new byte[(s.Length) / 2];
                    for (int i = 0; i < bytehex.Length; i++)
                    {
                        bytehex[i] = Convert.ToByte(s.Substring(i * 2, 2), 16);
                    }
                    return bytehex;
             
               
          
            
        }
        private void txtadd(string str)
        {
            try
            {
                txtrece.ForeColor = System.Drawing.Color.Blue;
                timenow = DateTime.Now.ToString();
                Invoke((new Action(() =>
                {
                    txtrece.AppendText(timenow + "本机发送:" +Environment.NewLine+ str+Environment.NewLine+"\r\n");
                })));
            }
            catch (Exception erra)
            {

                MessageBox.Show(erra.ToString());
            }
           
        }
        private void datarece()
        {
            try
            {
                serialPort1.Encoding = Encoding.GetEncoding("GB2312");
                int len = serialPort1.BytesToRead;
                byte[] byterece = new byte[len];
                serialPort1.Read(byterece, 0, len);
                if (byterece.Length>0)
                {
                    if (btnhexrece.Text=="打开")
                    {
                        string strrece = Encoding.Default.GetString(byterece);
                        msgadd(strrece);
                    }
                    else
                    {
                        string strhexrece = bytetohexstr(byterece);
                        msgadd(strhexrece);
                    }
                 
                }
              
                
            }
            catch (Exception errd)
            {

                MessageBox.Show(errd.ToString());
            }
            

        }
        private string bytetohexstr(byte[] b)
        {
            try
            {
                string strret = "";
                for (int i = 0; i < b.Length; i++)
                {

                    strret += b[i].ToString("X2") + " ";
                }
                return strret;
                
                
            }
            catch (Exception errbts)
            {

                MessageBox.Show(errbts.ToString());
                return "error";
            }
            
            
           
                

          
        }
        private void msgadd(string s)
        {
            try
            {
                txtrece.ForeColor = System.Drawing.Color.Red;
                timenow = DateTime.Now.ToString();
                string msg = timenow + "本机收到:" + Environment.NewLine + s + Environment.NewLine+"\r\n";
                
                Invoke((new Action(() =>
                {
                    txtrece.AppendText(msg);
                })));
            }
            catch (Exception errmgd)
            {

                MessageBox.Show(errmgd.ToString());
            }
           
        }

        private void btnopen_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnopen.Text == "打开串口")
                {
                    if (cbport.Text!="")
                    {
                        serialPort1.PortName = cbport.Text;
                        serialPort1.BaudRate = Convert.ToInt32(cbbaute.Text);
                        serialPort1.DataBits = Convert.ToInt32(cbdata.Text);
                        serialPort1.Open();
                        btnopen.Text = "关闭串口";
                    }
                    else
                    {
                        MessageBox.Show("没有可用串口!");
                    }
                  
                }
                else
                {
                    btnopen.Text = "打开串口";
                    serialPort1.Close();
                }
            }
            catch (Exception erro)
            {

                MessageBox.Show(erro.ToString());
            }
          
        }

        private void btnclerarsend_Click(object sender, EventArgs e)
        {
            txtsend.Clear();
        }

        private void btnclearrece_Click(object sender, EventArgs e)
        {
            txtrece.Clear();
        }

        private void btnsend_Click(object sender, EventArgs e)
        {
            try
            {
               
                    
                    if (cbtimeintervial.Text == "正常模式")
                    {
                        datasend();
                        txtsend.Clear();
                       
                    }
                    else
                    {
                        if (btnsend.Text=="发送")
                        {
                            timer1.Interval = Convert.ToInt32(cbtimeintervial.Text);
                            timer1.Tick += new EventHandler(timer1_Tick);
                            timer1.Start();
                            btnsend.Text = "停止定时发送";
                        }
                        else
                        {
                            timer1.Stop();
                            btnsend.Text = "发送";
                            cbtimeintervial.Text = "正常模式";
                        }
                      
                    }
                
                
            }
            catch (Exception errbtns)
            {

                
            }
           
           
           
           
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            datasend();
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
           
                datarece();
           
           
        }

        private void txtsend_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                txtsend.Text = "";
            }
        }

        private void txtsend_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                datasend();
            }
        }

        private void btnhexrece_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnhexrece.Text == "打开")
                {
                 
                    btnhexrece.Text = "关闭";
                }
                else
                {   
                    btnhexrece.Text = "打开";
                }
            }
            catch (Exception errhr)
            {

                MessageBox.Show(errhr.ToString());
            }
           
        }

        private void btnhexsend_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnhexsend.Text == "打开")
                {
                    btnhexsend.Text = "关闭";
                }
                else
                {
                    btnhexsend.Text = "打开";
                }
            }
            catch (Exception err)
            {

                MessageBox.Show(err.ToString());
            }
            
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0219)//设备改变
            {
                if (m.WParam.ToInt32() == 0x8004)//拔出usb
                {
                    if (btnopen.Text== "打开串口")//串口没打开
                    {
                       cbport.Items.Clear();
                        string[] ports = System.IO.Ports.SerialPort.GetPortNames();
                       cbport. Items.AddRange(ports);
                    }
                    else//已经打开串口了
                    {
                        if (!serialPort1.IsOpen)//虚拟串口被关闭了
                        {
                           btnopen.Text = "打开串口";
                            serialPort1.Dispose();
                           cbport .Items.Clear();
                            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
                            cbport.Items.AddRange(ports);
                        }
                        else//虚拟串口没被关闭
                        {
                            serialPort1.Close();
                            btnopen.Text = "打开串口";
                            cbport.Items.Clear();
                            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
                            cbport.Items.AddRange(ports);

                        }
                    }
                }
                else//插上usb
                {
                    if (btnopen.Text == "打开串口")//插上usb+串口没打开
                    {
                        cbport.Items.Clear();
                        string[] ports = System.IO.Ports.SerialPort.GetPortNames();
                        cbport.Items.AddRange(ports);
                    }
                    else//插上usb+串口打开了
                    {
                        serialPort1.Close();
                        btnopen.Text = "打开串口";
                        cbport.Items.Clear();
                        string[] ports = System.IO.Ports.SerialPort.GetPortNames();
                        cbport.Items.AddRange(ports);
                    }
                }
            }
            else//设备没改变
            { }
            base.WndProc(ref m);
        }

        
        
        
     
    }
}

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值