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

📄 irdaport.cpp

📁 这是专门针对PDA设备做的一个电视遥控器程序
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////
//
// IRDA Port class, a class for IRDA support.
//
// Copyright (c) 2003, Strigl Daniel.
//
// License:
// --------
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this source code; if not, write to the Free 
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
// MA 02111-1307 USA.
//
// Author: Strigl Daniel, daniel.strigl@web.de,
//         http://www.hh-system.com/danielstrigl
//
//////////////////////////////////////////////////////////////////////
//
// IrdaPort.cpp: implementation of the CIrdaPort class
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "IrdaPort.h"

#include <atlconv.h>


//////////////////////////////////////////////////////////////////////
// CIrdaPort construction/destruction
//////////////////////////////////////////////////////////////////////

CIrdaPort::CIrdaPort()
{
    m_hPort = INVALID_HANDLE_VALUE;
}

CIrdaPort::~CIrdaPort()
{
    Close();
}

IMPLEMENT_DYNAMIC(CIrdaPort, CObject);

//////////////////////////////////////////////////////////////////////
// CIrdaPort diagnostics
//////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
void CIrdaPort::AssertValid() const
{
	CObject::AssertValid();
}

void CIrdaPort::Dump(CDumpContext& dc) const
{
    CObject::Dump(dc);

    dc << "m_hPort = " << m_hPort;
}
#endif // _DEBUG

//////////////////////////////////////////////////////////////////////
// CIrdaPort functions
//////////////////////////////////////////////////////////////////////

UINT CIrdaPort::FindPortIndex()
{
    // Look into the registry for the IRDA port number

    HKEY hKey = NULL;

    //if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Drivers\\BuiltIn\\IrCOMM"), 0, 0, &hKey) == ERROR_SUCCESS)
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Comm\\IrDA"), 0, 0, &hKey) == ERROR_SUCCESS)
	{
        DWORD dwType = 0;
        DWORD dwData = 0;
        DWORD dwSize = sizeof(dwData);

        if (RegQueryValueEx(hKey, _T("Port"), NULL, &dwType, (LPBYTE) &dwData, &dwSize) == ERROR_SUCCESS)
        {
            if (dwType == REG_DWORD && dwSize == sizeof(dwData))
            {
                RegCloseKey(hKey);
                                
                return (UINT) dwData;
            }
        }

        RegCloseKey(hKey);
    }

    return 0;
}

BOOL CIrdaPort::Open(UINT uiPort)
{
    ASSERT(uiPort > 0 && uiPort <= 255);

    Close(); 

    // Open the IRDA port

    CString strPort;
    strPort.Format(_T("COM%d:"), uiPort);
    
    m_hPort = CreateFile((LPCTSTR) strPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

	if (m_hPort == INVALID_HANDLE_VALUE)
    {
        return FALSE;
    }

    // Set the size of the input and output buffer

    VERIFY(SetupComm(m_hPort, 2048, 2048));

    // Terminates all outstanding read and write operations and clear the buffers

    VERIFY(PurgeComm(m_hPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR));

    // Reinitializes all IRDA port settings

    DCB dcb;

    dcb.DCBlength = sizeof(DCB);

    VERIFY(GetCommState(m_hPort, &dcb));

    dcb.BaudRate          = CBR_115200;
    dcb.fBinary           = TRUE;
    dcb.fParity           = TRUE;
    dcb.fOutxCtsFlow      = FALSE;
    dcb.fOutxDsrFlow      = FALSE;
    dcb.fDtrControl       = DTR_CONTROL_DISABLE;
    dcb.fDsrSensitivity   = FALSE;
    dcb.fTXContinueOnXoff = FALSE;
    dcb.fOutX             = FALSE;
    dcb.fInX              = FALSE;
    dcb.fErrorChar        = FALSE;  
    dcb.fNull             = FALSE;
    dcb.fRtsControl       = RTS_CONTROL_DISABLE;
    dcb.fAbortOnError     = FALSE;
 // dcb.fDummy2
 // dcb.wReserved
 // dcb.XonLim 
 // dcb.XoffLim 
    dcb.ByteSize          = 8; 
    dcb.Parity            = EVENPARITY; 
    dcb.StopBits          = TWOSTOPBITS;
 // dcb.XonChar 
 // dcb.XoffChar 
 // dcb.ErrorChar
 // dcb.EofChar 
 // dcb.EvtChar 
 // dcb.wReserved1
    
    VERIFY(SetCommState(m_hPort, &dcb));

    // Set the timeouts for all read and write operations

    COMMTIMEOUTS timeouts;

    VERIFY(GetCommTimeouts(m_hPort, &timeouts));

    timeouts.ReadIntervalTimeout         = MAXDWORD;
    timeouts.ReadTotalTimeoutMultiplier  = 0;
    timeouts.ReadTotalTimeoutConstant    = 0;
    timeouts.WriteTotalTimeoutMultiplier = 0;
    timeouts.WriteTotalTimeoutConstant   = 0;

    VERIFY(SetCommTimeouts(m_hPort, &timeouts));

    // Set the serial port to infrared (IR) mode

    //EscapeCommFunction(m_hPort, SETIR);

    return TRUE;
}

void CIrdaPort::Close()
{
    // Close the IRDA port
    
    if (IsOpen())
    {
        VERIFY(PurgeComm(m_hPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR));

        EscapeCommFunction(m_hPort, CLRIR);

        VERIFY(CloseHandle(m_hPort));

        m_hPort = INVALID_HANDLE_VALUE;
    }
}

BOOL CIrdaPort::IsOpen() const
{ 
    return m_hPort != INVALID_HANDLE_VALUE; 
}

DWORD CIrdaPort::Read(void* pData, DWORD dwCount) const
{
    ASSERT(IsOpen());

    DWORD dwBytesRead = 0;

    ReadFile(m_hPort, pData, dwCount, &dwBytesRead, NULL);

    return dwBytesRead;
}

DWORD CIrdaPort::Write(const void* pData, DWORD dwCount) const
{
    ASSERT(IsOpen());

    DWORD dwBytesWritten = 0;

    WriteFile(m_hPort, pData, dwCount, &dwBytesWritten, NULL);

    return dwBytesWritten;
}

BOOL CIrdaPort::WaitForResponse(CString& strResponse, DWORD dwTimeout) const
{
    ASSERT(IsOpen());

    BOOL bData = FALSE;

    char cCurrentChar  = '\0';
    char cLastChar     = '\0';
    char cLastLastChar = '\0';

    strResponse.Empty();

    DWORD dwStartTicks = GetTickCount();

    while (TRUE)
    {
        // Has the timeout occured?

		if ((GetTickCount() - dwStartTicks) >= dwTimeout)
        {
 		    return FALSE;
        }

        // Receive the data from the serial port

        if (Read(&cCurrentChar, sizeof(cCurrentChar)) > 0)
        {
            // Reset the idle timeout since data was received
		  
            dwStartTicks = GetTickCount();

			if (isprint(cCurrentChar) && cLastLastChar == 0x0D && cLastChar == 0x0A && !bData)
			{
				bData = TRUE;  // Received start sequence (<CR><LF>)
			}
			else if (isprint(cLastLastChar) && cLastChar == 0x0D && cCurrentChar == 0x0A && bData)
			{
				break;  // Received end sequence (<CR><LF>)
			}

            // Add the character to the string

            if (bData && isprint(cCurrentChar)) 
            {
                char szANSI[2] = { cCurrentChar, '\0' };

                strResponse += CString(szANSI);
            }

            cLastLastChar = cLastChar;
			cLastChar     = cCurrentChar;
        }
        else
        {
            Sleep(10);
        }

    } // while (TRUE)

    return TRUE;
}

/*BOOL CIrdaPort::isprint(char ch) const
{

	return true;
} */

BOOL CIrdaPort::Send(const CString& strSend) const
{
    ASSERT(IsOpen());
    
    USES_CONVERSION; // Need for T2CA() macro

	VERIFY(PurgeComm(m_hPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR));

    CString str(strSend);
    
    //str += _T("\r"); // Add <CR> at the end of the string

    if (Write(T2CA((LPCTSTR) str), str.GetLength()) < (DWORD) str.GetLength())
    {
        return FALSE;
    }

    return TRUE;
}


⌨️ 快捷键说明

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