Serial Port Complete - Latest Microcontroller projects

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

 internal void DataReceived( object sender, SerialDataReceivedEventArgs e )
{
' Place code to read and process data here.
}
The code in the routine can be identical to the code in the
tmrPollForReceivedData_Tick routine above or whatever the application
requires. The ReceivedBytesThreshold property specifies how many bytes must
be available before the event is raised.



$ 5   
 .


The DataReceived routine executes in a secondary thread. To display data, mes-
sages, or other information from the DataReceived routine on an application’s
form, an application must marshal the data from the DataReceived routine to
the form.
In the example below, the DataReceived routine reads data from the port and
passes the data in a call to AccessFormMarshal. The AccessFormMarshal rou-
tine uses a delegate to pass the received data to the AccessForm routine, which
displays the received data on the form.
To enable marshaling data, the form’s module defines a delegate class with
parameters that will hold the data to be passed to the form. This example passes
a string to display:

 ' MyMainForm is an instance of the form MainForm.


Friend MyMainForm As MainForm

Private Delegate Sub AccessFormMarshalDelegate(ByVal textToDisplay As String)

 // MyMainForm is an instance of the form MainForm.


internal static MainForm MyMainForm;

private delegate void AccessFormMarshalDelegate(string textToDisplay);
Free download pdf