C#之windows桌面软件第五课:串口助手实现定时关闭设备、鼠标移动使按钮颜色变化功能
生活随笔
收集整理的這篇文章主要介紹了
C#之windows桌面软件第五课:串口助手实现定时关闭设备、鼠标移动使按钮颜色变化功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本節在串口助手上實現:
1.定時關閉設備
2.移動鼠標使按鈕顏色變換
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.IO.Ports;namespace 串口控制 {public partial class Form1 : Form{//device 1const byte DeviceOpen1 = 0x01;const byte DeviceClose1 = 0x81;//device 2const byte DeviceOpen2 = 0x02;const byte DeviceClose2 = 0x82;//device 3const byte DeviceOpen3 = 0x03;const byte DeviceClose3 = 0x83;//SerialPort Write Bufferbool Button1Statue;//記錄按鈕狀態byte[] SerialPortDataBuffer = new byte[1];public Form1(){InitializeComponent(); //窗口構造}private void button1_Click(object sender, EventArgs e)//串口開關按鈕{if (serialPort1.IsOpen) //串口打開就關閉{try{serialPort1.Close();}catch { } //確保萬無一失//button1.Text = "打開串口";button1.BackgroundImage = Properties.Resources.Image2; //滅Button1Statue = false; //按鈕狀態}else{try{serialPort1.PortName = comboBox1.Text; //端口號serialPort1.Open(); //打開端口//button1.Text = "關閉串口";button1.BackgroundImage = Properties.Resources.Image1;//亮Button1Statue = true; //按鈕狀態}catch{MessageBox.Show("串口打開失敗","錯誤");}}}private void Form1_Load(object sender, EventArgs e){SearchAndAddSerialToComboBox(serialPort1, comboBox1);}private void WriteByteToSerialPort(byte data) //單字節寫入串口{byte[] Buffer = new byte [2]{0x00, data }; //定義數組if (serialPort1.IsOpen) //傳輸數據的前提是端口已打開{try{serialPort1.Write(Buffer, 0, 2); //寫數據}catch {MessageBox.Show("串口數據發送出錯,請檢查.","錯誤");//錯誤處理}}}private void SearchAndAddSerialToComboBox(SerialPort MyPort,ComboBox MyBox){ //將可用端口號添加到ComboBoxstring[] MyString = new string[20]; //最多容納20個,太多會影響調試效率string Buffer; //緩存MyBox.Items.Clear(); //清空ComboBox內容for (int i = 1; i < 20; i++) //循環{try //核心原理是依靠try和catch完成遍歷{Buffer = "COM" + i.ToString();MyPort.PortName = Buffer;MyPort.Open(); //如果失敗,后面的代碼不會執行MyString[i - 1] = Buffer;MyBox.Items.Add(Buffer); //打開成功,添加至下倆列表MyPort.Close(); //關閉}catch {}}MyBox.Text = MyString[0]; //初始化}private void button2_Click(object sender, EventArgs e)//開1{int i = 0;try{//從文本框得到定時的值i = Convert.ToInt32(textBox1.Text.Substring(0, 2)); //先處理兩位數,如果出錯就處理一位數}catch{try{i = Convert.ToInt32(textBox1.Text.Substring(0, 1));//處理一位數}catch //處理0和大于兩位的數{MessageBox.Show("請輸入正確的數字,定時時間在1-99秒"); //錯誤提示return; //退出函數}}if (serialPort1.IsOpen) //避免定時器浪費時間和用戶等待{if (i == 0) //如果是0的話程序認為是定時模式關{//MessageBox.Show("請輸入大于0的數字","提示");//WriteByteToSerialPort(DeviceOpen1);return;}else{timer1.Interval = i * 1000; //可以這樣寫,不需要計數器(定時器單位:ms)timer1.Start(); //開定時器button2.Enabled = false; //開按鈕不能按了…}}}//當鼠標放在打開串口按鈕上的顏色private void button1_MouseHover(object sender, EventArgs e){button1.BackgroundImage = Properties.Resources.Image3;//鼠標指上去則使用Image3}//當鼠標離開打開串口按鈕上的顏色private void button1_MouseLeave(object sender, EventArgs e){if (Button1Statue) //鼠標移開,返回原來狀態{button1.BackgroundImage = Properties.Resources.Image1;}else{button1.BackgroundImage = Properties.Resources.Image2; }}private void button3_Click(object sender, EventArgs e)//關1{try{timer1.Stop(); //如果定時器沒開,則錯誤處理}catch{}button2.Enabled = true;//把開按鈕執為可用WriteByteToSerialPort(DeviceClose1); //發關數據到串口,達到關:器件一}private void button5_Click(object sender, EventArgs e){WriteByteToSerialPort(DeviceOpen2); //器件二開}private void button4_Click(object sender, EventArgs e){WriteByteToSerialPort(DeviceClose2); //器件二關}private void button7_Click(object sender, EventArgs e){WriteByteToSerialPort(DeviceOpen3); //器件三開}private void button6_Click(object sender, EventArgs e){WriteByteToSerialPort(DeviceClose3); //器件三關}private void button8_Click(object sender, EventArgs e){SearchAndAddSerialToComboBox(serialPort1, comboBox1); //掃描并講課用串口添加至下拉列表}private void timer1_Tick(object sender, EventArgs e)//計時結束后才執行這個函數{button2.Enabled = true; //開按鈕可以按timer1.Stop(); //一定要先關閉定時器//MessageBox.Show(null);WriteByteToSerialPort(DeviceClose1); //器件一關}} }Form1.Designer.cs代碼如下:
namespace 串口控制 {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.groupBox1 = new System.Windows.Forms.GroupBox();this.label1 = new System.Windows.Forms.Label();this.button8 = new System.Windows.Forms.Button();this.button1 = new System.Windows.Forms.Button();this.comboBox1 = new System.Windows.Forms.ComboBox();this.groupBox2 = new System.Windows.Forms.GroupBox();this.label2 = new System.Windows.Forms.Label();this.textBox1 = new System.Windows.Forms.TextBox();this.button3 = new System.Windows.Forms.Button();this.button2 = new System.Windows.Forms.Button();this.groupBox3 = new System.Windows.Forms.GroupBox();this.button4 = new System.Windows.Forms.Button();this.button5 = new System.Windows.Forms.Button();this.groupBox4 = new System.Windows.Forms.GroupBox();this.button6 = new System.Windows.Forms.Button();this.button7 = new System.Windows.Forms.Button();this.serialPort1 = new System.IO.Ports.SerialPort(this.components);this.timer1 = new System.Windows.Forms.Timer(this.components);this.groupBox1.SuspendLayout();this.groupBox2.SuspendLayout();this.groupBox3.SuspendLayout();this.groupBox4.SuspendLayout();this.SuspendLayout();// // groupBox1// this.groupBox1.Controls.Add(this.label1);this.groupBox1.Controls.Add(this.button8);this.groupBox1.Controls.Add(this.button1);this.groupBox1.Controls.Add(this.comboBox1);this.groupBox1.Location = new System.Drawing.Point(12, 6);this.groupBox1.Name = "groupBox1";this.groupBox1.Size = new System.Drawing.Size(307, 89);this.groupBox1.TabIndex = 0;this.groupBox1.TabStop = false;this.groupBox1.Text = "串口";// // label1// this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(183, 41);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(53, 12);this.label1.TabIndex = 3;this.label1.Text = "串口開關";// // button8// this.button8.Location = new System.Drawing.Point(86, 36);this.button8.Name = "button8";this.button8.Size = new System.Drawing.Size(52, 23);this.button8.TabIndex = 2;this.button8.Text = "掃描";this.button8.UseVisualStyleBackColor = true;this.button8.Click += new System.EventHandler(this.button8_Click);// // button1// this.button1.BackColor = System.Drawing.SystemColors.ButtonFace;this.button1.BackgroundImage = global::串口控制.Properties.Resources.Image2;this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;this.button1.Location = new System.Drawing.Point(242, 18);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(58, 58);this.button1.TabIndex = 1;this.button1.UseVisualStyleBackColor = false;this.button1.Click += new System.EventHandler(this.button1_Click);//控制鼠標放在打開串口按鈕上的顏色(自己添加)this.button1.MouseLeave += new System.EventHandler(this.button1_MouseLeave);this.button1.MouseHover += new System.EventHandler(this.button1_MouseHover);// // comboBox1// this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;this.comboBox1.FormattingEnabled = true;this.comboBox1.Location = new System.Drawing.Point(10, 36);this.comboBox1.Name = "comboBox1";this.comboBox1.Size = new System.Drawing.Size(70, 20);this.comboBox1.TabIndex = 0;// // groupBox2// this.groupBox2.Controls.Add(this.label2);this.groupBox2.Controls.Add(this.textBox1);this.groupBox2.Controls.Add(this.button3);this.groupBox2.Controls.Add(this.button2);this.groupBox2.Location = new System.Drawing.Point(13, 101);this.groupBox2.Name = "groupBox2";this.groupBox2.Size = new System.Drawing.Size(306, 89);this.groupBox2.TabIndex = 1;this.groupBox2.TabStop = false;this.groupBox2.Text = "NO.1";// // label2// this.label2.AutoSize = true;this.label2.Location = new System.Drawing.Point(170, 56);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(59, 12);this.label2.TabIndex = 5;this.label2.Text = "關定時(S)";// // textBox1// this.textBox1.Location = new System.Drawing.Point(235, 53);this.textBox1.Name = "textBox1";this.textBox1.Size = new System.Drawing.Size(63, 21);this.textBox1.TabIndex = 4;this.textBox1.Text = "0";// // button3// this.button3.Location = new System.Drawing.Point(168, 23);this.button3.Name = "button3";this.button3.Size = new System.Drawing.Size(130, 23);this.button3.TabIndex = 3;this.button3.Text = "關";this.button3.UseVisualStyleBackColor = true;this.button3.Click += new System.EventHandler(this.button3_Click);// // button2// this.button2.Location = new System.Drawing.Point(6, 23);this.button2.Name = "button2";this.button2.Size = new System.Drawing.Size(128, 23);this.button2.TabIndex = 2;this.button2.Text = "開";this.button2.UseVisualStyleBackColor = true;this.button2.Click += new System.EventHandler(this.button2_Click);// // groupBox3// this.groupBox3.Controls.Add(this.button4);this.groupBox3.Controls.Add(this.button5);this.groupBox3.Location = new System.Drawing.Point(11, 205);this.groupBox3.Name = "groupBox3";this.groupBox3.Size = new System.Drawing.Size(308, 55);this.groupBox3.TabIndex = 4;this.groupBox3.TabStop = false;this.groupBox3.Text = "NO.2";// // button4// this.button4.Location = new System.Drawing.Point(173, 20);this.button4.Name = "button4";this.button4.Size = new System.Drawing.Size(128, 23);this.button4.TabIndex = 3;this.button4.Text = "關";this.button4.UseVisualStyleBackColor = true;this.button4.Click += new System.EventHandler(this.button4_Click);// // button5// this.button5.Location = new System.Drawing.Point(10, 20);this.button5.Name = "button5";this.button5.Size = new System.Drawing.Size(128, 23);this.button5.TabIndex = 2;this.button5.Text = "開";this.button5.UseVisualStyleBackColor = true;this.button5.Click += new System.EventHandler(this.button5_Click);// // groupBox4// this.groupBox4.Controls.Add(this.button6);this.groupBox4.Controls.Add(this.button7);this.groupBox4.Location = new System.Drawing.Point(11, 266);this.groupBox4.Name = "groupBox4";this.groupBox4.Size = new System.Drawing.Size(308, 83);this.groupBox4.TabIndex = 5;this.groupBox4.TabStop = false;this.groupBox4.Text = "NO.3";// // button6// this.button6.Location = new System.Drawing.Point(172, 20);this.button6.Name = "button6";this.button6.Size = new System.Drawing.Size(128, 23);this.button6.TabIndex = 3;this.button6.Text = "關";this.button6.UseVisualStyleBackColor = true;this.button6.Click += new System.EventHandler(this.button6_Click);// // button7// this.button7.BackgroundImage = global::串口控制.Properties.Resources.Image2;this.button7.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;this.button7.Location = new System.Drawing.Point(10, 20);this.button7.Name = "button7";this.button7.Size = new System.Drawing.Size(57, 42);this.button7.TabIndex = 2;this.button7.UseVisualStyleBackColor = true;this.button7.Click += new System.EventHandler(this.button7_Click);// // timer1// this.timer1.Interval = 1000;this.timer1.Tick += new System.EventHandler(this.timer1_Tick);// // Form1// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(331, 371);this.Controls.Add(this.groupBox4);this.Controls.Add(this.groupBox3);this.Controls.Add(this.groupBox2);this.Controls.Add(this.groupBox1);this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;this.MaximizeBox = false;this.Name = "Form1";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.groupBox3.ResumeLayout(false);this.groupBox4.ResumeLayout(false);this.ResumeLayout(false);}#endregionprivate System.Windows.Forms.GroupBox groupBox1;private System.Windows.Forms.Button button1;private System.Windows.Forms.ComboBox comboBox1;private System.Windows.Forms.GroupBox groupBox2;private System.Windows.Forms.Button button3;private System.Windows.Forms.Button button2;private System.Windows.Forms.GroupBox groupBox3;private System.Windows.Forms.Button button4;private System.Windows.Forms.Button button5;private System.Windows.Forms.GroupBox groupBox4;private System.Windows.Forms.Button button6;private System.Windows.Forms.Button button7;private System.IO.Ports.SerialPort serialPort1;private System.Windows.Forms.Button button8;private System.Windows.Forms.Timer timer1;private System.Windows.Forms.TextBox textBox1;private System.Windows.Forms.Label label1;private System.Windows.Forms.Label label2;} }www.DoYoung.net(部分代碼來至杜洋工作室)
總結
以上是生活随笔為你收集整理的C#之windows桌面软件第五课:串口助手实现定时关闭设备、鼠标移动使按钮颜色变化功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#之windows桌面软件第四课:串口
- 下一篇: C#之windows桌面软件第六课:(上