-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaindlg.cpp
1069 lines (933 loc) · 28.4 KB
/
maindlg.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
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <commctrl.h>
#include <tchar.h>
#include <winsock.h>
#include "AppDialog.h"
#include "crc16.h"
#include "resource.h"
#include "ParseHex.h"
#include "Trace.h"
#ifdef FTD2XX
#include "ftd2xx.h"
#define CBUS_BITBANG 0x20
#define SYNC_FIFO 0x40
#endif
#define ProgressBar_SetRange(hwnd,lo,hi) SendMessage(hwnd, PBM_SETRANGE, 0, MAKELONG(lo,hi))
#define ProgressBar_SetPos(hwnd,pos) SendMessage(hwnd, PBM_SETPOS, pos, 0L)
#define BUF_SIZE 65536
#define DEFAULT_COUNT 100
#define MAX_COUNT 1024
#define LOG_SENT_FLAG 0x80000000
#define LOG_COUNT_MASK 0x7FFFFFFF
#define GET_TIME_MSEC timeGetTime
const TCHAR szSettings[] = "Settings";
const TCHAR szTimeOut[] = "TimeOut";
const TCHAR szHeaderBytes[] = "HeaderBytes";
const TCHAR szTrailerBytes[] = "TrailerBytes";
const TCHAR szLengthMSB[] = "LengthMSB";
const TCHAR szLengthLSB[] = "LengthLSB";
const TCHAR szCRC16[] = "CRC16";
const TCHAR szCRCInit[] = "CRCInit";
const TCHAR szFlush[] = "Flush";
const TCHAR szCmdFile[] = "CmdFile";
const TCHAR szUsbCmdTxt[] = "usbcmd.txt";
TCHAR szProfile[MAX_PATH];
TCHAR szFilename[MAX_PATH];
typedef struct list_item
{
DWORD time;
UINT count;
BYTE data[BUF_SIZE];
} LIST_ITEM;
LIST_ITEM g_log_buf[MAX_COUNT];
DWORD g_start;
UINT g_logindex = 0;
void Log_AddItem(HWND hListView, LPVOID pBuffer, size_t count)
{
int iItem = g_logindex++;
LIST_ITEM* pLog = &g_log_buf[iItem % MAX_COUNT];
pLog->time = GET_TIME_MSEC() - g_start;
pLog->count = count;
memcpy(pLog->data, pBuffer, count & LOG_COUNT_MASK);
LVITEM lvi;
ZeroMemory(&lvi, sizeof(lvi));
lvi.mask = LVIF_TEXT;
lvi.pszText = LPSTR_TEXTCALLBACK;
lvi.iItem = iItem + 1;
int index = ListView_InsertItem(hListView, &lvi);
ListView_EnsureVisible(hListView, index, /*bPartial=*/TRUE);
}
#define STR_MAX 80
#define DEFAULT_TIMEOUT 1000
UINT g_uTimeOut = DEFAULT_TIMEOUT;
#ifdef FTD2XX
FT_HANDLE W32_OpenDevice(LPTSTR pszFile, DWORD dwBaud)
{
FT_HANDLE hDevice;
FT_STATUS status;
ULONG bufsize = 2048;
status = FT_OpenEx(pszFile, FT_OPEN_BY_SERIAL_NUMBER, &hDevice);
if (status != FT_OK)
return INVALID_HANDLE_VALUE;
if (dwBaud == 0)
{
// Set Syncrhonous 245 Mode
status = FT_SetBitMode(hDevice, 0x00, SYNC_FIFO);
if (status != FT_OK)
{
return INVALID_HANDLE_VALUE;
}
bufsize = 65536;
}
else
{
// Set 8 data bits, 1 stop bit and no parity
status = FT_SetBaudRate(hDevice, dwBaud);
if (status != FT_OK)
return INVALID_HANDLE_VALUE;
status = FT_SetDataCharacteristics
(hDevice, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
if (status != FT_OK)
return INVALID_HANDLE_VALUE;
// Set CBUS to output all 0s
status = FT_SetBitMode(hDevice, 0xF0, CBUS_BITBANG);
if (status != FT_OK)
return INVALID_HANDLE_VALUE;
bufsize = 4096;
}
status = FT_SetLatencyTimer(hDevice, 2);
if (status != FT_OK)
return INVALID_HANDLE_VALUE;
status = FT_SetUSBParameters(hDevice, bufsize, bufsize);
if (status != FT_OK)
return INVALID_HANDLE_VALUE;
status = FT_SetTimeouts(hDevice, g_uTimeOut, g_uTimeOut);
if (status != FT_OK)
return INVALID_HANDLE_VALUE;
return hDevice;
}
BOOL W32_CloseDevice(FT_HANDLE handle, DWORD dwBaud)
{
if (handle != INVALID_HANDLE_VALUE && dwBaud != 0)
{
// Set CBUS back to all inputs
FT_STATUS status = FT_SetBitMode(handle, 0x00, CBUS_BITBANG);
}
return FT_Close(handle) == FT_OK;
}
size_t W32_WriteBytes(FT_HANDLE hDevice, LPVOID pBuffer, size_t count)
{
DWORD dwWritten = 0;
if (hDevice != INVALID_HANDLE_VALUE)
{
dwWritten = 0;
BOOL bResult = FT_Write(hDevice, pBuffer, count, &dwWritten);
TRACE4("W32_WriteBytes(%08x, %u)=%u dwWritten=%u\n", pBuffer, count, bResult, dwWritten);
}
return dwWritten;
}
size_t W32_ReadBytes(FT_HANDLE hDevice, LPVOID pBuffer, size_t count, DWORD timeout)
{
DWORD tickStart = GET_TIME_MSEC();
DWORD dwTotal = 0;
if (timeout == 0)
timeout = g_uTimeOut;
/* exit loop after timeout has occurred */
do
{
DWORD dwRead = 0;
/* Read bytes from serial port */
if (FT_Read(hDevice, (LPSTR) pBuffer + dwTotal, count - dwTotal, &dwRead) != FT_OK)
{
DWORD dwError = FT_W32_GetLastError(hDevice);
TRACE2("Error reading %08x: %d\n", hDevice, dwError);
return dwRead;
}
dwTotal += dwRead;
if (dwTotal >= count)
break;
}
while (GET_TIME_MSEC() - tickStart <= timeout);
return dwTotal;
}
int QueryCOMPorts(HWND hCombo)
{
FT_STATUS status;
DWORD dwNumDevs;
status = FT_ListDevices(&dwNumDevs, NULL, FT_LIST_NUMBER_ONLY);
if (status != FT_OK)
return -1;
ComboBox_ResetContent(hCombo);
for (DWORD index = 0; index < dwNumDevs; ++index)
{
char szDevice[64];
status = FT_ListDevices((PVOID) index, szDevice, FT_LIST_BY_INDEX|FT_OPEN_BY_SERIAL_NUMBER);
if (status < FT_OK)
return -2;
ComboBox_AddString(hCombo, szDevice);
}
return dwNumDevs;
}
#else
typedef HANDLE FT_HANDLE;
//
// Win32 wrapper functions
//
BOOL W32_SetupCommPort(HANDLE hDevice, DWORD baud, DWORD timeout)
{
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = timeout;
timeouts.ReadTotalTimeoutConstant = timeout;
timeouts.ReadTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
BOOL bResult = SetCommTimeouts(hDevice, &timeouts);
if (bResult && baud != 0)
{
DCB dcb;
::ZeroMemory(&dcb, sizeof(dcb));
//
// DCB settings not in the user's control
//
dcb.DCBlength = sizeof(dcb);
dcb.fParity = TRUE;
dcb.fBinary = TRUE;
dcb.BaudRate = baud;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
//
// set new state
//
bResult = SetCommState(hDevice, &dcb);
}
return bResult;
}
FT_HANDLE W32_OpenDevice(LPTSTR szDevice, DWORD dwBaud)
{
TCHAR szPort[STR_MAX];
wsprintf(szPort, "\\\\.\\%s", szDevice);
FT_HANDLE hDevice = CreateFile(szPort, GENERIC_READ | GENERIC_WRITE, /*dwShare=*/0L,
/* security=*/ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDevice != INVALID_HANDLE_VALUE)
{
W32_SetupCommPort(hDevice, dwBaud, g_uTimeOut);
}
return hDevice;
}
BOOL W32_CloseDevice(FT_HANDLE handle, DWORD dwBaud)
{
if (handle != INVALID_HANDLE_VALUE)
return CloseHandle(handle);
return FALSE;
}
size_t W32_WriteBytes(FT_HANDLE hDevice, LPVOID pBuffer, size_t count)
{
DWORD dwWritten = 0;
if (hDevice != INVALID_HANDLE_VALUE)
{
dwWritten = 0;
BOOL bResult = WriteFile(hDevice, pBuffer, count, &dwWritten, /*lpOverlapped=*/NULL);
TRACE4("W32_WriteBytes(%08x, %u)=%u dwWritten=%u\n", pBuffer, count, bResult, dwWritten);
}
return dwWritten;
}
size_t W32_ReadBytes(FT_HANDLE hDevice, LPVOID pBuffer, size_t count, DWORD timeout)
{
DWORD tickStart = GET_TIME_MSEC();
DWORD dwTotal = 0;
if (timeout == 0)
timeout = g_uTimeOut;
/* exit loop after timeout has occurred */
do
{
DWORD dwRead = 0;
/* Read bytes from serial port */
if (!::ReadFile(hDevice, (LPSTR) pBuffer + dwTotal, count - dwTotal, &dwRead, NULL))
{
DWORD dwError = ::GetLastError();
TRACE2("Error reading %08x: %d\n", hDevice, dwError);
return dwRead;
}
dwTotal += dwRead;
if (dwTotal >= count)
break;
}
while (GET_TIME_MSEC() - tickStart <= timeout);
return dwTotal;
}
BOOL IsNumeric(LPCSTR pszString)
{
size_t len = strlen(pszString);
BOOL bNumeric = FALSE;
//What will be the return value from this function (assume the best)
for (size_t u = 0; u < len; u++)
{
TCHAR ch = pszString[u];
bNumeric = (ch >= '0' && ch <= '9');
if (!bNumeric)
{
break;
}
}
return bNumeric;
}
int QueryCOMPorts(HWND hCombo)
{
int count = 0;
DWORD dwSize = 8192;
DWORD dwRet;
do
{
TCHAR* pszDevices = (TCHAR*) malloc(dwSize);
if (pszDevices == NULL)
{
return -1;
}
dwRet = QueryDosDevice(NULL, pszDevices, dwSize);
if (dwRet)
{
size_t u = 0;
ComboBox_ResetContent(hCombo);
while (pszDevices[u] != _T('\0'))
{
//Get the current device name
TCHAR* pszCurrentDevice = pszDevices + u;
//If it looks like "COMX" then
//add it to the array which will be returned
size_t len = _tcslen(pszCurrentDevice);
if (len > 3)
{
TRACE1("dev=%s\n", pszCurrentDevice);
if ((_tcsnicmp(pszCurrentDevice, _T("COM"), 3) == 0) && IsNumeric(pszCurrentDevice + 3))
{
ComboBox_AddString(hCombo, pszCurrentDevice);
++count;
}
}
//Go to next device name
u += (len + 1);
}
}
free(pszDevices);
dwSize *= 2;
}
while (dwRet == 0);
return count;
}
#endif
const UINT baud[] = { 57600, 115200, 230400, 921600, 3000000, 12000000 };
void FillBaudRates(HWND hWndCombo)
{
for (UINT u = 0; u < sizeof(baud) / sizeof(UINT); ++u)
{
TCHAR szText[STR_MAX];
wsprintf(szText, "%u", baud[u]);
ComboBox_AddString(hWndCombo, szText);
}
}
class CMainDlg : public CAppDialog
{
protected:
HWND m_hListView;
HWND m_hComboBaud;
HWND m_hComboPort;
HWND m_hComboData;
FT_HANDLE m_hDevice;
LPBYTE m_pDataFile;
BOOL m_bLog;
HWND m_hProgress;
DWORD m_dwThreadId;
UINT m_uRepeat;
size_t m_uHeaderBytes;
size_t m_uTrailerBytes;
size_t m_uLengthMSB;
size_t m_uLengthLSB;
BOOL m_bCRC16;
CRC16 m_crcinit;
BOOL m_bFlush;
public:
CMainDlg(HINSTANCE hInst);
~CMainDlg();
virtual BOOL OnInitDialog(WPARAM wParam, LPARAM lParam);
virtual BOOL OnDrawItem(WPARAM wParam, LPDRAWITEMSTRUCT lParam);
virtual BOOL OnCommand(WPARAM wId);
virtual BOOL OnNotify(WPARAM wId, LPNMHDR nmhdr);
void EnableControls(BOOL flag);
void UpdateUI();
size_t CommandResponse(LPBYTE txbuf, size_t count, LPBYTE rxbuf, size_t length, DWORD timeout);
void DoSend(void);
void FillCombo(LPCTSTR pszFilename);
void DoClear(void);
void UpdateRTS(void);
void UpdateDTR(void);
void GetCellRect(int col, LPCRECT pItemRect, LPRECT pCellRect);
BOOL DrawListView(LPDRAWITEMSTRUCT lpDIS);
};
// Constructor
CMainDlg::CMainDlg(HINSTANCE hInst) : CAppDialog(hInst)
{
m_hListView = m_hComboPort = m_hComboData = NULL;
m_hDevice = INVALID_HANDLE_VALUE;
m_pDataFile = NULL;
m_bLog = TRUE;
}
// Destructor
CMainDlg::~CMainDlg()
{
if (W32_CloseDevice(m_hDevice, 0))
{
m_hDevice = INVALID_HANDLE_VALUE;
}
if (m_pDataFile != NULL)
{
GlobalFreePtr(m_pDataFile);
m_pDataFile = NULL;
}
}
#define LIST_COLS 3
int msgColWidth[LIST_COLS] = { 30, 60, 229000 };
const RESIZE_INFO resizeInfo[] =
{
{ IDC_PORT, RESIZE_W },
{ IDC_SET_BAUD, RESIZE_X },
{ IDC_BAUD_RATE, RESIZE_X },
{ IDC_RTS, RESIZE_X },
{ IDC_OPEN_CLOSE, RESIZE_X },
{ IDC_DATA, RESIZE_W },
{ IDC_REPEAT, RESIZE_X },
{ IDC_COUNT, RESIZE_X },
{ IDC_DTR, RESIZE_X },
{ IDC_SEND, RESIZE_X },
{ IDC_LISTVIEW, RESIZE_W | RESIZE_H },
{ IDC_LOG, RESIZE_Y },
{ IDC_CAPTURE, RESIZE_Y|RESIZE_W },
{ IDC_PROGRESS, RESIZE_Y|RESIZE_W },
{ IDC_CLEAR, RESIZE_X|RESIZE_Y }
};
#define RESIZE_COUNT(a) (sizeof(a) / sizeof(RESIZE_INFO))
BOOL CMainDlg::OnInitDialog(WPARAM wParam, LPARAM lParam)
{
m_iResizeCount = RESIZE_COUNT(resizeInfo);
m_pResizeInfo = resizeInfo;
// Set icons
// Attach icon to main dialog
HICON hIcon = LoadIcon(m_hInst, MAKEINTRESOURCE (IDD_MAIN));
SendMessage(m_hWnd, WM_SETICON, TRUE, (LPARAM) hIcon);
SendMessage (m_hWnd, WM_SETICON, FALSE, (LPARAM) hIcon);
m_hListView = GetDlgItem(m_hWnd, IDC_LISTVIEW);
m_hComboBaud = GetDlgItem(m_hWnd, IDC_BAUD_RATE);
m_hComboPort = GetDlgItem(m_hWnd, IDC_PORT);
m_hComboData = GetDlgItem(m_hWnd, IDC_DATA);
m_hProgress = GetDlgItem(m_hWnd, IDC_PROGRESS);
// Fill in combobox Baud rates
FillBaudRates(m_hComboBaud);
ComboBox_SetCurSel(m_hComboBaud, 0);
// Initialize default baud rate & repeat count
CheckDlgButton(m_hWnd, IDC_SET_BAUD, TRUE);
SetDlgItemInt(m_hWnd, IDC_COUNT, DEFAULT_COUNT, FALSE);
GetModuleFileName(NULL, szProfile, MAX_PATH);
int len = lstrlen(szProfile);
if (szProfile[len - 4] == '.')
{
lstrcpy(szProfile + len - 3, "ini");
}
g_uTimeOut = GetPrivateProfileInt(szSettings, szTimeOut, DEFAULT_TIMEOUT, szProfile);
m_uHeaderBytes = GetPrivateProfileInt(szSettings, szHeaderBytes, 4, szProfile);
m_uTrailerBytes = GetPrivateProfileInt(szSettings, szTrailerBytes, 0, szProfile);
m_uLengthMSB = GetPrivateProfileInt(szSettings, szLengthMSB, 2, szProfile);
m_uLengthLSB = GetPrivateProfileInt(szSettings, szLengthLSB, 3, szProfile);
m_bCRC16 = GetPrivateProfileInt(szSettings, szCRC16, TRUE, szProfile);
m_crcinit = GetPrivateProfileInt(szSettings, szCRCInit, 0, szProfile);
m_bFlush = GetPrivateProfileInt(szSettings, szFlush, TRUE, szProfile);
CheckDlgButton(m_hWnd, IDC_LOG, m_bLog);
// Setup List View columns
TCHAR szHeaders[STR_MAX];
if (LoadString(m_hInst, IDS_LIST_HEADERS, szHeaders, STR_MAX))
{
LPTSTR pszText = szHeaders;
for (int col = 0; col < LIST_COLS; ++col)
{
LPTSTR pszTab = strchr(pszText, '\t');
if (pszTab != NULL)
{
*pszTab++ = '\0';
}
LV_COLUMN lvcol;
ZeroMemory(&lvcol, sizeof(lvcol));
lvcol.mask = LVCF_TEXT|LVCF_FMT|LVCF_WIDTH;
lvcol.pszText = pszText;
lvcol.fmt = LVCFMT_CENTER;
lvcol.cx = msgColWidth[col];
int index = ListView_InsertColumn(m_hListView, col + 1, &lvcol);
pszText = pszTab;
}
}
// Fill in USB device names
if (QueryCOMPorts(m_hComboPort) > 0)
{
ComboBox_SetCurSel(m_hComboPort, 0);
}
UpdateUI();
// Fill in combobox messages
GetPrivateProfileString(szSettings, szCmdFile, szUsbCmdTxt, szFilename, sizeof(szFilename), szProfile);
FillCombo(szFilename);
DoClear();
return TRUE;
}
void CMainDlg::EnableControls(BOOL flag)
{
HWND hWnd= GetDlgItem(m_hWnd, IDC_SEND);
EnableWindow(hWnd, !flag);
EnableWindow(m_hComboPort, flag);
hWnd = GetDlgItem(m_hWnd, IDC_BAUD_RATE);
EnableWindow(hWnd, flag);
hWnd = GetDlgItem(m_hWnd, IDC_SET_BAUD);
EnableWindow(hWnd, flag);
}
void CMainDlg::UpdateUI()
{
TCHAR szText[STR_MAX];
UINT uId;
if (m_hDevice == INVALID_HANDLE_VALUE)
{
uId = IDS_OPEN;
EnableControls(TRUE);
}
else
{
uId = IDS_CLOSE;
EnableControls(FALSE);
}
if (LoadString(m_hInst, uId, szText, STR_MAX))
{
SetDlgItemText(m_hWnd, IDC_OPEN_CLOSE, szText);
}
}
long BenchmarkRead(FT_HANDLE hDevice, LPBYTE rxbuf, size_t count, DWORD timeout)
{
DWORD dwStart = GET_TIME_MSEC();
do
{
size_t chunk = (count < BUF_SIZE) ? count : BUF_SIZE;
size_t read = W32_ReadBytes(hDevice, rxbuf, chunk, timeout);
if (read != chunk)
return -1L;
count -= read;
}
while (count > 0);
return GET_TIME_MSEC() - dwStart;
}
size_t CMainDlg::CommandResponse(LPBYTE txbuf, size_t count, LPBYTE rxbuf, size_t length, DWORD timeout)
{
size_t written;
size_t read = 0;
if (m_bFlush)
{
/* Flush receive buffer */
#ifdef FTD2XX
FT_W32_PurgeComm(m_hDevice, PURGE_TXCLEAR|PURGE_RXCLEAR);
#else
PurgeComm(m_hDevice, PURGE_TXCLEAR | PURGE_RXCLEAR);
#endif
}
if (m_bCRC16)
{
LPBYTE pbuf = txbuf;
int i = count;
CRC16 crc = m_crcinit;
while (i)
{
crc = crc16_byte(crc, *pbuf++);
--i;
}
txbuf[count] = crc;
txbuf[count + 1] = crc >> 8;
count += 2;
}
written = W32_WriteBytes(m_hDevice, txbuf, count);
if (m_bLog && written > 0)
{
Log_AddItem(m_hListView, txbuf, written | LOG_SENT_FLAG);
}
size_t chunk = m_uHeaderBytes;
if (chunk >= BUF_SIZE)
{
// 21-Dec-2016: added to measure read throughput on USB CDC device
TCHAR szText[STR_MAX];
long dwElapsed = BenchmarkRead(m_hDevice, rxbuf, chunk, timeout);
wsprintf(szText, "Read %u bytes=%d.%03u sec", chunk, dwElapsed / 1000, dwElapsed % 1000);
if (m_bLog)
{
MessageBox(m_hWnd, szText, "Benchmark", MB_ICONINFORMATION);
}
}
else
{
read = W32_ReadBytes(m_hDevice, rxbuf, m_uHeaderBytes, timeout);
if (read == m_uHeaderBytes && m_uLengthLSB != 0 && m_uLengthMSB != 0)
{
short length = rxbuf[m_uLengthMSB] * 256 + rxbuf[m_uLengthLSB];
if (length >= 0)
{
length += m_uTrailerBytes;
if (length > 0)
read += W32_ReadBytes(m_hDevice, rxbuf + m_uHeaderBytes, length, timeout);
}
}
if (m_bLog && read > 0)
{
Log_AddItem(m_hListView, rxbuf, read);
}
}
return read;
}
ULONG __stdcall SendThread(LPVOID pThis)
{
((CMainDlg*) pThis)->DoSend();
return 0;
}
void CMainDlg::DoSend(void)
{
TCHAR szText[BUF_SIZE];
BYTE txbuf[BUF_SIZE];
BYTE rxbuf[BUF_SIZE];
int count;
int index = ComboBox_GetCurSel(m_hComboData);
if (index >= 0)
{
LPCTSTR itemdata = (LPCTSTR) ComboBox_GetItemData(m_hComboData, index);
count = lstrlen(itemdata);
if (count > BUF_SIZE - 1)
count = BUF_SIZE - 1;
strncpy(szText, itemdata, count + 1);
}
else
{
count = GetWindowText(m_hComboData, szText, BUF_SIZE - 1);
}
if (count > 0)
{
count = ParseHexBuf(txbuf, szText);
if (count >= 0)
{
BOOL bOK;
m_uRepeat = IsDlgButtonChecked(m_hWnd, IDC_REPEAT) ? GetDlgItemInt(m_hWnd, IDC_COUNT, &bOK, FALSE) : 1U;
BOOL bCapture = IsDlgButtonChecked(m_hWnd, IDC_CAPTURE);
const DWORD dwStart = GET_TIME_MSEC();
ProgressBar_SetRange(m_hProgress, 0, m_uRepeat);
for (UINT loop = 0; loop < m_uRepeat; ++loop)
{
size_t read = CommandResponse(txbuf, count, rxbuf, BUF_SIZE, 0);
ProgressBar_SetPos(m_hProgress, loop + 1);
UpdateWindow(m_hProgress);
if (bCapture) {
TCHAR szFilename[MAX_PATH];
wsprintf(szFilename, "Data%04u.bin", loop);
FILE* fp = fopen(szFilename, "wb");
if (fp != NULL) {
fwrite(rxbuf, read, 1, fp);
fclose(fp);
}
}
}
if (m_uRepeat > 1)
{
TCHAR szText[STR_MAX];
DWORD dwElapsed = GET_TIME_MSEC() - dwStart;
wsprintf(szText, "Elapsed=%u.%03u sec", dwElapsed / 1000, dwElapsed % 1000);
MessageBox(m_hWnd, szText, "Info", MB_ICONINFORMATION);
}
}
}
}
void CMainDlg::FillCombo(LPCTSTR pszFilename)
{
HANDLE hFile = CreateFile(pszFilename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
/* security=*/ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD dwSize = GetFileSize(hFile, NULL);
m_pDataFile = (LPBYTE) GlobalAllocPtr(GMEM_SHARE, dwSize);
if (m_pDataFile != NULL)
{
DWORD dwRead = 0;
ReadFile(hFile, m_pDataFile, dwSize, &dwRead, NULL);
LPTSTR pszText = (LPTSTR) m_pDataFile;
// Search for CR
for (;;)
{
LPTSTR pEol = strchr(pszText, '\r');
if (pEol == NULL)
{
break;
}
*pEol++ = '\0';
// Zero (optional) LF
if (*pEol == '\n')
{
*pEol++ = '\0';
}
// Check for ':' or '\t' deliminter
LPTSTR pDelim = strchr(pszText, ':');
if (pDelim == NULL)
{
pDelim = strchr(pszText, '\t');
}
if (pDelim != NULL)
{
*pDelim++ = '\0';
}
int index = ComboBox_InsertString(m_hComboData, -1, pszText);
if (pDelim != NULL)
{
ComboBox_SetItemData(m_hComboData, index, pDelim);
}
pszText = pEol;
}
}
CloseHandle(hFile);
}
}
BOOL CMainDlg::OnNotify(WPARAM wId, LPNMHDR nmhdr)
{
TRACE2("OnNotify(%u) code=%08x\n", wId, nmhdr->code);
switch (wId)
{
case IDC_DATA:
break;
}
return TRUE;
}
#define LVCOLS 4
void CMainDlg::GetCellRect(int col, LPCRECT pItemRect, LPRECT pCellRect)
{
HWND hwndHeader = ListView_GetHeader(m_hListView);
RECT header_rect;
Header_GetItemRect(hwndHeader, col, &header_rect);
// If we don't do this, when we scroll to the right, we will be
// drawing as if we weren't and your cells won't line up with the
// columns.
int x_offset = GetScrollPos(m_hListView, SB_HORZ);
pCellRect->left = header_rect.left - x_offset;
pCellRect->right = header_rect.right - x_offset;
pCellRect->top = pItemRect->top;
pCellRect->bottom = pItemRect->bottom;
}
#define DT_FLAGS (DT_END_ELLIPSIS | DT_VCENTER | DT_SINGLELINE)
#define LIGHT_RED RGB(255,224,224)
#define LIGHT_GRN RGB(224,255,224)
#define LINE_MAX 16384
BOOL CMainDlg::DrawListView(LPDRAWITEMSTRUCT lpDIS)
{
char szText[LINE_MAX * 5 + 1];
int count;
int iItem = lpDIS->itemID;
LIST_ITEM* pLog = &g_log_buf[iItem % MAX_COUNT];
HDC hDC = lpDIS->hDC;
COLORREF rgbBack, rgbText;
BOOL bSelected = lpDIS->itemState & ODS_SELECTED;
if (bSelected)
{
rgbBack = GetSysColor(COLOR_HIGHLIGHT);
rgbText = GetSysColor(COLOR_HIGHLIGHTTEXT);
}
else
{
const BOOL bSent = (pLog->count & LOG_SENT_FLAG);
short length = pLog->data[1];
rgbBack = bSent ? GetSysColor(COLOR_WINDOW) : (length < 0) ? LIGHT_RED : LIGHT_GRN;
rgbText = GetSysColor(COLOR_WINDOWTEXT);
}
COLORREF oldBack = SetBkColor(hDC, rgbBack);
COLORREF oldText = SetTextColor(hDC, rgbText);
// Fill background
ExtTextOut(hDC, 0,0, ETO_OPAQUE | ETO_CLIPPED, &lpDIS->rcItem, NULL, 0, NULL);
for (int col = 0; col < LVCOLS + 1; ++col)
{
RECT rect;
GetCellRect(col, &lpDIS->rcItem, &rect);
DWORD msec;
int i, length = 0;
// TRACE5("col=%d (%d,%d, %d,%d)\n", col, rect.left,rect.top, rect.right,rect.bottom);
int iFormat = DT_FLAGS;
switch (col)
{
case 0:
length = wsprintf(szText, "%u", iItem);
iFormat = DT_FLAGS | DT_CENTER;
break;
case 1:
msec = pLog->time;
length = wsprintf(szText, "%u.%03u ", msec / 1000, msec % 1000);
iFormat = DT_FLAGS | DT_RIGHT;
break;
case 2:
iFormat = DT_FLAGS | DT_LEFT;
count = pLog->count & LOG_COUNT_MASK;
if (count > LINE_MAX)
count = LINE_MAX;
i = 0;
while (i < count)
{
int remain = count - i;
if (remain >= 2)
{
length += wsprintf(szText + length, " %04X", (pLog->data[i] << 8) | pLog->data[i + 1]);
i += 2;
}
else
{
length += wsprintf(szText + length, " %02X", pLog->data[i++]);
}
}
}
// Draw Text
DrawText(hDC, szText, length, &rect, iFormat);
}
// Restore DC
SetBkColor(hDC, oldBack);
SetTextColor(hDC, oldText);
return TRUE;
}
BOOL CMainDlg::OnDrawItem(WPARAM wId, LPDRAWITEMSTRUCT lpDIS)
{
LPRECT pRect = &lpDIS->rcItem;
LPTSTR pText = (LPTSTR) lpDIS->itemData;
switch (wId)
{
case IDC_DATA:
TRACE5("OnDrawItem id=%u rect=(%d,%d,%d,%d)\n", lpDIS->itemID, pRect->left, pRect->top, pRect->right, pRect->bottom);
if (pText != NULL)
{
LPCTSTR pDelim = strchr(pText, ':');
int len = (pDelim != NULL) ? pDelim - pText : -1;
DrawText(lpDIS->hDC, pText, len, pRect, DT_LEFT);
TRACE1("OnDrawItem text=%s\n", pText);
}
break;
case IDC_LISTVIEW:
DrawListView(lpDIS);
break;
default:
MsgBox(MB_ICONERROR, IDS_ERR_DRAWITEM, wId);
}
return TRUE;
}
void CMainDlg::DoClear()
{
if (m_uRepeat <= 1)
{
ProgressBar_SetPos(m_hProgress, 0);
}
m_uRepeat = 0; // Stop loop in progress
ListView_DeleteAllItems(m_hListView);
g_logindex = 0;
g_start = GET_TIME_MSEC();
}
void CMainDlg::UpdateRTS(void)
{
#ifdef FTD2XX
DWORD dwFunc = IsDlgButtonChecked(m_hWnd, IDC_RTS) ? SETRTS : CLRRTS;
FT_W32_EscapeCommFunction(m_hDevice, dwFunc);
#else
DWORD dwFunc = IsDlgButtonChecked(m_hWnd, IDC_RTS) ? SETRTS : CLRRTS;
EscapeCommFunction(m_hDevice, dwFunc);
#endif
}
void CMainDlg::UpdateDTR(void)
{
#ifdef FTD2XX
DWORD dwFunc = IsDlgButtonChecked(m_hWnd, IDC_DTR) ? SETDTR : CLRDTR;
FT_W32_EscapeCommFunction(m_hDevice, dwFunc);
#else
DWORD dwFunc = IsDlgButtonChecked(m_hWnd, IDC_RTS) ? SETRTS : CLRRTS;
EscapeCommFunction(m_hDevice, dwFunc);
#endif
}
BOOL CMainDlg::OnCommand(WPARAM wId)
{
WORD code = HIWORD(wId);
WORD id = LOWORD(wId);
TRACE2("OnCommand(code=%04x,id=%u\n", code, id);
int index, result;
LPCTSTR pszText;
switch (id)
{
case IDC_OPEN_CLOSE:
if (m_hDevice != INVALID_HANDLE_VALUE)
{
DWORD dwBaud = GetDlgItemInt(m_hWnd, IDC_BAUD_RATE, NULL, FALSE);
if (W32_CloseDevice(m_hDevice, dwBaud))
{
m_hDevice = INVALID_HANDLE_VALUE;
}
}
else