Winform 串口通訊之讀卡器


老板給我的第一個硬件就是一個讀卡器,

說讓我做一下試試,於是從網上查了查就寫了出來,相當的簡單。

但是后來還有一個地磅的串口通訊,我整整搞了一天。

 

在窗體類的構造函數中寫入

Form.CheckForIllegalCrossThreadCalls = false;

可以在線程外更新窗體,這樣就可以一直接收數據,一直更新ui了。

 

打開串口按鈕:

 1             //實例化
 2             SerialPort Myport = new SerialPort();
 3             //設置串口端口
 4             Myport.PortName = cbxPortName.Text;
 5             //設置比特率
 6             Myport.BaudRate = Convert.ToInt32(cmbbaud.Text);
 7             //設置數據位
 8             Myport.DataBits = Convert.ToInt32(cmbBits.Text);
 9             //根據選擇的數據,設置停止位
10             //if (cmbStop.SelectedIndex == 0)
11             //    Myport.StopBits = StopBits.None;
12             if (cmbStop.SelectedIndex == 1)
13                 Myport.StopBits = StopBits.One;
14             if (cmbStop.SelectedIndex == 2)
15                 Myport.StopBits = StopBits.OnePointFive;
16             if (cmbStop.SelectedIndex == 3)
17                 Myport.StopBits = StopBits.Two;
18 
19             //根據選擇的數據,設置奇偶校驗位
20             if (cmbParity.SelectedIndex == 0)
21                 Myport.Parity = Parity.Even;
22             if (cmbParity.SelectedIndex == 1)
23                 Myport.Parity = Parity.Mark;
24             if (cmbParity.SelectedIndex == 2)
25                 Myport.Parity = Parity.None;
26             if (cmbParity.SelectedIndex == 3)
27                 Myport.Parity = Parity.Odd;
28             if (cmbParity.SelectedIndex == 4)
29                 Myport.Parity = Parity.Space;
30 
31             //此委托應該是異步獲取數據的觸發事件,即是:當有串口有數據傳過來時觸發
32             Myport.DataReceived += new SerialDataReceivedEventHandler(port1_DataReceived);//DataReceived事件委托
33             //打開串口的方法
34             try
35             {
36                 Myport.Open();
37                 if (Myport.IsOpen)
38                 {
39                     MessageBox.Show("串口已打開");
40                 }
41                 else
42                 {
43                     MessageBox.Show("串口未能打開");
44                 }
45             }
46             catch (Exception ex)
47             {
48                 MessageBox.Show("串口未能打開"+ex.ToString());
49             }

 

關閉就使用 Myport.Close(); 在讀卡器的串口通訊中是不會有問題的

 

DataReceived事件委托的方法

 

 1  private void port1_DataReceived(object sender, SerialDataReceivedEventArgs e)
 2         {
 3             try
 4             {
 5                 string currentline = "";
 6                 //循環接收串口中的數據
 7                 while (Myport.BytesToRead > 0)
 8                 {
 9                     char ch = (char)Myport.ReadByte();
10                     currentline += ch.ToString();
11                 }
12                 //在這里對接收到的數據進行顯示
13                 //如果不在窗體加載的事件里寫上:Form.CheckForIllegalCrossThreadCalls = false; 就會報錯)
14                 this.txtReceive.Text = currentline;
15             }
16             catch (Exception ex)
17             {
18                 Console.WriteLine(ex.Message.ToString());
19             }
20         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM