-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMainFrame.cpp
More file actions
1319 lines (1219 loc) · 40.2 KB
/
MainFrame.cpp
File metadata and controls
1319 lines (1219 loc) · 40.2 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
/* Copyright (C) 2014-2026 Stefan-Mihai MOGA
This file is part of IntelliPort application developed by Stefan-Mihai MOGA.
IntelliPort is an alternative Windows version to the famous HyperTerminal!
IntelliPort is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Open
Source Initiative, either version 3 of the License, or any later version.
IntelliPort is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
IntelliPort. If not, see <http://www.opensource.org/licenses/gpl-3.0.html>*/
// MainFrame.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "IntelliPort.h"
#include "MainFrame.h"
#include "ConfigureDlg.h"
#include "InputDlg.h"
#include "WebBrowserDlg.h"
#include "CheckForUpdatesDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/**
* @class CMainFrame
* @brief Main application frame window for IntelliPort.
*
* This class manages the main window of the IntelliPort application, including:
* - Ribbon bar and status bar UI elements
* - Serial port and network socket connections
* - Background threads for data reception
* - Ring buffer for asynchronous data handling
* - Caption bar for displaying notifications
*
* The frame supports three connection types:
* - Serial port communication (RS-232)
* - TCP socket (client/server mode)
* - UDP socket (datagram mode)
*/
// Enable dynamic creation of this frame class
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWndEx)
// Message map - connects Windows messages and commands to handler functions
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
ON_WM_CREATE()
// Global help commands
ON_COMMAND(ID_HELP_FINDER, &CFrameWndEx::OnHelpFinder)
ON_COMMAND(ID_HELP, &CFrameWndEx::OnHelp)
ON_COMMAND(ID_CONTEXT_HELP, &CFrameWndEx::OnContextHelp)
ON_COMMAND(ID_DEFAULT_HELP, &CFrameWndEx::OnHelpFinder)
ON_COMMAND(ID_VIEW_CAPTION_BAR, &CMainFrame::OnViewCaptionBar)
ON_UPDATE_COMMAND_UI(ID_VIEW_CAPTION_BAR, &CMainFrame::OnUpdateViewCaptionBar)
ON_COMMAND(ID_TOOLS_OPTIONS, &CMainFrame::OnOptions)
ON_COMMAND(ID_FILE_PRINT, &CMainFrame::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CMainFrame::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CMainFrame::OnFilePrintPreview)
ON_UPDATE_COMMAND_UI(ID_FILE_PRINT_PREVIEW, &CMainFrame::OnUpdateFilePrintPreview)
// Custom implementation
ON_COMMAND(ID_CONFIG_SERIAL_PORT, &CMainFrame::OnConfigureSerialPort)
ON_COMMAND(ID_OPEN_SERIAL_PORT, &CMainFrame::OnOpenSerialPort)
ON_COMMAND(ID_CLOSE_SERIAL_PORT, &CMainFrame::OnCloseSerialPort)
ON_COMMAND(ID_SEND_RECEIVE, &CMainFrame::OnSendReceive)
ON_UPDATE_COMMAND_UI(ID_CONFIG_SERIAL_PORT, &CMainFrame::OnUpdateConfigureSerialPort)
ON_UPDATE_COMMAND_UI(ID_OPEN_SERIAL_PORT, &CMainFrame::OnUpdateOpenSerialPort)
ON_UPDATE_COMMAND_UI(ID_CLOSE_SERIAL_PORT, &CMainFrame::OnUpdateCloseSerialPort)
ON_UPDATE_COMMAND_UI(ID_SEND_RECEIVE, &CMainFrame::OnUpdateSendReceive)
ON_WM_TIMER()
ON_WM_DESTROY()
ON_COMMAND(IDC_TWITTER, &CMainFrame::OnTwitter)
ON_COMMAND(IDC_LINKEDIN, &CMainFrame::OnLinkedin)
ON_COMMAND(IDC_FACEBOOK, &CMainFrame::OnFacebook)
ON_COMMAND(IDC_INSTAGRAM, &CMainFrame::OnInstagram)
ON_COMMAND(IDC_ISSUES, &CMainFrame::OnIssues)
ON_COMMAND(IDC_DISCUSSIONS, &CMainFrame::OnDiscussions)
ON_COMMAND(IDC_WIKI, &CMainFrame::OnWiki)
ON_COMMAND(IDC_USER_MANUAL, &CMainFrame::OnUserManual)
ON_COMMAND(IDC_CHECK_FOR_UPDATES, &CMainFrame::OnCheckForUpdates)
END_MESSAGE_MAP()
/**
* @brief Constructor for CMainFrame.
*
* Initializes the main frame window and all member variables:
* - Loads application visual style from settings
* - Creates ring buffer for data communication (64KB)
* - Initializes threading variables to null/false
* - Sets default connection parameters (invalid state until configured)
* - Initializes default network settings (localhost:8080)
*/
CMainFrame::CMainFrame()
{
// Load application visual style from settings
theApp.m_nAppLook = theApp.GetInt(_T("ApplicationLook"), ID_VIEW_APPLOOK_OFF_2007_AQUA);
// Initialize time tracking for caption bar auto-hide
m_pCurrentDateTime = CTime::GetCurrentTime();
// Create ring buffer for data communication (64KB)
m_pRingBuffer.Create(0x10000);
// Initialize threading variables
m_nThreadRunning = false;
m_hSerialPortThread = nullptr;
m_hSocketThread = nullptr;
m_nSerialPortThreadID = 0;
m_nSocketTreadID = 0;
m_nTimerID = 0;
// Initialize serial port configuration to invalid state
theApp.m_nBaudRate = -1;
theApp.m_nDataBits = -1;
theApp.m_nParity = -1;
theApp.m_nStopBits = -1;
theApp.m_nFlowControl = -1;
// Initialize socket configuration
theApp.m_nSocketType = -1;
theApp.m_strServerIP = _T("127.0.0.1");
theApp.m_nServerPort = 8080;
theApp.m_strClientIP = _T("127.0.0.1");
theApp.m_nClientPort = 8080;
}
/**
* @brief Destructor for CMainFrame.
*
* Performs cleanup before the frame is destroyed:
* - Closes any open serial port or socket connection
* - Stops background reading threads
* - Destroys the ring buffer and releases memory
*/
CMainFrame::~CMainFrame()
{
// Close any open serial port or socket connection
OnCloseSerialPort();
// Destroy the ring buffer
m_pRingBuffer.Destroy();
}
/**
* @brief Handles the WM_CREATE message during window creation.
*
* Initializes all user interface elements and starts the timer:
* - Sets up Visual Studio 2005 style docking behavior
* - Creates and loads the ribbon bar from resources
* - Creates the status bar with a single pane
* - Creates the caption bar for notifications
* - Creates the incoming connection dialog (for TCP server mode)
* - Starts a 10ms timer for ring buffer checking
*
* @param lpCreateStruct Pointer to CREATESTRUCT containing window creation parameters.
* @return 0 on success, -1 if creation fails.
*/
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// Call base class OnCreate first
if (CFrameWndEx::OnCreate(lpCreateStruct) == -1)
return -1;
// Set up modern docking window behavior (VS 2005 style)
CDockingManager::SetDockingMode(DT_SMART);
// Allow panes to auto-hide when docked to any edge
EnableAutoHidePanes(CBRS_ALIGN_ANY);
// Use Windows-style visual theme for all UI elements
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
// Create the ribbon bar (Office-style toolbar)
m_wndRibbonBar.Create(this);
// Load ribbon configuration from resources
m_wndRibbonBar.LoadFromResource(IDR_RIBBON);
// Create the status bar at the bottom of the window
if (!m_wndStatusBar.Create(this))
{
TRACE0("Failed to create status bar\n");
return -1;
}
// Load the status pane title from string resources
bool bNameValid;
CString strTitlePane;
bNameValid = strTitlePane.LoadString(IDS_STATUS_PANE1);
ASSERT(bNameValid);
// Add a single status bar pane with fixed width (60 characters)
m_wndStatusBar.AddElement(new CMFCRibbonStatusBarPane(
ID_STATUSBAR_PANE1, strTitlePane, TRUE, NULL,
_T("012345678901234567890123456789012345678901234567890123456789")), strTitlePane);
// Load and display initial status message
VERIFY(strTitlePane.LoadString(IDS_LOG_HISTORY_CLEARED));
SetStatusBarText(strTitlePane);
// Create the caption bar (message notification area)
if (!CreateCaptionBar())
{
TRACE0("Failed to create caption bar\n");
return -1;
}
// Create the "waiting for connection" dialog (hidden initially)
VERIFY(m_dlgIncoming.Create(CIncomingDlg::IDD, this));
// Start a timer that fires every 10 milliseconds
// Timer is used to check ring buffer for incoming data
m_nTimerID = SetTimer(1, 10, NULL);
return 0;
}
/**
* @brief Called before the window is created.
*
* Allows modification of window creation parameters before the window is actually created.
* Can be overridden to change window class, styles, or other creation parameters.
*
* @param cs Reference to the CREATESTRUCT structure containing window creation parameters.
* @return TRUE if the window should be created, FALSE to prevent creation.
*/
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWndEx::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/**
* @brief Creates and configures the caption bar for displaying notifications.
*
* The caption bar is used to display temporary status messages and notifications:
* - Positioned at the top of the frame window
* - Displays an info icon on the left side
* - Includes tooltip support
* - Initially hidden (shown when messages are displayed)
* - Auto-hides after 10 seconds
*
* @return true if caption bar was created successfully, false on failure.
*/
bool CMainFrame::CreateCaptionBar()
{
// Create the caption bar window
if (!m_wndCaptionBar.Create(WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, this, ID_VIEW_CAPTION_BAR, -1, TRUE))
{
TRACE0("Failed to create caption bar\n");
return false;
}
bool bNameValid;
CString strTemp, strTemp2;
// Set the info icon on the caption bar
m_wndCaptionBar.SetBitmap(IDB_INFO, RGB(255, 255, 255), FALSE, CMFCCaptionBar::ALIGN_LEFT);
// Load and set the tooltip text
bNameValid = strTemp.LoadString(IDS_CAPTION_IMAGE_TIP);
ASSERT(bNameValid);
bNameValid = strTemp2.LoadString(IDS_CAPTION_IMAGE_TEXT);
ASSERT(bNameValid);
m_wndCaptionBar.SetImageToolTip(strTemp, strTemp2);
// Initially hide the message bar
HideMessageBar();
return true;
}
/**
* @brief Handles the WM_DESTROY message during window destruction.
*
* Performs cleanup before the window is destroyed:
* - Kills the timer used for ring buffer checking
* - Calls base class OnDestroy for standard cleanup
*/
void CMainFrame::OnDestroy()
{
// Kill the timer
VERIFY(KillTimer(m_nTimerID));
CFrameWndEx::OnDestroy();
}
/**
* @brief Handles the WM_TIMER message for periodic processing.
*
* Called every 10 milliseconds to:
* - Check the ring buffer for incoming data from serial port or socket
* - Convert UTF-8 data to Unicode and display in the edit view
* - Auto-hide the caption bar after 10 seconds of display
*
* The ring buffer is accessed with mutex locking to ensure thread safety
* between the UI thread and background reading threads.
*
* @param nIDEvent Timer identifier (must match m_nTimerID).
*/
void CMainFrame::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == m_nTimerID)
{
// Calculate how long the caption bar has been visible
CTime pDateTime = CTime::GetCurrentTime();
CTimeSpan pTimeSpan = pDateTime - m_pCurrentDateTime;
// Auto-hide caption bar after 10 seconds
if ((pTimeSpan.GetTotalSeconds() >= 10) && (m_wndCaptionBar.IsPaneVisible()))
{
HideMessageBar();
}
// Read incoming data from ring buffer (up to 4KB)
char pBuffer[0x1000] = { 0, };
// Lock mutex to prevent conflicts with reading threads
m_pMutualAccess.lock();
// Check how much data is available in the ring buffer
const int nLength = m_pRingBuffer.GetMaxReadSize();
if (nLength > 0)
{
// Read binary data from ring buffer into local buffer
m_pRingBuffer.ReadBinary(pBuffer, nLength);
// Null-terminate the buffer for string safety
pBuffer[nLength] = '\0';
// Convert UTF-8 encoded data to Unicode (wide string)
const std::string strRawText(pBuffer);
CString strBuffer(utf8_to_wstring(strRawText).c_str());
// Display the text in the edit view
AddText(strBuffer);
}
// Release mutex lock
m_pMutualAccess.unlock();
}
CFrameWndEx::OnTimer(nIDEvent);
}
/**
* @brief Sets the text displayed in the status bar.
*
* Updates the text in the first (and only) pane of the status bar.
* Forces an immediate repaint to ensure the text is displayed.
*
* @param strMessage The message to display in the status bar.
* @return true if the status bar exists and text was set, false otherwise.
*/
bool CMainFrame::SetStatusBarText(const CString& strMessage)
{
if (m_wndStatusBar.GetSafeHwnd() != nullptr)
{
m_wndStatusBar.GetElement(0)->SetText(strMessage);
m_wndStatusBar.Invalidate();
m_wndStatusBar.UpdateWindow();
return true;
}
return false;
}
/**
* @brief Sets the text displayed in the caption bar and shows it.
*
* Updates the caption bar message and makes it visible:
* - Resets the timestamp for the 10-second auto-hide timer
* - Shows the caption bar if it was hidden
* - Aligns text to the left
* - Forces a repaint and layout recalculation
*
* @param strMessage The message to display in the caption bar.
* @return true if the caption bar exists (note: currently always returns false).
*/
bool CMainFrame::SetCaptionBarText(const CString& strMessage)
{
if (m_wndCaptionBar.GetSafeHwnd() != nullptr)
{
// Update timestamp for auto-hide timer
m_pCurrentDateTime = CTime::GetCurrentTime();
// Show and update the caption bar
m_wndCaptionBar.ShowWindow(SW_SHOW);
m_wndCaptionBar.SetText(strMessage, CMFCCaptionBar::ALIGN_LEFT);
m_wndCaptionBar.Invalidate();
m_wndCaptionBar.UpdateWindow();
RecalcLayout();
}
return false;
}
/**
* @brief Hides the caption bar.
*
* Makes the caption bar invisible and recalculates the frame layout
* to adjust the space occupied by other elements.
*
* @return true if successful (always returns true).
*/
bool CMainFrame::HideMessageBar()
{
if (m_wndCaptionBar.GetSafeHwnd() != nullptr)
{
m_wndCaptionBar.ShowWindow(SW_HIDE);
m_wndCaptionBar.Invalidate();
m_wndCaptionBar.UpdateWindow();
RecalcLayout();
}
return true;
}
/**
* @brief Adds text to the active view's edit control.
*
* Appends text to the end of the edit control with proper line ending normalization:
* - Converts all line endings to CRLF (Windows standard)
* - Handles CRLF, CR, and LF input formats
* - Appends to the end of existing text
* - Maintains cursor position at the end
*
* @param strText The text to add to the view.
* @return true if successful (always returns true).
*/
bool CMainFrame::AddText(CString strText)
{
// Normalize line endings to Windows standard (CRLF)
// Step 1: Convert CRLF (\r\n) to LF (\n) to avoid double conversion
strText.Replace(CRLF, LF);
// Step 2: Convert any remaining CR (\r) to LF (\n)
strText.Replace(CR, LF);
// Step 3: Convert all LF (\n) to CRLF (\r\n) for Windows
strText.Replace(LF, CRLF);
// Get reference to the edit control in the active view
CEdit& pEdit = reinterpret_cast<CEditView*>(GetActiveView())->GetEditCtrl();
// Get current text length to find end position
int outLength = pEdit.GetWindowTextLength();
// Move cursor to the end of existing text
pEdit.SetSel(outLength, outLength);
// Insert new text at cursor position (with undo support)
pEdit.ReplaceSel(strText, TRUE);
// Reset selection (deselect text)
pEdit.SetSel(-1, 0);
return true;
}
// CMainFrame diagnostics
#ifdef _DEBUG
/**
* @brief Validates the main frame object in debug builds.
*
* Performs diagnostic validation to ensure the object is in a valid state.
* Used by MFC's debugging facilities to detect object corruption.
*/
void CMainFrame::AssertValid() const
{
CFrameWndEx::AssertValid();
}
/**
* @brief Dumps diagnostic information about the main frame.
*
* Outputs diagnostic information to the specified dump context for debugging.
* Called by MFC's diagnostic facilities.
*
* @param dc Reference to the CDumpContext for outputting diagnostic information.
*/
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWndEx::Dump(dc);
}
#endif //_DEBUG
// CMainFrame message handlers
/**
* @brief Toggles the visibility of the caption bar.
*
* Shows the caption bar if it's hidden, hides it if it's visible.
* Recalculates the frame layout after changing visibility.
*/
void CMainFrame::OnViewCaptionBar()
{
m_wndCaptionBar.ShowWindow(m_wndCaptionBar.IsVisible() ? SW_HIDE : SW_SHOW);
RecalcLayout(FALSE);
}
/**
* @brief Updates the UI state for the caption bar view menu item.
*
* Sets the check mark on the menu item based on caption bar visibility.
*
* @param pCmdUI Pointer to the CCmdUI object representing the menu item.
*/
void CMainFrame::OnUpdateViewCaptionBar(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_wndCaptionBar.IsVisible());
}
/**
* @brief Displays the ribbon customization dialog.
*
* Shows a modal dialog that allows users to customize the ribbon bar:
* - Add/remove buttons
* - Customize quick access toolbar
* - Modify keyboard shortcuts
*/
void CMainFrame::OnOptions()
{
CMFCRibbonCustomizeDialog *pOptionsDlg = new CMFCRibbonCustomizeDialog(this, &m_wndRibbonBar);
ASSERT(pOptionsDlg != NULL);
pOptionsDlg->DoModal();
delete pOptionsDlg;
}
/**
* @brief Handles the File Print command.
*
* If currently in print preview mode, forwards the print command to the preview.
* Otherwise, the base class handles normal printing.
*/
void CMainFrame::OnFilePrint()
{
if (IsPrintPreview())
{
PostMessage(WM_COMMAND, AFX_ID_PREVIEW_PRINT);
}
}
/**
* @brief Handles the File Print Preview command.
*
* Toggles print preview mode. If already in preview mode,
* closes the preview and returns to normal view.
*/
void CMainFrame::OnFilePrintPreview()
{
if (IsPrintPreview())
{
PostMessage(WM_COMMAND, AFX_ID_PREVIEW_CLOSE); // force Print Preview mode closed
}
}
/**
* @brief Updates the UI state for the print preview menu item.
*
* Sets the check mark on the menu item when in print preview mode.
*
* @param pCmdUI Pointer to the CCmdUI object representing the menu item.
*/
void CMainFrame::OnUpdateFilePrintPreview(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(IsPrintPreview());
}
/**
* @brief Displays the connection configuration dialog.
*
* Shows a modal dialog allowing the user to configure:
* - Connection type (Serial/TCP/UDP)
* - Serial port settings (port, baud rate, data bits, parity, stop bits, flow control)
* - Socket settings (server/client mode, IP addresses, ports)
*
* Configuration is saved to the application object when OK is clicked.
*/
void CMainFrame::OnConfigureSerialPort()
{
CConfigureDlg dlgConfigure(this);
if (dlgConfigure.DoModal() == IDOK)
{
// Configuration is saved within the dialog
}
}
/**
* @brief Opens a connection based on the configured settings.
*
* Creates and opens the appropriate connection type:
*
* Serial Port (Connection Type 0):
* - Opens the COM port with configured parameters
* - Starts SerialPortThreadFunc in a background thread
*
* TCP Socket (Connection Type 1):
* - Client mode: Connects to the configured server
* - Server mode: Binds, listens, and accepts incoming connection
* - Starts SocketThreadFunc in a background thread
*
* UDP Socket (Connection Type 2):
* - Creates and binds UDP socket for bidirectional communication
* - Starts SocketThreadFunc in a background thread
*
* Displays success/error messages in the caption bar.
* Handles CSerialException and CWSocketException errors gracefully.
*/
void CMainFrame::OnOpenSerialPort()
{
try
{
CString strFormat, strMessage;
switch (theApp.m_nConnection)
{
case 0: // Serial Port Connection
{
// Windows requires \\.\COMx format for COM ports >= 10
CString strFullPortName;
strFullPortName.Format(_T("\\\\.\\%s"), static_cast<LPCWSTR>(theApp.m_strSerialName));
// Open the serial port with all configured parameters
// FALSE = non-overlapped (synchronous) mode
m_pSerialPort.Open(
strFullPortName,
theApp.m_nBaudRate, // Baud rate (e.g., 9600, 115200)
(CSerialPort::Parity) theApp.m_nParity, // Parity (None, Odd, Even)
(BYTE)theApp.m_nDataBits, // Data bits (7 or 8)
(CSerialPort::StopBits) theApp.m_nStopBits, // Stop bits (1, 1.5, or 2)
(CSerialPort::FlowControl) theApp.m_nFlowControl, // Flow control
FALSE);
if (m_pSerialPort.IsOpen())
{
// Set flag to keep thread running
m_nThreadRunning = true;
// Create background thread to read incoming data
m_hSerialPortThread = CreateThread(nullptr, 0, SerialPortThreadFunc, this, 0, &m_nSerialPortThreadID);
// Show success message in caption bar
VERIFY(strFormat.LoadString(IDS_SERIAL_PORT_OPENED));
strMessage.Format(strFormat, static_cast<LPCWSTR>(theApp.m_strSerialName));
SetCaptionBarText(strMessage);
}
break;
}
case 1: // TCP Socket Connection
case 2: // UDP Socket Connection
{
// Get configured IP addresses and port numbers
CString strServerIP = theApp.m_strServerIP;
UINT nServerPort = theApp.m_nServerPort;
CString strClientIP = theApp.m_strClientIP;
UINT nClientPort = theApp.m_nClientPort;
if (theApp.m_nConnection == 1) // TCP Socket
{
if (theApp.m_nSocketType == 1) // TCP Client
{
// Connect to remote TCP server
m_pSocket.CreateAndConnect(strServerIP, nServerPort);
}
else // TCP Server
{
// Set up TCP server to accept incoming connections
// Bind to specified local IP address and port
m_pSocket.SetBindAddress(strClientIP);
// Create socket with SOCK_STREAM (TCP) type
m_pSocket.CreateAndBind(nClientPort, SOCK_STREAM, AF_INET);
// Display "waiting for connection" dialog
m_dlgIncoming.ShowWindow(SW_SHOW);
m_dlgIncoming.CenterWindow(this);
m_dlgIncoming.Invalidate();
m_dlgIncoming.UpdateWindow();
// Listen for incoming connections (backlog = 5)
m_pSocket.Listen();
// Block until a client connects (stores in m_pIncomming)
m_pSocket.Accept(m_pIncomming);
// Hide the waiting dialog
m_dlgIncoming.ShowWindow(SW_HIDE);
}
}
else // UDP Socket
{
// Create UDP socket for bidirectional communication
// Bind to local address/port
m_pSocket.SetBindAddress(strClientIP);
// Create socket with SOCK_DGRAM (UDP) type
m_pSocket.CreateAndBind(nClientPort, SOCK_DGRAM, AF_INET);
// For UDP, use client IP/port for display
strServerIP = strClientIP;
nServerPort = nClientPort;
}
if (m_pSocket.IsCreated())
{
// Set flag to keep thread running
m_nThreadRunning = true;
// Create background thread to read incoming data
m_hSocketThread = CreateThread(nullptr, 0, SocketThreadFunc, this, 0, &m_nSocketTreadID);
// Show success message in caption bar
VERIFY(strFormat.LoadString(IDS_SOCKET_CREATED));
strMessage.Format(strFormat, ((theApp.m_nConnection == 1) ? _T("TCP") : _T("UDP")), static_cast<LPCWSTR>(strServerIP), nServerPort);
SetCaptionBarText(strMessage);
}
break;
}
}
}
catch (CSerialException& pException)
{
// Handle serial port errors (port not available, access denied, etc.)
const int nErrorLength = 0x100;
TCHAR lpszErrorMessage[nErrorLength] = { 0, };
pException.GetErrorMessage2(lpszErrorMessage, nErrorLength);
TRACE(_T("%s\n"), lpszErrorMessage);
SetCaptionBarText(lpszErrorMessage);
m_nThreadRunning = false;
}
catch (CWSocketException* pException)
{
// Handle socket errors (connection refused, network unreachable, etc.)
const int nErrorLength = 0x100;
TCHAR lpszErrorMessage[nErrorLength] = { 0, };
pException->GetErrorMessage(lpszErrorMessage, nErrorLength);
TRACE(_T("%s\n"), lpszErrorMessage);
pException->Delete();
SetCaptionBarText(lpszErrorMessage);
m_nThreadRunning = false;
}
}
/**
* @brief Closes the active connection (serial port or socket).
*
* Performs graceful shutdown of the connection:
* - Sets m_nThreadRunning to false to signal thread termination
* - Waits for all background threads to complete (INFINITE timeout)
* - Closes serial port or socket resources
* - Displays confirmation message in caption bar
*
* Handles both serial port and socket connections (TCP/UDP).
* Thread synchronization ensures all I/O operations complete before closing.
*/
void CMainFrame::OnCloseSerialPort()
{
// Signal threads to stop running
if (m_nThreadRunning)
{
// Set flag to false - threads will check this and exit
m_nThreadRunning = false;
DWORD nThreadCount = 0;
HANDLE hThreadArray[2] = { 0, 0 };
// Build array of active thread handles
if (m_hSerialPortThread != nullptr)
{
hThreadArray[nThreadCount++] = m_hSerialPortThread;
}
if (m_hSocketThread != nullptr)
{
hThreadArray[nThreadCount++] = m_hSocketThread;
}
// Wait for all threads to finish (blocking call)
// TRUE = wait for ALL threads, INFINITE = no timeout
if (nThreadCount > 0)
{
WaitForMultipleObjects(nThreadCount, hThreadArray, TRUE, INFINITE);
}
}
try
{
CString strFormat, strMessage;
switch (theApp.m_nConnection)
{
case 0: // Serial Port
{
if (!m_pSerialPort.IsOpen())
{
VERIFY(strFormat.LoadString(IDS_SERIAL_PORT_CLOSED));
strMessage.Format(strFormat, static_cast<LPCWSTR>(theApp.m_strSerialName));
SetCaptionBarText(strMessage);
}
break;
}
case 1: // TCP Socket
case 2: // UDP Socket
{
CString strServerIP = theApp.m_strServerIP;
UINT nServerPort = theApp.m_nServerPort;
CString strClientIP = theApp.m_strClientIP;
UINT nClientPort = theApp.m_nClientPort;
if (!m_pSocket.IsCreated())
{
if (theApp.m_nConnection == 2) // UDP Socket
{
strServerIP = strClientIP;
nServerPort = nClientPort;
}
VERIFY(strFormat.LoadString(IDS_SOCKET_CLOSED));
strMessage.Format(strFormat, ((theApp.m_nConnection == 1) ? _T("TCP") : _T("UDP")), static_cast<LPCWSTR>(strServerIP), nServerPort);
SetCaptionBarText(strMessage);
}
break;
}
}
}
catch (CSerialException& pException)
{
const int nErrorLength = 0x100;
TCHAR lpszErrorMessage[nErrorLength] = { 0, };
pException.GetErrorMessage2(lpszErrorMessage, nErrorLength);
TRACE(_T("%s\n"), lpszErrorMessage);
SetCaptionBarText(lpszErrorMessage);
m_nThreadRunning = false;
}
catch (CWSocketException* pException)
{
const int nErrorLength = 0x100;
TCHAR lpszErrorMessage[nErrorLength] = { 0, };
pException->GetErrorMessage(lpszErrorMessage, nErrorLength);
TRACE(_T("%s\n"), lpszErrorMessage);
pException->Delete();
SetCaptionBarText(lpszErrorMessage);
m_nThreadRunning = false;
}
}
/**
* @brief Displays the input dialog and sends data through the active connection.
*
* Shows a modal input dialog for the user to enter data to send:
* - Converts Unicode input to UTF-8 for transmission
* - Sends data through the appropriate connection type:
* * Serial Port: Uses CSerialPort::Write()
* * TCP Client: Uses CWSocket::Send()
* * TCP Server: Uses incoming socket Send()
* * UDP: Uses CWSocket::SendTo() with configured server address
*
* Uses mutex locking to prevent conflicts with reading thread.
* Plays a beep sound on success or error.
* Displays error messages in caption bar if transmission fails.
*/
void CMainFrame::OnSendReceive()
{
int nLength = 0;
// Show input dialog for user to enter data
CInputDlg dlgInput(this);
if (dlgInput.DoModal() == IDOK)
{
// Convert Unicode (UTF-16) to UTF-8 for transmission
// Most serial/network protocols use UTF-8 encoding
const std::wstring strRawText(dlgInput.m_strSendData);
CStringA pBuffer(wstring_to_utf8(strRawText).c_str());
nLength = pBuffer.GetLength();
if (nLength > 0)
{
switch (theApp.m_nConnection)
{
case 0: // Serial Port
{
// Lock mutex to prevent conflicts with reading thread
m_pMutualAccess.lock();
try
{
// Write data to serial port
m_pSerialPort.Write(pBuffer.GetBuffer(nLength), nLength);
pBuffer.ReleaseBuffer();
}
catch (CSerialException& pException)
{
const int nErrorLength = 0x100;
TCHAR lpszErrorMessage[nErrorLength] = { 0, };
pException.GetErrorMessage2(lpszErrorMessage, nErrorLength);
TRACE(_T("%s\n"), lpszErrorMessage);
SetCaptionBarText(lpszErrorMessage);
m_nThreadRunning = false;
MessageBeep(MB_ICONERROR);
}
// Release mutex lock
m_pMutualAccess.unlock();
// Play success sound
MessageBeep(MB_OK);
break;
}
case 1: // TCP Socket
case 2: // UDP Socket
{
CString strServerIP = theApp.m_strServerIP;
const UINT nServerPort = theApp.m_nServerPort;
if (theApp.m_nConnection == 1) // TCP Socket
{
if (theApp.m_nSocketType == 1) // TCP Client
{
if (m_pSocket.IsWritable(1000))
{
m_pMutualAccess.lock();
try
{
m_pSocket.Send(pBuffer.GetBuffer(nLength), nLength, 0);
pBuffer.ReleaseBuffer();
}
catch (CWSocketException* pException)
{
const int nErrorLength = 0x100;
TCHAR lpszErrorMessage[nErrorLength] = { 0, };
pException->GetErrorMessage(lpszErrorMessage, nErrorLength);
TRACE(_T("%s\n"), lpszErrorMessage);
pException->Delete();
SetCaptionBarText(lpszErrorMessage);
m_nThreadRunning = false;
MessageBeep(MB_ICONERROR);
}
m_pMutualAccess.unlock();
MessageBeep(MB_OK);
}
}
else // TCP Server
{
if (m_pIncomming.IsWritable(1000))
{
m_pMutualAccess.lock();
try
{
m_pIncomming.Send(pBuffer.GetBuffer(nLength), nLength, 0);
pBuffer.ReleaseBuffer();
}
catch (CWSocketException* pException)
{
const int nErrorLength = 0x100;
TCHAR lpszErrorMessage[nErrorLength] = { 0, };
pException->GetErrorMessage(lpszErrorMessage, nErrorLength);
TRACE(_T("%s\n"), lpszErrorMessage);
pException->Delete();
SetCaptionBarText(lpszErrorMessage);
m_nThreadRunning = false;
MessageBeep(MB_ICONERROR);
}
m_pMutualAccess.unlock();
MessageBeep(MB_OK);
}
}
}
else // UDP Socket
{
if (m_pSocket.IsWritable(1000))
{
m_pMutualAccess.lock();
try
{
m_pSocket.SendTo(pBuffer.GetBuffer(nLength), nLength, nServerPort, strServerIP, 0);
pBuffer.ReleaseBuffer();
}
catch (CWSocketException* pException)
{
const int nErrorLength = 0x100;
TCHAR lpszErrorMessage[nErrorLength] = { 0, };
pException->GetErrorMessage(lpszErrorMessage, nErrorLength);
TRACE(_T("%s\n"), lpszErrorMessage);
pException->Delete();
SetCaptionBarText(lpszErrorMessage);
m_nThreadRunning = false;
MessageBeep(MB_ICONERROR);
}
m_pMutualAccess.unlock();
MessageBeep(MB_OK);
}
}
break;
}
}
}
}
}
/**
* @brief Updates the UI state for the configure command.
*
* Enables the configure menu item only when no connection is active.
* Prevents configuration changes while connected.
*
* @param pCmdUI Pointer to the CCmdUI object representing the menu item.
*/
void CMainFrame::OnUpdateConfigureSerialPort(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_pSerialPort.IsOpen() && !m_pSocket.IsCreated());
}
/**
* @brief Updates the UI state for the open connection command.
*
* Enables the open menu item only when no connection is currently active.
* Prevents multiple simultaneous connections.
*
* @param pCmdUI Pointer to the CCmdUI object representing the menu item.
*/
void CMainFrame::OnUpdateOpenSerialPort(CCmdUI* pCmdUI)
{