AN197 Silicon_Laboratories, AN197 Datasheet - Page 7

no-image

AN197

Manufacturer Part Number
AN197
Description
Serial Communications Guide FOR THE Cp210x
Manufacturer
Silicon_Laboratories
Datasheet
7.1. Windows XP/2000/Server 2003/Vista
To find the port number in Windows XP/2000/Server 2003/Vista, the corresponding key listed above needs to be
opened. This is done using several registry calls using Windows API functions. The function is passed the VID,
PID, and serial number, and the return value is either the port number that the CP210x is located on, or a –1 if
there is a failure. The function below will find the CP210x port number in Windows XP/2000/Server 2003/Vista (this
function can be copied straight into application code that needs to discover the COM port number):
int GetPortNumXP2000Vista(WORD vid, WORD pid, char* ser)
{
HKLM\System\CurrentControlSet\Enum\USB\Vid_xxxx&Pid_yyyy&Mi_00\zzzz_00\DeviceParameters\PortN
ame
KEY_READ, &tmpKey))
- version less than 5.0
version greater than or equal to 5.0
pid, ser);
&tmpPortKey))
(unsigned char *)portString, &length))
//Variables used for Registry access
HKEY tmpKey, tmpSubKey, tmpPortKey;
CString portKeyString;
DWORD valtype;
char* portString;
DWORD length = 100;
portString = new char[101];
//Set portnum to -1, so if there is an error we will
//know by returning a negative port value
int portNum = -1;
//
//
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\", 0,
{
if (ERROR_SUCCESS == RegOpenKey(tmpKey, "Enum\\USB\\", &tmpSubKey))
{
Open keys to get to the key where the port number is located. This key is:
//Loop through and replace spaces for WinXP2000Vista
int i = 0;
while (ser[i] != '\0')
{
}
//The portkey string should look like this
//"Vid_XXXX&Pid_XXXX&MI_00\\XXXX_00" where the XXXX's are Vid, Pid and serial string
//"Vid_XXXX&Pid_XXXX\\XXXX" where the XXXX's are Vid, Pid and serial string -
portKeyString.Format("Vid_%04x&Pid_%04x&Mi_00\\%s_00\\Device
//If the portkey string is in the registry, then go ahead and open the portname
if
{
if (ser[i] == 0x20)
i++;
if (ERROR_SUCCESS == RegQueryValueEx(tmpPortKey, "PortName", NULL, &valtype,
{
(ERROR_SUCCESS
ser[i] = '_';
// When we obtain this key, it will be in string format of
// "COMXX" where XX is the port. Simply make the first three
// elements of the string 0, and call the atoi function to obtain
// the number of the port.
==
RegOpenKeyEx(tmpSubKey,
Rev. 0.6
portKeyString,
Parameters\\",
0,
AN197
KEY_READ,
vid,
7

Related parts for AN197