Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 9


 if (!( myComPort.IsOpen ))
{
myComPort.Open();
}
Of course it’s possible that another resource might open the port after reading
IsOpen but before calling the Open method. In that case, the application can
catch the exception and display a message or take other action as needed:


 Try
myComPort.Open()


Catch ex As UnauthorizedAccessException

MessageBox.Show(ex.Message)

End Try

 try
{
myComPort.Open();
}
catch ( UnauthorizedAccessException ex )
{
MessageBox.Show(ex.Message);
}
The examples that follow in this and following chapters assume the application
has an open port called myComPort.


1  


An application might want a way to bail out if the remote port isn’t ready to
send or receive data. The ReadTimeout and WriteTimeout properties set the
amount of time to wait in milliseconds:

 myComPort.ReadTimeout = 3000
myComPort.WriteTimeout = 5000


 myComPort.ReadTimeout = 3000;
myComPort.WriteTimeout = 5000;

Free download pdf