Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 10


# +


Some errors in COM-port communications raise exceptions while others raise
the SerialPort object’s ErrorReceived event. Application code can also use
checksums to detect errors in transmitted data.

+ 


This chapter and Chapter 9 have discussed some of the COM-port events and
conditions that can raise exceptions. In general, application code can minimize
the likelihood of exceptions by checking for valid ports and port states before
accessing a port. When needed, a Catch block can display messages to help the
user fix the problem by selecting another port, attaching a port, or taking other
action.

1 +   8  + 8 


Several error conditions can raise SerialPort’s ErrorReceived event. The Error-
Received code below is similar to the code for the PinChanged and DataRe-
ceived events in this chapter.
To use the ErrorReceived event, the application must assign a routine that will
execute when an ErrorReceived event occurs on an open port:

 ' Define a delegate class to handle ErrorReceived events.


Friend Delegate Sub SerialErrorReceivedEventHandlerDelegate _
(ByVal sender As Object, ByVal e As SerialErrorReceivedEventArgs)

' Create an instance of the delegate.

Private SerialErrorReceivedEventHandler1 _
As New SerialErrorReceivedEventHandler(AddressOf ErrorReceived)
Free download pdf