📄 uart_test.cpp
字号:
//======================================================================
// CeChat - A Windows CE communication demo//
// Written for the book Programming Windows CE// Copyright (C) 2001 Douglas Boling
//======================================================================
#include <windows.h>
// For all that Windows stuff
#include <commctrl.h> // Command bar includes
#include "UART_TEST.h" // Program-specific stuff
#if defined(WIN32_PLATFORM_PSPC)
#include <aygshell.h> // Add Pocket PC includes.
#pragma comment( lib, "aygshell" ) // Link Pocket PC lib for menu bar.
#endif
//----------------------------------------------------------------------
// Global data//
const TCHAR szAppName[] = TEXT ("UART_TEST");
HINSTANCE hInst;
DWORD dwThreadID=0;
// Program instance handle.
BOOL fContinue = TRUE;
HANDLE hComPort = INVALID_HANDLE_VALUE;
INT nSpeed = CBR_9600;
int nLastDev = -1;
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
SHACTIVATEINFO sai;
#endif
HANDLE g_hSendEvent = INVALID_HANDLE_VALUE;
HANDLE hReadThread = INVALID_HANDLE_VALUE;
// Message dispatch table for MainWindowProcconst
struct decodeUINT MainMessages[] =
{
WM_CREATE, DoCreateMain,
WM_SIZE, DoSizeMain,
WM_COMMAND, DoCommandMain,
WM_SETTINGCHANGE,
DoPocketPCShell,
WM_ACTIVATE,
DoPocketPCShell,
WM_SETFOCUS,
DoSetFocusMain,
WM_DESTROY,
DoDestroyMain,
};
// Command Message dispatch for MainWindowProcconst
struct decodeCMD MainCommandItems[] =
{
IDC_COMPORT, DoMainCommandComPort,
ID_SENDBTN, DoMainCommandSendText,
IDM_EXIT, DoMainCommandExit,
IDM_ABOUT, DoMainCommandAbout,
};
//======================================================================
// Program entry point//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow)
{
HWND hwndMain;
HACCEL hAccel;
MSG msg;
int rc = 0; // Initialize application.
rc = InitApp (hInstance);
if (rc) return rc;
// Initialize this instance.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0)
return 0x10;
// Load accelerator table.
hAccel = LoadAccelerators (hInst, MAKEINTRESOURCE (ID_ACCEL));
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
if (!TranslateAccelerator (hwndMain, hAccel, &msg)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}//----------------------------------------------------------------------
// InitApp - Application initialization//
int InitApp (HINSTANCE hInstance)
{
WNDCLASS wc;
INITCOMMONCONTROLSEX icex;
#if defined(WIN32_PLATFORM_PSPC)
// If Pocket PC, allow only one instance of the application.
HWND hWnd = FindWindow (szAppName, NULL);
if (hWnd)
{
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return -1;
}
#endif
// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL; // Application icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 1; // Load the command bar common control class.
icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
icex.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx (&icex);
return 0;
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
DWORD rc=0;
HANDLE hThread;
// Save program instance handle in global variable.
hInst = hInstance; // Create unnamed auto-reset event initially false.
g_hSendEvent = CreateEvent (NULL, FALSE, FALSE, NULL); // Create main window.
hWnd = CreateWindow (szAppName, TEXT ("UART_TEST"),
WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL,
NULL, hInstance, NULL);
// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;
// Create write thread. Read thread created when port opened.
hThread = CreateThread (NULL, 0, SendThread, hWnd, 0, &dwThreadID);
// hThread1=CreateThread(NULL,0,Thread2,0,0,&dwThreadID);
if (hThread)
CloseHandle(hThread);
else {
DestroyWindow (hWnd);
return 0;
}
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup//
int TermInstance (HINSTANCE hInstance, int nDefRC)
{
HANDLE hPort = hComPort;
fContinue = FALSE;
hComPort = INVALID_HANDLE_VALUE;
if (hPort != INVALID_HANDLE_VALUE)
CloseHandle (hPort);
if (g_hSendEvent != INVALID_HANDLE_VALUE)
{
PulseEvent (g_hSendEvent);
Sleep(100);
CloseHandle (g_hSendEvent);
}
return nDefRC;
}
//======================================================================
// Message handling procedures for MainWindow
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam)
{
INT i; //
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++)
{
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam)
{
HWND hwndCB, hC1, hC2, hC3;
INT i, j;
TCHAR szFirstDev[32];
LPCREATESTRUCT lpcs = (LPCREATESTRUCT) lParam;
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
memset (&sai, 0, sizeof (sai));
sai.cbSize = sizeof (sai);
{
SHMENUBARINFO mbi; // For Pocket PC, create
memset(&mbi, 0, sizeof(SHMENUBARINFO)); // menu bar so that we
mbi.cbSize = sizeof(SHMENUBARINFO); // have a sip button.
mbi.hwndParent = hWnd;
mbi.dwFlags = SHCMBF_EMPTYBAR;
SHCreateMenuBar(&mbi);
SetWindowPos (hWnd, 0, 0, 0, lpcs->cx, lpcs->cy-26,
SWP_NOZORDER | SWP_NOMOVE);
}
#endif
// Create a command bar.
hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
CommandBar_InsertMenubar (hwndCB, hInst, ID_MENU, 0);
// Insert the COM port combo box.
CommandBar_InsertComboBox (hwndCB, hInst, 140, CBS_DROPDOWNLIST,
IDC_COMPORT, 1);
FillComComboBox (hWnd);
// Add exit button to command bar.
CommandBar_AddAdornments (hwndCB, 0, 0);
// Create child windows. They will be positioned in WM_SIZE.
// Create receive text window.
hC1 = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("edit"),
TEXT (""), WS_VISIBLE | WS_CHILD |
WS_VSCROLL | ES_MULTILINE | ES_AUTOHSCROLL |
ES_READONLY, 0, 0, 10, 10, hWnd,
(HMENU)ID_RCVTEXT, hInst, NULL);
// Create send text window.
hC2 = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("edit"),
TEXT (""), WS_VISIBLE | WS_CHILD,
0, 0, 10, 10, hWnd, (HMENU)ID_SENDTEXT,
hInst, NULL);
// Create send text window.
hC3 = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("button"),
TEXT ("&Send"), WS_VISIBLE | WS_CHILD |
BS_DEFPUSHBUTTON, 0, 0, 10, 10,
hWnd, (HMENU)ID_SENDBTN, hInst, NULL);
// Destroy frame if window not created.
if (!IsWindow (hC1) || !IsWindow (hC2) || !IsWindow (hC3))
{
DestroyWindow (hWnd);
return 0;
}
// Open a COM port.
for (i = 0; i < 6; i++)
{
SendDlgItemMessage (hwndCB, IDC_COMPORT, CB_GETLBTEXT, i,
(LPARAM)szFirstDev);
j = lstrlen (szFirstDev);
// Really bad hack to determine which is the RAW IR port
if (InitCommunication (hWnd, szFirstDev) !=
INVALID_HANDLE_VALUE)
{
SendDlgItemMessage (hwndCB, IDC_COMPORT, CB_SETCURSEL, i,
(LPARAM)szFirstDev);
break;
}
}
return 0;
}
//----------------------------------------------------------------------
// DoSizeMain - Process WM_SIZE message for window.//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
RECT rect; // Adjust the size of the client rect to take into account
// the command bar height.
GetClientRect (hWnd, &rect);
rect.top += CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR));
SetWindowPos (GetDlgItem (hWnd, ID_RCVTEXT), NULL, rect.left,
rect.top, (rect.right - rect.left),
rect.bottom - rect.top - 25, SWP_NOZORDER);
SetWindowPos (GetDlgItem (hWnd, ID_SENDTEXT), NULL, rect.left,
rect.bottom - 25, (rect.right - rect.left) - 50,
25, SWP_NOZORDER);
SetWindowPos (GetDlgItem (hWnd, ID_SENDBTN), NULL,
(rect.right - rect.left) - 50, rect.bottom - 25,
50, 25, SWP_NOZORDER); return 0;
}
//----------------------------------------------------------------------
// DoPocketPCShell - Process Pocket PC required messages.//
LRESULT DoPocketPCShell (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam)
{
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
if (wMsg == WM_SETTINGCHANGE)
return SHHandleWMSettingChange(hWnd, wParam, lParam, &sai);
if (wMsg == WM_ACTIVATE)
return SHHandleWMActivate(hWnd, wParam, lParam, &sai, 0);
#endif
return 0;
}
//----------------------------------------------------------------------
// DoFocusMain - Process WM_SETFOCUS message for window.//
LRESULT DoSetFocusMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam)
{
SetFocus (GetDlgItem (hWnd, ID_SENDTEXT));
return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam)
{
WORD idItem, wNotifyCode;
HWND hwndCtl;
INT i;
// Parse the parameters.
idItem = (WORD) LOWORD (wParam);
wNotifyCode = (WORD) HIWORD (wParam);
hwndCtl = (HWND) lParam;
// Call routine to handle control message.
for (i = 0; i < dim(MainCommandItems); i++) {
if (idItem == MainCommandItems[i].Code)
return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
wNotifyCode);
}
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam)
{
PostQuitMessage (0);
return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode)
{
SendMessage (hWnd, WM_CLOSE, 0, 0);
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandComPort - Process the COM port combo box commands.//
LPARAM DoMainCommandComPort (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode)
{
INT i;
TCHAR szDev[32];
if (wNotifyCode == CBN_SELCHANGE) {
i = SendMessage (hwndCtl, CB_GETCURSEL, 0, 0);
if (i != nLastDev) {
nLastDev = i;
SendMessage (hwndCtl, CB_GETLBTEXT, i, (LPARAM)szDev);
InitCommunication (hWnd, szDev);
SetFocus (GetDlgItem (hWnd, ID_SENDTEXT));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -