Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 10


the method returns, if any. A delegate enables application code to invoke a
method indirectly and safely. The examples use delegates in several ways. Dele-
gates specify routines to handle events that receive data, detect serial-port errors,
and detect changes on RS-232 status pins. Delegates also enable passing
received data to a form and performing write operations without blocking user
input or other operations performed by the application’s main thread.

 


Many applications need to detect COM-port data that arrives at unpredictable
times. Some applications need to collect and manage data that arrives over a
period of time. This section shows strategies for accomplishing these tasks.

!
1  


The ReadTimeout property introduced in Chapter 9 can keep a program
thread from wasting too much time waiting for data to arrive. When ReadTim-
eout is greater than zero, all of the read methods except ReadExisting will block
the thread that called the method until data is available or a timeout. A thread
that calls a blocking method can do no other work, including responding to
user input, until the method returns.
On a timeout when transmitting data, the application might want to set a vari-
able that tells the application to retry. On a timeout when waiting to receive
expected data, the application might want to send a message to notify the trans-
mitting computer of the problem.

  


To detect received data, applications can poll the port or use the DataReceived
event.



An application can poll a port at specific times when the application expects to
receive data or at defined intervals. If no data is available, a blocking read
method will wait for data or a timeout.
Free download pdf