簡體   English   中英

如何在串口C#中檢查數據接收事件

[英]How to check the data received event in serial port C#

我正在嘗試使用串口讀取/寫入一些數據,

byte[] txTestData = new byte[] { 0x03, 0x0, 0x01, 0x0};
Serial.Write(txTestData, 0, txTestData.Length);

但我只想在從串口接收數據時讀取數據。

if(Data received from serial port)
{
   byte[] rxTestResponse = new byte[2];
   Serial.Read(rxTestResponse, 0, 2);
}

有沒有API告訴我收到的數據事件?

謝謝,Dattatarya

是的,有一個API可以告訴您何時收到數據,它被稱為事件。 閱讀更多有關活動的信息

附加事件:

mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

創建處理程序

private static void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
{
     byte[] rxTestResponse = new byte[2];
     Serial.Read(rxTestResponse, 0, 2);
}

每次SerialPort接收數據時,它都會調用DataReceived事件,並且您的事件處理程序將執行

暫無
暫無

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

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