-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathSerialConnectionWinAPI.h
More file actions
261 lines (227 loc) · 8.9 KB
/
SerialConnectionWinAPI.h
File metadata and controls
261 lines (227 loc) · 8.9 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
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
/* Serial Connection for Windows
*
* From: https://github.com/PokemonAutomation/
*
*/
#ifndef PokemonAutomation_SerialConnectionWinAPI_H
#define PokemonAutomation_SerialConnectionWinAPI_H
#include <string>
#include <atomic>
#include <windows.h>
#include "Common/Compiler.h"
#include "Common/Cpp/Time.h"
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/PanicDump.h"
#include "Common/Cpp/Strings/Unicode.h"
#include "Common/Cpp/Concurrency/SpinLock.h"
#include "Common/Cpp/Concurrency/AsyncTask.h"
#include "Common/Cpp/Concurrency/ThreadPool.h"
#include "Common/Cpp/StreamConnections/PushingStreamConnections.h"
namespace PokemonAutomation{
void serial_debug_log(const std::string& msg);
class SerialConnection : public UnreliableStreamConnectionPushing{
public:
// UTF-8
SerialConnection(
ThreadPool& thread_pool,
const std::string& name,
uint32_t baud_rate
)
: SerialConnection(thread_pool, name, utf8_to_wstr(name), baud_rate)
{}
SerialConnection(
ThreadPool& thread_pool,
const std::string& name,
const std::wstring& wname,
uint32_t baud_rate
)
: m_exit(false)
, m_consecutive_errors(0)
{
m_handle = CreateFileW(
(L"\\\\.\\" + wname).c_str(),
GENERIC_READ | GENERIC_WRITE,
0, 0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);
if (m_handle == INVALID_HANDLE_VALUE){
DWORD error = GetLastError();
throw ConnectionException(nullptr, "Unable to open serial connection (" + name + "). Error = " + std::to_string(error));
}
set_baud_rate(baud_rate);
#if 1
COMMTIMEOUTS timeouts{};
if (!GetCommTimeouts(m_handle, &timeouts)){
DWORD error = GetLastError();
CloseHandle(m_handle);
throw ConnectionException(nullptr, "GetCommTimeouts() failed. Error = " + std::to_string(error));
}
//std::cout << "ReadIntervalTimeout = " << timeouts.ReadIntervalTimeout << std::endl;
//std::cout << "ReadTotalTimeoutMultiplier = " << timeouts.ReadTotalTimeoutMultiplier << std::endl;
//std::cout << "ReadTotalTimeoutConstant = " << timeouts.ReadTotalTimeoutConstant << std::endl;
//std::cout << "WriteTotalTimeoutMultiplier = " << timeouts.WriteTotalTimeoutMultiplier << std::endl;
//std::cout << "WriteTotalTimeoutConstant = " << timeouts.WriteTotalTimeoutConstant << std::endl;
#if 1
timeouts = COMMTIMEOUTS{(DWORD)-1, 0, 0, 0, 100};
#else
// Need to set a read timer. In some cases, a pending ReadFile() call
// will block a WriteFile() call until it returns - leading to a
// deadlock if the device isn't sending any information.
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 1;
#endif
if (!SetCommTimeouts(m_handle, &timeouts)){
DWORD error = GetLastError();
CloseHandle(m_handle);
throw ConnectionException(nullptr, "SetCommTimeouts() failed. Error = " + std::to_string(error));
}
#endif
// Start receiver thread.
try{
m_listener = thread_pool.dispatch_now_blocking([this]{
run_with_catch(
"SerialConnection::SerialConnection()",
[this]{ recv_loop(); }
);
});
}catch (...){
CloseHandle(m_handle);
throw;
}
}
virtual ~SerialConnection(){
if (!m_exit.load(std::memory_order_acquire)){
stop();
}
}
virtual void stop() noexcept final{
m_exit.store(true, std::memory_order_release);
CloseHandle(m_handle);
m_listener.wait_and_ignore_exceptions();
}
void set_baud_rate(uint32_t baud_rate){
DCB serial_params{};
serial_params.DCBlength = sizeof(serial_params);
if (!GetCommState(m_handle, &serial_params)){
DWORD error = GetLastError();
CloseHandle(m_handle);
throw ConnectionException(nullptr, "GetCommState() failed. Error = " + std::to_string(error));
}
// cout << "BaudRate = " << (int)serial_params.BaudRate << endl;
// cout << "ByteSize = " << (int)serial_params.ByteSize << endl;
// cout << "StopBits = " << (int)serial_params.StopBits << "0 means 1 bit" << endl;
// cout << "Parity = " << (int)serial_params.Parity << endl;
serial_params.BaudRate = baud_rate;
serial_params.ByteSize = 8;
serial_params.StopBits = 0;
serial_params.Parity = 0;
if (!SetCommState(m_handle, &serial_params)){
DWORD error = GetLastError();
CloseHandle(m_handle);
throw ConnectionException(nullptr, "SetCommState() failed. Error = " + std::to_string(error));
}
}
private:
void process_error(const std::string& message, bool allow_throw){
WriteSpinLock lg(m_error_lock);
size_t consecutive_errors = m_consecutive_errors.fetch_add(1);
serial_debug_log(message);
std::string clear_error;
DWORD comm_error;
if (ClearCommError(m_handle, &comm_error, nullptr) == 0){
DWORD error = GetLastError();
clear_error = "ClearCommError() failed. Error = " + std::to_string(error);
}else{
clear_error = "ClearCommError error flag = " + std::to_string(comm_error);
}
if (consecutive_errors <= 10){
serial_debug_log(message);
}
if (consecutive_errors == 10){
serial_debug_log("Further error messages will be suppressed.");
}
if (consecutive_errors >= 100 && allow_throw){
throw ConnectionException(nullptr, "Serial Connection failed.");
}
}
virtual size_t unreliable_send(const void* data, size_t bytes) override{
WriteSpinLock lg(m_send_lock, "SerialConnection::send()");
#if 0
for (size_t c = 0; c < bytes; c++){
std::cout << "Send: " << (int)((const char*)data)[c] << std::endl;
}
#endif
// std::cout << "start write" << std::endl;
// auto start = current_time();
DWORD written;
if (WriteFile(m_handle, data, (DWORD)bytes, &written, nullptr) != 0 && bytes == written){
m_consecutive_errors.store(0, std::memory_order_release);
return written;
}
DWORD error = GetLastError();
process_error(
"Failed to write: " + std::to_string(written) +
" / " + std::to_string(bytes) +
", error = " + std::to_string(error),
true
);
// auto stop = current_time();
// cout << "WriteFile() : " << std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count() << endl;
// std::cout << "end send()" << std::endl;
return written;
}
void recv_loop(){
// std::lock_guard<Mutex> lg(m_send_lock);
char buffer[32];
auto last_recv = current_time();
while (!m_exit.load(std::memory_order_acquire)){
// auto start = current_time();
// std::cout << "start read" << std::endl;
DWORD read;
if (ReadFile(m_handle, buffer, 32, &read, nullptr) == 0){
DWORD error = GetLastError();
process_error("ReadFile() failed. Error = " + std::to_string(error), false);
}
// auto stop = current_time();
// cout << "ReadFile() : " << std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count() << endl;
// std::cout << "read = " << read << std::endl;
// set_timouts();
#if 0
for (size_t c = 0; c < read; c++){
cout << "Recv: " << (int)buffer[c] << endl;
}
#endif
if (read != 0){
m_consecutive_errors.store(0, std::memory_order_release);
on_unreliable_recv(buffer, read);
last_recv = current_time();
continue;
}
#if 1
// auto start = current_time();
Sleep(1);
// auto stop = current_time();
#else
auto now = current_time();
uint64_t millis_since_last_recv = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_recv).count();
if (millis_since_last_recv > 100){
Sleep(1);
}else{
pause();
}
#endif
}
}
private:
HANDLE m_handle;
std::atomic<bool> m_exit;
std::atomic<size_t> m_consecutive_errors;
SpinLock m_send_lock;
SpinLock m_error_lock;
AsyncTask m_listener;
};
}
#endif