-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial_windows.cpp
269 lines (214 loc) · 6.67 KB
/
serial_windows.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
#include <windows.h>
#include "serial.h"
HANDLE hSerialPort;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
std::string data;
void (*errorCallback)(int errorCode);
void (*readCallback)(int bytes);
void (*writeCallback)(int bytes);
void serialOpen(
void* port,
const int baudrate,
const int dataBits,
const int parity,
const int stopBits
) {
char *portName = static_cast<char*>(port);
dcbSerialParams.DCBlength = sizeof(DCB);
hSerialPort = CreateFile(
portName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
// Error if open fails
if (hSerialPort == INVALID_HANDLE_VALUE) {
errorCallback(status(StatusCodes::INVALID_HANDLE_ERROR));
return;
}
// Error if configuration get fails
if (!GetCommState(hSerialPort, &dcbSerialParams)) {
// Error if close fails
if (!CloseHandle(hSerialPort)) {
errorCallback(status(StatusCodes::CLOSE_HANDLE_ERROR));
return;
}
errorCallback(status(StatusCodes::GET_STATE_ERROR));
return;
}
dcbSerialParams.BaudRate = baudrate;
dcbSerialParams.ByteSize = dataBits;
dcbSerialParams.Parity = static_cast<BYTE>(parity);
dcbSerialParams.StopBits = static_cast<BYTE>(stopBits);
// Error if configuration set fails
if (!SetCommState(hSerialPort, &dcbSerialParams)) {
// Error if close fails
if (!CloseHandle(hSerialPort)) {
errorCallback(status(StatusCodes::CLOSE_HANDLE_ERROR));
return;
}
errorCallback(status(StatusCodes::SET_STATE_ERROR));
return;
}
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
// Error if timeout set fails
if (!SetCommTimeouts(hSerialPort, &timeouts)) {
// Error if close fails
if (!CloseHandle(hSerialPort)) {
errorCallback(status(StatusCodes::CLOSE_HANDLE_ERROR));
return;
}
errorCallback(status(StatusCodes::SET_TIMEOUT_ERROR));
return;
}
}
void serialClose() {
// Error if handle is invalid
if (hSerialPort == INVALID_HANDLE_VALUE) {
errorCallback(status(StatusCodes::INVALID_HANDLE_ERROR));
return;
}
// Error if close fails
if (!CloseHandle(hSerialPort)) {
errorCallback(status(StatusCodes::CLOSE_HANDLE_ERROR));
return;
}
}
auto serialRead(
void* buffer,
const int bufferSize,
const int timeout,
const int multiplier
) -> int {
// Error if handle is invalid
if (hSerialPort == INVALID_HANDLE_VALUE) {
errorCallback(status(StatusCodes::INVALID_HANDLE_ERROR));
return 0;
}
timeouts.ReadIntervalTimeout = timeout;
timeouts.ReadTotalTimeoutConstant = timeout;
timeouts.ReadTotalTimeoutMultiplier = multiplier;
// Error if timeout set fails
if (!SetCommTimeouts(hSerialPort, &timeouts)) {
errorCallback(status(StatusCodes::SET_TIMEOUT_ERROR));
return 0;
}
DWORD bytesRead = 0;
// Error if read fails
if (!ReadFile(hSerialPort, buffer, bufferSize, &bytesRead, NULL)) {
errorCallback(status(StatusCodes::READ_ERROR));
return 0;
}
readCallback(bytesRead);
return bytesRead;
}
auto serialReadUntil(
void* buffer,
const int bufferSize,
const int timeout,
const int multiplier,
void* searchString
) -> int {
// Error if handle is invalid
if (hSerialPort == INVALID_HANDLE_VALUE) {
errorCallback(status(StatusCodes::INVALID_HANDLE_ERROR));
return 0;
}
timeouts.ReadIntervalTimeout = timeout;
timeouts.ReadTotalTimeoutConstant = timeout;
timeouts.ReadTotalTimeoutMultiplier = multiplier;
// Error if timeout set fails
if (!SetCommTimeouts(hSerialPort, &timeouts)) {
errorCallback(status(StatusCodes::SET_TIMEOUT_ERROR));
return 0;
}
data = "";
for (int i{0}; i < bufferSize && data.find(std::string(static_cast<char*>(searchString))) == std::string::npos; i++) {
DWORD bytesRead;
char bufferChar[1];
// Error if read fails
if (!ReadFile(hSerialPort, bufferChar, sizeof(bufferChar), &bytesRead, NULL)) {
errorCallback(status(StatusCodes::READ_ERROR));
return 0;
}
if (bytesRead == 0) {
break;
}
data.append(std::string(bufferChar, bytesRead));
}
memcpy(buffer, data.c_str(), data.length() + 1);
readCallback(data.length());
return data.length();
}
auto serialWrite(
void* buffer,
const int bufferSize,
const int timeout,
const int multiplier
) -> int {
DWORD bytesWritten = 0;
timeouts.WriteTotalTimeoutConstant = timeout;
timeouts.WriteTotalTimeoutMultiplier = multiplier;
// Error if timeout set fails
if (!SetCommTimeouts(hSerialPort, &timeouts)) {
errorCallback(status(StatusCodes::SET_TIMEOUT_ERROR));
return 0;
}
// Error if write fails
if (!WriteFile(hSerialPort, buffer, bufferSize, &bytesWritten, NULL)) {
errorCallback(status(StatusCodes::WRITE_ERROR));
return 0;
}
writeCallback(bytesWritten);
return bytesWritten;
}
auto serialGetPortsInfo(
void* buffer,
const int bufferSize,
void* separator
) -> int {
std::string result;
int portsCounter = 0;
for (int i = 1; i <= 256; ++i) {
std::string portName = "COM" + std::to_string(i);
HANDLE hPort = CreateFileA(portName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
// Error if open fails
if (hPort == INVALID_HANDLE_VALUE) {
CloseHandle(hPort);
continue;
}
portsCounter++;
result += portName.append(std::string(static_cast<char*>(separator)));
CloseHandle(hPort);
}
// Remove last trailing comma
if (result.length() > 0) {
result.erase(result.length() - 1);
}
// Error if buffer size is to small
if (result.length() + 1 > bufferSize) {
errorCallback(status(StatusCodes::BUFFER_ERROR));
return 0;
}
memcpy(buffer, result.c_str(), result.length() + 1);
return portsCounter;
}
auto serialOnError(void (*func)(int code)) -> void {
errorCallback = func;
};
auto serialOnRead(void (*func)(int bytes)) -> void {
readCallback = func;
};
auto serialOnWrite(void (*func)(int bytes)) -> void {
writeCallback = func;
};
#endif