自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解C#串口監(jiān)聽(tīng)的實(shí)現(xiàn)

開(kāi)發(fā) 后端
C#串口監(jiān)聽(tīng)在實(shí)際開(kāi)發(fā)中是如何實(shí)現(xiàn)的呢?下面我們向你詳細(xì)介紹了C#串口監(jiān)聽(tīng)的實(shí)現(xiàn)過(guò)程,希望對(duì)你開(kāi)發(fā)C#串口監(jiān)聽(tīng)相關(guān)的工程有所幫助。

C#串口監(jiān)聽(tīng)的實(shí)現(xiàn)在 Visual Stdio 2005中,對(duì)于串口操作Framework提供了一個(gè)很好的類(lèi)接口-SerialPort,在這當(dāng)中,串口數(shù)據(jù)的讀取與寫(xiě)入有較大的不同。C#串口監(jiān)聽(tīng)的實(shí)現(xiàn)由于串口不知道數(shù)據(jù)何時(shí)到達(dá),因此有兩種方法可以實(shí)現(xiàn)C#串口監(jiān)聽(tīng)之串口數(shù)據(jù)的讀取。1.用線(xiàn)程實(shí)時(shí)讀串口2.用事件觸發(fā)方式實(shí)現(xiàn)。但由于線(xiàn)程實(shí)時(shí)讀串口的效率不是十分高效,因此比較好的方法是事件觸發(fā)的方式。在SerialPort類(lèi)中有DataReceived事件,當(dāng)串口的讀緩存有數(shù)據(jù)到達(dá)時(shí)則觸發(fā)DataReceived事件,其中SerialPort.ReceivedBytesThreshold屬性決定了當(dāng)串口讀緩存中數(shù)據(jù)多少個(gè)時(shí)才觸發(fā)DataReceived事件,默認(rèn)為1。

此外,SerialPort.DataReceived事件運(yùn)行比較特殊,其運(yùn)行在輔線(xiàn)程,不能與主線(xiàn)程中的顯示數(shù)據(jù)控件直接進(jìn)行數(shù)據(jù)傳輸,必須用間接的方式實(shí)現(xiàn)。

C#串口監(jiān)聽(tīng)實(shí)現(xiàn)一、創(chuàng)建WIndow項(xiàng)目,設(shè)計(jì)界面

C#串口監(jiān)聽(tīng)界面 

C#串口監(jiān)聽(tīng)實(shí)現(xiàn)二、編寫(xiě)實(shí)現(xiàn)代碼

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.IO.Ports;  
  9. //using Microsoft.VisualBasic.Devices;  
  10.  
  11. //C#串口監(jiān)聽(tīng)  
  12. namespace Demo  
  13. ...{  
  14. public partial class Form1 : Form  
  15. ...{  
  16. public Form1()  
  17. ...{  
  18. InitializeComponent();  
  19. }  
  20.  
  21. private SerialPort Sp = new SerialPort();  
  22. public delegate void HandleInterfaceUpdataDelegate(string text);  
  23. private HandleInterfaceUpdataDelegate interfaceUpdataHandle;  
  24.  
  25. private void Form1_Load(object sender, EventArgs e)  
  26. ...{  
  27. tbID.Focus();  
  28. BtPause.Enabled = false;  
  29. }  
  30.  
  31. private void UpdateTextBox(string text)  
  32. ...{  
  33. tbData.Text = text;  
  34. }  
  35.  
  36. public void Sp_DataReceived(object sender,  
  37. System.IO.Ports.SerialDataReceivedEventArgs e)  
  38. ...{  
  39. byte[] readBuffer = new byte[Sp.ReadBufferSize];  
  40. Sp.Read(readBuffer, 0, readBuffer.Length);  
  41. this.Invoke(interfaceUpdataHandle,  
  42.  new string[] ...{ Encoding.UTF8.GetString(readBuffer) });  
  43. }  
  44.  
  45. private void Form1_FormClosing(  
  46. object sender, FormClosingEventArgs e)  
  47. ...{  
  48. Sp.Close();  
  49. }  
  50.  
  51. private void btENT_Click(object sender, EventArgs e)  
  52. ...{  
  53. if ((tbID.Text.Trim() != "") &&   
  54. (cmRate.Text != ""))  
  55. ...{  
  56. interfaceUpdataHandle =   
  57. new HandleInterfaceUpdataDelegate(UpdateTextBox);  
  58. //C#串口監(jiān)聽(tīng)之實(shí)例化委托對(duì)象  
  59. Sp.PortName = tbID.Text.Trim();  
  60. serialPort1.BaudRate =   
  61. Convert.ToInt32(cmRate.Text.Trim());  
  62. Sp.Parity = Parity.None;  
  63. Sp.StopBits = StopBits.One;  
  64. Sp.DataReceived +=   
  65. new SerialDataReceivedEventHandler(Sp_DataReceived);  
  66. Sp.ReceivedBytesThreshold = 1;  
  67. try 
  68. ...{  
  69. Sp.Open();  
  70. tbID.ReadOnly = true;  
  71. BtPause.Enabled = true;  
  72. btENT.Enabled = false;  
  73. }  
  74. catch 
  75. ...{  
  76. MessageBox.Show(  
  77. "端口" + tbID.Text.Trim() + "打開(kāi)失?。?);  
  78. }  
  79. }//C#串口監(jiān)聽(tīng)  
  80. else 
  81. ...{  
  82. MessageBox.Show("請(qǐng)輸入正確的端口號(hào)和波特率!");  
  83. tbID.Focus();  
  84. }  
  85. }  
  86.  
  87. private void BtPause_Click(  
  88. object sender, EventArgs e)  
  89. ...{  
  90. Sp.Close();  
  91. tbID.ReadOnly = false;  
  92. btENT.Enabled = true;  
  93. BtPause.Enabled = false;  
  94. }  
  95. }//C#串口監(jiān)聽(tīng)  
  96. }  

C#串口監(jiān)聽(tīng)具體的實(shí)現(xiàn)操作就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#串口監(jiān)聽(tīng)的實(shí)現(xiàn)有所幫助。

【編輯推薦】

  1. C#串口操作的使用淺析
  2. 深入了解Mscomm控件
  3. C#串口操作實(shí)際應(yīng)用開(kāi)發(fā)詳解
  4. C#串口編程步驟詳解
  5. 創(chuàng)建C#串口通信程序詳解
責(zé)任編輯:仲衡 來(lái)源: CSDN
相關(guān)推薦

2009-08-25 17:13:57

C#串口編程

2024-06-03 10:11:13

2009-08-25 17:24:55

C#串口通信程序

2009-08-25 17:02:20

C#串口操作

2009-08-25 18:04:30

C#實(shí)現(xiàn)Singlet

2009-09-09 18:50:23

C# 加密RSA

2009-08-31 16:23:13

C#接口

2024-12-24 07:38:44

C#串口通信

2009-08-25 15:59:28

C#串口操作

2009-08-25 10:44:50

C#實(shí)現(xiàn)多語(yǔ)言

2009-09-09 18:57:26

C# 加密TripleDES

2009-08-21 10:13:02

C#異步初步

2009-08-26 09:22:44

C#實(shí)現(xiàn)打印功能

2009-08-26 11:07:36

C#打印窗體

2009-08-26 11:32:37

C#打印文檔

2009-08-26 12:59:08

C#打印設(shè)置

2009-09-03 14:55:56

C#實(shí)現(xiàn)DataGri

2009-09-09 12:55:59

C# TextBox事

2009-08-20 16:33:44

Socket異步通訊

2009-09-07 03:44:50

C#窗體間傳值
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)