.NET Framework Class Library
SerialPort..::.DataReceived Event

Represents the method that will handle the data received event of a SerialPort object.

Namespace:  System.IO.Ports
Assembly:  System (in System.dll)
Syntax

Visual Basic (Declaration)
Public Event DataReceived As SerialDataReceivedEventHandler
Visual Basic (Usage)
Dim instance As SerialPort
Dim handler As SerialDataReceivedEventHandler

AddHandler instance.DataReceived, handler
C#
public event SerialDataReceivedEventHandler DataReceived
Visual C++
public:
 event SerialDataReceivedEventHandler^ DataReceived {
    void add (SerialDataReceivedEventHandler^ value);
    void remove (SerialDataReceivedEventHandler^ value);
}
JScript
JScript does not support events.
Remarks

Serial received events can be caused by any of the items in the SerialData enumeration. Because the operating system determines whether to raise this event or not, not all parity errors may be reported.

PinChanged, DataReceived, and ErrorReceived events may be called out of order, and there may be a slight delay between when the underlying stream reports the error and when the event handler is executed. Only one event handler can execute at a time.

The DataReceived event is not guaranteed to be raised for every byte received. Use the BytesToRead property to determine how much data is left to be read in the buffer.

The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.

For more information about handling events, see Consuming Events.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0
See Also

Reference

Tags :


Community Content

Dan Randolph
Advice to use Invoke is incorrect
Do not use Invoke from this event to update form controls. Use the non-blocking BeginInvoke instead. Otherwise, calling the Close() method on the SerialPort object (and also the standard Form's Dispose method) will cause a deadlock.

Page view tracker