SerialPort.DataReceived 事件

定义

指示已通过由 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 

事件类型

示例

本示例将 添加到 SerialDataReceivedEventHandlerDataReceived 以读取 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

PinChangedDataReceivedErrorReceived 事件可能无序调用,并且基础流报告错误和执行事件处理程序之间可能存在轻微延迟。 一次只能执行一个事件处理程序。

DataReceived不保证为每个接收的字节引发 事件。 BytesToRead使用 属性确定缓冲区中剩余的数据量。

DataReceived从 对象接收数据时,在辅助线程上引发 事件SerialPort。 由于此事件是在辅助线程而不是main线程上引发的,因此尝试修改main线程中的某些元素(如 UI 元素)可能会引发线程异常。 如果需要修改 main FormControl中的元素,请使用 将更改请求发布回 Invoke,这将在正确的线程上执行工作。

有关处理事件的详细信息,请参阅 处理和引发事件

适用于