| Finding the Com Port that is assigned to
the USB to TTL cable. The following code extract
demonstrates how to search the registry for the COM port number that is
assigned to the USB to TTL cable.
C Example
|
#include
<string.h>
void FindSerialComm(void)
{
DWORD Index = 0;
DWORD dwValueNameLength, dwTypeCode, dwValueLength;
char
ValueName[256];
char Value[256];
dwValueNameLength = sizeof(ValueName);
dwValueLength = sizeof(Value);
LONG lnResult;
HKEY hKey;
lnResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"HARDWARE\\DEVICEMAP\\SERIALCOMM",
0, KEY_READ, &hKey);
if (lnResult ==
ERROR_FILE_NOT_FOUND)
{
MessageBox( "Could
not open registry key",
"Com port scan",
MB_OK | MB_ICONEXCLAMATION);
}
else
{
while(RegEnumValue(hKey,
Index, ValueName, &dwValueNameLength,
NULL,
&dwTypeCode, (BYTE *)Value, &dwValueLength) == ERROR_SUCCESS)
{
if(dwTypeCode == REG_SZ)
{
if(strstr(ValueName,
"slabser")
|| strstr(ValueName,
"cyg_ser"))
{
//USB2TTL Device Found...
MessageBox(Value,
"Found Com Port",
MB_OK);
}
}
++Index;
dwValueNameLength = sizeof(ValueName);
dwValueLength = sizeof(Value);
}
RegCloseKey(hKey);
}
} |
VB (v5/v6) Example
The following declarations are require in order to allow
Visual Basic to access the Windows API. (Thanks to Alan Munday for
his input on this example).
|
Private
Declare
Function
RegOpenKeyEx Lib
"advapi32.dll"
_
Alias "RegOpenKeyExA"
_
( ByVal
hKey As
Long,
_
ByVal lpSubKey
As
String,
_
ByVal ulOptions
As
Long,
_
ByVal samDesired
As
Long, _
phkResult
As
Long)
As
Long
Private
Declare
Function
RegEnumValue Lib
"advapi32.dll"
_
Alias "RegEnumValueA"
_
(ByVal
hKey As
Long,
_
ByVal dwIndex
As
Long,
_
ByVal lpValueName
As
String,
_
lpcbValueName As
Long,
_
ByVal lpReserved
As
Long,
_
lpType
As
Long,
_
lpData
As
Any, _
lpcbData
As
Long)
As
Long
Private
Declare
Function
RegCloseKey Lib
"advapi32.dll"
_
( ByVal
hKey As
Long)
As
Long
Const REG_SZ = 1
Const
HKEY_LOCAL_MACHINE = &H80000002
Const
ERROR_SUCCESS = 0&
Const
SYNCHRONIZE = &H100000
Const
STANDARD_RIGHTS_READ = &H20000
Const
STANDARD_RIGHTS_WRITE = &H20000
Const
STANDARD_RIGHTS_EXECUTE = &H20000
Const
STANDARD_RIGHTS_REQUIRED = &HF0000
Const
STANDARD_RIGHTS_ALL = &H1F0000
Const
KEY_QUERY_VALUE = &H1
Const
KEY_SET_VALUE = &H2
Const
KEY_CREATE_SUB_KEY = &H4
Const
KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY
= &H10
Const
KEY_CREATE_LINK = &H20
Const KEY_READ =
((STANDARD_RIGHTS_READ Or
_
KEY_QUERY_VALUE
Or
_
KEY_ENUMERATE_SUB_KEYS
Or
_
KEY_NOTIFY)
And
_
( Not
SYNCHRONIZE)) |
|
Private
Sub
FindSerialComm()
Dim typecode
As
Long
Dim lngKeyHandle
As
Long
Dim
lngResult As
Long
Dim lngCurIdx
As
Long
Dim
ValueName As
String
* 256
Dim ValueNameLen
As
Long
Dim Value
As String
* 256
Dim ValueLen
As
Long
lngResult =
RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"HARDWARE\DEVICEMAP\SERIALCOMM",
_
0&, KEY_READ, lngKeyHandle)
If lngResult <> ERROR_SUCCESS
Then
MsgBox( "Cannot
open key")
Exit
Sub
End
If
lngCurIdx = 0
ValueNameLen = 256
ValueLen = 256
While RegEnumValue(lngKeyHandle, lngCurIdx,
ByVal
ValueName, _
ValueNameLen, 0&, typecode,
ByVal
Value, ValueLen) = ERROR_SUCCESS
If typecode = REG_SZ
Then
If InStr(ValueName,
"slabser")
Or
InStr(ValueName, "cyg_ser")
Then
'USB2TTL Device Found...
Call MsgBox(Value, vbOKOnly,
"Found Com Port")
End
If
End
If
lngCurIdx = lngCurIdx + 1
ValueNameLen = 256
ValueLen = 256
WEnd
Call
RegCloseKey(lngKeyHandle)
End
Sub |
C++ 2005 (.net)
Example
|
using
namespace
Microsoft::Win32;
void
FindSerialComm(void)
{
RegistryKey ^
SerialCommKey;
array<String^> ^
SerialCommString;
int
index;
SerialCommKey =
Registry::LocalMachine->OpenSubKey(
"HARDWARE\\DEVICEMAP\\SERIALCOMM",
false);
SerialCommString = SerialCommKey->GetValueNames();
for(index
= 0; index< SerialCommString->Length; index++)
{
if((SerialCommString[index]->Contains("slabser"))
||
(SerialCommString[index]->Contains("cyg_ser")))
{
//USB2TTL Device
Found...
MessageBox::Show(SerialCommKey->GetValue(SerialCommString[index])->ToString(),
"Found Com Port",
MessageBoxButtons::OK);
}
}
SerialCommKey->Close();
} |
VB 2005 (.net)
Example
|
Private
Sub
FindSerialComm()
Dim
SerialCommKey As
Microsoft.Win32.RegistryKey
Dim SerialCommString()
As
String
Dim index
As
Integer
SerialCommKey =
My.Computer.Registry.LocalMachine.OpenSubKey( _
"HARDWARE\\DEVICEMAP\\SERIALCOMM",
False)
SerialCommString = SerialCommKey.GetValueNames()
For index = 0
To
SerialCommString.Length - 1
If
(SerialCommString(index).Contains("slabser")
Or
_
(SerialCommString(index).Contains( "cyg_ser")
Then
'USB2TTL Device Found...
MsgBox(SerialCommKey.GetValue(SerialCommString(index)), _
MsgBoxStyle.OkOnly, _
"Found
Com Port")
End
If
Next
SerialCommKey.Close()
End
Sub |
|