Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 10


 ' Define a delegate class to handle writes to the port.


Friend Delegate Function WriteToComPortDelegate(ByVal textToWrite As String) _
As Boolean

' Create an instance of the delegate.

Friend WriteToComPortDelegate1 _
As New WriteToComPortDelegate(AddressOf WriteToComPort)

 // Define a delegate class to handle writes to the port.


internal delegate Boolean WriteToComPortDelegate(string textToWrite);

// Create an instance of the delegate.

internal static WriteToComPortDelegate WriteToComPortDelegate1 =
new WriteToComPortDelegate(WriteToComPort);
When the application has data to send, call the delegate’s BeginInvoke method,
passing the delegate’s parameter (textToWrite), the address of an AsyncCallback
routine to execute when the write operation completes (WriteCompleted), and
a parameter that can be either Nothing/null or a value to pass to the callback
routine (msg):

 Dim ar As IAsyncResult
Dim msg As String
Dim textToWrite As String


' Text to write to the port.

textToWrite = "hello, world"

' A value to pass to the callback routine (optional).

msg = DateTime.Now.ToString

' Call a routine to write to the COM port.

ar = WriteToComPortDelegate1.BeginInvoke (textToWrite, _
New AsyncCallback(AddressOf WriteCompleted), msg)
Free download pdf