C#中怎么實(shí)現(xiàn)監(jiān)聽串口,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)是一家專業(yè)提供閩侯企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、html5、小程序制作等業(yè)務(wù)。10年已為閩侯眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
C#串口監(jiān)聽的實(shí)現(xiàn)在 Visual Stdio 2005中,對(duì)于串口操作Framework提供了一個(gè)很好的類接口-SerialPort,在這當(dāng)中,串口數(shù)據(jù)的讀取與寫入有較大的不同。C#串口監(jiān)聽的實(shí)現(xiàn)由于串口不知道數(shù)據(jù)何時(shí)到達(dá),因此有兩種方法可以實(shí)現(xiàn)C#串口監(jiān)聽之串口數(shù)據(jù)的讀取。1.用線程實(shí)時(shí)讀串口2.用事件觸發(fā)方式實(shí)現(xiàn)。但由于線程實(shí)時(shí)讀串口的效率不是十分高效,因此比較好的方法是事件觸發(fā)的方式。在SerialPort類中有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)行在輔線程,不能與主線程中的顯示數(shù)據(jù)控件直接進(jìn)行數(shù)據(jù)傳輸,必須用間接的方式實(shí)現(xiàn)。
C#串口監(jiān)聽實(shí)現(xiàn)一、創(chuàng)建WIndow項(xiàng)目,設(shè)計(jì)界面
C#串口監(jiān)聽實(shí)現(xiàn)二、編寫實(shí)現(xiàn)代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO.Ports; //using Microsoft.VisualBasic.Devices; //C#串口監(jiān)聽 namespace Demo ...{ public partial class Form1 : Form ...{ public Form1() ...{ InitializeComponent(); } private SerialPort Sp = new SerialPort(); public delegate void HandleInterfaceUpdataDelegate(string text); private HandleInterfaceUpdataDelegate interfaceUpdataHandle; private void Form1_Load(object sender, EventArgs e) ...{ tbID.Focus(); BtPause.Enabled = false; } private void UpdateTextBox(string text) ...{ tbData.Text = text; } public void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) ...{ byte[] readBuffer = new byte[Sp.ReadBufferSize]; Sp.Read(readBuffer, 0, readBuffer.Length); this.Invoke(interfaceUpdataHandle, new string[] ...{ Encoding.UTF8.GetString(readBuffer) }); } private void Form1_FormClosing( object sender, FormClosingEventArgs e) ...{ Sp.Close(); } private void btENT_Click(object sender, EventArgs e) ...{ if ((tbID.Text.Trim() != "") && (cmRate.Text != "")) ...{ interfaceUpdataHandle = new HandleInterfaceUpdataDelegate(UpdateTextBox); //C#串口監(jiān)聽之實(shí)例化委托對(duì)象 Sp.PortName = tbID.Text.Trim(); serialPort1.BaudRate = Convert.ToInt32(cmRate.Text.Trim()); Sp.Parity = Parity.None; Sp.StopBits = StopBits.One; Sp.DataReceived += new SerialDataReceivedEventHandler(Sp_DataReceived); Sp.ReceivedBytesThreshold = 1; try ...{ Sp.Open(); tbID.ReadOnly = true; BtPause.Enabled = true; btENT.Enabled = false; } catch ...{ MessageBox.Show( "端口" + tbID.Text.Trim() + "打開失?。?quot;); } }//C#串口監(jiān)聽 else ...{ MessageBox.Show("請(qǐng)輸入正確的端口號(hào)和波特率!"); tbID.Focus(); } } private void BtPause_Click( object sender, EventArgs e) ...{ Sp.Close(); tbID.ReadOnly = false; btENT.Enabled = true; BtPause.Enabled = false; } }//C#串口監(jiān)聽 }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
文章標(biāo)題:C#中怎么實(shí)現(xiàn)監(jiān)聽串口
標(biāo)題URL:http://www.rwnh.cn/article30/ipcspo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、用戶體驗(yàn)、虛擬主機(jī)、動(dòng)態(tài)網(wǎng)站、ChatGPT、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)