-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOMDriver.cpp
More file actions
173 lines (142 loc) · 5.48 KB
/
COMDriver.cpp
File metadata and controls
173 lines (142 loc) · 5.48 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "pch.h"
#include "COMDriver.h"
COMDriver::COMDriver() : hdevice(INVALID_HANDLE_VALUE) {
devices.clear();
UpdateDevices();
}
ErrType COMDriver::GetDevices(std::vector<COMInfo>& d) {
d = this->devices;
return LastError;
}
void COMDriver::UpdateDevices() {
this->devices.clear();
HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE) {
LastError = ErrType::UPDATE_ERROR;
return;
}
SP_DEVICE_INTERFACE_DATA interfaceData = { 0 };
interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &GUID_DEVINTERFACE_COMPORT, i, &interfaceData); ++i) {
DWORD requiredSize = 0;
SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, NULL, 0, &requiredSize, NULL);
std::vector<BYTE> detailBuffer(requiredSize);
PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail = reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(detailBuffer.data());
pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SP_DEVINFO_DATA devInfoData = { 0 };
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, pDetail, requiredSize, NULL, &devInfoData)) {
continue;
}
WCHAR deviceName[256] = { 0 };
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_FRIENDLYNAME, NULL,
reinterpret_cast<PBYTE>(deviceName), sizeof(deviceName) - sizeof(WCHAR), NULL)) {
SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_DEVICEDESC, NULL,
reinterpret_cast<PBYTE>(deviceName), sizeof(deviceName) - sizeof(WCHAR), NULL);
}
HKEY hDeviceKey = SetupDiOpenDevRegKey(hDevInfo, &devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
if (hDeviceKey == INVALID_HANDLE_VALUE) {
continue;
}
WCHAR portName[256] = { 0 };
DWORD size = sizeof(portName);
DWORD type = 0;
if (RegQueryValueEx(hDeviceKey, L"PortName", NULL, &type,
reinterpret_cast<LPBYTE>(portName), &size) == ERROR_SUCCESS && type == REG_SZ) {
if (wcsncmp(portName, L"COM", 3) == 0) {
int comNumber = _wtoi(portName + 3);
if (comNumber > 0) {
COMInfo info;
info.deviceName = deviceName;
info.com_id = comNumber;
devices.push_back(info);
}
}
}
RegCloseKey(hDeviceKey);
}
DWORD error = GetLastError();
if (error != ERROR_NO_MORE_ITEMS) {
SetupDiDestroyDeviceInfoList(hDevInfo);
LastError = ErrType::UPDATE_ERROR;
}
SetupDiDestroyDeviceInfoList(hDevInfo);
LastError = ErrType::OK;
}
ErrType COMDriver::OpenDevice(int port) {
//std::wstring portName = L"\\\\.\\COM" + std::to_wstring(info.com_id);
std::wstring portName = L"COM" + std::to_wstring(port);
// 打开串口
hdevice = CreateFileW(
portName.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr
);
if (hdevice == INVALID_HANDLE_VALUE) {
return LastError = ErrType::OPEN_ERROR;
}
// 配置串口参数
DCB dcb = { 0 };
dcb.DCBlength = sizeof(DCB);
if (!GetCommState(hdevice, &dcb)) {
CloseHandle(hdevice);
hdevice = INVALID_HANDLE_VALUE;
return LastError = ErrType::OPEN_ERROR;
}
// 配置波特率38400,8N1
dcb.BaudRate = CBR_38400;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if (!SetCommState(hdevice, &dcb)) {
CloseHandle(hdevice);
hdevice = INVALID_HANDLE_VALUE;
return LastError = ErrType::OPEN_ERROR;
}
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 200; // 关键参数:字节间隔超时
timeouts.ReadTotalTimeoutMultiplier = 20;
timeouts.ReadTotalTimeoutConstant = 200;
if (!SetCommTimeouts(hdevice, &timeouts)) {
CloseHandle(hdevice);
hdevice = INVALID_HANDLE_VALUE;
return LastError = ErrType::OPEN_ERROR;
}
return LastError = ErrType::OK;
}
ErrType COMDriver::ReleaseDevice(int port) {
if (hdevice != INVALID_HANDLE_VALUE && hdevice != NULL) {
if (!CloseHandle(hdevice)) {
return LastError = ErrType::RELEASE_ERROR;
}
return LastError = ErrType::OK;
}
else return LastError = ErrType::RELEASE_ERROR;
}
ErrType COMDriver::SendData(UINT8 data) {
if (hdevice == INVALID_HANDLE_VALUE) {
LastError = ErrType::SEND_ERROR;
return LastError;
}
DWORD bytesWritten;
if (!WriteFile(hdevice, &data, 1, &bytesWritten, nullptr)) {
LastError = ErrType::SEND_ERROR;
return LastError;
}
return LastError = (bytesWritten == 1) ? ErrType::OK : ErrType::SEND_ERROR;
}
ErrType COMDriver::RecvData(UINT8* buffer, DWORD& bytesRead) {
bytesRead = 0;
if (hdevice == INVALID_HANDLE_VALUE) {
LastError = ErrType::NOT_INIT;
return LastError;
}
if (!ReadFile(hdevice, buffer, 8, &bytesRead, nullptr)) {
return LastError = ErrType::RECV_ERROR;
}
return LastError = ErrType::OK;
}