C#之windows桌面软件第四课:串口助手控制设备的开关
生活随笔
收集整理的這篇文章主要介紹了
C#之windows桌面软件第四课:串口助手控制设备的开关
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? ? ? ? ? ?串口助手控制設(shè)備的開關(guān)
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;//關(guān)//device 2const byte DeviceOpen2 = 0x02;const byte DeviceClose2 = 0x82;//device 3const byte DeviceOpen3 = 0x03;const byte DeviceClose3 = 0x83;//SerialPort Write Bufferbyte[] SerialPortDataBuffer = new byte[1];public Form1(){InitializeComponent(); //窗口構(gòu)造}private void Form1_Load(object sender, EventArgs e){ovalShape1.FillColor = Color.Gray;//燈變灰SearchAndAddSerialToComboBox(serialPort1, comboBox1);}private void button1_Click(object sender, EventArgs e)//打開、關(guān)閉按鍵{if (serialPort1.IsOpen) //串口打開就關(guān)閉{try{serialPort1.Close();}catch { } //確保萬無一失ovalShape1.FillColor = Color.Gray;//燈變灰(當(dāng)button按鈕為“打開串口”)button1.Text = "打開串口";}else{try{serialPort1.PortName = comboBox1.Text; //端口號serialPort1.Open(); //打開端口ovalShape1.FillColor = Color.Green;//燈變綠(當(dāng)button按鈕為“關(guān)閉串口”)button1.Text = "關(guān)閉串口";}catch{MessageBox.Show("串口打開失敗","錯(cuò)誤");}}}private void WriteByteToSerialPort(byte data) //單字節(jié)寫入串口{byte[] Buffer = new byte [2]{0x00, data }; //定義數(shù)組if (serialPort1.IsOpen) //傳輸數(shù)據(jù)的前提是端口已打開{try{serialPort1.Write(Buffer, 0, 2); //寫數(shù)據(jù)}catch{MessageBox.Show("串口數(shù)據(jù)發(fā)送出錯(cuò),請檢查.", "錯(cuò)誤");//錯(cuò)誤處理}}else{MessageBox.Show("串口沒有打開!","錯(cuò)誤");//如果串口沒有打開,彈出提示窗口}}/*****************掃描封裝函數(shù)******************/private void SearchAndAddSerialToComboBox(SerialPort MyPort,ComboBox MyBox){ //將可用端口號添加到ComboBox//string[] MyString = new string[20]; //最多容納20個(gè),太多會影響調(diào)試效率string Buffer; //緩存MyBox.Items.Clear(); //清空ComboBox內(nèi)容//int count = 0;for (int i = 1; i < 20; i++) //循環(huán){try //核心原理是依靠try和catch完成遍歷{Buffer = "COM" + i.ToString();MyPort.PortName = Buffer;MyPort.Open(); //如果失敗,后面的代碼不會執(zhí)行// MyString[count] = Buffer;MyBox.Items.Add(Buffer); //打開成功,添加至下倆列表MyPort.Close(); //關(guān)閉//count++;}catch {//count--;}}//MyBox.Text = MyString[0]; //初始化}private void button2_Click(object sender, EventArgs e){WriteByteToSerialPort(DeviceOpen1); //器件一開}private void button3_Click(object sender, EventArgs e){WriteByteToSerialPort(DeviceClose1); //器件一關(guān)}private void button5_Click(object sender, EventArgs e){WriteByteToSerialPort(DeviceOpen2); //器件二開}private void button4_Click(object sender, EventArgs e){WriteByteToSerialPort(DeviceClose2); //器件二關(guān)}private void button7_Click(object sender, EventArgs e){WriteByteToSerialPort(DeviceOpen3); //器件三開}private void button6_Click(object sender, EventArgs e){WriteByteToSerialPort(DeviceClose3); //器件三關(guān)}private void button8_Click(object sender, EventArgs e){SearchAndAddSerialToComboBox(serialPort1, comboBox1); //掃描并講課用串口添加至下拉列表}private void ovalShape1_Click(object sender, EventArgs e){}} }www.DoYoung.net(部分代碼來至杜洋工作室)
總結(jié)
以上是生活随笔為你收集整理的C#之windows桌面软件第四课:串口助手控制设备的开关的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#之windows桌面软件第二课:向单
- 下一篇: C#之windows桌面软件第五课:串口