1. 程式人生 > >基於C# Winform的串列埠資料接收

基於C# Winform的串列埠資料接收

https://blog.csdn.net/woolnil/article/details/78048272
今天,我分享一篇文章,講述 基於C# Winform的串列埠資料接收程式設計(適合大概明白WinForm程式設計的同學,沒有接觸過WinForm的同學下載原始碼學習)


首先新增串列埠(自行拖拽)

[csharp]  view plain  copy
  1. SerialPort serialPort1 = 
    new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);      //初始化串列埠設定  

建立一個數據接收方法

[csharp]  view plain  copy
  1. void Comm_DataReceived(
    object sender, SerialDataReceivedEventArgs e)  
  2.         {  
  3.   
  4.             Byte[] InputBuf = new Byte[128];  
  5.   
  6.             try  
  7.             {  
  8.                 serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead);                                //讀取緩衝區的資料直到“}”即0x7D為結束符  
  9.                 //InputBuf = UnicodeEncoding.Default.GetBytes(strRD);             //將得到的資料轉換成byte的格式  
  10.                 System.Threading.Thread.Sleep(50);  
  11.                 this.Invoke(disp_delegate, InputBuf);  
  12.   
  13.             }  
  14.             catch (TimeoutException ex)         //超時處理  
  15.             {  
  16.                 MessageBox.Show(ex.ToString());  
  17.             }  
  18.         }  

建立一個委託事件:(關於C#委託事件,下文會有參考文章)

[csharp]  view plain  copy
  1. public void DispUI(byte[] InputBuf)  
  2. {  
  3.     //textBox1.Text = Convert.ToString(InputBuf);  
  4.   
  5.     ASCIIEncoding encoding = new ASCIIEncoding();  
  6.     richTextBox1.Text = encoding.GetString(InputBuf);  
  7. }  

完整的程式程式碼:

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO.Ports;  
  10.   
  11. namespace WindowsFormsApplication3  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         SerialPort serialPort1 = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);      //初始化串列埠設定  
  16.         public delegate void Displaydelegate(byte[] InputBuf);  
  17.         Byte[] OutputBuf = new Byte[128];  
  18.         public Displaydelegate disp_delegate;  
  19.   
  20.         public Form1()  
  21.         {  
  22.             disp_delegate = new Displaydelegate(DispUI);  
  23.             serialPort1.DataReceived += new SerialDataReceivedEventHandler(Comm_DataReceived);  
  24.   
  25.             InitializeComponent();  
  26.         }  
  27.   
  28.         private void button1_Click(object sender, EventArgs e)  
  29.         {  
  30.             try  
  31.             {  
  32.                 if (button1.Text == "開啟")  
  33.                 {  
  34.                     serialPort1.Open();  
  35.                     button1.Text = "關閉";  
  36.                 }  
  37.                 else  
  38.                 {  
  39.                     serialPort1.Close();  
  40.                     button1.Text = "開啟";  
  41.                 }  
  42.   
  43.             }  
  44.             catch (Exception ex)  
  45.             {  
  46.                 MessageBox.Show(ex.Message, "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  47.                 return;  
  48.             }  
  49.         }  
  50.   
  51.         void Comm_DataReceived(object sender, SerialDataReceivedEventArgs e)  
  52.         {  
  53.   
  54.             Byte[] InputBuf = new Byte[128];  
  55.   
  56.             try  
  57.             {  
  58.                 serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead);                                //讀取緩衝區的資料直到“}”即0x7D為結束符  
  59.                 //InputBuf = UnicodeEncoding.Default.GetBytes(strRD);             //將得到的資料轉換成byte的格式  
  60.                 System.Threading.Thread.Sleep(50);  
  61.                 this.Invoke(disp_delegate, InputBuf);  
  62.   
  63.             }  
  64.             catch (TimeoutException ex)         //超時處理  
  65.             {  
  66.                 MessageBox.Show(ex.ToString());  
  67.             }  
  68.         }  
  69.   
  70.         public void DispUI(byte[] InputBuf)  
  71.         {  
  72.             //textBox1.Text = Convert.ToString(InputBuf);  
  73.   
  74.             ASCIIEncoding encoding = new ASCIIEncoding();  
  75.             richTextBox1.Text = encoding.GetString(InputBuf);  
  76.         }  
  77.     }  
  78. }