700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > C#之windows桌面软件第五课:串口助手实现定时关闭设备 鼠标移动使按钮颜色变化功能

C#之windows桌面软件第五课:串口助手实现定时关闭设备 鼠标移动使按钮颜色变化功能

时间:2023-01-01 19:47:05

相关推荐

C#之windows桌面软件第五课:串口助手实现定时关闭设备 鼠标移动使按钮颜色变化功能

本节在串口助手上实现:

1.定时关闭设备

2.移动鼠标使按钮颜色变换

Form1.cs代码如下:

using System;using System.Collections.Generic;using ponentModel;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 ponentModel.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(){ponents = new ponentModel.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();boBox1 = new System.boBox();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(ponents);this.timer1 = new System.Windows.Forms.Timer(ponents);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(boBox1);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// boBox1.DropDownStyle = System.boBoxStyle.DropDownList;boBox1.FormattingEnabled = true;boBox1.Location = new System.Drawing.Point(10, 36);boBox1.Name = "comboBox1";boBox1.Size = new System.Drawing.Size(70, 20);boBox1.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.boBox 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;}}

(部分代码来至杜洋工作室)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。