1 /* fdebug.c : Defines the entry point for the application. */
15 #define MAX_LOADSTRING 100
18 HINSTANCE hInst
; // current instance
19 TCHAR szTitle
[MAX_LOADSTRING
]; // The title bar text
20 TCHAR szWindowClass
[MAX_LOADSTRING
]; // The title bar text
21 HWND hMainWnd
; // The main window handle
22 HWND hDisplayWnd
; // The window to display the incoming data
23 HWND hEditWnd
; // The edit window to get input from the user
24 TCHAR strComPort
[MAX_PATH
] = TEXT("COM1"); // The COM port to use
25 TCHAR strBaudRate
[MAX_PATH
] = TEXT("115200"); // The baud rate to use
26 TCHAR strCaptureFileName
[MAX_PATH
] = TEXT(""); // The file name to capture to
27 BOOL bConnected
= FALSE
; // Tells us if we are currently connected
28 BOOL bCapturing
= FALSE
; // Tells us if we are currently capturing data
29 BOOL bLocalEcho
= FALSE
; // Tells us if local echo is currently enabled
30 HANDLE hCaptureFile
; // Handle to the capture file
31 DWORD dwThreadId
= 0; // Thread id of RS232 communication thread
33 // Forward declarations of functions included in this code module:
34 ATOM
MyRegisterClass(HINSTANCE hInstance
);
35 BOOL
InitInstance(HINSTANCE
, int);
36 LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
37 INT_PTR CALLBACK
About(HWND
, UINT
, WPARAM
, LPARAM
);
38 INT_PTR CALLBACK
ConnectionDialogProc(HWND
, UINT
, WPARAM
, LPARAM
);
39 INT_PTR CALLBACK
CaptureDialogProc(HWND
, UINT
, WPARAM
, LPARAM
);
40 VOID
EnableFileMenuItemByID(UINT Id
, BOOL Enable
);
41 VOID
CheckLocalEchoMenuItem(BOOL Checked
);
42 VOID __cdecl
Rs232Thread(VOID
* Parameter
);
44 int APIENTRY
_tWinMain(HINSTANCE hInstance
,
45 HINSTANCE hPrevInstance
,
49 // TODO: Place code here.
53 UNREFERENCED_PARAMETER(lpCmdLine
);
54 UNREFERENCED_PARAMETER(hPrevInstance
);
56 // Initialize global strings
57 LoadString(hInstance
, IDS_APP_TITLE
, szTitle
, MAX_LOADSTRING
);
58 LoadString(hInstance
, IDC_FDEBUG
, szWindowClass
, MAX_LOADSTRING
);
59 MyRegisterClass(hInstance
);
61 // Perform application initialization:
62 if (!InitInstance (hInstance
, nCmdShow
))
67 hAccelTable
= LoadAccelerators(hInstance
, (LPCTSTR
)IDC_FDEBUG
);
70 while (GetMessage(&msg
, NULL
, 0, 0))
72 if (!TranslateAccelerator(hMainWnd
, hAccelTable
, &msg
))
74 TranslateMessage(&msg
);
75 DispatchMessage(&msg
);
79 return (int)msg
.wParam
;
85 // FUNCTION: MyRegisterClass()
87 // PURPOSE: Registers the window class.
91 // This function and its usage is only necessary if you want this code
92 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
93 // function that was added to Windows 95. It is important to call this function
94 // so that the application will get 'well formed' small icons associated
97 ATOM
MyRegisterClass(HINSTANCE hInstance
)
101 wcex
.cbSize
= sizeof(WNDCLASSEX
);
103 wcex
.style
= CS_HREDRAW
| CS_VREDRAW
;
104 wcex
.lpfnWndProc
= WndProc
;
107 wcex
.hInstance
= hInstance
;
108 wcex
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_FDEBUG
));
109 wcex
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
110 wcex
.hbrBackground
= NULL
;//(HBRUSH)(COLOR_WINDOW+1);
111 wcex
.lpszMenuName
= MAKEINTRESOURCE(IDC_FDEBUG
);
112 wcex
.lpszClassName
= szWindowClass
;
113 wcex
.hIconSm
= (HICON
)LoadImage(hInstance
,
114 MAKEINTRESOURCE(IDI_FDEBUG
),
120 return RegisterClassEx(&wcex
);
124 // FUNCTION: InitInstance(HANDLE, int)
126 // PURPOSE: Saves instance handle and creates main window
130 // In this function, we save the instance handle in a global variable and
131 // create and display the main program window.
133 BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
137 hInst
= hInstance
; // Store instance handle in our global variable
139 hWnd
= CreateWindow(szWindowClass
, szTitle
, WS_OVERLAPPEDWINDOW
,
140 CW_USEDEFAULT
, 0, CW_USEDEFAULT
, 0, NULL
, NULL
, hInstance
, NULL
);
149 ShowWindow(hWnd
, nCmdShow
);
155 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
161 TCHAR WndText
[MAX_PATH
];
163 NONCLIENTMETRICS ncm
;
170 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
);
171 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
);
173 memset(&ncm
, 0, sizeof(NONCLIENTMETRICS
));
174 ncm
.cbSize
= sizeof(NONCLIENTMETRICS
);
175 SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, sizeof(NONCLIENTMETRICS
), &ncm
, 0);
177 hFont
= CreateFontIndirect(&ncm
.lfMessageFont
);
179 SendMessage(hEditWnd
, WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
180 SendMessage(hDisplayWnd
, WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
184 wmId
= LOWORD(wParam
);
185 wmEvent
= HIWORD(wParam
);
187 if (lParam
== (LPARAM
)hEditWnd
&& wmEvent
== EN_CHANGE
)
189 GetWindowText(hEditWnd
, WndText
, MAX_PATH
);
191 if (_tcslen(WndText
) > 0)
193 SetWindowText(hEditWnd
, TEXT(""));
197 MessageBox(hWnd
, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
201 for (Index
=0; Index
<_tcslen(WndText
); Index
++)
205 PostThreadMessage(dwThreadId
, WM_CHAR
, (WPARAM
)WndText
[Index
], (LPARAM
)0);
211 // Parse the menu selections:
215 DialogBox(hInst
, (LPCTSTR
)IDD_ABOUTBOX
, hWnd
, About
);
220 case IDM_FILE_CLEARDISPLAY
:
221 SetWindowText(hDisplayWnd
, TEXT(""));
223 case IDM_FILE_CONNECT
:
226 MessageBox(hWnd
, TEXT("You are already connected!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
230 if (DialogBox(hInst
, (LPCTSTR
)IDD_CONNECTION
, hWnd
, ConnectionDialogProc
) == IDOK
)
233 EnableFileMenuItemByID(IDM_FILE_DISCONNECT
, TRUE
);
234 EnableFileMenuItemByID(IDM_FILE_CONNECT
, FALSE
);
235 _beginthread(Rs232Thread
, 0, NULL
);
239 case IDM_FILE_DISCONNECT
:
243 EnableFileMenuItemByID(IDM_FILE_DISCONNECT
, FALSE
);
244 EnableFileMenuItemByID(IDM_FILE_CONNECT
, TRUE
);
248 MessageBox(hWnd
, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
251 case IDM_FILE_STARTCAPTURE
:
252 if (DialogBox(hInst
, (LPCTSTR
)IDD_CAPTURE
, hWnd
, CaptureDialogProc
) == IDOK
)
255 EnableFileMenuItemByID(IDM_FILE_STOPCAPTURE
, TRUE
);
256 EnableFileMenuItemByID(IDM_FILE_STARTCAPTURE
, FALSE
);
257 hCaptureFile
= CreateFile(strCaptureFileName
, FILE_APPEND_DATA
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
260 case IDM_FILE_STOPCAPTURE
:
264 EnableFileMenuItemByID(IDM_FILE_STOPCAPTURE
, FALSE
);
265 EnableFileMenuItemByID(IDM_FILE_STARTCAPTURE
, TRUE
);
266 CloseHandle(hCaptureFile
);
270 case IDM_FILE_LOCALECHO
:
274 CheckLocalEchoMenuItem(bLocalEcho
);
279 CheckLocalEchoMenuItem(bLocalEcho
);
283 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
287 hdc
= BeginPaint(hWnd
, &ps
);
293 GetClientRect(hWnd
, &rc
);
295 MoveWindow(hDisplayWnd
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
- 20, TRUE
);
296 MoveWindow(hEditWnd
, rc
.left
, rc
.bottom
- 20, rc
.right
- rc
.left
, 20, TRUE
);
303 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
308 INT_PTR CALLBACK
About(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
310 HWND hLicenseEditWnd
;
311 TCHAR strLicense
[0x1000];
313 UNREFERENCED_PARAMETER(lParam
);
319 hLicenseEditWnd
= GetDlgItem(hDlg
, IDC_LICENSE_EDIT
);
321 LoadString(hInst
, IDS_LICENSE
, strLicense
, 0x1000);
323 SetWindowText(hLicenseEditWnd
, strLicense
);
328 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
330 EndDialog(hDlg
, LOWORD(wParam
));
338 INT_PTR CALLBACK
ConnectionDialogProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
340 UNREFERENCED_PARAMETER(lParam
);
346 SetWindowText(GetDlgItem(hDlg
, IDC_COMPORT
), strComPort
);
347 SetWindowText(GetDlgItem(hDlg
, IDC_BAUTRATE
), strBaudRate
);
352 if (LOWORD(wParam
) == IDOK
)
354 GetWindowText(GetDlgItem(hDlg
, IDC_COMPORT
), strComPort
, MAX_PATH
);
355 GetWindowText(GetDlgItem(hDlg
, IDC_BAUTRATE
), strBaudRate
, MAX_PATH
);
358 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
360 EndDialog(hDlg
, LOWORD(wParam
));
368 INT_PTR CALLBACK
CaptureDialogProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
372 UNREFERENCED_PARAMETER(lParam
);
378 SetWindowText(GetDlgItem(hDlg
, IDC_CAPTUREFILENAME
), strCaptureFileName
);
383 if (LOWORD(wParam
) == IDC_BROWSE
)
385 memset(&ofn
, 0, sizeof(OPENFILENAME
));
386 ofn
.lStructSize
= sizeof(OPENFILENAME
);
387 ofn
.hwndOwner
= hDlg
;
388 ofn
.hInstance
= hInst
;
389 ofn
.lpstrFilter
= NULL
;
390 ofn
.lpstrCustomFilter
= NULL
;
391 ofn
.nMaxCustFilter
= 0;
392 ofn
.nFilterIndex
= 0;
393 ofn
.lpstrFile
= strCaptureFileName
;
394 ofn
.nMaxFile
= MAX_PATH
;
395 ofn
.lpstrFileTitle
= NULL
;
396 ofn
.nMaxFileTitle
= 0;
397 ofn
.lpstrInitialDir
= NULL
;
398 ofn
.lpstrTitle
= NULL
;
399 ofn
.Flags
= OFN_HIDEREADONLY
|OFN_NOREADONLYRETURN
;
401 if (GetOpenFileName(&ofn
))
403 SetWindowText(GetDlgItem(hDlg
, IDC_CAPTUREFILENAME
), strCaptureFileName
);
407 if (LOWORD(wParam
) == IDOK
)
409 GetWindowText(GetDlgItem(hDlg
, IDC_CAPTUREFILENAME
), strCaptureFileName
, MAX_PATH
);
412 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
414 EndDialog(hDlg
, LOWORD(wParam
));
422 VOID
EnableFileMenuItemByID(UINT Id
, BOOL Enable
)
427 hMenuBar
= GetMenu(hMainWnd
);
428 hFileMenu
= GetSubMenu(hMenuBar
, 0);
429 EnableMenuItem(hFileMenu
, Id
, MF_BYCOMMAND
|(Enable
? MF_ENABLED
: MF_GRAYED
));
432 VOID
CheckLocalEchoMenuItem(BOOL Checked
)
437 hMenuBar
= GetMenu(hMainWnd
);
438 hFileMenu
= GetSubMenu(hMenuBar
, 0);
439 CheckMenuItem(hFileMenu
, IDM_FILE_LOCALECHO
, MF_BYCOMMAND
|(Checked
? MF_CHECKED
: MF_UNCHECKED
));
442 VOID __cdecl
Rs232Thread(VOID
* Parameter
)
445 TCHAR String
[MAX_PATH
];
447 DWORD dwNumberOfBytesWritten
;
449 UNREFERENCED_PARAMETER(Parameter
);
451 dwThreadId
= GetCurrentThreadId();
453 if (!Rs232OpenPortWin32(strComPort
))
455 MessageBox(hMainWnd
, TEXT("Error opening port!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
460 _stprintf(String
, TEXT("%s,n,8,1"), strBaudRate
);
461 if (!Rs232ConfigurePortWin32(String
))
463 MessageBox(hMainWnd
, TEXT("Error configuring port!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
470 if (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
472 if (msg
.message
== WM_CHAR
)
474 Rs232WriteByteWin32((BYTE
)msg
.wParam
);
476 if (bLocalEcho
&& msg
.wParam
!= (WPARAM
)TEXT('\r'))
478 PostMessage(hDisplayWnd
, WM_CHAR
, (WPARAM
)msg
.wParam
, (LPARAM
)0);
482 WriteFile(hCaptureFile
, &msg
.wParam
, sizeof(TCHAR
), &dwNumberOfBytesWritten
, NULL
);
488 if (Rs232ReadByteWin32(&Byte
))
490 _stprintf(String
, TEXT("%c"), Byte
);
492 PostMessage(hDisplayWnd
, WM_CHAR
, (WPARAM
)String
[0], (LPARAM
)0);
496 WriteFile(hCaptureFile
, &String
[0], sizeof(TCHAR
), &dwNumberOfBytesWritten
, NULL
);
502 Rs232ClosePortWin32();