簡體   English   中英

如何連續從SerialPort讀取

[英]How to read continuously from the SerialPort

我需要從Arduino板上連續讀取字節流。 為此,我創建了一個循環讀取Serialport並將ByteStream公開給其他類的方法:

public async void ReadSerialPort 
   {
     dataReader = new DataReader(SerialPort.InputStream)
     while (true) {
      uint ReadBufferLength = 16;

      Task < UInt32 > loadAsyncTask;


      loadAsyncTask = dataReader.LoadAsync(ReadBufferLength).AsTask();

      UInt32 rxbytes = await loadAsyncTask;

      RxBytes = new byte[rxbytes];
      if (rxbytes > 0) {
       datareader.ReadBytes(ByteStream);

      }
     }
    }

但是我認為這不是連續讀取串行端口的最有效方法,因為該方法需要幾毫秒的時間才能獲取字節流。 有讀取字節流的替代方法嗎?

使用事件從SerialPort讀取數據:

static void Main(string[] args)
{
    SerialPort sp=  new SerialPort();
    sp.DataReceived += Sp_DataReceived;
}

private static void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    throw new NotImplementedException();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM