⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ir chat.cpp

📁 windows mobile下运用红外线端口进行聊天的小程序。windows ce programme书中给的代码是用在PPC2002上的
💻 CPP
字号:
#include <windows.h>
#include <aygshell.h>
#include "resource.h"
#include "Ir Chat.h"
#include <tpcshell.h>
#pragma comment(lib,"aygshell")

const TCHAR szAppName[]=TEXT("IrComm");
const char chzAppName[] = "IrComm";
HWND hMain;
HINSTANCE	hInst;

INT nSpeed = CBR_9600;
HANDLE hComPort = INVALID_HANDLE_VALUE;
BOOL fContinue = TRUE;
HANDLE hReadThread = INVALID_HANDLE_VALUE;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
                   LPWSTR lpCmdLine,int nCmdShow){
//	MSG msg;
	HWND hWnd=0;
	hInst = hInstance;

	hWnd = FindWindow(NULL,szAppName);
	if(hWnd)
	{
		SetForegroundWindow((HWND)(((DWORD)hWnd)|0x01));    
		return 0;
    }

	DialogBoxParam(hInst,TEXT("DLG_MAIN"),NULL,Main_DlgProc,LPARAM(lpCmdLine));

	return 0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HWND InitDialog(HWND hWnd){
//	HANDLE hThread;
	SHINITDLGINFO shidi;
	memset(&shidi, 0, sizeof(shidi));
	shidi.dwMask=SHIDIM_FLAGS;
	shidi.dwFlags=SHIDIF_SIZEDLGFULLSCREEN;
	shidi.hDlg=hWnd;
	if(!SHInitDialog(&shidi))
        return FALSE;

	 return hWnd;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
BOOL ShowMenubar(HWND hwnd,int menuid){
	 SHMENUBARINFO mbi;
     memset(&mbi, 0, sizeof(SHMENUBARINFO));
     mbi.cbSize=sizeof(SHMENUBARINFO);
     mbi.hwndParent=hwnd;
     mbi.nToolBarId=menuid;
     mbi.hInstRes=hInst;
     if (!SHCreateMenuBar(&mbi))
		 return FALSE;
     return TRUE;
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
BOOL CALLBACK Main_DlgProc(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp){

	switch(msg){
		case WM_INITDIALOG:
			InitDialog(hWnd);
			ShowMenubar(hWnd,MB_MAIN);
			SetFocus(GetDlgItem(hWnd,IDLB_IrComm));
			return TRUE;
		case WM_HOTKEY:
			SHSendBackToFocusWindow(msg,wp,lp);
			return TRUE;
		case WM_COMMAND:
			switch(LOWORD(wp)){
				case IDBegin:
					FillIrCommPort(hWnd);
					InitCommunication (hWnd, TEXT("COM3:"));
					return TRUE;
				case IDStop:
					StopRecv();
					return TRUE;
				case IDM_OK:
					PostQuitMessage(0);
					return TRUE;
			}
			return TRUE;
	}
	return FALSE;
}

// Add2List - Add string to the report list box.
void Add2List (HWND hWnd, LPTSTR lpszFormat, ...) {
    int i, nBuf;
    TCHAR szBuffer[512];
   
    va_list args;
    va_start(args, lpszFormat);
   
    nBuf = _vstprintf(szBuffer, lpszFormat, args);
   
    i = SendDlgItemMessage (hWnd, IDLB_IrComm, LB_ADDSTRING, 0,
                            (LPARAM)(LPCTSTR)szBuffer);
    if (i != LB_ERR)
        SendDlgItemMessage (hWnd, IDLB_IrComm, LB_SETTOPINDEX, i,
                            (LPARAM)(LPCTSTR)szBuffer);
    va_end(args);
}

int FillIrCommPort(HWND hWnd) {
    TCHAR szDev[64];

    lstrcpy (szDev, TEXT ("IrComm Port   "));
    GetIrCommDeviceName (&szDev[lstrlen (szDev)]);
    Add2List(hWnd, szDev);

    return 0;
}

INT GetIrCommDeviceName (LPTSTR pDevName) {
    DWORD dwSize, dwType, dwData;
    HKEY hKey;

    *pDevName = TEXT ('\0');
    // Open the IrDA key.
    if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
                      TEXT ("Drivers\\BuiltIn\\IrCOMM"), 0,
                      0, &hKey) == ERROR_SUCCESS) {

        // Query the device number.
        dwSize = sizeof (dwData);
        if (RegQueryValueEx (hKey, TEXT ("Index"), 0, &dwType,
                             (PBYTE)&dwData, &dwSize) == ERROR_SUCCESS)

            // Check for valid port number. Assume buffer > 5 chars.
            if (dwData < 10)
                wsprintf (pDevName, TEXT ("COM%d:"), dwData);

        RegCloseKey (hKey);
    }
    return lstrlen (pDevName);
}

// InitCommunication - Open and initialize selected COM port.
HANDLE InitCommunication (HWND hWnd, LPTSTR pszDevName) {
    DCB dcb;
    INT i;
    TCHAR szDbg[128];
    COMMTIMEOUTS cto;
    HANDLE hLocal;
    DWORD dwTStat;

    hLocal = hComPort;
    hComPort = INVALID_HANDLE_VALUE;

    if (hLocal != INVALID_HANDLE_VALUE)
        CloseHandle (hLocal);  // This causes WaitCommEvent to return.

    // The com port name is the last 5 characters of the string.
    i = lstrlen (pszDevName);
    hLocal = CreateFile (&pszDevName[i-5], GENERIC_READ | GENERIC_WRITE,
                           0, NULL, OPEN_EXISTING, 0, NULL);

    if (hLocal != INVALID_HANDLE_VALUE) {
        // Configure port.
        GetCommState (hLocal, &dcb);
        dcb.BaudRate = nSpeed;
        dcb.fParity = FALSE;
        dcb.fNull = FALSE;
        dcb.StopBits = ONESTOPBIT;//每字节一位停止位
        dcb.Parity = NOPARITY;//奇偶校验
        dcb.ByteSize = 8;//每字节位数
        SetCommState (hLocal, &dcb);

        // Set the timeouts.  Set infinite read timeout.
        cto.ReadIntervalTimeout = 0;
        cto.ReadTotalTimeoutMultiplier = 0;
        cto.ReadTotalTimeoutConstant = 0;
        cto.WriteTotalTimeoutMultiplier = 0;
        cto.WriteTotalTimeoutConstant = 0;
        SetCommTimeouts (hLocal, &cto);

        wsprintf (szDbg, TEXT ("Port %s opened\r\n"), pszDevName);
		Add2List(hWnd, szDbg);
//        SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0, (LPARAM)szDbg);


        // Really bad hack to determine which is the raw IR selection.
        // We need to enable IR on the raw IR port in case port is
        // shared with the standard serial port.
        if (*pszDevName == TEXT ('R')) {
            if (!EscapeCommFunction (hLocal, SETIR)) {
                wsprintf (szDbg, TEXT ("Set IR failed. rc %d\r\n"), GetLastError());
//				SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0, (LPARAM)szDbg);
				Add2List(hWnd, szDbg);
            }
        }
        // Start read thread if not already started.
        hComPort = hLocal;
        if (!GetExitCodeThread (hReadThread, &dwTStat) ||
            (dwTStat != STILL_ACTIVE)) {
            hReadThread = CreateThread (NULL, 0, ReadThread, hWnd, 0, &dwTStat);
            if (hReadThread)
                CloseHandle (hReadThread);
        }
    } else {
        wsprintf (szDbg, TEXT ("Couldn\'t open port %s. rc=%d\r\n"), pszDevName, GetLastError());
		Add2List(hWnd, szDbg);
//        SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0, (LPARAM)szDbg);
    }
    return hComPort;
}

DWORD WINAPI ReadThread (PVOID pArg) {
//int ReadThread (PVOID pArg) {
    HWND hWnd;
//    int cBytes, i;
	int i;
	DWORD cBytes;
    BYTE szText[256], *pPtr;
    TCHAR tch;

    hWnd = (HWND)pArg;
    while (fContinue) {
        tch = 0;
        pPtr = szText;
        for (i = 0; i < sizeof (szText)-sizeof (TCHAR); i++) {

            while (!ReadFile (hComPort, pPtr, 1, &cBytes, 0))
                if (hComPort == INVALID_HANDLE_VALUE)
                    return 0;

            // This syncs the proper byte order for Unicode.
            tch = (tch << 8) & 0xff00;
            tch |= *pPtr++;
            if (tch == TEXT ('\n'))
                break;
        }
        *pPtr++ = 0;  // Avoid alignment probs by addressing as bytes.
        *pPtr++ = 0;

        // If out of byte sync, move bytes down one.
        if (i % 2) {
            pPtr = szText;
            while (*pPtr || *(pPtr+1)) {
                *pPtr = *(pPtr+1);
                pPtr++;
            }
            *pPtr = 0;
        }
//        SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0, (LPARAM)szText);
		Add2List(hWnd, (unsigned short *)szText);
    }
    return 0;
}

void StopRecv() {
    HANDLE hPort = hComPort;

    fContinue = FALSE;

    hComPort = INVALID_HANDLE_VALUE;
    if (hPort != INVALID_HANDLE_VALUE)
        CloseHandle (hPort);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -