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
EnableConnectMenuItem(BOOL Enable
);
39 VOID
EnableDisconnectMenuItem(BOOL Enable
);
40 VOID
EnableStartCaptureMenuItem(BOOL Enable
);
41 VOID
EnableStopCaptureMenuItem(BOOL Enable
);
42 VOID
CheckLocalEchoMenuItem(BOOL Checked
);
43 VOID
Rs232Thread(VOID
* Parameter
);
45 int APIENTRY
WinMain(HINSTANCE hInstance
,
46 HINSTANCE hPrevInstance
,
50 // TODO: Place code here.
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
);
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
)WndProc
;
105 wcex
.hInstance
= hInstance
;
106 wcex
.hIcon
= LoadIcon(hInstance
, (LPCTSTR
)IDI_FDEBUG
);
107 wcex
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
108 wcex
.hbrBackground
= NULL
;//(HBRUSH)(COLOR_WINDOW+1);
109 wcex
.lpszMenuName
= (LPCSTR
)IDC_FDEBUG
;
110 wcex
.lpszClassName
= szWindowClass
;
111 wcex
.hIconSm
= LoadIcon(wcex
.hInstance
, (LPCTSTR
)IDI_SMALL
);
113 return RegisterClassEx(&wcex
);
117 // FUNCTION: InitInstance(HANDLE, int)
119 // PURPOSE: Saves instance handle and creates main window
123 // In this function, we save the instance handle in a global variable and
124 // create and display the main program window.
126 BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
130 hInst
= hInstance
; // Store instance handle in our global variable
132 hWnd
= CreateWindow(szWindowClass
, szTitle
, WS_OVERLAPPEDWINDOW
,
133 CW_USEDEFAULT
, 0, CW_USEDEFAULT
, 0, NULL
, NULL
, hInstance
, NULL
);
142 ShowWindow(hWnd
, nCmdShow
);
148 LRESULT CALLBACK
WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
154 TCHAR WndText
[MAX_PATH
];
156 NONCLIENTMETRICS ncm
;
163 hEditWnd
= CreateWindowEx(WS_EX_CLIENTEDGE
, TEXT("EDIT"), "", WS_CHILD
|WS_VISIBLE
|WS_VSCROLL
|ES_AUTOHSCROLL
|ES_LEFT
|ES_MULTILINE
, 0, 0, 0, 0, hWnd
, NULL
, hInst
, NULL
);
164 hDisplayWnd
= CreateWindowEx(WS_EX_CLIENTEDGE
, TEXT("EDIT"), "", WS_CHILD
|WS_VISIBLE
|WS_HSCROLL
|WS_VSCROLL
|ES_MULTILINE
, 0, 0, 0, 0, hWnd
, NULL
, hInst
, NULL
);
166 memset(&ncm
, 0, sizeof(NONCLIENTMETRICS
));
167 ncm
.cbSize
= sizeof(NONCLIENTMETRICS
);
168 SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, sizeof(NONCLIENTMETRICS
), &ncm
, 0);
170 hFont
= CreateFontIndirect(&ncm
.lfMessageFont
);
172 SendMessage(hEditWnd
, WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
173 SendMessage(hDisplayWnd
, WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
177 wmId
= LOWORD(wParam
);
178 wmEvent
= HIWORD(wParam
);
180 if (lParam
== (LPARAM
)hEditWnd
&& wmEvent
== EN_CHANGE
)
182 GetWindowText(hEditWnd
, WndText
, MAX_PATH
);
184 if (_tcslen(WndText
) > 0)
186 SetWindowText(hEditWnd
, TEXT(""));
190 MessageBox(hWnd
, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
194 for (Index
=0; Index
<_tcslen(WndText
); Index
++)
198 PostThreadMessage(dwThreadId
, WM_CHAR
, (WPARAM
)WndText
[Index
], (LPARAM
)0);
204 // Parse the menu selections:
208 DialogBox(hInst
, (LPCTSTR
)IDD_ABOUTBOX
, hWnd
, (DLGPROC
)About
);
213 case ID_FILE_CONNECT
:
216 MessageBox(hWnd
, TEXT("You are already connected!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
220 if (DialogBox(hInst
, (LPCTSTR
)IDD_CONNECTION
, hWnd
, (DLGPROC
)ConnectionDialogProc
) == IDOK
)
223 EnableDisconnectMenuItem(TRUE
);
224 EnableConnectMenuItem(FALSE
);
225 _beginthread(Rs232Thread
, 0, NULL
);
229 case ID_FILE_DISCONNECT
:
233 EnableDisconnectMenuItem(FALSE
);
234 EnableConnectMenuItem(TRUE
);
238 MessageBox(hWnd
, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
241 case ID_FILE_STARTCAPTURE
:
242 if (DialogBox(hInst
, (LPCTSTR
)IDD_CAPTURE
, hWnd
, (DLGPROC
)CaptureDialogProc
) == IDOK
)
245 EnableStopCaptureMenuItem(TRUE
);
246 EnableStartCaptureMenuItem(FALSE
);
247 hCaptureFile
= CreateFile(strCaptureFileName
, FILE_APPEND_DATA
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
250 case ID_FILE_STOPCAPTURE
:
254 EnableStopCaptureMenuItem(FALSE
);
255 EnableStartCaptureMenuItem(TRUE
);
256 CloseHandle(hCaptureFile
);
260 case ID_FILE_LOCALECHO
:
264 CheckLocalEchoMenuItem(bLocalEcho
);
269 CheckLocalEchoMenuItem(bLocalEcho
);
273 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
277 hdc
= BeginPaint(hWnd
, &ps
);
282 GetClientRect(hWnd
, &rc
);
284 MoveWindow(hDisplayWnd
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
- 20, TRUE
);
285 MoveWindow(hEditWnd
, rc
.left
, rc
.bottom
- 20, rc
.right
- rc
.left
, 20, TRUE
);
292 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
297 LRESULT CALLBACK
About(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
299 HWND hLicenseEditWnd
;
300 TCHAR strLicense
[0x1000];
306 hLicenseEditWnd
= GetDlgItem(hDlg
, IDC_LICENSE_EDIT
);
308 LoadString(hInst
, IDS_LICENSE
, strLicense
, 0x1000);
310 SetWindowText(hLicenseEditWnd
, strLicense
);
315 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
317 EndDialog(hDlg
, LOWORD(wParam
));
325 LRESULT CALLBACK
ConnectionDialogProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
331 SetWindowText(GetDlgItem(hDlg
, IDC_COMPORT
), strComPort
);
332 SetWindowText(GetDlgItem(hDlg
, IDC_BAUTRATE
), strBaudRate
);
337 if (LOWORD(wParam
) == IDOK
)
339 GetWindowText(GetDlgItem(hDlg
, IDC_COMPORT
), strComPort
, MAX_PATH
);
340 GetWindowText(GetDlgItem(hDlg
, IDC_BAUTRATE
), strBaudRate
, MAX_PATH
);
343 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
345 EndDialog(hDlg
, LOWORD(wParam
));
353 LRESULT CALLBACK
CaptureDialogProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
361 SetWindowText(GetDlgItem(hDlg
, IDC_CAPTUREFILENAME
), strCaptureFileName
);
366 if (LOWORD(wParam
) == IDC_BROWSE
)
368 memset(&ofn
, 0, sizeof(OPENFILENAME
));
369 ofn
.lStructSize
= sizeof(OPENFILENAME
);
370 ofn
.hwndOwner
= hDlg
;
371 ofn
.hInstance
= hInst
;
372 ofn
.lpstrFilter
= NULL
;
373 ofn
.lpstrCustomFilter
= NULL
;
374 ofn
.nMaxCustFilter
= 0;
375 ofn
.nFilterIndex
= 0;
376 ofn
.lpstrFile
= strCaptureFileName
;
377 ofn
.nMaxFile
= MAX_PATH
;
378 ofn
.lpstrFileTitle
= NULL
;
379 ofn
.nMaxFileTitle
= 0;
380 ofn
.lpstrInitialDir
= NULL
;
381 ofn
.lpstrTitle
= NULL
;
382 ofn
.Flags
= OFN_HIDEREADONLY
|OFN_NOREADONLYRETURN
;
384 if (GetOpenFileName(&ofn
))
386 SetWindowText(GetDlgItem(hDlg
, IDC_CAPTUREFILENAME
), strCaptureFileName
);
390 if (LOWORD(wParam
) == IDOK
)
392 GetWindowText(GetDlgItem(hDlg
, IDC_CAPTUREFILENAME
), strCaptureFileName
, MAX_PATH
);
395 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
)
397 EndDialog(hDlg
, LOWORD(wParam
));
405 VOID
EnableConnectMenuItem(BOOL Enable
)
410 hMenuBar
= GetMenu(hMainWnd
);
411 hFileMenu
= GetSubMenu(hMenuBar
, 0);
415 EnableMenuItem(hFileMenu
, ID_FILE_CONNECT
, MF_BYCOMMAND
|MF_ENABLED
);
419 EnableMenuItem(hFileMenu
, ID_FILE_CONNECT
, MF_BYCOMMAND
|MF_GRAYED
);
423 VOID
EnableDisconnectMenuItem(BOOL Enable
)
428 hMenuBar
= GetMenu(hMainWnd
);
429 hFileMenu
= GetSubMenu(hMenuBar
, 0);
433 EnableMenuItem(hFileMenu
, ID_FILE_DISCONNECT
, MF_BYCOMMAND
|MF_ENABLED
);
437 EnableMenuItem(hFileMenu
, ID_FILE_DISCONNECT
, MF_BYCOMMAND
|MF_GRAYED
);
441 VOID
EnableStartCaptureMenuItem(BOOL Enable
)
446 hMenuBar
= GetMenu(hMainWnd
);
447 hFileMenu
= GetSubMenu(hMenuBar
, 0);
451 EnableMenuItem(hFileMenu
, ID_FILE_STARTCAPTURE
, MF_BYCOMMAND
|MF_ENABLED
);
455 EnableMenuItem(hFileMenu
, ID_FILE_STARTCAPTURE
, MF_BYCOMMAND
|MF_GRAYED
);
459 VOID
EnableStopCaptureMenuItem(BOOL Enable
)
464 hMenuBar
= GetMenu(hMainWnd
);
465 hFileMenu
= GetSubMenu(hMenuBar
, 0);
469 EnableMenuItem(hFileMenu
, ID_FILE_STOPCAPTURE
, MF_BYCOMMAND
|MF_ENABLED
);
473 EnableMenuItem(hFileMenu
, ID_FILE_STOPCAPTURE
, MF_BYCOMMAND
|MF_GRAYED
);
477 VOID
CheckLocalEchoMenuItem(BOOL Checked
)
482 hMenuBar
= GetMenu(hMainWnd
);
483 hFileMenu
= GetSubMenu(hMenuBar
, 0);
487 CheckMenuItem(hFileMenu
, ID_FILE_LOCALECHO
, MF_BYCOMMAND
|MF_CHECKED
);
491 CheckMenuItem(hFileMenu
, ID_FILE_LOCALECHO
, MF_BYCOMMAND
|MF_UNCHECKED
);
495 VOID
Rs232Thread(VOID
* Parameter
)
498 TCHAR String
[MAX_PATH
];
500 DWORD dwNumberOfBytesWritten
;
502 dwThreadId
= GetCurrentThreadId();
504 if (!Rs232OpenPortWin32(strComPort
))
506 MessageBox(hMainWnd
, TEXT("Error opening port!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
511 _stprintf(String
, TEXT("%s,n,8,1"), strBaudRate
);
512 if (!Rs232ConfigurePortWin32(String
))
514 MessageBox(hMainWnd
, TEXT("Error configuring port!"), TEXT("Error"), MB_OK
|MB_ICONSTOP
);
521 if (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
523 if (msg
.message
== WM_CHAR
)
525 Rs232WriteByteWin32((BYTE
)msg
.wParam
);
527 if (bLocalEcho
&& msg
.wParam
!= (WPARAM
)TEXT('\r'))
529 PostMessage(hDisplayWnd
, WM_CHAR
, (WPARAM
)msg
.wParam
, (LPARAM
)0);
533 WriteFile(hCaptureFile
, &msg
.wParam
, sizeof(TCHAR
), &dwNumberOfBytesWritten
, NULL
);
539 if (Rs232ReadByteWin32(&Byte
))
541 _stprintf(String
, TEXT("%c"), Byte
);
543 PostMessage(hDisplayWnd
, WM_CHAR
, (WPARAM
)String
[0], (LPARAM
)0);
547 WriteFile(hCaptureFile
, &String
[0], sizeof(TCHAR
), &dwNumberOfBytesWritten
, NULL
);
553 Rs232ClosePortWin32();