SerialPort.DataReceived 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示已從 SerialPort 物件所代表的連接埠上接收資料。
public:
event System::IO::Ports::SerialDataReceivedEventHandler ^ DataReceived;
public event System.IO.Ports.SerialDataReceivedEventHandler DataReceived;
member this.DataReceived : System.IO.Ports.SerialDataReceivedEventHandler
Public Custom Event DataReceived As SerialDataReceivedEventHandler
Public Event DataReceived As SerialDataReceivedEventHandler
事件類型
範例
本範例會將 新增 SerialDataReceivedEventHandler 至 DataReceived ,以讀取 COM1 埠上接收的所有可用數據。 請注意,若要測試此程式碼,必須將硬體連結至將傳送數據的 COM1。
#using <System.dll>
using namespace System;
using namespace System::IO::Ports;
ref class PortDataReceived
{
public:
static void Main()
{
SerialPort^ mySerialPort = gcnew SerialPort("COM1");
mySerialPort->BaudRate = 9600;
mySerialPort->Parity = Parity::None;
mySerialPort->StopBits = StopBits::One;
mySerialPort->DataBits = 8;
mySerialPort->Handshake = Handshake::None;
mySerialPort->RtsEnable = true;
mySerialPort->DataReceived += gcnew SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort->Open();
Console::WriteLine("Press any key to continue...");
Console::WriteLine();
Console::ReadKey();
mySerialPort->Close();
}
private:
static void DataReceivedHandler(
Object^ sender,
SerialDataReceivedEventArgs^ e)
{
SerialPort^ sp = (SerialPort^)sender;
String^ indata = sp->ReadExisting();
Console::WriteLine("Data Received:");
Console::Write(indata);
}
};
int main()
{
PortDataReceived::Main();
}
using System;
using System.IO.Ports;
class PortDataReceived
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM1");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
}
}
Imports System.IO.Ports
Class PortDataReceived
Public Shared Sub Main()
Dim mySerialPort As New SerialPort("COM1")
mySerialPort.BaudRate = 9600
mySerialPort.Parity = Parity.None
mySerialPort.StopBits = StopBits.One
mySerialPort.DataBits = 8
mySerialPort.Handshake = Handshake.None
mySerialPort.RtsEnable = True
AddHandler mySerialPort.DataReceived, AddressOf DataReceivedHandler
mySerialPort.Open()
Console.WriteLine("Press any key to continue...")
Console.WriteLine()
Console.ReadKey()
mySerialPort.Close()
End Sub
Private Shared Sub DataReceivedHandler(
sender As Object,
e As SerialDataReceivedEventArgs)
Dim sp As SerialPort = CType(sender, SerialPort)
Dim indata As String = sp.ReadExisting()
Console.WriteLine("Data Received:")
Console.Write(indata)
End Sub
End Class
備註
數據事件可能是由 列舉中的任何 SerialData 專案所造成。 因為操作系統會判斷是否要引發此事件,所以可能不會報告所有同位錯誤。
DataReceived如果收到 Eof 字元,不論內部輸入緩衝區中的位元組數目和屬性的值ReceivedBytesThreshold為何,也會引發事件。
PinChanged、 DataReceived和 ErrorReceived 事件可能會依序呼叫,而且基礎數據流報告錯誤時和執行事件處理程式時,可能會稍有延遲。 一次只能執行一個事件處理程式。
DataReceived事件不保證會針對收到的每個位元組引發。 BytesToRead使用 屬性來判斷緩衝區中要讀取的數據量。
DataReceived從 SerialPort 物件接收數據時,會在次要線程上引發事件。 因為這個事件是在次要線程上引發,而不是主線程,所以嘗試修改主線程中的某些元素,例如UI元素可能會引發線程例外狀況。 如果需要修改主要 Form 或 Control中的專案,請使用 將變更要求張貼回 Invoke,這會在適當的線程上執行工作。
如需處理事件的詳細資訊,請參閱 處理和引發事件。