-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSerialForWindowsTerminal.cpp
More file actions
677 lines (612 loc) · 25.9 KB
/
SerialForWindowsTerminal.cpp
File metadata and controls
677 lines (612 loc) · 25.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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// SerialForWindowsTerminal.cpp : 定义应用程序的入口点。
//
#include "framework.h"
#include "SerialForWindowsTerminal.h"
#include <vector>
#include <string>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/windows/stream_handle.hpp>
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInstance;
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK SettingFunc(HWND, UINT, WPARAM, LPARAM);
// 添加全局标志来跟踪Ctrl+A状态
bool g_isCtrlAPressed = false;
// 添加全局标志来控制程序退出
bool g_shouldExit = false;
std::vector<uint8_t> g_lineBuffer;
using PortsArray = std::vector<std::pair<std::wstring, int>>;
static PortsArray GetAllPorts(void)
{
PortsArray ports;
HKEY hRegKey;
int nCount = 0;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Hardware\\DeviceMap\\SerialComm", 0, KEY_READ, &hRegKey) == ERROR_SUCCESS)
{
while (true)
{
TCHAR szName[MAX_PATH] = { 0 };
TCHAR szPort[MAX_PATH] = { 0 };
DWORD nValueSize = MAX_PATH - 1;
DWORD nDataSize = MAX_PATH - 1;
DWORD nType;
if (::RegEnumValue(hRegKey, nCount, szName, &nValueSize, NULL, &nType, (LPBYTE)szPort, &nDataSize) == ERROR_NO_MORE_ITEMS)
{
break;
}
std::wstring name(szName);
auto idx = name.find_last_of('\\');
if (idx != name.npos)
{
name = name.substr(idx + 1);
}
name += L" (";
name += szPort;
name += L")";
ports.push_back(std::make_pair(name, (int)std::wcstoul(szPort + 3, nullptr, 10)));
nCount++;
}
::RegCloseKey(hRegKey);
}
return ports;
}
static void UpdatePortControl(HWND hDlg)
{
auto allPorts = GetAllPorts();
auto hWndPort = GetDlgItem(hDlg, IDC_COMBO_PORT);
ComboBox_ResetContent(hWndPort);
for (auto port : allPorts)
{
auto index = ComboBox_AddString(hWndPort, port.first.c_str());
ComboBox_SetItemData(hWndPort, index, port.second);
}
}
static void CenterParentWindow(HWND hWnd)
{
RECT rcDlg;
::GetWindowRect(hWnd, &rcDlg);
RECT rcParent;
HWND hWndParent = GetParent(hWnd);
GetClientRect(hWndParent, &rcParent);
POINT ptParentInScreen;
ptParentInScreen.x = rcParent.left;
ptParentInScreen.y = rcParent.top;
::ClientToScreen(hWndParent, (LPPOINT)&ptParentInScreen);
SetWindowPos(
hWnd,
NULL,
ptParentInScreen.x + (rcParent.right - rcParent.left - (rcDlg.right - rcDlg.left)) / 2,
ptParentInScreen.y + (rcParent.bottom - rcParent.top - (rcDlg.bottom - rcDlg.top)) / 2,
0,
0,
SWP_NOZORDER | SWP_NOSIZE);
}
typedef struct
{
DWORD Serial;
DWORD BaudRate;
DWORD WordLength;
DWORD StopBit;
DWORD Parity;
DWORD FlowControl;
DWORD EncodingFormat; // 添加编码格式字段:0=UTF8, 1=GBK
DWORD EchoMode; // 添加回显模式字段:0=关闭(字符模式), 1=开启(行模式)
}SERIAL_CONFIG;
static void WriteSerialConfig(const SERIAL_CONFIG& cfg)
{
HKEY hKey;
if (ERROR_SUCCESS == ::RegCreateKey(HKEY_CURRENT_USER, L"SOFTWARE\\SerialForWindowsTerminal", &hKey))
{
DWORD dwSize = sizeof(DWORD);
DWORD dwType = REG_DWORD;
::RegSetValueEx(hKey, L"Serial", 0, dwType, (CONST LPBYTE)&cfg.Serial, dwSize);
::RegSetValueEx(hKey, L"BaudRate", 0, dwType, (CONST LPBYTE)&cfg.BaudRate, dwSize);
::RegSetValueEx(hKey, L"WordLength", 0, dwType, (CONST LPBYTE)&cfg.WordLength, dwSize);
::RegSetValueEx(hKey, L"StopBit", 0, dwType, (CONST LPBYTE)&cfg.StopBit, dwSize);
::RegSetValueEx(hKey, L"Parity", 0, dwType, (CONST LPBYTE)&cfg.Parity, dwSize);
::RegSetValueEx(hKey, L"FlowControl", 0, dwType, (CONST LPBYTE)&cfg.FlowControl, dwSize);
::RegSetValueEx(hKey, L"EncodingFormat", 0, dwType, (CONST LPBYTE)&cfg.EncodingFormat, dwSize);
::RegSetValueEx(hKey, L"EchoMode", 0, dwType, (CONST LPBYTE)&cfg.EchoMode, dwSize); // 写入回显模式
::RegCloseKey(hKey);
}
}
static void ToggleEncodingFormat(SERIAL_CONFIG& cfg)
{
// 切换编码格式
cfg.EncodingFormat = (cfg.EncodingFormat == 0) ? 1 : 0;
// 应用新的编码格式
if (cfg.EncodingFormat == 0) // UTF-8
{
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
std::cout << "\033[32mConsole encoding: UTF-8\033[0m" << std::endl;
}
else // GBK
{
SetConsoleOutputCP(936);
SetConsoleCP(936);
std::cout << "\033[32mConsole encoding: GBK\033[0m" << std::endl;
}
// 保存配置到注册表
WriteSerialConfig(cfg);
}
static SERIAL_CONFIG ReadSerialConfig()
{
HKEY hKey;
SERIAL_CONFIG cfg;
cfg.Serial = 0;
cfg.BaudRate = 9600;
cfg.WordLength = 8;
cfg.StopBit = ONESTOPBIT;
cfg.Parity = NOPARITY;
cfg.FlowControl = 0;
cfg.EncodingFormat = 0; // 默认使用UTF-8编码
cfg.EchoMode = 0; // 默认关闭回显模式(字符模式)
if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\\SerialForWindowsTerminal", 0, KEY_READ, &hKey))
{
DWORD dwSize = sizeof(DWORD);
DWORD dwType = REG_DWORD;
::RegQueryValueEx(hKey, L"Serial", 0, &dwType, (LPBYTE)&cfg.Serial, &dwSize);
::RegQueryValueEx(hKey, L"BaudRate", 0, &dwType, (LPBYTE)&cfg.BaudRate, &dwSize);
::RegQueryValueEx(hKey, L"WordLength", 0, &dwType, (LPBYTE)&cfg.WordLength, &dwSize);
::RegQueryValueEx(hKey, L"StopBit", 0, &dwType, (LPBYTE)&cfg.StopBit, &dwSize);
::RegQueryValueEx(hKey, L"Parity", 0, &dwType, (LPBYTE)&cfg.Parity, &dwSize);
::RegQueryValueEx(hKey, L"FlowControl", 0, &dwType, (LPBYTE)&cfg.FlowControl, &dwSize);
::RegQueryValueEx(hKey, L"EncodingFormat", 0, &dwType, (LPBYTE)&cfg.EncodingFormat, &dwSize);
::RegQueryValueEx(hKey, L"EchoMode", 0, &dwType, (LPBYTE)&cfg.EchoMode, &dwSize); // 读取回显模式
::RegCloseKey(hKey);
}
return cfg;
}
static void ToggleEchoMode(SERIAL_CONFIG& cfg)
{
// 切换回显模式
cfg.EchoMode = (cfg.EchoMode == 0) ? 1 : 0;
// 清空行缓冲区
g_lineBuffer.clear();
// 显示当前回显模式
std::cout << "\033[32mEcho mode: " << (cfg.EchoMode == 0 ? "Off" : "On") << "\033[0m" << std::endl;
// 保存配置到注册表
WriteSerialConfig(cfg);
}
static boost::system::error_code InitializeSerialPort(boost::asio::serial_port& serialPort,const SERIAL_CONFIG& cfg, boost::system::error_code& ec)
{
serialPort.set_option(boost::asio::serial_port::baud_rate(cfg.BaudRate), ec);
if (ec)
return ec;
serialPort.set_option(boost::asio::serial_port::character_size(cfg.WordLength), ec);
if (ec)
return ec;
switch (cfg.StopBit)
{
case 0:
serialPort.set_option(boost::asio::serial_port::stop_bits(boost::asio::serial_port::stop_bits::one), ec);
break;
case 1:
serialPort.set_option(boost::asio::serial_port::stop_bits(boost::asio::serial_port::stop_bits::onepointfive), ec);
break;
case 2:
serialPort.set_option(boost::asio::serial_port::stop_bits(boost::asio::serial_port::stop_bits::two), ec);
break;
default:
serialPort.set_option(boost::asio::serial_port::stop_bits(boost::asio::serial_port::stop_bits::one), ec);
break;
}
if (ec)
return ec;
switch (cfg.Parity)
{
case 0:
serialPort.set_option(boost::asio::serial_port::parity(boost::asio::serial_port::parity::none), ec);
break;
case 1:
serialPort.set_option(boost::asio::serial_port::parity(boost::asio::serial_port::parity::odd), ec);
break;
case 2:
serialPort.set_option(boost::asio::serial_port::parity(boost::asio::serial_port::parity::even), ec);
break;
default:
serialPort.set_option(boost::asio::serial_port::parity(boost::asio::serial_port::parity::none), ec);
break;
}
if (ec)
return ec;
switch (cfg.FlowControl)
{
case 0:
serialPort.set_option(boost::asio::serial_port::flow_control(boost::asio::serial_port::flow_control::none), ec);
break;
case 1:
serialPort.set_option(boost::asio::serial_port::flow_control(boost::asio::serial_port::flow_control::software), ec);
break;
case 2:
serialPort.set_option(boost::asio::serial_port::flow_control(boost::asio::serial_port::flow_control::hardware), ec);
break;
default:
serialPort.set_option(boost::asio::serial_port::flow_control(boost::asio::serial_port::flow_control::none), ec);
break;
}
return ec;
}
template <class TStream1, class TStream2>
static void DoStreamToStream(TStream1& stream1, TStream2& stream2, std::vector<uint8_t>& buffer, SERIAL_CONFIG* pCfg = nullptr)
{
stream1.async_read_some(
boost::asio::buffer(buffer.data(), buffer.size()),
[&stream1, &stream2, &buffer, pCfg](const boost::system::error_code& ec, std::size_t bytes_transferred)
{
if (ec)
{
std::cerr << "\033[31m" << "error : " << ec.message() << "\033[0m" << std::endl;
}
else
{
// 检查是否需要特殊处理键盘输入(仅当stream1是标准输入且有配置指针时)
bool skipWrite = false;
if (pCfg != nullptr)
{
for (size_t i = 0; i < bytes_transferred; i++)
{
uint8_t ch = buffer[i];
// 检查Ctrl+A (ASCII 1)
if (ch == 1)
{
g_isCtrlAPressed = true;
skipWrite = true;
break;
}
// 如果Ctrl+A已按下,检查后续按键
if (g_isCtrlAPressed)
{
g_isCtrlAPressed = false; // 重置标志
skipWrite = true;
// Ctrl+X: 退出程序
if (ch == 24) // Ctrl+X
{
if (MessageBoxW(NULL, L"确定要退出程序吗?", L"退出确认", MB_YESNO | MB_ICONQUESTION) == IDYES)
{
g_shouldExit = true;
// 通过取消所有异步操作来退出io_service
stream1.cancel();
stream2.cancel();
exit(0);
}
}
// Ctrl+C: 切换编码格式
else if (ch == 3) // Ctrl+C
{
ToggleEncodingFormat(*pCfg);
}
// Ctrl+E: 切换回显模式
else if (ch == 5) // Ctrl+E
{
ToggleEchoMode(*pCfg);
}
break;
}
}
// 如果是回显模式且不是特殊按键,进行行缓冲处理
if (!skipWrite && pCfg->EchoMode == 1)
{
skipWrite = true; // 总是跳过直接写入,由行模式处理
for (size_t i = 0; i < bytes_transferred; i++)
{
uint8_t ch = buffer[i];
// 处理退格键
if (ch == 8 || ch == 127) // Backspace或Delete
{
if (!g_lineBuffer.empty())
{
g_lineBuffer.pop_back();
// 输出退格、空格、退格以清除屏幕上的字符
std::cout << "\b \b";
}
continue;
}
// 处理回车换行
if (ch == '\r' || ch == '\n')
{
// 发送累积的行数据
// if (!g_lineBuffer.empty())
// {
// 添加回车换行
g_lineBuffer.push_back('\r');
g_lineBuffer.push_back('\n');
// 写入串口
boost::asio::write(stream2, boost::asio::buffer(g_lineBuffer.data(), g_lineBuffer.size()));
// 清空缓冲区
g_lineBuffer.clear();
// }
// 显示换行到终端
std::cout << "\n";
continue;
}
// 普通字符,添加到缓冲区并回显
g_lineBuffer.push_back(ch);
std::cout << static_cast<char>(ch);
std::cout.flush(); // 确保立即显示
}
}
}
// 只有在不需要特殊处理且不是回显模式时才进行正常的数据流传输
if (!skipWrite && (pCfg == nullptr || pCfg->EchoMode == 0))
{
boost::asio::async_write(
stream2,
boost::asio::const_buffer(buffer.data(), bytes_transferred),
[&stream1, &stream2, &buffer, pCfg](const boost::system::error_code& ec, std::size_t bytes_transferred)
{
if (ec)
{
std::cerr << "\033[31m" << "error : " << ec.message() << "\033[0m" << std::endl;
}
else
{
DoStreamToStream(stream1, stream2, buffer, pCfg);
}
}
);
}
else if (!g_shouldExit)
{
// 继续监听输入
DoStreamToStream(stream1, stream2, buffer, pCfg);
}
}
}
);
}
static boost::system::error_code DoWork(boost::asio::io_service& ioctx, boost::asio::serial_port& serialPort, SERIAL_CONFIG& cfg)
{
boost::system::error_code ec;
boost::asio::windows::stream_handle stdinput(ioctx);
boost::asio::windows::stream_handle stdoutput(ioctx);
const auto kBufferSize = 1024;
std::vector<uint8_t> serialPortRecvBuffer;
std::vector<uint8_t> serialPortSendBuffer;
serialPortRecvBuffer.resize(kBufferSize);
serialPortSendBuffer.resize(kBufferSize);
auto conin = CreateFile(L"CONIN$", FILE_GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
auto conout = CreateFile(L"CONOUT$", FILE_GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (stdinput.assign(conin, ec))
return ec;
if (stdoutput.assign(conout, ec))
return ec;
// 对于串口到输出的流,不需要特殊处理
DoStreamToStream(serialPort, stdoutput, serialPortRecvBuffer);
// 对于输入到串口的流,传递配置指针以支持快捷键处理
DoStreamToStream(stdinput, serialPort, serialPortSendBuffer, &cfg);
// 重置退出标志
g_shouldExit = false;
ioctx.run(ec);
return ec;
}
int wmain(int argc, const WCHAR* args[])
{
boost::system::error_code ec;
boost::asio::io_service ioctx;
boost::asio::serial_port serialPort(ioctx);
hInstance = GetModuleHandle(nullptr);
DWORD consoleMode = 0;
auto conin = GetStdHandle(STD_INPUT_HANDLE);
auto conout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(conin, &consoleMode);
consoleMode |= ENABLE_MOUSE_INPUT;
consoleMode &= ~ENABLE_ECHO_INPUT;
consoleMode &= ~ENABLE_PROCESSED_INPUT;
consoleMode &= ~ENABLE_LINE_INPUT;
consoleMode |= ENABLE_QUICK_EDIT_MODE;
consoleMode |= ENABLE_WINDOW_INPUT;
consoleMode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
SetConsoleMode(conin, consoleMode);
GetConsoleMode(conout, &consoleMode);
SetConsoleMode(conout, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT);
while (true)
{
auto hWndParent = ::GetForegroundWindow();
if (hWndParent == nullptr)
hWndParent = GetConsoleWindow();
if (DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_SETTING_DIALOG), hWndParent, SettingFunc, 1) == IDOK)
{
ioctx.restart();
auto cfg = ReadSerialConfig();
auto portName = std::string("COM") + std::to_string(cfg.Serial);
if (serialPort.open(portName, ec))
{
std::cerr << "\033[31m" << "can not open " << portName << "\033[0m" <<std::endl;
std::cerr << "\033[31m" << "error : " << ec.message() << "\033[0m" << std::endl;
continue;
}
if (InitializeSerialPort(serialPort, cfg, ec))
{
std::cerr << "\033[31m" << "can not initialize " << portName << "\033[0m" << std::endl;
std::cerr << "\033[31m" << "error : " << ec.message() << "\033[0m" << std::endl;
continue;
}
else
{
// 根据用户选择的编码格式设置控制台
if (cfg.EncodingFormat == 0) // UTF-8
{
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
}
else // GBK
{
SetConsoleOutputCP(936); // 936是GBK的代码页
SetConsoleCP(936);
}
// 显示当前编码格式
std::cout << "\033[36mVersion: v1.0.0\033[0m" << std::endl;
std::cout << "\033[32mConsole encoding: " << (cfg.EncodingFormat == 0 ? "UTF-8" : "GBK") << "\033[0m" << std::endl;
std::cout << "\033[32mEcho mode: " << (cfg.EchoMode == 0 ? "Off" : "On") << "\033[0m" << std::endl;
std::cout << "\033[33mPress Ctrl+A then Ctrl+C to toggle encoding format\033[0m" << std::endl;
std::cout << "\033[33mPress Ctrl+A then Ctrl+E to toggle echo mode\033[0m" << std::endl;
std::cout << "\033[33mPress Ctrl+A then Ctrl+X to exit\033[0m" << std::endl;
// 运行工作循环,传递配置
DoWork(ioctx, serialPort, cfg);
// 检查是否需要退出程序
if (g_shouldExit)
{
break;
}
}
break;
}
else
{
return ERROR_CANCELLED;
}
}
return ERROR_SUCCESS;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
CenterParentWindow(hDlg);
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
INT_PTR CALLBACK SettingFunc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
{
CenterParentWindow(hDlg);
auto cfg = ReadSerialConfig();
auto hWndPort = GetDlgItem(hDlg, IDC_COMBO_PORT);
UpdatePortControl(hDlg);
auto portCount = ComboBox_GetCount(hWndPort);
if (cfg.Serial != 0)
{
for (int i = 0; i < portCount; ++i)
{
auto com = (int)ComboBox_GetItemData(hWndPort, i);
if (com == cfg.Serial)
{
ComboBox_SetCurSel(hWndPort, i);
break;
}
}
}
else
{
ComboBox_SetCurSel(hWndPort, 0);
}
auto hWndBaudRate = GetDlgItem(hDlg, IDC_COMBO_SPEED);
ComboBox_AddString(hWndBaudRate, L"50");
ComboBox_AddString(hWndBaudRate, L"75");
ComboBox_AddString(hWndBaudRate, L"100");
ComboBox_AddString(hWndBaudRate, L"105");
ComboBox_AddString(hWndBaudRate, L"300");
ComboBox_AddString(hWndBaudRate, L"600");
ComboBox_AddString(hWndBaudRate, L"1200");
ComboBox_AddString(hWndBaudRate, L"2400");
ComboBox_AddString(hWndBaudRate, L"4800");
ComboBox_AddString(hWndBaudRate, L"9600");
ComboBox_AddString(hWndBaudRate, L"19200");
ComboBox_AddString(hWndBaudRate, L"38400");
ComboBox_AddString(hWndBaudRate, L"57600");
ComboBox_AddString(hWndBaudRate, L"115200");
ComboBox_AddString(hWndBaudRate, L"128000");
ComboBox_AddString(hWndBaudRate, L"256000");
ComboBox_SetText(hWndBaudRate, std::to_wstring(cfg.BaudRate).c_str());
auto hWndWordLength = GetDlgItem(hDlg, IDC_COMBO_WORD);
ComboBox_AddString(hWndWordLength, L"4");
ComboBox_AddString(hWndWordLength, L"5");
ComboBox_AddString(hWndWordLength, L"6");
ComboBox_AddString(hWndWordLength, L"7");
ComboBox_AddString(hWndWordLength, L"8");
ComboBox_AddString(hWndWordLength, L"9");
ComboBox_AddString(hWndWordLength, L"10");
ComboBox_SetCurSel(hWndWordLength, (int)(cfg.WordLength - 4));
auto hWndStopBit = GetDlgItem(hDlg, IDC_COMBO_STOP);
ComboBox_AddString(hWndStopBit, L"1");
ComboBox_AddString(hWndStopBit, L"1.5");
ComboBox_AddString(hWndStopBit, L"2");
ComboBox_SetCurSel(hWndStopBit, (int)(cfg.StopBit));
auto hWndParity = GetDlgItem(hDlg, IDC_COMBO_PARITY);
ComboBox_AddString(hWndParity, L"无");
ComboBox_AddString(hWndParity, L"奇");
ComboBox_AddString(hWndParity, L"偶");
ComboBox_SetCurSel(hWndParity, (int)(cfg.Parity));
auto hWndFlowControl = GetDlgItem(hDlg, IDC_COMBO_FLOW_CONTROL);
ComboBox_AddString(hWndFlowControl, L"无");
ComboBox_AddString(hWndFlowControl, L"软件(Xon/Xoff)");
ComboBox_AddString(hWndFlowControl, L"硬件");
ComboBox_SetCurSel(hWndFlowControl, (int)(cfg.FlowControl));
// 在WM_INITDIALOG处理部分,在FlowControl下拉框之后添加编码格式下拉框
auto hWndEncoding = GetDlgItem(hDlg, IDC_COMBO_ENCODING);
ComboBox_AddString(hWndEncoding, L"UTF-8");
ComboBox_AddString(hWndEncoding, L"GBK");
ComboBox_SetCurSel(hWndEncoding, (int)(cfg.EncodingFormat));
// 添加回显模式复选框
auto hWndEchoMode = GetDlgItem(hDlg, IDC_COMBO_ECHO);
ComboBox_AddString(hWndEchoMode, L"关闭");
ComboBox_AddString(hWndEchoMode, L"开启");
ComboBox_SetCurSel(hWndEchoMode, (int)(cfg.EchoMode));
return (INT_PTR)TRUE;
}
case WM_DEVICECHANGE:
UpdatePortControl(hDlg);
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
if (LOWORD(wParam) == IDOK)
{
SERIAL_CONFIG cfg = {0};
auto hWndPort = GetDlgItem(hDlg, IDC_COMBO_PORT);
auto hWndBaudRate = GetDlgItem(hDlg, IDC_COMBO_SPEED);
auto hWndWordLength = GetDlgItem(hDlg, IDC_COMBO_WORD);
auto hWndStopBit = GetDlgItem(hDlg, IDC_COMBO_STOP);
auto hWndParity = GetDlgItem(hDlg, IDC_COMBO_PARITY);
auto hWndFlowControl = GetDlgItem(hDlg, IDC_COMBO_FLOW_CONTROL);
WCHAR txtBuffer[32] = {0};
auto curSel = ComboBox_GetCurSel(hWndPort);
if (curSel >= 0)
{
cfg.Serial = (DWORD)ComboBox_GetItemData(hWndPort, curSel);
}
else
{
ComboBox_GetText(hWndPort, txtBuffer, 32);
cfg.Serial = std::wcstoul(txtBuffer + 3, nullptr, 10);
}
ComboBox_GetText(hWndBaudRate, txtBuffer, 32);
cfg.BaudRate = std::wcstoul(txtBuffer, nullptr, 10);
cfg.WordLength = ComboBox_GetCurSel(hWndWordLength) + 4;
cfg.StopBit = ComboBox_GetCurSel(hWndStopBit);
cfg.Parity = ComboBox_GetCurSel(hWndParity);
cfg.FlowControl = ComboBox_GetCurSel(hWndFlowControl);
// 获取编码格式下拉框的当前选择
auto hWndEncoding = GetDlgItem(hDlg, IDC_COMBO_ENCODING);
cfg.EncodingFormat = ComboBox_GetCurSel(hWndEncoding);
// 获取回显模式复选框状态
auto hWndEchoMode = GetDlgItem(hDlg, IDC_COMBO_ECHO);
cfg.EchoMode = ComboBox_GetCurSel(hWndEchoMode);
WriteSerialConfig(cfg);
}
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}