AN197 Silicon_Laboratories, AN197 Datasheet - Page 6

no-image

AN197

Manufacturer Part Number
AN197
Description
Serial Communications Guide FOR THE Cp210x
Manufacturer
Silicon_Laboratories
Datasheet
AN197
5. Closing the COM Port
After all communication is finished, then the COM port should then be closed. First, the COM port should be set
back to its initial state, and then the handle to the COM port should be closed and set to an invalid handle. Example
code is shown below:
SetCommState(hMasterCOM, &dcbMasterInitState);
Delay(60);
CloseHandle(hMasterCOM);
hMasterCOM = INVALID_HANDLE_VALUE;
The SetCommState() function works the same as described in "3.3. Setting up a DCB Structure to Set the New
COM State" on page 2. A delay of 60 ms is used to make sure the settings have time to be set. Finally the device is
closed using the CloseHandle() function. This function just takes in the handle of the COM port. After this function
is called, it is important to set the variable to an INVALID_HANDLE_VALUE.
6. Sample program to Demonstrate Serial Communications
Included in the AN197SW.zip is a directory named "CP210xSerialTest" which contains the source code and
executables for a Visual Studio project that makes use of all the serial communication functions described in
section 3., section 4., and section 5. The program is a basic dialog based application that accepts two COM port
numbers, and then will send a test array of 64 bytes of data back and forth between them.
7. Discovering CP210x COM Port
To use the described functionality with a COM port, the number of the COM port needs to be known. In order to find
out the COM port number of a CP210x device, the VID, PID, and serial number are used to lookup a registry key.
This key is different between Windows XP/2000/Server 2003/Vista and Windows 98. Here are the keys that will
need to be looked up:
WinXP/2000/Server 2003/Vista (Driver Version 5.0 and higher):
HKLM\System\CurrentControlSet\Enum\USB\Vid_xxxx&Pid_yyyy\zzzz\DeviceParameters\PortName
WinXP/2000/Server 2003/Vista (Driver Version 4.40 and lower):
HKLM\System\CurrentControlSet\Enum\USB\Vid_xxxx&Pid_yyyy&Mi_00\zzzz_00\DeviceParameters\PortName
(where xxxx is the VID, yyyy is the PID, and zzzz is the serial number)
6
Rev. 0.6

Related parts for AN197