SerialUSB
struct SerialUSB {
std::string VID;
std::string PID;
std::string Port;
std::string Path;
SYSTEMTIME TimeInput;
SerialUSB() {
this->VID = "";
this->PID = "";
this->Path = "";
this->Port = "";
}
bool valid() {
if (this->VID == "") return false;
if (this->PID == "") return false;
if (this->Port == "") return false;
if (this->Path == "") return false;
return true;
}
void out() {
printf("VID=%s PID=%s Port=%s Path=%s \n", this->VID.c_str(), this->PID.c_str(), this->Port.c_str(), this->Path.c_str());
}
};
SerialUSB getSerialUSB(std::wstring token)
{
SerialUSB usb;
std::wstring upperToken = token;
std::transform(upperToken.begin(), upperToken.end(), upperToken.begin(), ::toupper);
std::wstring path(upperToken);
size_t vid_pos = path.find(L"VID_");
size_t pid_pos = path.find(L"PID_");
if (vid_pos != std::wstring::npos && pid_pos != std::wstring::npos) {
std::wstring vid = path.substr(vid_pos + 4, 4);
std::wstring pid = path.substr(pid_pos + 4, 4);
usb.VID = std::string( vid.begin(), vid.end());
usb.PID = std::string( pid.begin(), pid.end());
usb.Path = std::string(path.begin(), path.end());
usb.Port = GetComPortByPath(usb.VID, usb.PID);
}
return usb;
}
Exit Callback
void ExitEventCallbackProc(const char* portName, int eventType) {
if (eventType == DBT_DEVICEARRIVAL) {
printf("Plugin : 0x%.4X %s \n", eventType, portName);
// TODO
}
if (eventType == DBT_DEVICEREMOVECOMPLETE) {
printf("Plugout : 0x%.4X %s \n", eventType, portName);
// TODO
}
}
std::wstring ⇒ std::string
if (TRUE) {
WCHAR hardwareID[256] = { 0 };
DWORD size = sizeof(hardwareID);
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &devData, SPDRP_HARDWAREID, NULL, (PBYTE)hardwareID, size, &size)) continue;
std::wstring wdid = std::wstring(hardwareID);
std::string did = std::string(wdid.begin(), wdid.end());;
}
Scenario 1
BOOL EnumerateDevice() {
// Initialize the GUID for the Serial COM ports
GUID guid = GUID_DEVINTERFACE_COMPORT;
HDEVINFO deviceInfoSet;
SP_DEVINFO_DATA deviceInfoData;
CHAR hardwareId[MAX_PATH], serialNumber[MAX_PATH];
deviceInfoSet = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (deviceInfoSet == INVALID_HANDLE_VALUE) {
printf("SetupDiGetClassDevs failed!\n");
return (FALSE);
}
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD index = 0; SetupDiEnumDeviceInfo(deviceInfoSet, index, &deviceInfoData); index++) {
if (SetupDiGetDeviceRegistryPropertyA(deviceInfoSet, &deviceInfoData, SPDRP_HARDWAREID, NULL, (BYTE*)hardwareId, sizeof(hardwareId), NULL)) {
if (SetupDiGetDeviceRegistryPropertyA(deviceInfoSet, &deviceInfoData, SPDRP_FRIENDLYNAME, NULL, (BYTE*)serialNumber, sizeof(serialNumber), NULL)) {
std::string path = serialNumber;
size_t bof_pos = path.find("(");
size_t eof_pos = path.find(")");
std::string port_name = path.substr(bof_pos+1, (eof_pos - bof_pos - 1));
printf("EnumerateDevice > %s, %s, %s\n", hardwareId, port_name.c_str(), serialNumber);
}
}
}
SetupDiDestroyDeviceInfoList(deviceInfoSet);
return (TRUE);
}
Scenario 2
std::vector<std::string> EnumerateComPort() {
std::vector<std::string> content;
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
wchar_t valueName[256], portName[256];
DWORD index = 0, valueNameSize, portNameSize, type;
for (int i = 0; TRUE; i++) {
valueNameSize = sizeof(valueName);
portNameSize = sizeof(portName);
LONG ret = RegEnumValue(hKey, index++, valueName, &valueNameSize, NULL, &type, (LPBYTE)portName, &portNameSize);
if (ret != ERROR_SUCCESS) break;
std::wstring wdat = portName;
content.push_back(std::string(wdat.begin(), wdat.end()));
}
RegCloseKey(hKey);
}
return content;
}
Scenario 3
std::vector<std::string> plugQueue(std::vector<std::string> left, std::vector<std::string> right)
{
std::set<std::string> leftSet(left.begin(), left.end());
std::set<std::string> rightSet(right.begin(), right.end());
std::vector<std::string> plug;
std::set_difference(rightSet.begin(), rightSet.end(), leftSet.begin(), leftSet.end(), std::inserter(plug, plug.begin()));
return plug;
}