c# 串口__c#

來源:互聯網
上載者:User

在.NET Framework 2.0中提供了SerialPort類,該類主要實現串口資料通訊等。本文章將本人在學習過程中從網路上搜集到的相關資訊寫出來供大家參考。

下面主要介紹該類的主要屬性(表1)和方法(表.2)。

如果需要瞭解更多的資訊請登入http://msdn.microsoft.com/zh-cn/library/system.io.ports.serialport(VS.80).aspx查看。

相關文章

《使用System.IO.Ports讀取COM口資料》

http://www.devasp.net/net/articles/display/727.html

 

 

表1                                                      SerialPort類的常用屬性

名  稱

說  明

BaseStream

擷取 SerialPort 對象的基礎 Stream 對象

BaudRate

擷取或設定串列傳輸速率

BreakState

擷取或設定中斷訊號狀態

BytesToRead

擷取接收緩衝區中資料的位元組數

BytesToWrite

擷取發送緩衝區中資料的位元組數

CDHolding

擷取連接埠的偵測載波行的狀態

CtsHolding

擷取“可以發送”行的狀態

DataBits

擷取或設定每個位元組的標準資料位元長度

DiscardNull

擷取或設定一個值,該值指示 Null 位元組在連接埠和接收緩衝區之間傳輸時是否被忽略

DsrHolding

擷取資料設定就緒 (DSR) 訊號的狀態

DtrEnable

擷取或設定一個值,該值在串列通訊過程中啟用資料終端機就緒 (DTR) 訊號

Encoding

擷取或設定傳輸前後文本轉換的位元組編碼

Handshake

擷取或設定序列埠資料轉送的握手協議

IsOpen

擷取一個值,該值指示 SerialPort 對象的開啟或關閉狀態

NewLine

擷取或設定用於解釋 ReadLine( )和WriteLine( )方法調用結束的值

Parity

擷取或設定同位檢查協議

續表

名  稱

說  明

ParityReplace

擷取或設定一個位元組,該位元組在發生同位錯誤時替換資料流中的無效位元組

PortName

擷取或設定通訊連接埠,包括但不限於所有可用的 COM 連接埠

ReadBufferSize

擷取或設定 SerialPort 輸入緩衝區的大小

ReadTimeout

擷取或設定讀取操作未完成時發生逾時之前的毫秒數

ReceivedBytesThreshold

擷取或設定 DataReceived 事件發生前內部輸入緩衝區中的位元組數

RtsEnable

擷取或設定一個值,該值指示在串列通訊中是否啟用請求發送 (RTS) 訊號

StopBits

擷取或設定每個位元組的標準停止位元

WriteBufferSize

擷取或設定序列埠輸出緩衝區的大小

WriteTimeout

擷取或設定寫入操作未完成時發生逾時之前的毫秒數

表2                                                     SerialPort類的常用方法

方 法 名 稱

說  明

Close

關閉連接埠串連,將 IsOpen 屬性設定為False,並釋放內部 Stream 對象

Open

開啟一個新的序列埠串連

Read

從 SerialPort 輸入緩衝區中讀取

ReadByte

從 SerialPort 輸入緩衝區中同步讀取一個位元組

ReadChar

從 SerialPort 輸入緩衝區中同步讀取一個字元

ReadLine

一直讀取到輸入緩衝區中的 NewLine 值

ReadTo

一直讀取到輸入緩衝區中指定 value 的字串

Write

已重載。將資料寫入序列埠輸出緩衝區

WriteLine

將指定的字串和 NewLine 值寫入輸出緩衝區

 

使用SerialPort類的方法:

方法一:

首先要添加

using System.IO;
using System.IO.Ports;

1...在類的內部定義SerialPort com;

2...開啟串口

            com = new SerialPort();
            com.BaudRate = 115200;
            com.PortName = "COM1";
            com.DataBits = 8;
            com.Open();//開啟串口

3...發送資料

            Byte[] TxData ={1,2,3,4,5,6,7,8 };
            com.Write(TxData, 0, 8);

4...接收資料

     4.1使用事件接收

     this.com.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.OnDataReceived);

private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)

    4.2使用線程接收

     接收資料啟動一個線程,使其接收。

在類的內部定義

        Thread _readThread;
        bool _keepReading;

開啟串口後啟動線程

            _keepReading = true;
            _readThread = new Thread(ReadPort);
            _readThread.Start();

線程函數

[c-sharp]  view plain copy private void ReadPort()   {       while (_keepReading)       {           if (com.IsOpen)           {               byte[] readBuffer = new byte[com.ReadBufferSize + 1];               try               {                   // If there are bytes available on the serial port,                   // Read returns up to "count" bytes, but will not block (wait)                   // for the remaining bytes. If there are no bytes available                   // on the serial port, Read will block until at least one byte                   // is available on the port, up until the ReadTimeout milliseconds                   // have elapsed, at which time a TimeoutException will be thrown.                   int count = com.Read(readBuffer, 0, com.ReadBufferSize);                   String SerialIn = System.Text.Encoding.ASCII.GetString(readBuffer, 0, count);                   if (count != 0)                       //byteToHexStr(readBuffer);                       ThreadFunction(byteToHexStr(readBuffer,count));               }               catch (TimeoutException) { }           }           else           {  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.