本节在串口助手上实现:

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桌面软件第五课:串口助手实现定时关闭设备、鼠标移动使按钮颜色变化功能相关推荐

  1. C#之windows桌面软件第十三课:C#中常用的类有哪些?构造函数怎么用?

    C#之windows桌面软件第十三课:C#中常用的类有哪些?构造函数怎么用? using System; using System.Collections.Generic; using System. ...

  2. C#之windows桌面软件第十一课:电脑ADC值显示(上位机)(多通道显示)

    C#之windows桌面软件第十一课:电脑ADC值显示(上位机)(多通道显示) using System; using System.Collections.Generic; using System ...

  3. C#之windows桌面软件第十课:电脑ADC值显示(上位机)(单通道显示)

    C#之windows桌面软件第十课:电脑ADC值显示(上位机) (单通道显示) using System; using System.Collections.Generic; using System ...

  4. C#之windows桌面软件第八课:汉字(GB2312)与编码(UTF-8)之间的相互转换

    C#之windows桌面软件第八课:汉字(GB2312)与编码(UTF-8)之间的相互转换 using System; using System.Collections.Generic; using ...

  5. C#之windows桌面软件第七课:(下集)串口工具实现数据校验、用灯反应设备状态

    C#之windows桌面软件第七课:(下集)串口工具实现数据校验.用灯反应设备状态 using System; using System.Collections.Generic; using Syst ...

  6. C#之windows桌面软件第六课:(上集)串口工具实现数据校验、用灯反应设备状态

    C#之windows桌面软件第六课:(上集)串口工具实现数据校验.用灯反应设备状态 using System; using System.Collections.Generic; using Syst ...

  7. C#之windows桌面软件第三课:完整的串口调试助手

    接上一节,这节来编写一个完整的串口调试助手! using System; using System.Collections.Generic; using System.ComponentModel; ...

  8. C#之windows桌面软件第四课:串口助手控制设备的开关

    串口助手控制设备的开关 using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...

  9. C#之windows桌面软件第十二课:电脑ADC值显示(上位机),记忆上次串口号,并用TrackBar控件显示ADC值

    C#之windows桌面软件第十二课:电脑ADC值显示(上位机),记忆上次串口号,并用TrackBar控件显示ADC值 using System; using System.Collections.G ...

最新文章

  1. canvas 图片不能缩放显示在画布的问题 忘记设置dw,dh
  2. java初学者的书中收获
  3. Java Web之POI操作Excel2016模板
  4. 每日一博 - Spring Boot Application as a Service
  5. ICCV 2019 | VrR-VG:聚焦视觉相关关系
  6. qt中如何模拟按钮点击_qt – 在Windows中模拟鼠标按钮单击
  7. UILabel的高度自适应
  8. matplotlib plt.plot
  9. uva 138——Street Numbers
  10. 图片批量转换pdf文件
  11. 云存储技术-Zookeeper集群的安装
  12. 用自己数据集训练Mask_RCNN代码
  13. 如何使用EasyRecovery巧妙恢复被误删的办公文档?
  14. Spring 核心框架体系结构
  15. linux网关管理,利用Linux打造安全的管理型网关
  16. 杭电oj-----Farm Irrigation(BFS)
  17. tftp路由器刷机修复工具_小米路由器mini刷写不死breed
  18. GitHub客户端上传本地代码
  19. VMware虚拟机安装Kali破解WiFi密码
  20. linux编辑文件发生错误E45: ‘readonly‘ option is set (add ! to override)

热门文章

  1. 大龄开发人员如何破局
  2. 什么是面向对象(OOP)
  3. dubbo与springboot的集成
  4. Windows下为PHP安装redis扩展
  5. 移动端 像素渲染流水线与GPU Hack
  6. 学习node js 之微信公众帐号接口开发 准备工作之三
  7. python中的id()函数及读取list的例子
  8. LongAdder解析 1
  9. vue-cli项目打包多个与static文件同级的静态资源目录(copy-webpack-plugin插件的使用)...
  10. 规范-编码规范总结(微信分销系统)