AN197 Silicon_Laboratories, AN197 Datasheet - Page 11

no-image

AN197

Manufacturer Part Number
AN197
Description
Serial Communications Guide FOR THE Cp210x
Manufacturer
Silicon_Laboratories
Datasheet
AN197
8. Application Design Notes
The functions used in section 3., section 4., and section 5. are Windows COMM API functions. The examples
provided are just samples of the recommended way of dealing with serial communication. For more specific
information on these functions, see the MSDN website at: http://msdn.microsoft.com/library/default.asp.
It should also be noted that the SetCommState() function does not save the settings between opening and closing
the COM port. As stated before, it is good practice to get the current settings after the COM port is opened, and
then restore them before it is closed.
All of the functions here will return an error code. It is a good idea to nest these functions in order to catch errors if
they occur by using the GetLastError() function. This will also solve any surprise removal problems by allowing the
discovery of an invalid handle to be found and dealt with. The example application (CP210xSerialTest) has several
cases that will detect surprise removal. In this example, there are checks on every function to make sure that the
return code is true. If it is not, then it will display where the error occurred in the output window. As long as correct
and supported settings are passed to the functions they should execute normally. Most failures can occur from
having an INVALID_HANDLE_VALUE, however, the handles must be set to this value after a surprise removal
occurs.
Because regular COM ports will always be visible, then data can always be written to them successfully, even if
there is no way to read it. However, because the CP210x is a virtual COM port, if the device is removed, then the
handle that it uses becomes invalid when trying to write to it. If for some reason the CP210x device is unplugged
the write will fail and ERROR_OPERATION_ABORTED will be returned by GetLastError(). When this happens, the
handle needs to be closed and then set to INVALID_HANDLE_VALUE. Alternatively, a regular COM port can
always be read from, but if there is no data then it will time out. When using the CP210x as the virtual COM port
and it is removed before a read occurs, then the read will fail and ERROR_ACCESS_DENIED will be returned by
GetLastError().
Again
when
this
happens,
the
handle
needs
to
be
closed
and
then
set
to
INVALID_HANDLE_VALUE.
9. References
MSDN - use this to search for specific Windows API functions
http://msdn.microsoft.com/
Serial Communication Reference
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp
Communication Resource Reference
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/communications_resources.asp
Rev. 0.6
11

Related parts for AN197