如果,翻看我之前的博客,會找到一篇用I/O模擬IIC通信的程序文章。好吧,如果找不到可以點擊這里,這里就不在贅述了,系統也已經完全調試通過了。
今天的任務是,把測試得到的數據在上位機的界面上顯示出來,於是鍵盤手花了兩天的時間模仿着巨人的肩膀通過了用C#編寫的界面程序,界面很簡單就像下面顯示的一樣。
下面就一步一步給大伙展示一下我的程序吧。
C#非常的強大而且友好,串口的通信可以通過編程環境(這里我用的是Visual Studio 2010),如果有需要的話可以給我信息,我有完整版的安裝包。如下圖,簡單的調用和選擇就完成了串口的基本配置。
下面主要就是編程的問題了,在窗體Load的進程中可以完成串口的啟動
1 private void Form1_Load(object sender, EventArgs e) 2 { 3 serialPort1.PortName = "COM4"; 4 serialPort1.BaudRate = 9600; 5 serialPort1.Open(); 6 this.textBox1.Clear(); 7 thread = new Thread(CrossThreadFlush); 8 thread.IsBackground = true; 9 thread.Start(); 10 }
而后就是讀取數據的操作,這時候用到事件
1 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
在這個事件里面編程就可以了,但是在對窗體內文本進行操作的時候會發現出現了線程的沖突和錯誤。網上給出的一種解決方法是采用代理的方式具體的程序如下
1 public partial class Form1 : Form 2 { 3 private delegate void FlushClient(); //代理 4 Thread thread = null; 5 uint DateTemp; 6 uint Datefalg = 0; 7 uint counter = 0; 8 uint DateTemp1 = 0; 9 uint TMP1; 10 uint RH1; 11 uint PRESS1; 12 double TMP; 13 double RH; 14 double PRESS; 15 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 serialPort1.PortName = "COM4"; 25 serialPort1.BaudRate = 9600; 26 serialPort1.Open(); 27 this.textBox1.Clear(); 28 thread = new Thread(CrossThreadFlush); 29 thread.IsBackground = true; 30 thread.Start(); 31 } 32 33 34 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 35 { 36 counter=counter+1; 37 DateTemp = (uint)this.serialPort1.ReadByte(); 38 if (DateTemp == 0xF1) 39 { 40 Datefalg = 1; 41 DateTemp1 = 0; 42 DateTemp = 0; 43 counter=0; 44 } 45 if (DateTemp == 0xF2) 46 { 47 Datefalg = 2; 48 DateTemp1 = 0; 49 DateTemp = 0; 50 counter=0; 51 } 52 if (DateTemp == 0xF3) 53 { 54 Datefalg = 3; 55 DateTemp1 = 0; 56 DateTemp = 0; 57 counter=0; 58 } 59 if (Datefalg == 1 && DateTemp != 0xF1) 60 { if(counter==1) 61 DateTemp1 = DateTemp; 62 if (counter == 2) 63 { 64 TMP1 = DateTemp1 * 256 + DateTemp; 65 TMP = (0.01 * TMP1) - 40; 66 } 67 68 } 69 if (Datefalg == 2 && DateTemp != 0xF2) 70 { 71 if (counter == 1) 72 DateTemp1 = DateTemp; 73 if (counter == 2) 74 { 75 RH1 = DateTemp1 * 256 + DateTemp; 76 RH = (0.0405 * RH1) - 4-0.0000028*RH1*RH1; 77 } 78 79 } 80 if (Datefalg == 3 && DateTemp != 0xF3) 81 { 82 if (counter == 1) 83 DateTemp1 = DateTemp; 84 if (counter == 2) 85 { 86 PRESS1 = DateTemp1 * 256 + DateTemp; 87 PRESS = (PRESS1-16384)*90/16384 + 30; 88 } 89 } 90 } 91 92 private void CrossThreadFlush() 93 { 94 while (true) 95 { 96 //將sleep和無限循環放在等待異步的外面 97 Thread.Sleep(500); 98 ThreadFunction(); 99 } 100 } 101 102 private void ThreadFunction() 103 { 104 if (this.textBox1.InvokeRequired)//等待異步 105 { 106 FlushClient fc = new FlushClient(ThreadFunction); 107 this.Invoke(fc); //通過代理調用刷新方法 108 109 } 110 else 111 textBox1.Text = TMP.ToString("0.00"); 112 if (this.textBox2.InvokeRequired)//等待異步 113 { 114 FlushClient fc = new FlushClient(ThreadFunction); 115 this.Invoke(fc); //通過代理調用刷新方法 116 117 } 118 else 119 textBox2.Text = RH.ToString("0.00"); 120 if (this.textBox3.InvokeRequired)//等待異步 121 { 122 FlushClient fc = new FlushClient(ThreadFunction); 123 this.Invoke(fc); //通過代理調用刷新方法 124 125 } 126 else 127 textBox3.Text = PRESS.ToString("0.00"); 128 129 } 130 131 132 }
通過這樣的一番調試之后才得以程序正確的運行,由於工科程序員和計算機程序員的編程思想還是有差別的,所以寫的不好的地方請輕噴。
【重要通知】
為了豐富給位程序狗&硬件狗們的業余生活,我們做了一個艱難的決定,我們“泡泡魚工作室”開通了“硬件為王”公共訂閱號,微信號:king_hardware。關注智能硬件和互聯網創業,以及日常學習資料/搞笑段子推送。請猛掃下面的二維碼!
Powered By Bubble_fish ; Email:bubble_fish@yeah.net