詳解C#串口監(jiān)聽的實(shí)現(xiàn)
C#串口監(jiān)聽的實(shí)現(xiàn)在 Visual Stdio 2005中,對于串口操作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í)例化委托對象
 - 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() + "打開失??!");
 - }
 - }//C#串口監(jiān)聽
 - else
 - ...{
 - MessageBox.Show("請輸入正確的端口號和波特率!");
 - tbID.Focus();
 - }
 - }
 - private void BtPause_Click(
 - object sender, EventArgs e)
 - ...{
 - Sp.Close();
 - tbID.ReadOnly = false;
 - btENT.Enabled = true;
 - BtPause.Enabled = false;
 - }
 - }//C#串口監(jiān)聽
 - }
 
C#串口監(jiān)聽具體的實(shí)現(xiàn)操作就向你介紹到這里,希望對你了解和學(xué)習(xí)C#串口監(jiān)聽的實(shí)現(xiàn)有所幫助。
【編輯推薦】















 
 
 

 
 
 
 