1 // fdebug.cpp : Defines the entry point for the application.
13 #define MAX_LOADSTRING 100
16 HINSTANCE hInst
; // current instance
17 TCHAR szTitle
[MAX_LOADSTRING
]; // The title bar text
18 TCHAR szWindowClass
[MAX_LOADSTRING
]; // The title bar text
19 HWND hMainWnd
; // The main window handle
20 HWND hDisplayWnd
; // The window to display the incoming data
21 HWND hEditWnd
; // The edit window to get input from the user
22 TCHAR strComPort
[MAX_PATH
] = TEXT("COM1"); // The COM port to use
23 TCHAR strBaudRate
[MAX_PATH
] = TEXT("115200"); // The baud rate to use
24 TCHAR strCaptureFileName
[MAX_PATH
] = TEXT(""); // The file name to capture to
25 BOOL bConnected
= FALSE
; // Tells us if we are currently connected
26 BOOL bCapturing
= FALSE
; // Tells us if we are currently capturing data
27 BOOL bLocalEcho
= FALSE
; // Tells us if local echo is currently enabled
28 HANDLE hCaptureFile
; // Handle to the capture file
29 DWORD dwThreadId
= 0; // Thread id of RS232 communication thread
31 // Foward declarations of functions included in this code module:
32 ATOM
MyRegisterClass(HINSTANCE hInstance
);
33 BOOL
InitInstance(HINSTANCE
, int);
34 LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
35 LRESULT CALLBACK
About(HWND
, UINT
, WPARAM
, LPARAM
);
36 LRESULT CALLBACK
ConnectionDialogProc(HWND
, UINT
, WPARAM
, LPARAM
);
37 LRESULT CALLBACK
CaptureDialogProc(HWND
, UINT
, WPARAM
, LPARAM
);
38 VOID
EnableFileMenuItemByID(UINT Id
, BOOL Enable
);
39 VOID
CheckLocalEchoMenuItem(BOOL Checked
);
40 VOID
Rs232Thread(VOID
* Parameter
);
42 int APIENTRY
_tWinMain(HINSTANCE hInstance
,
43 HINSTANCE hPrevInstance
,
47 // TODO: Place code here.
51 UNREFERENCED_PARAMETER(lpCmdLine
);
52 UNREFERENCED_PARAMETER(hPrevInstance
);
54 // Initialize global strings
55 LoadString(hInstance
, IDS_APP_TITLE
, szTitle
, MAX_LOADSTRING
);
56 LoadString(hInstance
, IDC_FDEBUG
, szWindowClass
, MAX_LOADSTRING
);
57 MyRegisterClass(hInstance
);
59 // Perform application initialization:
60 if (!InitInstance (hInstance
, nCmdShow
))
65 hAccelTable
= LoadAccelerators(hInstance
, (LPCTSTR
)IDC_FDEBUG
);
68 while (GetMessage(&msg
, NULL
, 0, 0))
70 if (!TranslateAccelerator(msg
.hwnd
, hAccelTable
, &msg
))
72 TranslateMessage(&msg
);
73 DispatchMessage(&msg
);
77 return (int)msg
.wParam
;
83 // FUNCTION: MyRegisterClass()
85 // PURPOSE: Registers the window class.
89 // This function and its usage is only necessary if you want this code
90 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
91 // function that was added to Windows 95. It is important to call this function
92 // so that the application will get 'well formed' small icons associated
95 ATOM
MyRegisterClass(HINSTANCE hInstance
)
99 wcex
.cbSize
= sizeof(WNDCLASSEX
);
101 wcex
.style
= CS_HREDRAW
| CS_VREDRAW
;
102 wcex
.lpfnWndProc
= WndProc
;
105 wcex
.hInstance
= hInstance
;
106 wcex
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_FDEBUG
));
107 wcex
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
108 wcex
.hbrBackground
= NULL
;//(HBRUSH)(COLOR_WINDOW+1);
109 wcex
.lpszMenuName
= MAKEINTRESOURCE(IDC_FDEBUG
);
110 wcex
.lpszClassName
= szWindowClass
;
111 wcex
.hIconSm
= (HICON
)LoadImage(hInstance
,
112 MAKEINTRESOURCE(IDI_FDEBUG
),
118 return RegisterClassEx(&wcex
);
122 // FUNCTION: InitInstance(HANDLE, int)
124 // PURPOSE: Saves instance handle and creates main window
128 // In this function, we save the instance handle in a global variable and
129 // create and display the main program window.
131 BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
135 hInst
= hInstance
; // Store instance handle in our global variable
137 hWnd
= CreateWindow(szWindowClass
, szTitle
, WS_OVERLAPPEDWINDOW
,
138 CW_USEDEFAULT
, 0, CW_USEDEFAULT
, 0, NULL
, NULL
, hInstance
, NULL
);
147 ShowWindow(hWnd
, nCmdShow
);
153 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
159 TCHAR WndText
[MAX_PATH
];
161 NONCLIENTMETRICS ncm
;
168 hEditWnd
= CreateWindowEx(WS_EX_CLIENTEDGE
, TEXT("EDIT"), TEXT(""), WS_CHILD
|WS_VISIBLE
|WS_VSCROLL
|ES_AUTOHSCROLL
|ES_LEFT
|ES_MULTILINE
, 0, 0, 0, 0, hWnd
, NULL
, hInst
, NULL
);
169 hDisplayWnd
= CreateWindowEx(WS_EX_CLIENTEDGE
, TEXT("EDIT"), TEXT(""), WS_CHILD
|WS_VISIBLE
|WS_HSCROLL
|WS_VSCROLL
|ES_MULTILINE
, 0, 0, 0, 0, hWnd
, NULL
, hInst
, NULL
);
171 memset(&ncm
, 0, sizeof(NONCLIENTMETRICS
));
172 ncm
.cbSize
= sizeof(NONCLIENTMETRICS
);
173 SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, sizeof(NONCLIENTMETRICS
), &ncm
, 0);
175 hFont
= CreateFontIndirect(&ncm
.lfMessageFont
);
177 SendMessage(hEditWnd
, WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
178 SendMessage(hDisplayWnd
, WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
182 wmId
= LOWORD(wParam
);
183 wmEvent
= HIWORD(wParam
);
185 if (lParam
== (LPARAM
)hEditWnd
&& wmEvent
== EN_CHANGE
)
187 GetWindowText(hEditWnd
, WndText
, MAX_PATH
);
189 if (_tcslen(WndText
) > 0)
191 SetWindowText(hEditWnd
, TEXT(""));
195 MessageBox(hWnd
, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
199 for (Index
=0; Index
<_tcslen(WndText
); Index
++)
203 PostThreadMessage(dwThreadId
, WM_CHAR
, (WPARAM
)WndText
[Index
], (LPARAM
)0);
209 // Parse the menu selections:
213 DialogBox(hInst
, (LPCTSTR
)IDD_ABOUTBOX
, hWnd
, (DLGPROC
)About
);
218 case IDM_FILE_CLEARDISPLAY
:
219 SetWindowText(hDisplayWnd
, TEXT(""));
221 case IDM_FILE_CONNECT
:
224 MessageBox(hWnd
, TEXT("You are already connected!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
228 if (DialogBox(hInst
, (LPCTSTR
)IDD_CONNECTION
, hWnd
, (DLGPROC
)ConnectionDialogProc
) == IDOK
)
231 EnableFileMenuItemByID(IDM_FILE_DISCONNECT
, TRUE
);
232 EnableFileMenuItemByID(IDM_FILE_CONNECT
, FALSE
);
233 _beginthread(Rs232Thread
, 0, NULL
);
237 case IDM_FILE_DISCONNECT
:
241 EnableFileMenuItemByID(IDM_FILE_DISCONNECT
, FALSE
);
242 EnableFileMenuItemByID(IDM_FILE_CONNECT
, TRUE
);
246 MessageBox(hWnd
, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
249 case IDM_FILE_STARTCAPTURE
:
250 if (DialogBox(hInst
, (LPCTSTR
)IDD_CAPTURE
, hWnd
, (DLGPROC
)CaptureDialogProc
) == IDOK
)
253 EnableFileMenuItemByID(IDM_FILE_STOPCAPTURE
, TRUE
);
254 EnableFileMenuItemByID(IDM_FILE_STARTCAPTURE
, FALSE
);
255 hCaptureFile
= CreateFile(strCaptureFileName
, FILE_APPEND_DATA
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
258 case IDM_FILE_STOPCAPTURE
:
262 EnableFileMenuItemByID(IDM_FILE_STOPCAPTURE
, FALSE
);
263 EnableFileMenuItemByID(IDM_FILE_STARTCAPTURE
, TRUE
);
264 CloseHandle(hCaptureFile
);
268 case IDM_FILE_LOCALECHO
:
272 CheckLocalEchoMenuItem(bLocalEcho
);
277 CheckLocalEchoMenuItem(bLocalEcho
);
281 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
285 hdc
= BeginPaint(hWnd
, &ps
);
291 GetClientRect(hWnd
, &rc
);
293 MoveWindow(hDisplayWnd
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
- 20, TRUE
);
294 MoveWindow(hEditWnd
, rc
.left
, rc
.bottom
- 20, rc
.right
- rc
.left
, 20, TRUE
);
301 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
306 LRESULT CALLBACK
About(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
308 HWND hLicenseEditWnd
;
309 TCHAR strLicense
[0x1000];
311 UNREFERENCED_PARAMETER(lParam
);
317 hLicenseEditWnd
= GetDlgItem(hDlg
, IDC_LICENSE_EDIT
);
319 LoadString(hInst
, IDS_LICENSE
, strLicense
, 0x1000);
321 SetWindowText(hLicenseEditWnd
, strLicense
);
326 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
328 EndDialog(hDlg
, LOWORD(wParam
));
336 LRESULT CALLBACK
ConnectionDialogProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
338 UNREFERENCED_PARAMETER(lParam
);
344 SetWindowText(GetDlgItem(hDlg
, IDC_COMPORT
), strComPort
);
345 SetWindowText(GetDlgItem(hDlg
, IDC_BAUTRATE
), strBaudRate
);
350 if (LOWORD(wParam
) == IDOK
)
352 GetWindowText(GetDlgItem(hDlg
, IDC_COMPORT
), strComPort
, MAX_PATH
);
353 GetWindowText(GetDlgItem(hDlg
, IDC_BAUTRATE
), strBaudRate
, MAX_PATH
);
356 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
358 EndDialog(hDlg
, LOWORD(wParam
));
366 LRESULT CALLBACK
CaptureDialogProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
370 UNREFERENCED_PARAMETER(lParam
);
376 SetWindowText(GetDlgItem(hDlg
, IDC_CAPTUREFILENAME
), strCaptureFileName
);
381 if (LOWORD(wParam
) == IDC_BROWSE
)
383 memset(&ofn
, 0, sizeof(OPENFILENAME
));
384 ofn
.lStructSize
= sizeof(OPENFILENAME
);
385 ofn
.hwndOwner
= hDlg
;
386 ofn
.hInstance
= hInst
;
387 ofn
.lpstrFilter
= NULL
;
388 ofn
.lpstrCustomFilter
= NULL
;
389 ofn
.nMaxCustFilter
= 0;
390 ofn
.nFilterIndex
= 0;
391 ofn
.lpstrFile
= strCaptureFileName
;
392 ofn
.nMaxFile
= MAX_PATH
;
393 ofn
.lpstrFileTitle
= NULL
;
394 ofn
.nMaxFileTitle
= 0;
395 ofn
.lpstrInitialDir
= NULL
;
396 ofn
.lpstrTitle
= NULL
;
397 ofn
.Flags
= OFN_HIDEREADONLY
|OFN_NOREADONLYRETURN
;
399 if (GetOpenFileName(&ofn
))
401 SetWindowText(GetDlgItem(hDlg
, IDC_CAPTUREFILENAME
), strCaptureFileName
);
405 if (LOWORD(wParam
) == IDOK
)
407 GetWindowText(GetDlgItem(hDlg
, IDC_CAPTUREFILENAME
), strCaptureFileName
, MAX_PATH
);
410 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
412 EndDialog(hDlg
, LOWORD(wParam
));
420 VOID
EnableFileMenuItemByID(UINT Id
, BOOL Enable
)
425 hMenuBar
= GetMenu(hMainWnd
);
426 hFileMenu
= GetSubMenu(hMenuBar
, 0);
427 EnableMenuItem(hFileMenu
, Id
, MF_BYCOMMAND
|(Enable
? MF_ENABLED
: MF_GRAYED
));
430 VOID
CheckLocalEchoMenuItem(BOOL Checked
)
435 hMenuBar
= GetMenu(hMainWnd
);
436 hFileMenu
= GetSubMenu(hMenuBar
, 0);
437 CheckMenuItem(hFileMenu
, IDM_FILE_LOCALECHO
, MF_BYCOMMAND
|(Checked
? MF_CHECKED
: MF_UNCHECKED
));
440 VOID
Rs232Thread(VOID
* Parameter
)
443 TCHAR String
[MAX_PATH
];
445 DWORD dwNumberOfBytesWritten
;
447 UNREFERENCED_PARAMETER(Parameter
);
449 dwThreadId
= GetCurrentThreadId();
451 if (!Rs232OpenPortWin32(strComPort
))
453 MessageBox(hMainWnd
, TEXT("Error opening port!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
458 _stprintf(String
, TEXT("%s,n,8,1"), strBaudRate
);
459 if (!Rs232ConfigurePortWin32(String
))
461 MessageBox(hMainWnd
, TEXT("Error configuring port!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
468 if (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
470 if (msg
.message
== WM_CHAR
)
472 Rs232WriteByteWin32((BYTE
)msg
.wParam
);
474 if (bLocalEcho
&& msg
.wParam
!= (WPARAM
)TEXT('\r'))
476 PostMessage(hDisplayWnd
, WM_CHAR
, (WPARAM
)msg
.wParam
, (LPARAM
)0);
480 WriteFile(hCaptureFile
, &msg
.wParam
, sizeof(TCHAR
), &dwNumberOfBytesWritten
, NULL
);
486 if (Rs232ReadByteWin32(&Byte
))
488 _stprintf(String
, TEXT("%c"), Byte
);
490 PostMessage(hDisplayWnd
, WM_CHAR
, (WPARAM
)String
[0], (LPARAM
)0);
494 WriteFile(hCaptureFile
, &String
[0], sizeof(TCHAR
), &dwNumberOfBytesWritten
, NULL
);
500 Rs232ClosePortWin32();