C#之windows桌面软件第七课:(下集)串口工具实现数据校验、用灯反应设备状态
C#之windows桌面软件第七课:(下集)串口工具实现数据校验、用灯反应设备状态
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 Reply
{public partial class Form1 : Form{byte DataSended = 0;byte[] DataToSend = new byte[] { 0x01, 0x02, 0x03 }; //数据发送public Form1(){InitializeComponent();System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;serialPort1.DataReceived += new SerialDataReceivedEventHandler(SerialPortDataReceived); //添加串口中断事件}private void SetOvlShape(int which) //填充颜色 {switch(which){case 0x01:ovalShape1.FillColor = Color.Green;break;case 0x11:ovalShape1.FillColor = Color.Red;break;case 0x02:ovalShape2.FillColor = Color.Green;break;case 0x12:ovalShape2.FillColor = Color.Red;break;case 0x03:ovalShape3.FillColor = Color.Green;break;case 0x13:ovalShape3.FillColor = Color.Red;break;case 0x04:ovalShape1.FillColor = Color.Green;ovalShape2.FillColor = Color.Green;ovalShape3.FillColor = Color.Green;break;case 0x14:ovalShape1.FillColor = Color.Red;ovalShape2.FillColor = Color.Red;ovalShape3.FillColor = Color.Red;break;default:break;}}private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e){byte DataReceived = (byte)(serialPort1.ReadByte()); //单字节读取SetOvlShape(DataReceived);}private void button1_Click(object sender, EventArgs e) //打开/关闭串口{if (serialPort1.IsOpen) //一堆处理……{try{serialPort1.Close();}catch{}button1.Text = "打开串口";}else{try{serialPort1.PortName = comboBox1.Text; //串口号 serialPort1.Open(); //打开}catch{MessageBox.Show("串口打开错误,请检查", "串口");}button1.Text = "关闭串口";}}private void SendDataToSerialPort(SerialPort MyPort, byte DataToSend) //单字节发送数据 {byte[] DatasToWrite = new byte[] { DataToSend }; //数据包if (serialPort1.IsOpen){try{MyPort.Write(DatasToWrite, 0, 1); //发数据}catch{MessageBox.Show("串口数据写入错误", "错误");}}}private void Button_Click(object sender, EventArgs e) //三个按键共用一个处理函数{Button MyButton = (Button)sender; //通过tag属性来区分DataSended = Convert.ToByte(MyButton.Tag);SendDataToSerialPort(serialPort1, DataToSend[DataSended - 1]); //发数据}}
}
www.DoYoung.net(部分代码来至杜洋工作室)
C#之windows桌面软件第七课:(下集)串口工具实现数据校验、用灯反应设备状态相关推荐
- C#之windows桌面软件第五课:串口助手实现定时关闭设备、鼠标移动使按钮颜色变化功能
本节在串口助手上实现: 1.定时关闭设备 2.移动鼠标使按钮颜色变换 Form1.cs代码如下: using System; using System.Collections.Generic; usi ...
- C#之windows桌面软件第四课:串口助手控制设备的开关
串口助手控制设备的开关 using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...
- C#之windows桌面软件第十三课:C#中常用的类有哪些?构造函数怎么用?
C#之windows桌面软件第十三课:C#中常用的类有哪些?构造函数怎么用? using System; using System.Collections.Generic; using System. ...
- C#之windows桌面软件第十一课:电脑ADC值显示(上位机)(多通道显示)
C#之windows桌面软件第十一课:电脑ADC值显示(上位机)(多通道显示) using System; using System.Collections.Generic; using System ...
- C#之windows桌面软件第十课:电脑ADC值显示(上位机)(单通道显示)
C#之windows桌面软件第十课:电脑ADC值显示(上位机) (单通道显示) using System; using System.Collections.Generic; using System ...
- C#之windows桌面软件第八课:汉字(GB2312)与编码(UTF-8)之间的相互转换
C#之windows桌面软件第八课:汉字(GB2312)与编码(UTF-8)之间的相互转换 using System; using System.Collections.Generic; using ...
- C#之windows桌面软件第六课:(上集)串口工具实现数据校验、用灯反应设备状态
C#之windows桌面软件第六课:(上集)串口工具实现数据校验.用灯反应设备状态 using System; using System.Collections.Generic; using Syst ...
- C#之windows桌面软件第三课:完整的串口调试助手
接上一节,这节来编写一个完整的串口调试助手! using System; using System.Collections.Generic; using System.ComponentModel; ...
- C#之windows桌面软件第十二课:电脑ADC值显示(上位机),记忆上次串口号,并用TrackBar控件显示ADC值
C#之windows桌面软件第十二课:电脑ADC值显示(上位机),记忆上次串口号,并用TrackBar控件显示ADC值 using System; using System.Collections.G ...
最新文章
- 什么是清华大学的“三好”学生?
- Azure SQL 数据库最新版本现已提供预览版
- vue组件的实例使用
- linux的安全性能,技术|Linux 系统安全性能检查小记
- weka分类器怎么设置样本类别_NeurIPS 2019 少样本学习研究亮点全解析
- 怎样在hdfs上创建多级目录文件夹_【HDFS API编程】第一个应用程序的开发-创建文件夹...
- C++安全方向(三):3.5 SHA-1 算法分析和代码演示
- 支付宝小程序升级,一统阿里大生态!
- 1.2 批量生成MySQL建表语句
- 《Mimics软件临床应用:计算机辅助外科手术》目录摘要
- windows7系统iis安装不了应该怎么办
- BASIC-9	特殊回文数 C语言版
- 软件测试cmm等级划分,CMM的五个等级及关键过程域
- SourceSafe的命令行
- CTF主办方指南之对抗搅屎棍
- Python超越函数积分运算以及绘图实现
- Assigned 函数
- 分析google adsence
- 使用计算机正确坐姿,电脑族的正确坐姿
- Android学习笔记(2)——探究活动