Repository navigation
Expand file tree
/
Copy pathScale3MainWin.cpp
More file actions
3535 lines (2831 loc) · 114 KB
/
Copy pathScale3MainWin.cpp
File metadata and controls
3535 lines (2831 loc) · 114 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
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
/***************************************************************
System: BioWindow -- Data Acquisition, Analysis, and Report Generator
File: CScale3MainWin.CPP
Procedure: Implements all of the methods contained in the CScale3MainWin
class.
Programmer: James D. Matthews
Date: Oct. 1, 2002
Copyright (C) 2002, Modular Instruments, Inc.
=================================================================
Revision Block
--------------
ECO No. Date Init. Procedure and Description of Change
-------- -------- ----- -----------------------------------
*****************************************************************/
/*****************************************************************
June 12th thru 19th, 2015
So I want to test this code in the real world. I don't have a MT scale or any old style
RS232 instrument to hook up to the Statech device. I do have some Parrllax Basic Stamp twos
and associated training boards from the 1990's. All I have to do is find one.
I found a populated Board of Education, serial cable, and power supply in the attic. Never throw
away anything! I downloaded the latest editor from the Parrallax website and installed it on
the Win8.1 Asus netbook that the Startech serial hub is connected to. (This netbook also acts as my
remote debugging target.) I start the BS2 editor up, power up the Board of Education, (BE), and
connect the BE to the Startech serial hub via the straight thru DB9 serial cable. The Parrallax
editor identifies it. So I'm in business and can program the BE to act as a Mettler Toledo lab scale.
I programmed the BE to wait until it receives the "SI\r\n" string. When it gets the string it sends back
numbers followed by a \r\n combo. Just like the MT lab scale. See my notes above the function that uses
CreateFile for issues and resolutions. But as of June 21st, 2015, I'm satisfied that this code works and
that I should continue with the BioWindow port.
My Test platform consists of a Basic Stamp 2, (BS2), pluged into a Board of Education, (BE). These devices
date from around 1998. The BE is used as a programming conection between the computer and the BS2. It uses
an RS232 serial connection to do this. All BS2 modules have a dedicated serial I/O pin, pin 16. The editor
uses this pin for programming the BS2, but the user is free to use it for RS232 serial communication within
their program. Using pin 16 eliminates the need for external wiring/components to complete an RS232 connection
to one of the 0 thru 15 pins that can also be used but are not internally setup for the RS232 standard. There
is a downside to using the BS2's dedicated serial pin. The downside is that it echos everything it receives. So
the subsequent read buffer will contain the command string in addition to the manufactured data. This echo
doesn't happen in real life but since all I expect to receive from the lab instrument is a numerical string,
I can just accept characters from 0 to 9. Alternatively, I could just filter out the command string or just
ignore the first x characters in the read buffer, where x equals the sizeof the write buffer.
*****************************************************************/
/***************** Controls in Scale3MainWin *********************
ID_OPENING1_SHUTDOWN
ID_OPENING1_PORTTOOLS_CREATEFILE
ID_OPENING1_PORTTOOLS_GETCOMMPROPS
ID_OPENING1_PORTTOOLS_GETCOMMSTATE
ID_OPENING1_PORTTOOLS_SETCOMMSTATE
ID_OPENING1_PORTTOOLS_GETCOMMTIMEOUTS
ID_OPENING1_PORTTOOLS_SETCOMMTIMEOUTS
ID_OPENING1_PORTTOOLS_DOITALL
ID_OPENING1_PORTTOOLS_CLOSEHANDLE
ID_OPENING1_SCALETOOLS_RDWRT_CURRENT
ID_OPENING1_SCALETOOLS_RDWRT_CONFIGURED
m_ComPortLBTitle IDC_COM_PORT_LB_TITLE
m_ComPortLB IDC_COM_PORT_LB
m_BaudRateCBTitle IDC_BAUD_RATE_CB_TITLE
m_BaudRateCB IDC_BAUD_RATE_CB
m_DataBitsCBTitle IDC_DATA_BITS_CB_TITLE
m_DataBitsCB IDC_DATA_BITS_CB
m_StopBitsCBTitle IDC_STOP_BITS_CB_TITLE
m_StopBitsCB IDC_STOP_BITS_CB
m_ParityCBTitle IDC_PARITY_CB_TITLE
m_ParityCB IDC_PARITY_CB
m_StatusText1 IDC_STATUS_TEXT_1
m_StatusText2 IDC_STATUS_TEXT_2
m_StructEFTitle IDC_STRUCT_EF_TITLE
m_StructEF IDC_STRUCT_EF
m_ConfiguredLBTitle IDC_CONFIG_LB_TITLE
m_ConfiguredLB IDC_CONFIG_LB
m_StartThreadsPB IDC_STRT_THREAD_PB
m_UseThreadsPB IDC_USE_THREAD_PB
m_TimerUseThreadsPB IDC_TIMER_USE_THREAD_PB
m_EndThreadsPB IDC_END_THREAD_PB
m_ThreadTimerEBTitle IDC_THREAD_TIMER_EB_TITLE
m_ThreadTimerEB IDC_THREAD_TIMER_EB
*****************************************************************/
#include <assert.h>
#include "stdafx.h"
#include "Scale3MainWin.hpp"
ULONG ulBaudRates[13] =
{
110, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 128000, 256000
};
short sDataBits[4] =
{
5, 6, 7, 8
};
float fStopBits[3] =
{
1, 1.5, 2
};
//CString szParity[5] =
//{
//"NONE", "ODD", "EVEN", "MARK", "SPACE"
//};
CString szParity2[5] =
{
_T("N"),
_T("O"),
_T("E"),
_T("M"),
_T("S")
};
CString szParity[5] =
{
_T("NOPARITY"),
_T("ODDPARITY"),
_T("EVENPARITY"),
_T("MARKPARITY"),
_T("SPACEPARITY")
};
/*************************************************
This is a major weakness or point of contention in the age of UniCode.
Assuming that the device you're communicating with is using/expecting ANSI instead of
UniCode. For now it seems safe to use ANSI. So I'm using unsigned char in conjunction
with the expectation that the device is
1) sending and receiving a byte at a time
2) each received or sent byte is an ANSI representation of a United States character set
3) all values from 0x00 to 0xff are available.
I don't like this since the code must be modified if it tries to communicate with a
UTF-8, UTF-16 UniCode compliante device.
Also I'm using the /J compilier flag to force char type to be unsigned char as opposed
to the default signed char.
Also by using char type, string and char functions, like strcpy and strlen, are
available for use.
*************************************************/
char szCurrentCmd[] = {"SI\r\n"};
CString COMMPROPMembers[18] =
{
_T("WORD wPacketLength"),
_T("WORD wPacketVersion"),
_T("DWORD dwServiceMask"),
_T("DWORD dwReserved1"),
_T("DWORD dwMaxTxQueue"),
_T("DWORD dwMaxRxQueue"),
_T("DWORD dwMaxBaud"),
_T("DWORD dwProvSubType"),
_T("DWORD dwProvCapabilities"),
_T("DWORD dwSettableParams"),
_T("DWORD dwSettableBaud"),
_T("WORD wSettableData"),
_T("WORD wSettableStopParity"),
_T("DWORD dwCurrentTxQueue"),
_T("DWORD dwCurrentRxQueue"),
_T("DWORD dwProvSpec1"),
_T("DWORD dwProvSpec2"),
_T("WCHAR wcProvChar[1]")
};
CString DCBMembers[28] =
{
_T("DWORD DCBlength"),
_T("DWORD BaudRate"),
_T("DWORD fBinary"),
_T("DWORD fParity"),
_T("DWORD fOutxCtsFlow"),
_T("DWORD fOutxDsrFlow"),
_T("DWORD fDtrControl"),
_T("DWORD fDsrSensitivity"),
_T("DWORD fTXContinueOnXoff"),
_T("DWORD fOutX"),
_T("DWORD fInX"),
_T("DWORD fErrorChar"),
_T("DWORD fNull"),
_T("DWORD fRtsControl"),
_T("DWORD fAbortOnError"),
_T("DWORD fDummy2"),
_T("WORD wReserved"),
_T("WORD XonLim"),
_T("WORD XoffLim"),
_T("BYTE ByteSize"),
_T("BYTE Parity"),
_T("BYTE StopBits"),
_T("char XonChar"),
_T("char XoffChar"),
_T("char ErrorChar"),
_T("char EofChar"),
_T("char EvtChar"),
_T("WORD wReserved1")
};
CString COMMTIMEOUTSMembers[5] =
{
_T("DWORD ReadIntervalTimeout"),
_T("DWORD ReadTotalTimeoutMultiplier"),
_T("DWORD ReadTotalTimeoutConstant"),
_T("DWORD WriteTotalTimeoutMultiplier"),
_T("DWORD WriteTotalTimeoutConstant")
};
BEGIN_MESSAGE_MAP (CScale3MainWin, CFrameWnd)
ON_WM_CREATE ()
ON_WM_CLOSE ()
ON_WM_SIZE ()
ON_WM_PAINT ()
ON_WM_TIMER ()
ON_UPDATE_COMMAND_UI (ID_OPENING1_SHUTDOWN, OnUpdateOpening1ShutDown)
ON_UPDATE_COMMAND_UI (ID_OPENING1_PORTTOOLS_CREATEFILE, OnUpdateOpening1CreateFile)
ON_UPDATE_COMMAND_UI (ID_OPENING1_PORTTOOLS_GETCOMMPROPS, OnUpdateOpening1GetCommProps)
ON_UPDATE_COMMAND_UI (ID_OPENING1_PORTTOOLS_GETCOMMSTATE, OnUpdateOpening1GetCommState)
ON_UPDATE_COMMAND_UI (ID_OPENING1_PORTTOOLS_SETCOMMSTATE, OnUpdateOpening1SetCommState)
ON_UPDATE_COMMAND_UI (ID_OPENING1_PORTTOOLS_GETCOMMTIMEOUTS, OnUpdateOpening1GetCommTimeouts)
ON_UPDATE_COMMAND_UI (ID_OPENING1_PORTTOOLS_SETCOMMTIMEOUTS, OnUpdateOpening1SetCommTimeouts)
ON_UPDATE_COMMAND_UI (ID_OPENING1_PORTTOOLS_DOITALL, OnUpdateOpening1DoAllOfTheAbove)
ON_UPDATE_COMMAND_UI (ID_OPENING1_PORTTOOLS_CLOSEHANDLE, OnUpdateOpening1CloseHandle)
ON_UPDATE_COMMAND_UI (ID_OPENING1_SCALETOOLS_RDWRT_CURRENT, OnUpdateOpening1RdWrtCurrent)
ON_UPDATE_COMMAND_UI (ID_OPENING1_SCALETOOLS_RDWRT_CONFIGURED, OnUpdateOpening1RdWrtConfigured)
ON_COMMAND (ID_OPENING1_SHUTDOWN, OnOpening1ShutDown)
ON_COMMAND (ID_OPENING1_PORTTOOLS_CREATEFILE, OnOpening1CreateFile)
ON_COMMAND (ID_OPENING1_PORTTOOLS_GETCOMMPROPS, OnOpening1GetCommProps)
ON_COMMAND (ID_OPENING1_PORTTOOLS_GETCOMMSTATE, OnOpening1GetCommState)
ON_COMMAND (ID_OPENING1_PORTTOOLS_SETCOMMSTATE, OnOpening1SetCommState)
ON_COMMAND (ID_OPENING1_PORTTOOLS_GETCOMMTIMEOUTS, OnOpening1GetCommTimeouts)
ON_COMMAND (ID_OPENING1_PORTTOOLS_SETCOMMTIMEOUTS, OnOpening1SetCommTimeouts)
ON_COMMAND (ID_OPENING1_PORTTOOLS_DOITALL, OnOpening1DoAllOfTheAbove)
ON_COMMAND (ID_OPENING1_PORTTOOLS_CLOSEHANDLE, OnOpening1CloseHandle)
ON_COMMAND (ID_OPENING1_SCALETOOLS_RDWRT_CURRENT, OnOpening1RdWrtCurrent)
ON_COMMAND (ID_OPENING1_SCALETOOLS_RDWRT_CONFIGURED, OnOpening1RdWrtConfigured)
ON_LBN_SELCHANGE (IDC_COM_PORT_LB, OnSelchangePortList)
ON_CBN_CLOSEUP (IDC_BAUD_RATE_CB, OnCloseUpBaudRate)
ON_CBN_CLOSEUP (IDC_DATA_BITS_CB, OnCloseUpDataBits)
ON_CBN_CLOSEUP (IDC_STOP_BITS_CB, OnCloseUpStopBits)
ON_CBN_CLOSEUP (IDC_PARITY_CB, OnCloseUpParity)
ON_BN_CLICKED (IDC_STRT_THREAD_PB, OnClickedStartThreads)
ON_BN_CLICKED (IDC_USE_THREAD_PB, OnClickedUseThreads)
ON_BN_CLICKED (IDC_TIMER_USE_THREAD_PB, OnClickedTimerUseThreads)
ON_BN_CLICKED (IDC_END_THREAD_PB, OnClickedEndThreads)
ON_BN_CLICKED (IDC_TEST_PB, OnClickedTest)
ON_MESSAGE (WM_USER_PORT1_THREAD_FINISHED_DATA, OnPort1ThreadFinishedData)
ON_MESSAGE (WM_USER_PORT2_THREAD_FINISHED_DATA, OnPort2ThreadFinishedData)
ON_MESSAGE (WM_USER_PORT3_THREAD_FINISHED_DATA, OnPort3ThreadFinishedData)
ON_MESSAGE (WM_USER_PORT4_THREAD_FINISHED_DATA, OnPort4ThreadFinishedData)
ON_MESSAGE (WM_USER_PORT1_THREAD_FINISHED_ERRORS, OnPort1ThreadFinishedErrors)
ON_MESSAGE (WM_USER_PORT2_THREAD_FINISHED_ERRORS, OnPort2ThreadFinishedErrors)
ON_MESSAGE (WM_USER_PORT3_THREAD_FINISHED_ERRORS, OnPort3ThreadFinishedErrors)
ON_MESSAGE (WM_USER_PORT4_THREAD_FINISHED_ERRORS, OnPort4ThreadFinishedErrors)
END_MESSAGE_MAP ()
CScale3MainWin::CScale3MainWin ()
{
int i;
for (i = 0; i < MAX_NUM_COMPORT; i++)
{
SecureZeroMemory (&tp[i], sizeof (THREADPARAMS));
SecureZeroMemory (&tr[i], sizeof (THREADRETURNS));
InitializeCriticalSectionAndSpinCount (&(tp[i].CritSec), 0x4);
}
m_sThread1Running = m_sThread2Running = m_sThread3Running = m_sThread4Running = 0;
// Initialize some stuff, MI2 Icon Stuff, and more
m_MI2Icon1 = (HICON)NULL;
m_MI2Icon2 = (HICON)NULL;
m_sCurrentPortIndex = 0;
// Set them to the known defaults for the MT Scales
m_sCurrentBaudRateIndex = 6;
m_sCurrentParityIndex = 2;
m_sCurrentDataBitsIndex = 2;
m_sCurrentStopBitsIndex = 0;
// Set them to the known defaults for the BS2 Simulated Scales
#ifdef BS2PIN16
m_sCurrentBaudRateIndex = 6;
m_sCurrentParityIndex = 0;
m_sCurrentDataBitsIndex = 3;
m_sCurrentStopBitsIndex = 0;
#endif
SecureZeroMemory (&m_oCurrentOverlappedStruct, sizeof (OVERLAPPED));
SecureZeroMemory (&m_CurrentCommTimeouts, sizeof (COMMTIMEOUTS));
SecureZeroMemory (&m_dcbCurrentDCB, sizeof (DCB));
m_dcbCurrentDCB.DCBlength = sizeof (DCB);
// Fill in DCB: 2400 bps, 7 data bits, even parity, and 1 stop bit.
m_dcbCurrentDCB.BaudRate = CBR_9600;
m_dcbCurrentDCB.ByteSize = 7;
m_dcbCurrentDCB.Parity = EVENPARITY;
m_dcbCurrentDCB.StopBits = ONESTOPBIT;
m_hCurrentHandle = (HANDLE)NULL;
m_sHandleFlag = 0;
m_sGotCommState = 0;
m_sDCBBuiltFlag = 0;
m_sGotTimeouts = 0;
m_sCommStateSet = 0;
m_sTimeoutsSet = 0;
m_sPortsConfigured = 0;
m_sShowConfig = 1;
m_sShowStruct = 1;
m_iThreadTimerValue = DEFAULTTHREADTIMERVALUE;
m_ThreadTimer = 0;
SecureZeroMemory (&m_CurrentCommProp, sizeof (COMMPROP));
//m_CurrentCommProp.dwProvSpec1 = COMMPROP_INITIALIZED;
try
{
strMyClass = AfxRegisterWndClass(
CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_BYTEALIGNWINDOW | CS_PARENTDC,
::LoadCursor(NULL, IDC_ARROW),
(HBRUSH)::GetStockObject(LTGRAY_BRUSH),
::LoadIcon(NULL, IDI_ERROR));
}
catch (CResourceException* pEx)
{
AfxMessageBox(_T("Couldn't register class! (Already registered?)"));
pEx->Delete();
}
Create (strMyClass, _T("MI2's Mettler Toledo SR16000 Scale Specific Proof of Concept Main Window"));
} // End of constructor CScale3MainWin
CScale3MainWin::~CScale3MainWin ()
{
COMSTAT ComStat;
DWORD ComError = 0;
BOOL bResult1;
int i;
if (m_sThread1Running | m_sThread2Running | m_sThread3Running | m_sThread4Running)
EndThreads ();
if (m_hCurrentHandle && !m_sPortsConfigured)
{
SecureZeroMemory (&ComStat, sizeof (COMSTAT));
bResult1 = ClearCommError (
m_hCurrentHandle,
&ComError,
&ComStat);
CloseHandle (m_hCurrentHandle);
}
for (i = 0; i < MAX_NUM_COMPORT; i++)
{
if (m_ComPorts[i].hCom)
{
SecureZeroMemory (&ComStat, sizeof (COMSTAT));
bResult1 = ClearCommError (
m_ComPorts[i].hCom,
&ComError,
&ComStat);
CloseHandle (m_ComPorts[i].hCom);
}
DeleteCriticalSection (&(tp[i].CritSec));
}
} // End of ~CScale3MainWin
int CScale3MainWin::OnCreate (LPCREATESTRUCT lpcs)
{
// Error Stuff
DWORD MyError = 0;
BOOL bResult1 = 0;
TCHAR szTemp1[16], szTemp2[STANDARD_STRING];
int iX, iY;
CRect rect;
HWND DTHwnd;
RECT DTRect;
if (CFrameWnd::OnCreate (lpcs) == -1)
return -1;
iX = AreTherSerialPorts();
if (!iX)
{
AfxMessageBox(_T("There are no SerialPorts. Terminating program."));
PostMessage(WM_CLOSE, 0, 0);
return 0;
}
iY = AreTherStartechSerialPorts();
if (!iY)
{
AfxMessageBox(_T("There are no Startech SerialPorts. Terminating program."));
PostMessage(WM_CLOSE, 0, 0);
return 0;
}
else
{
_stprintf_s(
szTemp2,
STANDARD_STRING,
_T("%d%s"),
iY,
_T(" Startech Serial Ports exist."));
AfxMessageBox(szTemp2, 0, 0);
}
// Take care of the MI2 Icon. I just want to make this guy available for use
// latter on, as in the Paint routine. This is a dark two tone icon.
m_MI2Icon1 = LoadIcon (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDI_MI2_1));
if (m_MI2Icon1 == (HICON)NULL)
DisplaySystemError (&MyError);
// Take care of the second MI2 Icon. After this the icon should appear everywhere.
// This is the Icon I want to appear everywhere, so this one gets used in the
// SetIcon call. Note that m_MI2Icon1 doesn't get used in this fashion.
// m_MI2Icon2 is a more colorful three tone icon that is easy to see in the
// title bar.
m_MI2Icon2 = LoadIcon (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDI_MI2_2));
if (m_MI2Icon2 == (HICON)NULL)
DisplaySystemError (&MyError);
else
SetIcon (m_MI2Icon2, TRUE);
CMenu *pSystemMenu = GetSystemMenu (FALSE);
pSystemMenu->DeleteMenu (SC_CLOSE, MF_BYCOMMAND);
// Initialize Sizing Stuff
GetClientRect(&m_rect);
m_iClientWidth = m_rect.Width ();
m_iClientHeight = m_rect.Height ();
// Load opening menus. Since they're members of the CGenericWindow class
// their destructors will be called when CGenericWindow goes into destruction.
// You should see no resource leaks.
m_menuOpeningMenu1.LoadMenu (IDR_OPENING_MENU1);
SetMenu (&m_menuOpeningMenu1);
m_fontMain.CreatePointFont (80, _T("MS Sans Serif"));
CClientDC dc (this);
CFont *pOldFont = dc.SelectObject (&m_fontMain);
TEXTMETRIC tm;
dc.GetTextMetrics (&tm);
m_cxChar = tm.tmAveCharWidth;
m_cyChar = tm.tmHeight + tm.tmExternalLeading;
dc.SelectObject (pOldFont);
// Com-Port listbox
// m_ComPortLBTitle IDC_COM_PORT_LB_TITLE
// m_ComPortLB IDC_COM_PORT_LB
rect.SetRect (m_cxChar * 2, m_cyChar, m_cxChar * 40, m_cyChar * 2);
m_ComPortLBTitle.Create (
_T("Available Com Ports"),
WS_CHILD | WS_VISIBLE | SS_LEFT,
rect, this, IDC_COM_PORT_LB_TITLE);
rect.SetRect (m_cxChar * 2, m_cyChar * 2, m_cxChar * 40, m_cyChar * 18);
m_ComPortLB.Create (
WS_CHILD | WS_HSCROLL | WS_VISIBLE | LBS_NOTIFY,
rect,
this,
IDC_COM_PORT_LB);
m_ComPortLBTitle.SetFont (&m_fontMain, false);
m_ComPortLB.SetFont (&m_fontMain, false);
FillComPortLB ();
// Baud Rate combobox
// m_BaudRateCBTitle IDC_BAUD_RATE_CB_TITLE
// m_BaudRateCB IDC_BAUD_RATE_CB
rect.SetRect (m_cxChar * 45, m_cyChar, m_cxChar * 60, m_cyChar * 2);
bResult1 = m_BaudRateCBTitle.Create (
_T("BAUD Rates"),
WS_CHILD | WS_VISIBLE | SS_LEFT,
rect, this, IDC_BAUD_RATE_CB_TITLE);
rect.SetRect (m_cxChar * 45, m_cyChar * 2, m_cxChar * 60, m_cyChar * 9);
bResult1 = m_BaudRateCB.Create (
WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
rect,
this,
IDC_BAUD_RATE_CB);
m_BaudRateCBTitle.SetFont (&m_fontMain, false);
m_BaudRateCB.SetFont (&m_fontMain, false);
// Parity combobox
// m_ParityCBTitle IDC_PARITY_CB_TITLE
// m_ParityCB IDC_PARITY_CB
rect.SetRect (m_cxChar * 60, m_cyChar, m_cxChar * 80, m_cyChar * 2);
bResult1 = m_ParityCBTitle.Create (
_T("Parity"),
WS_CHILD | WS_VISIBLE | SS_LEFT,
rect, this, IDC_PARITY_CB_TITLE);
rect.SetRect (m_cxChar * 60, m_cyChar * 2, m_cxChar * 80, m_cyChar * 9);
bResult1 = m_ParityCB.Create (
WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
rect,
this,
IDC_PARITY_CB);
m_ParityCBTitle.SetFont (&m_fontMain, false);
m_ParityCB.SetFont (&m_fontMain, false);
// Data Bits combobox
// m_DataBitsCBTitle IDC_DATA_BITS_CB_TITLE
// m_DataBitsCB IDC_DATA_BITS_CB
rect.SetRect (m_cxChar * 80, m_cyChar, m_cxChar * 90, m_cyChar * 2);
bResult1 = m_DataBitsCBTitle.Create (
_T("Data Bits"),
WS_CHILD | WS_VISIBLE | SS_LEFT,
rect, this, IDC_DATA_BITS_CB_TITLE);
rect.SetRect (m_cxChar * 80, m_cyChar * 2, m_cxChar * 90, m_cyChar * 9);
bResult1 = m_DataBitsCB.Create (
WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
rect,
this,
IDC_DATA_BITS_CB);
m_DataBitsCBTitle.SetFont (&m_fontMain, false);
m_DataBitsCB.SetFont (&m_fontMain, false);
// Stop Bits combobox
// m_StopBitsCBTitle IDC_STOP_BITS_CB_TITLE
// m_StopBitsCB IDC_STOP_BITS_CB
rect.SetRect (m_cxChar * 90, m_cyChar, m_cxChar * 100, m_cyChar * 2);
bResult1 = m_StopBitsCBTitle.Create (
_T("Stop Bits"),
WS_CHILD | WS_VISIBLE | SS_LEFT,
rect, this, IDC_STOP_BITS_CB_TITLE);
rect.SetRect (m_cxChar * 90, m_cyChar * 2, m_cxChar * 100, m_cyChar * 9);
bResult1 = m_StopBitsCB.Create (
WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
rect,
this,
IDC_STOP_BITS_CB);
m_StopBitsCBTitle.SetFont (&m_fontMain, false);
m_StopBitsCB.SetFont (&m_fontMain, false);
SetComboBoxes ();
// Status Text Fields
// m_StatusText1 IDC_STATUS_TEXT_1
rect.SetRect (m_cxChar * 2, m_cyChar * 26, m_cxChar * 123, m_cyChar * 27);
bResult1 = m_StatusText1.Create (
NULL,
WS_CHILD | WS_VISIBLE | SS_LEFT,
rect, this, IDC_STATUS_TEXT_1);
rect.SetRect (m_cxChar * 2, m_cyChar * 27, m_cxChar * 123, m_cyChar * 28);
bResult1 = m_StatusText2.Create (
NULL,
WS_CHILD | WS_VISIBLE | SS_LEFT,
rect, this, IDC_STATUS_TEXT_2);
m_StatusText1.SetFont (&m_fontMain, false);
m_StatusText2.SetFont (&m_fontMain, false);
// Structure Members Display
// m_StructLBTitle IDC_STRUCT_LB_TITLE
// m_StructLB IDC_STRUCT_LB
rect.SetRect (m_cxChar * 45, m_cyChar * 5, m_cxChar * 90, m_cyChar * 6);
bResult1 = m_StructLBTitle.Create (
_T(""),
WS_CHILD | WS_VISIBLE | SS_LEFT,
rect, this, IDC_STRUCT_LB_TITLE);
rect.SetRect (m_cxChar * 45, m_cyChar * 6, m_cxChar * 90, m_cyChar * 19);
bResult1 = m_StructLB.Create (
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | LBS_NOSEL,
rect,
this,
IDC_STRUCT_LB);
m_StructLBTitle.SetFont (&m_fontMain, false);
m_StructLBTitle.ShowWindow (SW_HIDE);
m_StructLB.SetFont (&m_fontMain, false);
m_StructLB.ShowWindow (SW_HIDE);
// Configured Ports Data Display
// m_ConfiguredLBTitle IDC_CONFIG_LB_TITLE
// m_ConfiguredLB IDC_CONFIG_LB
rect.SetRect (m_cxChar * 2, m_cyChar * 20, m_cxChar * 100, m_cyChar * 21);
bResult1 = m_ConfiguredLBTitle.Create (
_T("Data received from configured ports"),
WS_CHILD | WS_VISIBLE | SS_LEFT,
rect, this, IDC_CONFIG_LB_TITLE);
rect.SetRect (m_cxChar * 2, m_cyChar * 21, m_cxChar * 100, m_cyChar * 26);
bResult1 = m_ConfiguredLB.Create (
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | LBS_NOSEL,
rect,
this,
IDC_CONFIG_LB);
m_ConfiguredLBTitle.SetFont (&m_fontMain, false);
m_ConfiguredLBTitle.ShowWindow (SW_HIDE);
m_ConfiguredLB.SetFont (&m_fontMain, false);
m_ConfiguredLB.ShowWindow (SW_HIDE);
// m_TestPB IDC_TEST_PB
//rect.SetRect (m_cxChar * 105, m_cyChar * 2, m_cxChar * 120, m_cyChar * 5);
//bResult1 = m_TestPB.Create (
//"Test",
//WS_CHILD | WS_VISIBLE | BS_MULTILINE | BS_CENTER | BS_PUSHBUTTON,
//rect,
//this,
//IDC_TEST_PB);
// Configured Ports Threads Start, Use, Use w/ Timers, and End
// m_StartThreadsPB IDC_STRT_THREAD_PB
// m_UseThreadsPB IDC_USE_THREAD_PB
// m_TimerUseThreadsPB IDC_TIMER_USE_THREAD_PB
// m_EndThreadsPB IDC_END_THREAD_PB
rect.SetRect (m_cxChar * 105, m_cyChar * 2, m_cxChar * 120, m_cyChar * 5);
bResult1 = m_StartThreadsPB.Create (
_T("Start Threads"),
WS_CHILD | WS_VISIBLE | BS_MULTILINE | BS_CENTER | BS_PUSHBUTTON,
rect,
this,
IDC_STRT_THREAD_PB);
rect.SetRect (m_cxChar * 105, m_cyChar * 6, m_cxChar * 120, m_cyChar * 9);
bResult1 = m_UseThreadsPB.Create (
_T("Use Threads"),
WS_CHILD | WS_VISIBLE | BS_MULTILINE | BS_CENTER | BS_PUSHBUTTON,
rect,
this,
IDC_USE_THREAD_PB);
rect.SetRect (m_cxChar * 105, m_cyChar * 10, m_cxChar * 120, m_cyChar * 13);
bResult1 = m_TimerUseThreadsPB.Create (
_T("Use Threads w/ Timers"),
WS_CHILD | WS_VISIBLE | BS_MULTILINE | BS_CENTER | BS_PUSHBUTTON,
rect,
this,
IDC_TIMER_USE_THREAD_PB);
rect.SetRect (m_cxChar * 105, m_cyChar * 14, m_cxChar * 120, m_cyChar * 17);
bResult1 = m_EndThreadsPB.Create (
_T("End Threads"),
WS_CHILD | WS_VISIBLE | BS_MULTILINE | BS_CENTER | BS_PUSHBUTTON,
rect,
this,
IDC_END_THREAD_PB);
m_StartThreadsPB.SetFont (&m_fontMain, false);
m_StartThreadsPB.ShowWindow (SW_HIDE);
m_UseThreadsPB.SetFont (&m_fontMain, false);
m_UseThreadsPB.ShowWindow (SW_HIDE);
m_TimerUseThreadsPB.SetFont (&m_fontMain, false);
m_TimerUseThreadsPB.ShowWindow (SW_HIDE);
m_EndThreadsPB.SetFont (&m_fontMain, false);
m_EndThreadsPB.ShowWindow (SW_HIDE);
// m_ThreadTimerEBTitle IDC_THREAD_TIMER_EB_TITLE
// m_ThreadTimerEB IDC_THREAD_TIMER_EB
rect.SetRect (m_cxChar * 80, m_cyChar * 10, m_cxChar * 100, m_cyChar * 11);
bResult1 = m_ThreadTimerEBTitle.Create (
_T("Enter Timer in mS"),
WS_CHILD | WS_VISIBLE | SS_LEFT,
rect,
this,
IDC_THREAD_TIMER_EB_TITLE);
rect.SetRect (m_cxChar * 80, m_cyChar * 11, m_cxChar * 100, m_cyChar * 12);
bResult1 = m_ThreadTimerEB.Create (
WS_CHILD | WS_VISIBLE | ES_LEFT | ES_NUMBER,
rect,
this,
IDC_THREAD_TIMER_EB);
m_ThreadTimerEBTitle.SetFont (&m_fontMain, false);
m_ThreadTimerEBTitle.ShowWindow (SW_HIDE);
m_ThreadTimerEB.SetFont (&m_fontMain, false);
m_ThreadTimerEB.ShowWindow (SW_HIDE);
m_ThreadTimerEB.LimitText (5);
_stprintf_s(szTemp1, 16, _T("%d"), DEFAULTTHREADTIMERVALUE);
m_ThreadTimerEB.SetWindowText (szTemp1);
m_StatusText1.SetWindowText(_T("Current Device Path :"));
m_StatusText2.SetWindowText (m_ComPorts[m_sCurrentPortIndex].szDevicePath);
DTHwnd = ::GetDesktopWindow ();
::GetWindowRect (DTHwnd, &DTRect);
iX = (int)(DTRect.right * 0.25);
iY = (int)(DTRect.bottom * 0.6);
//SetWindowPos (
//&wndTop,
//iX,
//0,
//2 * iX,
//iY,
//SWP_SHOWWINDOW | SWP_NOZORDER);
CRect MyRect (0, 0, m_cxChar * 125, m_cyChar * 30);
CalcWindowRect (&MyRect);
SetWindowPos (&wndTop, iX, 0, MyRect.Width (), MyRect.Height (), SWP_SHOWWINDOW | SWP_NOZORDER);
return 1;
} // End of OnCreate
void CScale3MainWin::OnClose (void)
{
CFrameWnd::OnClose ();
} // End of OnClose
///////////////////// MFC/WIN-32 Menu-Update Messages ///////////////////////////
void CScale3MainWin::OnUpdateOpening1ShutDown (CCmdUI *pCmdUI)
{
bool bTemp1;
bTemp1 = true;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1ShutDown
void CScale3MainWin::OnUpdateOpening1CreateFile (CCmdUI *pCmdUI)
{
bool bTemp1;
if (!m_sHandleFlag)
bTemp1 = true;
else
bTemp1 = false;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1CreateFile
void CScale3MainWin::OnUpdateOpening1GetCommProps (CCmdUI *pCmdUI)
{
bool bTemp1;
if (!m_sHandleFlag)
bTemp1 = false;
else
bTemp1 = true;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1GetCommProps
void CScale3MainWin::OnUpdateOpening1GetCommState (CCmdUI *pCmdUI)
{
bool bTemp1;
if (!m_sHandleFlag)
bTemp1 = false;
else
bTemp1 = true;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1GetCommState
void CScale3MainWin::OnUpdateOpening1SetCommState (CCmdUI *pCmdUI)
{
bool bTemp1;
if (!m_sHandleFlag || !m_sGotCommState)
bTemp1 = false;
else
bTemp1 = true;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1SetCommState
void CScale3MainWin::OnUpdateOpening1GetCommTimeouts (CCmdUI *pCmdUI)
{
bool bTemp1;
if (!m_sHandleFlag || !m_sDCBBuiltFlag)
bTemp1 = false;
else
bTemp1 = true;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1GetCommTimeouts
void CScale3MainWin::OnUpdateOpening1SetCommTimeouts (CCmdUI *pCmdUI)
{
bool bTemp1;
if (!m_sHandleFlag || !m_sGotTimeouts)
bTemp1 = false;
else
bTemp1 = true;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1SetCommTimeouts
void CScale3MainWin::OnUpdateOpening1DoAllOfTheAbove (CCmdUI *pCmdUI)
{
bool bTemp1;
if (!m_sHandleFlag)
bTemp1 = true;
else
bTemp1 = false;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1DoAllOfTheAbove
void CScale3MainWin::OnUpdateOpening1CloseHandle (CCmdUI *pCmdUI)
{
bool bTemp1;
if (!m_sHandleFlag)
bTemp1 = false;
else
bTemp1 = true;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1CloseHandle
void CScale3MainWin::OnUpdateOpening1RdWrtCurrent (CCmdUI *pCmdUI)
{
bool bTemp1;
if (!m_sHandleFlag || !m_sCommStateSet || !m_sTimeoutsSet)
bTemp1 = false;
else
bTemp1 = true;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1RdWrtCurrent
void CScale3MainWin::OnUpdateOpening1RdWrtConfigured (CCmdUI *pCmdUI)
{
bool bTemp1;
if (!m_sPortsConfigured)
bTemp1 = false;
else
bTemp1 = true;
pCmdUI->Enable (bTemp1);
} // OnUpdateOpening1RdWrtConfigured
/////////////////////////// User Messages //////////////////////////
long CScale3MainWin::OnPort1ThreadFinishedData (WPARAM wParam, LPARAM lParam)
{
TCHAR szTemp1[STANDARD_STRING];
EnterCriticalSection (&(tp[0].CritSec));
_stprintf_s(szTemp1, STANDARD_STRING, _T("%s - %S"), m_ComPorts[0].szName, (char *)tr[0].Data);
LeaveCriticalSection(&(tp[0].CritSec));
m_ConfiguredLB.AddString (szTemp1);
return (0);
} // End of OnPort1ThreadFinished
long CScale3MainWin::OnPort2ThreadFinishedData (WPARAM wParam, LPARAM lParam)
{
TCHAR szTemp1[STANDARD_STRING];
EnterCriticalSection (&(tp[1].CritSec));
_stprintf_s(szTemp1, STANDARD_STRING, _T("%s - %S"), m_ComPorts[1].szName, (char *)tr[1].Data);
LeaveCriticalSection(&(tp[1].CritSec));
m_ConfiguredLB.AddString (szTemp1);
return (0);
} // End of OnPort1ThreadFinished
long CScale3MainWin::OnPort3ThreadFinishedData (WPARAM wParam, LPARAM lParam)
{
TCHAR szTemp1[STANDARD_STRING];
EnterCriticalSection (&(tp[2].CritSec));
_stprintf_s(szTemp1, STANDARD_STRING, _T("%s - %S"), m_ComPorts[2].szName, (char *)tr[2].Data);
LeaveCriticalSection (&(tp[2].CritSec));
m_ConfiguredLB.AddString (szTemp1);
return (0);
} // End of OnPort1ThreadFinished
long CScale3MainWin::OnPort4ThreadFinishedData (WPARAM wParam, LPARAM lParam)
{
TCHAR szTemp1[STANDARD_STRING];
EnterCriticalSection (&(tp[3].CritSec));
_stprintf_s(szTemp1, STANDARD_STRING, _T("%s - %S"), m_ComPorts[3].szName, (char *)tr[3].Data);
LeaveCriticalSection (&(tp[3].CritSec));
m_ConfiguredLB.AddString (szTemp1);
return (0);
} // End of OnPort1ThreadFinished
long CScale3MainWin::OnPort1ThreadFinishedErrors (WPARAM wParam, LPARAM lParam)
{
EnterCriticalSection (&(tp[0].CritSec));
m_StatusText1.SetWindowText (tr[0].ErrorMsg1);
m_StatusText2.SetWindowText (tr[0].ErrorMsg2);
LeaveCriticalSection (&(tp[0].CritSec));
return (0);
} // End of OnPort1ThreadFinished
long CScale3MainWin::OnPort2ThreadFinishedErrors (WPARAM wParam, LPARAM lParam)
{
EnterCriticalSection (&(tp[1].CritSec));
m_StatusText1.SetWindowText (tr[1].ErrorMsg1);
m_StatusText2.SetWindowText (tr[1].ErrorMsg2);
LeaveCriticalSection (&(tp[1].CritSec));
return (0);
} // End of OnPort2ThreadFinished
long CScale3MainWin::OnPort3ThreadFinishedErrors (WPARAM wParam, LPARAM lParam)
{
EnterCriticalSection (&(tp[2].CritSec));
m_StatusText1.SetWindowText (tr[2].ErrorMsg1);