-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDCTerm.cpp
448 lines (369 loc) · 13.5 KB
/
CDCTerm.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
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
// CDCTerm.cpp
// Simple terminal which auto-(re)connects
// defaults to last free USB COM port device connected
// override with command line [COMnn] [baud]
// or run more instances to cycle through ports
// TODO:
// backspace vs. Del
// Alt key to switch ports (or run another instance)
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include "dbt.h"
#include "usbiodef.h"
#include "initguid.h"
#include "setupapi.h"
#pragma comment(lib, "setupAPI.lib")
#if 0 // TODO: watch for USB events instead of polling registry
void handlerProc(DWORD control) {
printf("Control %d\n", control);
}
HANDLE registerForDeviceEvents() {
// TODO: add rest of Microsoft service overcomplexity
// CreateService();
HANDLE hHandler = RegisterServiceCtrlHandler("USB Serial Hook", &handlerProc);
DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
ZeroMemory(¬ificationFilter, sizeof(notificationFilter));
notificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
// GUID UsbGUID = { 0xA5DCBF10, 0x6530, 0x11D2, {0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED} };
// GUID UsbSerialGUID = { 0x25dbce51, 0x6c8f, 0x4a72, 0x8a,0x6d,0xb5,0x4c,0x2b,0x4f,0xc8,0x35 };
notificationFilter.dbcc_classguid = GUID_CLASS_COMPORT;
HANDLE hDeviceNotify = RegisterDeviceNotification(hHandler, ¬ificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE);
if (hDeviceNotify == NULL) printf("Couldn't register for USB events!");
return hDeviceNotify;
}
#endif
FILETIME prev = {MAXDWORD, MAXDWORD};
char friendlyName[128], commName[128];
const char* lastActiveComPort() {
FILETIME latest = { 0 };
static char comPortName[8] = "none";
HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_CLASS_COMPORT, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
for (int index = 0; ; index++) {
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
if (!SetupDiEnumDeviceInfo(hDevInfo, index, &DeviceInfoData)) break;
char hardwareID[256];
SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID, NULL, (BYTE*)hardwareID, sizeof(hardwareID), NULL);
// truncate to make registry key for common USB CDC devices:
char* truncate;
if ((truncate = strstr(hardwareID, "&REV"))) *truncate = 0;
if ((truncate = strstr(hardwareID, "\\COMPORT"))) *truncate = 0; // FTDI
if (strstr(hardwareID, "VID_1CBE"))
strcat_s(hardwareID, sizeof(hardwareID), "&MI_00"); // Stellaris
char devKeyName[256] = "System\\CurrentControlSet\\Enum\\";
strcat_s(devKeyName, sizeof(devKeyName), hardwareID);
HKEY devKey = 0;
LSTATUS res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, devKeyName, 0, KEY_READ, &devKey);
if (devKey) {
DWORD idx = 0;
while (1) {
char serNum[64]; DWORD len = sizeof(serNum);
FILETIME lastWritten = { 0 }; // 100 ns
if (RegEnumKeyEx(devKey, idx++, serNum, &len, NULL, NULL, NULL, &lastWritten)) break;
if (CompareFileTime(&lastWritten, &latest) > 0 && CompareFileTime(&lastWritten, &prev) < 0) { // latest device connected
latest = lastWritten;
if (strstr(devKeyName, "FTDIBUS"))
strcat_s(serNum, sizeof(serNum), "\\0000"); // TODO: enumerate FTDIBUS also
len = sizeof(friendlyName);
RegGetValue(devKey, serNum, "FriendlyName", RRF_RT_REG_SZ, NULL, friendlyName, &len);
strcat_s(serNum, sizeof(serNum), "\\Device Parameters");
len = sizeof(comPortName);
RegGetValue(devKey, serNum, "PortName", RRF_RT_REG_SZ, NULL, comPortName, &len);
// SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, (BYTE*)devName, sizeof(devName), NULL);
}
}
RegCloseKey(devKey);
} else if (strstr(hardwareID, "USB")) printf("%s not found", devKeyName); // low numbered non-removable ports ignored
}
prev = latest;
SetupDiDestroyDeviceInfoList(hDevInfo);
// move COMxx to start of friendlyName
char* comPort = strchr(friendlyName, '(');
if (comPort) {
strcpy_s(commName, sizeof(commName), comPort+1);
*strchr(commName, ')') = ' ';
strcat_s(commName, sizeof(commName), friendlyName);
*(strchr(commName, '(') - 1) = 0;
} else strcpy_s(commName, sizeof(commName), friendlyName);
return comPortName;
// see also HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
// can check if disconnect removes from list
// can't tell which is last enumerated
}
HANDLE hCom = NULL;
HANDLE openSerial(const char* portName, int baudRate = 921600) {
char portDev[16] = "\\\\.\\";
strcat_s(portDev, sizeof(portDev), portName);
hCom = CreateFile(portDev, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); // better OVERLAPPED
if (hCom == INVALID_HANDLE_VALUE) return NULL;
// configure far end bridge COM port - only for bridges - could check endpoint capabilites??
DCB dcb = { 0 };
dcb.DCBlength = sizeof(DCB);
GetCommState(hCom, &dcb);
#if 1
dcb.BaudRate = baudRate;
// PL2303HX: Divisor = 12 * 1000 * 1000 * 32 / baud --> 12M, 6M, 3M, 2457600, 1228800, 921600, ... baud
// FTDI 3 MHz / (n + 0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875; where n is an integer between 2 and 16384)
// Note: special cases n = 0 -> 3 MBaud; n = 1 -> 2 MBaud; Sub-integer divisors between 0 and 2 not allowed.
// CP2102N: 24 MHz / N
// CH340: 3, 2, 1.5, 1.2, 1M
dcb.ByteSize = DATABITS_8;
dcb.StopBits = 0; // STOPBITS_10; // BUG in SetCommState or VCP??
dcb.fBinary = TRUE; // no EOF check
#if 1
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fOutxDsrFlow = false;
dcb.fOutxCtsFlow = false;
#else
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
dcb.fOutxDsrFlow = true;
dcb.fOutxCtsFlow = true;
#endif
#endif
if (!SetCommState(hCom, &dcb)) {printf("Can't set baud\n"); }
if (!SetupComm(hCom, 16384, 16)) printf("Can't SetupComm\n"); // Set size of I/O buffers (max 16384 on Win7)
// USB bulk packets arrive at 1 kHz rate
COMMTIMEOUTS timeouts = { 0 }; // in ms
timeouts.ReadIntervalTimeout = 200;
timeouts.ReadTotalTimeoutConstant = 16;
timeouts.ReadTotalTimeoutMultiplier = 0;
if (!SetCommTimeouts(hCom, &timeouts)) printf("Can't SetCommTimeouts\n");
return hCom;
}
int rxRdy(void) {
COMSTAT cs;
DWORD commErrors;
if (!ClearCommError(hCom, &commErrors, &cs)) return -1;
static DWORD lastCommErrors;
if (commErrors && commErrors != lastCommErrors) { // else annoying
lastCommErrors = commErrors;
printf("\n\rCommErr %X\n", commErrors); // 8 = framing (wrong baud rate); 2 = overrun; 1 = overflow
}
return cs.cbInQue;
}
void setInputEcho(bool on = true) {
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(hStdin, &mode);
if (on) mode |= ENABLE_ECHO_INPUT;
else mode &= ~ENABLE_ECHO_INPUT;
// mode &= ~ENABLE_LINE_INPUT;
SetConsoleMode(hStdin, mode);
}
bool EnableVTMode() { // Handle VT100 terminal sequences
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) return false;
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode)) return false;
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; // | ENABLE_QUICK_EDIT_MODE;
return SetConsoleMode(hOut, dwMode);
}
#define KEY_F1 59
#define KEY_F2 60
#define KEY_F3 61
#define KEY_F4 62
#define KEY_F5 63
#define KEY_F6 64
#define KEY_F7 65
#define KEY_F8 66
#define KEY_F9 67
#define KEY_HOME 71
#define KEY_UP 72
#define KEY_PGUP 73
#define KEY_LEFT 75
#define KEY_CENTER 76
#define KEY_RIGHT 77
#define KEY_END 79
#define KEY_DOWN 80
#define KEY_PGDN 81
#define KEY_INSERT 82
#define KEY_DELETE 83
enum { off, minLSB, minMSB, maxLSB, maxMSB, binLSB, binMSB } binMode;
void escapeKeys(unsigned char ch) { // for TERM=vt100
switch (ch) { // VT100 Escape sequences
case KEY_UP: ch = 'A'; break;
case KEY_DOWN: ch = 'B'; break;
case KEY_RIGHT: ch = 'C'; break;
case KEY_LEFT: ch = 'D'; break;
case KEY_HOME: ch = 'H'; break;
case KEY_END: ch = 'F'; break;
case 134: // F12
binMode = off;
system("cls");
return;
default :
switch (ch) {
case KEY_HOME: ch = '1'; break;
case KEY_INSERT: ch = '2'; break;
case KEY_DELETE: ch = '3'; break;
case KEY_END: ch = '4'; break;
case KEY_PGUP: ch = '5'; break;
case KEY_PGDN: ch = '6'; break;
default : return;
}
char csi4[] = "\x1B" "[n~";
csi4[2] = ch;
WriteFile(hCom, &csi4, 4, NULL, NULL);
return;
}
char CSI[3] = "\x1B" "[";
CSI[2] = ch;
if (!WriteFile(hCom, &CSI, 3, NULL, NULL)) throw "close";
}
unsigned char processKey() {
unsigned char ch = _getch();
if (ch == 0 || ch == 0xE0) {
escapeKeys(_getch()); // handle arrow keys: 0 or 0xE0 followed by key code
return 0;
} else if (!WriteFile(hCom, &ch, 1, NULL, NULL)) throw "close";
return ch;
}
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) {
char ch = 3; // ^C
WriteFile(hCom, &ch, 1, NULL, NULL);
return true;
}
HDC plot;
int maxX, maxY;
// TODO: better two threads
void processComms() {
switch (rxRdy()) {
case -1: throw "close";
case 0: // no incoming comms
Sleep(50);
break;
default:
unsigned char buf[64 * 2 + 1]; // two USB buffers
DWORD bytesRead;
if (!ReadFile(hCom, (char*)buf, sizeof(buf)-1, &bytesRead, NULL)) throw "close";
buf[bytesRead] = 0;
static int s, minADC, maxADC, x, binChs, binCh;
for (DWORD p = 0; p < bytesRead; ++p) {
switch (binMode) {
case off :
if (0 && (buf[p] & 0xF0) == 0xB0) { // too easy to enter *** TODO: require another mode character
binChs = buf[p] & 0xF;
binMode = minLSB;
binCh = 0;
x = 0;
}
break;
case minLSB:
minADC = buf[p];
binMode = minMSB;
break;
case minMSB:
minADC |= buf[p] << 8;
binMode = maxLSB;
break;
case maxLSB:
maxADC = buf[p];
binMode = maxMSB;
break;
case maxMSB:
maxADC |= buf[p] << 8;
binMode = binLSB;
break;
case binLSB:
s = buf[p];
binMode = binMSB;
break;
case binMSB:
s |= buf[p] << 8;
if (s == 0xFFFF) { // terminate plot
binMode = off;
printf("%s", buf + p);
return;
}
binMode = binLSB;
// TODO: color, gain per binCh
s = (s - minADC) * maxY / (maxADC - minADC);
SetPixel(plot, x, maxY - s, RGB(255, 128, 128));
if (++binCh >= binChs) {
binCh = 0;
if (++x >= maxX) x = 0;
}
break;
}
}
if (binMode != off) break;
while (1) { // remove ^Os at both ends of top lines
unsigned char* p = (unsigned char*)strchr((char*)buf, 15);
if (!p) break;
memmove(p, p + 1, bytesRead + buf - p); // remove ^O
}
printf("%s", buf);
break;
}
}
int main(int argc, char** argv) {
#if 0
HANDLE hDeviceNotify = registerForDeviceEvents();
printf("watching...");
#endif
HWND consoleWnd = GetConsoleWindow();
plot = GetDC(consoleWnd);
RECT rect;
SetConsoleCtrlHandler(CtrlHandler, TRUE); // doesn't work when run under debugger
EnableVTMode();
// args in any order "bbbbb" or "COMnn"
int baudRate = 921600; // 115200 -- doesn't matter unless bridged
const char* comPort = NULL;
for (int arg = 1; arg < argc; arg++) {
int val = atoi(argv[arg]);
if (val)
baudRate = val;
else comPort = argv[arg];
}
if (!comPort)
comPort = lastActiveComPort(); // "COMnn"
while (!openSerial(comPort, baudRate))
comPort = lastActiveComPort();
if (hCom <= (HANDLE)0) exit(-1);
SetWindowText(GetConsoleWindow(), commName);
printf("\nConnected to %s\n", commName);
while (1) {
try {
while (1) {
int pasteCount = 0;
while (_kbhit()) {
GetClientRect(consoleWnd, &rect);
maxX = rect.right;
maxY = rect.bottom;
unsigned char ch = processKey();
if (!ch) break; // Escape seq
if (ch == ' ') {
binMode = off;
InvalidateRect(consoleWnd, NULL, true);
}
if (ch <= '\r') {
++pasteCount;
SetWindowText(GetConsoleWindow(), commName); // typing: restore title after select
}
while (pasteCount >= 2 && _kbhit()) {
// pasting, not typing; process line at a time for speed -> Must CR at end of pasted text!!!
if (pasteCount++ == 2) setInputEcho(false);
char buf[64 * 2 + 1]; // two USB buffers
fgets(buf, sizeof(buf)-1, stdin); // to buffer or newline
DWORD len = (DWORD)strlen(buf);
if (!WriteFile(hCom, buf, len, NULL, NULL)) throw "close";
processComms();
}
}
setInputEcho(true);
processComms();
}
} catch (...) {
if (hCom > (HANDLE)0) {
CloseHandle(hCom);
hCom = 0;
}
}
while (!openSerial(comPort, baudRate)) Sleep(1000); // better on USB reconnect event
}
// UnregisterDeviceNotification(hDeviceNotify);
}