Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 10


!   $ 5      
The DataReceived event provides an efficient way to detect received data. To
use the event, create a delegate for a routine that will execute when a DataRe-
ceived event occurs on an open port.

 ' Define a delegate class to handle DataReceived events.


Friend Delegate Sub SerialDataReceivedEventHandlerDelegate _
(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)

' Create an instance of the delegate.

Private SerialDataReceivedEventHandler1 _
As New SerialDataReceivedEventHandler(AddressOf DataReceived)

 // Define a delegate class to handle DataReceived events.


internal delegate void SerialDataReceivedEventHandlerDelegate
(object sender, SerialDataReceivedEventArgs e);

// Create an instance of the delegate.

private static SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 =
new SerialDataReceivedEventHandler( ComPort.DataReceived );
Assign the delegate as the handler routine to execute when a DataReceived
event occurs on a port:

 AddHandler myComPort.DataReceived, SerialDataReceivedEventHandler1


 myComPort.DataReceived += SerialDataReceivedEventHandler1;


The DataReceived routine can then read and process received data as needed.

 Friend Sub DataReceived _
(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)


' Place code to read and process data here.

End Sub
Free download pdf