C#之windows桌面软件第十一课:电脑ADC值显示(上位机)(多通道显示)
生活随笔
收集整理的這篇文章主要介紹了
C#之windows桌面软件第十一课:电脑ADC值显示(上位机)(多通道显示)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?C#之windows桌面軟件第十一課:電腦ADC值顯示(上位機)(多通道顯示)
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 Adc_Display {public partial class Form1 : Form{public Form1(){InitializeComponent();serialPort1.DataReceived += new SerialDataReceivedEventHandler(PortDataReceivedEvent); //串口數據接收事件 System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; //不檢查線程間的操作 }//剛開始讓窗口遮蔽住下面的進度條private void Form1_Load(object sender, EventArgs e){this.Size = new Size(620, 107);//縮小窗口}//定義10個進度條對象ProgressBar[] GetProgressBar(){return new ProgressBar[] { progressBar1, progressBar2, progressBar3, progressBar4, progressBar5, progressBar6, progressBar7, progressBar8, progressBar9, progressBar10,};//返回一個對象數組}//串口數據接收private void PortDataReceivedEvent(object sender, SerialDataReceivedEventArgs args) {ProgressBar[] MyProgressBar = GetProgressBar();byte[] Data = new byte[serialPort1.BytesToRead]; serialPort1.Read(Data,0,Data.Length);//讀foreach (byte MyData in Data){for(int i = 1;i < 10;i++){MyProgressBar[10 - i].Value = MyProgressBar[10 - i - 1].Value;//把9的值給10,8給9,,,,,,}progressBar1.Value = (int)MyData;/*把值顯示在textBox1中*/string str = MyData.ToString("x");textBox1.Text = "0x" + (str.Length == 1 ? "0" + str : str);}}private void button1_Click(object sender, EventArgs e){if (serialPort1.IsOpen)//判斷{groupBox2.Visible = false; //第二個框框不可見this.Size = new Size(620, 107); //縮小serialPort1.Close();//此處可不加try,catchProgressBar[] MyProgeressBar = GetProgressBar();foreach(ProgressBar theBar in MyProgeressBar)//遍歷,每個progressbar歸零{theBar.Value = 0;}button1.Text = "打開串口";}else{try{serialPort1.PortName = comboBox1.Text;//串口號serialPort1.Open(); //打開groupBox2.Visible = true; //第二個框框可見this.Size = new Size(620, 436); //放大button1.Text = "關閉串口";//按鍵標題}catch{MessageBox.Show("串口打開錯誤", "錯誤");}}}} }www.DoYoung.net(部分代碼來至杜洋工作室)
總結
以上是生活随笔為你收集整理的C#之windows桌面软件第十一课:电脑ADC值显示(上位机)(多通道显示)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#之windows桌面软件第十课:电脑
- 下一篇: C#之windows桌面软件第十二课:电