Serial Port Complete - Latest Microcontroller projects

(lily) #1
Managing Ports and Transfers in .NET

Events provide a way to pass data and event notifications between modules
while requiring only loose coupling between modules. The module that raises
the event doesn’t have to know anything about a module that handles the event.
The module that raises the event just makes the event and its parameters avail-
able to any module that wants to receive data or take other action when the
event is raised. On being notified of an event, a module can run a routine that
accepts passed data and performs any other actions as needed.
For example, a ComPorts class can declare an event that passes text to a form
module:

 Friend Shared Event UserInterfaceData (ByVal textToAdd As String)


 internal delegate void UserInterfaceDataEventHandler(string textToAdd);


internal static event UserInterfaceDataEventHandler UserInterfaceData;
A routine that reads data from the port can raise the event:

 Dim newReceivedData As String


' Get data from the COM port.

newReceivedData = selectedPort.ReadExisting

' Raise the event to make the data available to other modules.

RaiseEvent UserInterfaceData(newReceivedData)

 string newReceivedData = null;


// Get data from the COM port.

newReceivedData = SelectedPort.ReadExisting();

// Raise the event to make the data available to other modules.

if (null != UserInterfaceData)
{
UserInterfaceData(newReceivedData);
}
To receive the data, the form’s module specifies a routine to execute when the
event occurs. The routine must have the same type and number of parameters
Free download pdf