Repository navigation
Expand file tree
/
Copy pathserialmonitor.cpp
More file actions
148 lines (126 loc) · 4.99 KB
/
Copy pathserialmonitor.cpp
File metadata and controls
148 lines (126 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "serialmonitor.h"
#include <QRegularExpression>
#include <algorithm>
#ifdef Q_OS_WIN
#include <windows.h>
#include <setupapi.h>
#include <initguid.h>
#include <devguid.h>
#pragma comment(lib, "setupapi.lib")
#endif
SerialMonitor::SerialMonitor(QObject *parent)
: QThread(parent)
, running(false)
{
}
void SerialMonitor::stop()
{
running = false;
}
QList<SimplePortInfo> SerialMonitor::listPortsNow()
{
QList<SimplePortInfo> result;
#ifdef Q_OS_WIN
// Step 1: get COMx names from HKLM\HARDWARE\DEVICEMAP\SERIALCOMM
HKEY hKey;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"HARDWARE\\DEVICEMAP\\SERIALCOMM",
0, KEY_READ, &hKey) != ERROR_SUCCESS) {
return result;
}
DWORD index = 0;
wchar_t valueData[256];
for (;;) {
wchar_t valueName[16383];
DWORD valueNameSize = 16383;
DWORD valueDataSize = sizeof(valueData);
DWORD type = 0;
LONG enumRes = RegEnumValueW(hKey, index, valueName, &valueNameSize,
nullptr, &type,
reinterpret_cast<LPBYTE>(valueData),
&valueDataSize);
if (enumRes == ERROR_NO_MORE_ITEMS) break;
if (enumRes == ERROR_SUCCESS && type == REG_SZ) {
SimplePortInfo info;
info.portName = QString::fromWCharArray(valueData);
result.append(info);
}
++index;
}
RegCloseKey(hKey);
// Step 2: enrich with friendly names + VID/PID from SetupAPI (Ports class)
HDEVINFO devInfo = SetupDiGetClassDevsW(&GUID_DEVCLASS_PORTS, nullptr, nullptr,
DIGCF_PRESENT);
if (devInfo != INVALID_HANDLE_VALUE) {
SP_DEVINFO_DATA devData;
devData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(devInfo, i, &devData); ++i) {
wchar_t friendlyName[512] = {0};
wchar_t hardwareId[512] = {0};
SetupDiGetDeviceRegistryPropertyW(devInfo, &devData, SPDRP_FRIENDLYNAME,
nullptr, reinterpret_cast<PBYTE>(friendlyName),
sizeof(friendlyName), nullptr);
SetupDiGetDeviceRegistryPropertyW(devInfo, &devData, SPDRP_HARDWAREID,
nullptr, reinterpret_cast<PBYTE>(hardwareId),
sizeof(hardwareId), nullptr);
QString friendly = QString::fromWCharArray(friendlyName);
QString hwId = QString::fromWCharArray(hardwareId);
// Friendly name typically looks like "Silicon Labs CP210x ... (COM5)"
int openParen = friendly.lastIndexOf('(');
int closeParen = friendly.lastIndexOf(')');
if (openParen < 0 || closeParen < 0) continue;
QString comName = friendly.mid(openParen + 1, closeParen - openParen - 1);
for (auto &info : result) {
if (info.portName.compare(comName, Qt::CaseInsensitive) == 0) {
info.description = friendly.left(openParen).trimmed();
// hwId looks like "USB\VID_10C4&PID_EA60\..."
QRegularExpression vidRe("VID_([0-9A-Fa-f]{4})");
QRegularExpression pidRe("PID_([0-9A-Fa-f]{4})");
auto vidMatch = vidRe.match(hwId);
auto pidMatch = pidRe.match(hwId);
if (vidMatch.hasMatch()) {
info.vendorIdentifier = vidMatch.captured(1).toUShort(nullptr, 16);
info.hasVendorIdentifier = true;
}
if (pidMatch.hasMatch()) {
info.productIdentifier = pidMatch.captured(1).toUShort(nullptr, 16);
info.hasProductIdentifier = true;
}
break;
}
}
}
SetupDiDestroyDeviceInfoList(devInfo);
}
std::sort(result.begin(), result.end(), [](const SimplePortInfo &a, const SimplePortInfo &b) {
auto numOf = [](const QString &s) -> int {
QString digits;
for (QChar c : s) if (c.isDigit()) digits += c;
return digits.isEmpty() ? 0 : digits.toInt();
};
return numOf(a.portName) < numOf(b.portName);
});
#endif
return result;
}
void SerialMonitor::run()
{
running = true;
while (running) {
const auto ports = listPortsNow();
QSet<QString> currentPorts;
for (const auto &info : ports) {
currentPorts.insert(info.portName);
if (!knownPorts.contains(info.portName)) {
emit portDetected(info);
}
}
for (const QString &known : std::as_const(knownPorts)) {
if (!currentPorts.contains(known)) {
emit portRemoved(known);
}
}
knownPorts = currentPorts;
QThread::msleep(1500);
}
}