Dedicated driver is required to use URG in Windows.
When the driver is installed, URG will be recognized as both USB
connection and COM. To know the COM number used by the URG, use "Device
Manager".
To check the port information using Device Manager.
COM ports that are recognized by Windows are managed by Registers.
Therefore, COM port that is using the URG's driver can be considered as
connected to URG.
The following program checks whether specified driver name is used by Windows or not. (program tested ony in Windows XP)
#include "isUsingComDriver.h"
#include "DetectOS.h"
#ifdef WINDOWS_OS
#include <windows.h>
#include <string>
#endif
using namespace std;
#ifdef WINDOWS_OS
namespace
{
bool existRegValue(HKEY hkey, LPCSTR subkey, const char* find_value)
{
HKEY next_hkey;
if (RegOpenKeyExA(hkey, subkey, 0, KEY_READ, &next_hkey) != ERROR_SUCCESS) {
return false;
}
enum { MaxLength = 1024 };
// searches whether find_value exists or not.
CHAR device[MaxLength + 1];
char name[MaxLength + 1];
DWORD ret = ERROR_SUCCESS;
for (int i = 0; ret == ERROR_SUCCESS; ++i) {
DWORD dl = MaxLength, nl = MaxLength;
ret = RegEnumValueA(hkey, i, device, &dl, NULL, NULL, (BYTE*)name, &nl);
if (ret != ERROR_SUCCESS) {
break;
}
if (! strncmp(name, find_value, nl)) {
RegCloseKey(next_hkey);
return true;
}
}
// If find_value doesn't exist, it searches recursively.
char next_subkey[MaxLength];
FILETIME filetime;
ret = ERROR_SUCCESS;
for (int i = 0; ret == ERROR_SUCCESS; ++i) {
DWORD dl = MaxLength, nl = MaxLength;
ret = RegEnumKeyExA(next_hkey, i, next_subkey,
&dl, NULL, NULL, &nl, &filetime);
if (ret != ERROR_SUCCESS) {
break;
}
bool value_exist = existRegValue(next_hkey, next_subkey, find_value);
if (value_exist) {
RegCloseKey(next_hkey);
return true;
}
}
RegCloseKey(next_hkey);
return false;
}
}
bool qrk::isUsingComDriver(const char* com_port, const char* driver_name)
{
//Seaches whether specified driver name is included in "Value".
string value_pattern = string(driver_name) + " (" + com_port + ")";
if (existRegValue(HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Enum\\USB",
value_pattern.c_str())) {
return true;
}
return false;
}
#else
bool qrk::isUsingComDriver(const char* com_port, const char* driver_name)
{
static_cast<void>(com_port);
static_cast<void>(driver_name);
return false;
}
#endif
This is a program which gets the list of the available COM in Windows XP using
FindComPorts.cpp and
UrgUsbCom.cpp
#include "UrgUsbCom.h"
#include "FindComPorts.h"
#include <SDL.h>
using namespace qrk;
using namespace std;
int main(int argc, char *argv[]) {
UrgUsbCom urg_usb;
FindComPorts urg_finder(&urg_usb);
vector<string> ports = urg_finder.find();
// 見付かったポートを全て出力する
printf("ports: %u\n", ports.size());
for (vector<string>::iterator it = ports.begin();
it != ports.end(); ++it) {
printf("%s\n", it->c_str());
}
return 0;
}