-
Notifications
You must be signed in to change notification settings - Fork 7
/
KmboxB.h
541 lines (469 loc) · 13.3 KB
/
KmboxB.h
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#pragma once
#ifndef _COM_H_
#define _COM_H_
#pragma warning(disable: 4530)
#pragma warning(disable: 4786)
#pragma warning(disable: 4800)
#include <cmath>
#include <cassert>
#include <Winsock2.h>
#include <windows.h>
class _base_com //虚基类 基本串口接口
{
protected:
volatile int _port; //串口号
volatile HANDLE _com_handle;//串口句柄
char _com_str[20];
DCB _dcb; //波特率,停止位,等
COMMTIMEOUTS _co; // 超时时间
virtual bool open_port() = 0;
void init() //初始化
{
memset(_com_str, 0, 20);
memset(&_co, 0, sizeof(_co));
memset(&_dcb, 0, sizeof(_dcb));
_dcb.DCBlength = sizeof(_dcb);
_com_handle = INVALID_HANDLE_VALUE;
}
virtual bool setup_port()
{
if (!is_open())
return false;
if (!SetupComm(_com_handle, 8192, 8192))
return false; //设置推荐缓冲区
if (!GetCommTimeouts(_com_handle, &_co))
return false;
_co.ReadIntervalTimeout = 0xFFFFFFFF;
_co.ReadTotalTimeoutMultiplier = 0;
_co.ReadTotalTimeoutConstant = 0;
_co.WriteTotalTimeoutMultiplier = 0;
_co.WriteTotalTimeoutConstant = 2000;
if (!SetCommTimeouts(_com_handle, &_co))
return false; //设置超时时间
if (!PurgeComm(_com_handle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
return false; //清空串口缓冲区
return true;
}
inline void set_com_port(int port)
{
char p[12];
_port = port;
strcpy_s(_com_str, "\\\\.\\COM");
_ltoa_s(_port, p, 10);
strcat_s(_com_str, p);
}
public:
_base_com()
{
init();
}
virtual ~_base_com()
{
close();
}
//设置串口参数:波特率,停止位,等 支持设置字符串 "115200, 8, n, 1"
bool set_state(char* set_str)
{
if (is_open())
{
if (!GetCommState(_com_handle, &_dcb))
return false;
if (!BuildCommDCBA(set_str, &_dcb))
return false;
return SetCommState(_com_handle, &_dcb) == TRUE;
}
return false;
}
//设置内置结构串口参数:波特率,停止位
bool set_state(int BaudRate, int ByteSize = 8, int Parity = NOPARITY, int StopBits = ONESTOPBIT)
{
if (is_open())
{
if (!GetCommState(_com_handle, &_dcb))
return false;
_dcb.BaudRate = BaudRate;
_dcb.ByteSize = ByteSize;
_dcb.Parity = Parity;
_dcb.StopBits = StopBits;
return SetCommState(_com_handle, &_dcb) == TRUE;
}
return false;
}
//打开串口 缺省 115200, 8, n, 1
inline bool open(int port)
{
return open(port, 115200);
}
//打开串口 缺省 baud_rate, 8, n, 1
inline bool open(int port, int baud_rate)
{
if (port < 1 || port > 1024)
return false;
set_com_port(port);
if (!open_port())
return false;
if (!setup_port())
return false;
return set_state(baud_rate);
}
//打开串口
inline bool open(int port, char* set_str)
{
if (port < 1 || port > 1024)
return false;
set_com_port(port);
if (!open_port())
return false;
if (!setup_port())
return false;
return set_state(set_str);
}
inline bool set_buf(int in, int out)
{
return is_open() ? SetupComm(_com_handle, in, out) : false;
}
//关闭串口
inline virtual void close()
{
if (is_open())
{
CloseHandle(_com_handle);
_com_handle = INVALID_HANDLE_VALUE;
}
}
//判断串口是或打开
inline bool is_open()
{
return _com_handle != INVALID_HANDLE_VALUE;
}
//获得串口句炳
HANDLE get_handle()
{
return _com_handle;
}
operator HANDLE()
{
return _com_handle;
}
};
class _sync_com : public _base_com
{
protected:
//打开串口
virtual bool open_port()
{
if (is_open())
close();
_com_handle = CreateFileA(
_com_str,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
assert(is_open());
return is_open();//检测串口是否成功打开
}
public:
_sync_com()
{
}
//同步读
int read(char* buf, int buf_len)
{
if (!is_open())
return 0;
buf[0] = '\0';
COMSTAT stat;
DWORD error;
if (ClearCommError(_com_handle, &error, &stat) && error > 0) //清除错误
{
PurgeComm(_com_handle, PURGE_RXABORT | PURGE_RXCLEAR); /*清除输入缓冲区*/
return 0;
}
unsigned long r_len = 0;
buf_len = min(buf_len - 1, (int)stat.cbInQue);
if (!ReadFile(_com_handle, buf, buf_len, &r_len, NULL))
r_len = 0;
buf[r_len] = '\0';
return r_len;
}
//同步写
int write(char* buf, int buf_len)
{
if (!is_open() || !buf)
return 0;
DWORD error;
if (ClearCommError(_com_handle, &error, NULL) && error > 0) //清除错误
PurgeComm(_com_handle, PURGE_TXABORT | PURGE_TXCLEAR);
unsigned long w_len = 0;
if (!WriteFile(_com_handle, buf, buf_len, &w_len, NULL))
w_len = 0;
return w_len;
}
//同步写
inline int write(char* buf)
{
assert(buf);
return write(buf, strlen(buf));
}
};
class _asyn_com : public _base_com
{
protected:
OVERLAPPED _ro, _wo; // 重叠I/O
virtual bool open_port()
{
if (is_open())
close();
_com_handle = CreateFileA(
_com_str,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, //重叠I/O
NULL
);
assert(is_open());
return is_open();//检测串口是否成功打开
}
public:
_asyn_com()
{
memset(&_ro, 0, sizeof(_ro));
memset(&_wo, 0, sizeof(_wo));
_ro.hEvent = CreateEvent(NULL, true, false, NULL);
assert(_ro.hEvent != INVALID_HANDLE_VALUE);
_wo.hEvent = CreateEvent(NULL, true, false, NULL);
assert(_wo.hEvent != INVALID_HANDLE_VALUE);
}
virtual ~_asyn_com()
{
close();
if (_ro.hEvent != INVALID_HANDLE_VALUE)
CloseHandle(_ro.hEvent);
if (_wo.hEvent != INVALID_HANDLE_VALUE)
CloseHandle(_wo.hEvent);
}
//异步读
int read(char* buf, int buf_len, int time_wait = 20)
{
if (!is_open())
return 0;
buf[0] = '\0';
COMSTAT stat;
DWORD error;
if (ClearCommError(_com_handle, &error, &stat) && error > 0) //清除错误
{
PurgeComm(_com_handle, PURGE_RXABORT | PURGE_RXCLEAR); /*清除输入缓冲区*/
return 0;
}
if (!stat.cbInQue)// 缓冲区无数据
return 0;
unsigned long r_len = 0;
buf_len = min((int)(buf_len - 1), (int)stat.cbInQue);
if (!ReadFile(_com_handle, buf, buf_len, &r_len, &_ro)) //2000 下 ReadFile 始终返回 True
{
if (GetLastError() == ERROR_IO_PENDING) // 结束异步I/O
{
WaitForSingleObject(_ro.hEvent, time_wait); //等待20ms
if (!GetOverlappedResult(_com_handle, &_ro, &r_len, false))
{
if (GetLastError() != ERROR_IO_INCOMPLETE)//其他错误
r_len = 0;
}
}
else
r_len = 0;
}
buf[r_len] = '\0';
return r_len;
}
//异步写
int write(char* buf, int buf_len)
{
if (!is_open())
return 0;
DWORD error;
if (ClearCommError(_com_handle, &error, NULL) && error > 0) //清除错误
PurgeComm(_com_handle, PURGE_TXABORT | PURGE_TXCLEAR);
unsigned long w_len = 0, o_len = 0;
if (!WriteFile(_com_handle, buf, buf_len, &w_len, &_wo))
if (GetLastError() != ERROR_IO_PENDING)
w_len = 0;
return w_len;
}
//异步写
inline int write(char* buf)
{
assert(buf);
return write(buf, strlen(buf));
}
};
//当接受到数据送到窗口的消息
#define ON_COM_RECEIVE WM_USER + 618 // WPARAM 端口号
class _thread_com : public _asyn_com
{
protected:
volatile HANDLE _thread_handle; //辅助线程
volatile HWND _notify_hwnd; // 通知窗口
volatile long _notify_num;//接受多少字节(>_notify_num)发送通知消息
volatile bool _run_flag; //线程运行循环标志
void (*_func)(int port);
OVERLAPPED _wait_o; //WaitCommEvent use
//线程收到消息自动调用, 如窗口句柄有效, 送出消息, 包含窗口编号
virtual void on_receive()
{
if (_notify_hwnd)
PostMessage(_notify_hwnd, ON_COM_RECEIVE, WPARAM(_port), LPARAM(0));
else
{
if (_func)
_func(_port);
}
}
//打开串口,同时打开监视线程
virtual bool open_port()
{
if (_asyn_com::open_port())
{
_run_flag = true;
DWORD id;
_thread_handle = CreateThread(NULL, 0, com_thread, this, 0, &id); //辅助线程
assert(_thread_handle);
if (!_thread_handle)
{
CloseHandle(_com_handle);
_com_handle = INVALID_HANDLE_VALUE;
}
else
return true;
}
return false;
}
public:
_thread_com()
{
_notify_num = 0;
_notify_hwnd = NULL;
_thread_handle = NULL;
_func = NULL;
memset(&_wait_o, 0, sizeof(_wait_o));
_wait_o.hEvent = CreateEvent(NULL, true, false, NULL);
assert(_wait_o.hEvent != INVALID_HANDLE_VALUE);
}
~_thread_com()
{
close();
if (_wait_o.hEvent != INVALID_HANDLE_VALUE)
CloseHandle(_wait_o.hEvent);
}
//设定发送通知, 接受字符最小值
void set_notify_num(int num)
{
_notify_num = num;
}
int get_notify_num()
{
return _notify_num;
}
//送消息的窗口句柄
inline void set_hwnd(HWND hWnd)
{
_notify_hwnd = hWnd;
}
inline HWND get_hwnd()
{
return _notify_hwnd;
}
inline void set_func(void (*f)(int))
{
_func = f;
}
//关闭线程及串口
virtual void close()
{
if (is_open())
{
_run_flag = false;
SetCommMask(_com_handle, 0);
SetEvent(_wait_o.hEvent);
if (WaitForSingleObject(_thread_handle, 100) != WAIT_OBJECT_0)
TerminateThread(_thread_handle, 0);
CloseHandle(_com_handle);
CloseHandle(_thread_handle);
_thread_handle = NULL;
_com_handle = INVALID_HANDLE_VALUE;
ResetEvent(_wait_o.hEvent);
}
}
/*辅助线程控制*/
//获得线程句柄
HANDLE get_thread()
{
return _thread_handle;
}
//暂停监视线程
bool suspend()
{
return _thread_handle != NULL ? SuspendThread(_thread_handle) != 0xFFFFFFFF : false;
}
//恢复监视线程
bool resume()
{
return _thread_handle != NULL ? ResumeThread(_thread_handle) != 0xFFFFFFFF : false;
}
//重建监视线程
bool restart()
{
if (_thread_handle) /*只有已有存在线程时*/
{
_run_flag = false;
SetCommMask(_com_handle, 0);
SetEvent(_wait_o.hEvent);
if (WaitForSingleObject(_thread_handle, 100) != WAIT_OBJECT_0)
TerminateThread(_thread_handle, 0);
CloseHandle(_thread_handle);
_run_flag = true;
_thread_handle = NULL;
DWORD id;
_thread_handle = CreateThread(NULL, 0, com_thread, this, 0, &id);
return (_thread_handle != NULL); //辅助线程
}
return false;
}
private:
//监视线程
static DWORD WINAPI com_thread(LPVOID para)
{
_thread_com* pcom = (_thread_com*)para;
if (!SetCommMask(pcom->_com_handle, EV_RXCHAR | EV_ERR))
return 0;
COMSTAT stat;
DWORD error;
for (DWORD length, mask = 0; pcom->_run_flag && pcom->is_open(); mask = 0)
{
if (!WaitCommEvent(pcom->_com_handle, &mask, &pcom->_wait_o))
{
if (GetLastError() == ERROR_IO_PENDING)
{
GetOverlappedResult(pcom->_com_handle, &pcom->_wait_o, &length, true);
}
}
if (mask & EV_ERR) // == EV_ERR
ClearCommError(pcom->_com_handle, &error, &stat);
if (mask & EV_RXCHAR) // == EV_RXCHAR
{
ClearCommError(pcom->_com_handle, &error, &stat);
if (stat.cbInQue > pcom->_notify_num)
pcom->on_receive();
}
}
return 0;
}
};
typedef _thread_com _com; //名称简化
#endif //_COM_H_