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 1:ovalShape1.FillColor = Color.Green;ovalShape2.FillColor = Color.Red;ovalShape3.FillColor = Color.Red;break;case 2:ovalShape1.FillColor = Color.Red;ovalShape2.FillColor = Color.Green;ovalShape3.FillColor = Color.Red;break;case 3:ovalShape1.FillColor = Color.Red;ovalShape2.FillColor = Color.Red;ovalShape3.FillColor = Color.Green;break;case 4:ovalShape1.FillColor = Color.Green;ovalShape2.FillColor = Color.Green;ovalShape3.FillColor = Color.Green;break;default:break;}}//串口中断private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e){//单片机中常用的类型是无符号型(在取反的时候不做无符号处理),而C#默认数据类型是整型,所以前面加(byte)强制转换byte DataReceived = (byte)(~serialPort1.ReadByte()); //单字节读取。(取反校验的优点是不破坏原始数据)try{timer1.Stop(); //关定时器}catch { }if (DataSended == 0) //防止下位机乱发,不处理return;SetOvlShape(DataReceived);//灯try{if (DataToSend[DataSended - 1] == DataReceived) //校验数据{MessageBox.Show("数据校验成功", "成功!"); //弹出提示}else{MessageBox.Show("数据校验失败", "数据校验失败");}}catch{}}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); //发数据timer1.Interval = 3 * 1000; //设定超时时间(3S)timer1.Start(); //定时器}catch{MessageBox.Show("串口数据写入错误", "错误");}}}//三个按键共用一个处理函数private void Button_Click(object sender, EventArgs e) {Button MyButton = (Button)sender; //定义一个按键对象 //通过tag属性来区分DataSended = Convert.ToByte(MyButton.Tag); //字符串 -》数字型 (用tag的值来区分是那个按键)SendDataToSerialPort(serialPort1, DataToSend[DataSended - 1]); //向串口发送单字节数据}//定时器事件(3s后会执行此函数)private void timer1_Tick(object sender, EventArgs e) {string MyStr = DataSended.ToString() + "路数据校验超时,请检查"; //Messagebox内容 timer1.Stop();MessageBox.Show(MyStr, "错误");}}
}
www.DoYoung.net(部分代码来至杜洋工作室)
C#之windows桌面软件第六课:(上集)串口工具实现数据校验、用灯反应设备状态相关推荐
- 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桌面软件第五课:串口助手实现定时关闭设备、鼠标移动使按钮颜色变化功能
本节在串口助手上实现: 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桌面软件第十二课:电脑ADC值显示(上位机),记忆上次串口号,并用TrackBar控件显示ADC值
C#之windows桌面软件第十二课:电脑ADC值显示(上位机),记忆上次串口号,并用TrackBar控件显示ADC值 using System; using System.Collections.G ...
最新文章
- 50 行 Python 代码,带你追到最心爱的人
- 使用 supervisor 管理进程
- [bzoj1187][HNOI2007]神奇游乐园
- Exception in thread main java.time.format.DateTimeParseException: Text '31-Dec-13' could not be pa
- Ubuntu启动显示System program problem detected 原因及解决方法
- 从输入url开始,完善前端体系架构
- Mac电脑上java如何手动释放内存?
- js基础-14-JS阻止事件冒泡和默认事件
- 2019软件测试最新视频教程大合集汇总
- 【叨、校长】一个基于Extjs、Pushlet美轮美奂的Web聊天室
- 京东物流数据安全体系
- 服务端渲染技术之Nuxt.js的详细使用
- linux之mail命令发邮件
- 南头中学2021年高考成绩查询,深圳新安中学和南头中学哪个好
- python实战笔记之(8):下载知乎视频
- 安装宝塔远程工具流程
- 【教你赚钱】5分钟成为副业致富的独立开发者
- 支持爱普生r330的打印服务器,软件让照片打印更专业_爱普生 R330_办公打印评测试用-中关村在线...
- 【微电子】半导体器件物理:1-1半导体材料与晶体结构
- MacRansom:首款以RaaS服务形式出现的Mac勒索软件