C#之windows桌面软件第六课:(上集)串口工具实现数据校验、用灯反应设备状态
生活随笔
收集整理的這篇文章主要介紹了
C#之windows桌面软件第六课:(上集)串口工具实现数据校验、用灯反应设备状态
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
C#之windows桌面軟件第六課:(上集)串口工具實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)、用燈反應(yīng)設(shè)備狀態(tài)
?
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 }; //數(shù)據(jù)發(fā)送public Form1(){InitializeComponent();System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;/*此句代碼的作用:把我們自己寫的函數(shù)添加到系統(tǒng)中,使系統(tǒng)可以使用我們自己定義的函數(shù)*/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){//單片機(jī)中常用的類型是無符號(hào)型(在取反的時(shí)候不做無符號(hào)處理),而C#默認(rèn)數(shù)據(jù)類型是整型,所以前面加(byte)強(qiáng)制轉(zhuǎn)換byte DataReceived = (byte)(~serialPort1.ReadByte()); //單字節(jié)讀取。(取反校驗(yàn)的優(yōu)點(diǎn)是不破壞原始數(shù)據(jù))try{timer1.Stop(); //關(guān)定時(shí)器}catch { }if (DataSended == 0) //防止下位機(jī)亂發(fā),不處理return;SetOvlShape(DataReceived);//燈try{if (DataToSend[DataSended - 1] == DataReceived) //校驗(yàn)數(shù)據(jù){MessageBox.Show("數(shù)據(jù)校驗(yàn)成功", "成功!"); //彈出提示}else{MessageBox.Show("數(shù)據(jù)校驗(yàn)失敗", "數(shù)據(jù)校驗(yàn)失敗");}}catch{}}private void button1_Click(object sender, EventArgs e) //打開/關(guān)閉串口{if (serialPort1.IsOpen) //一堆處理……{try{serialPort1.Close();}catch{}button1.Text = "打開串口";}else{try{serialPort1.PortName = comboBox1.Text; //串口號(hào) serialPort1.Open(); //打開}catch{MessageBox.Show("串口打開錯(cuò)誤,請(qǐng)檢查", "串口");}button1.Text = "關(guān)閉串口";}}//單字節(jié)發(fā)送數(shù)據(jù)到串口private void SendDataToSerialPort(SerialPort MyPort, byte DataToSend) {byte[] DatasToWrite = new byte[] { DataToSend }; //數(shù)據(jù)包if (serialPort1.IsOpen){try{MyPort.Write(DatasToWrite, 0, 1); //發(fā)數(shù)據(jù)timer1.Interval = 3 * 1000; //設(shè)定超時(shí)時(shí)間(3S)timer1.Start(); //定時(shí)器}catch{MessageBox.Show("串口數(shù)據(jù)寫入錯(cuò)誤", "錯(cuò)誤");}}}//三個(gè)按鍵共用一個(gè)處理函數(shù)private void Button_Click(object sender, EventArgs e) {Button MyButton = (Button)sender; //定義一個(gè)按鍵對(duì)象 //通過tag屬性來區(qū)分DataSended = Convert.ToByte(MyButton.Tag); //字符串 -》數(shù)字型 (用tag的值來區(qū)分是那個(gè)按鍵)SendDataToSerialPort(serialPort1, DataToSend[DataSended - 1]); //向串口發(fā)送單字節(jié)數(shù)據(jù)}//定時(shí)器事件(3s后會(huì)執(zhí)行此函數(shù))private void timer1_Tick(object sender, EventArgs e) {string MyStr = DataSended.ToString() + "路數(shù)據(jù)校驗(yàn)超時(shí),請(qǐng)檢查"; //Messagebox內(nèi)容 timer1.Stop();MessageBox.Show(MyStr, "錯(cuò)誤");}} }www.DoYoung.net(部分代碼來至杜洋工作室)
總結(jié)
以上是生活随笔為你收集整理的C#之windows桌面软件第六课:(上集)串口工具实现数据校验、用灯反应设备状态的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#之windows桌面软件第五课:串口
- 下一篇: C#之windows桌面软件第七课:(下