您好,登錄后才能下訂單哦!
C#中怎么實(shí)現(xiàn)監(jiān)聽(tīng)串口,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
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.用線程實(shí)時(shí)讀串口2.用事件觸發(fā)方式實(shí)現(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)行在輔線程,不能與主線程中的顯示數(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)實(shí)現(xiàn)二、編寫(xiě)實(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)聽(tīng) 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)聽(tīng)之實(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() + "打開(kāi)失??!"); } }//C#串口監(jiān)聽(tīng) 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)聽(tīng) }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。