2010年2月19日 星期五

CuGPSDevice

class CuGPSDevice : public CuPort  
{
    CuFile m_LogFile;
    CuFile m_CheckLogFile;
    CuFile m_CheckErrorLogFile;
    CuFile m_Error_Message_File;


    DWORD m_dReadDataBufferSize;
    DWORD m_dReadDataBuffseIndex;
    BYTE *m_pReadDataBuffer;

    bool _writeFileCheck(CuFile &File, void *pData, int nSize);

public:
    BOOL CheckReadData(BYTE* pReadData);
    virtual void OnWrite(BYTE *pByte, int nSize);
    virtual void OnRead(BYTE *pByte, int nSize);
    CuGPSDevice();
    virtual ~CuGPSDevice();

};




//

CuGPSDevice::CuGPSDevice()
:m_dReadDataBufferSize(1024)
,m_dReadDataBuffseIndex(0)
,m_pReadDataBuffer(NULL)
{

    m_pReadDataBuffer = new BYTE[m_dReadDataBufferSize];

    CuModuleFile mf;
    wstring strLogFile = mf.GetModuleFileName(L"GPS_Log.txt");
    m_LogFile.Open(strLogFile, L"w");


    strLogFile = mf.GetModuleFileName(L"GPS_Check_Log.txt");
    m_CheckLogFile.Open(strLogFile, L"w");

    strLogFile = mf.GetModuleFileName(L"GPS_Check_Error_Log.txt");
    m_CheckErrorLogFile.Open(strLogFile, L"w");


    strLogFile = mf.GetModuleFileName(L"GPS_Error_Message_Log.txt");
    m_Error_Message_File.Open(strLogFile, L"w");
    


}

CuGPSDevice::~CuGPSDevice()
{

}
void CuGPSDevice::OnWrite(BYTE *pByte, int nSize)
{

    unsigned char checksum = 0 ;
    for(int i=0 ; i < nSize; i++)
    {
        checksum ^= pByte[i];
    }
    
    char strCheckSum[2];

    sprintf( strCheckSum, "%x", checksum);
    char pSendStr[2048];
    memset(pSendStr, 0, 2048);

    ::strcat(pSendStr, "$");
    ::strcat(pSendStr, (char*)pByte);

    ::strcat(pSendStr, "*");
    ::strcat(pSendStr, strCheckSum);
    ::strcat(pSendStr, "\x0d");
    ::strcat(pSendStr, "\x0a");
        
    CuPort::OnWrite((BYTE*)pSendStr, strlen(pSendStr));

}

void CuGPSDevice::OnRead(BYTE *pByte, int nSize)
{

    for( int i = 0 ; i < nSize ; i++ )
    {

    
        if( pByte[i] == 0x0a )
        {
            m_pReadDataBuffer[m_dReadDataBuffseIndex++] = pByte[i];
            if( CheckReadData( m_pReadDataBuffer) )
                _writeFileCheck(m_CheckLogFile, (void*)m_pReadDataBuffer , m_dReadDataBuffseIndex);
                m_CheckLogFile.Write(()m_pReadDataBuffer, m_dReadDataBuffseIndex);
            else
                _writeFileCheck(m_CheckErrorLogFile, (void*)m_pReadDataBuffer , m_dReadDataBuffseIndex);
                m_CheckErrorLogFile.Write((void*)m_pReadDataBuffer, m_dReadDataBuffseIndex);


            memset(m_pReadDataBuffer, 0, m_dReadDataBuffseIndex);
            m_dReadDataBuffseIndex = 0;
            continue;
        }

        m_pReadDataBuffer[m_dReadDataBuffseIndex++] = pByte[i];


    }

    {
        m_LogFile.Write((void*)pByte, nSize);
        _writeFileCheck(m_LogFile, (void*)pByte , nSize);
    }


}

BOOL CuGPSDevice::CheckReadData(BYTE* pReadData)
{
    int nSize;
    nSize = strlen((char*)pReadData);

    unsigned char checksumtemp;

    TCHAR checkstr[3];
    checkstr[0] = pReadData[ nSize - 4 ];
    checkstr[1] = pReadData[ nSize - 3 ];
    checkstr[2] = 0;

    swscanf(checkstr, L"%x", &checksumtemp);

    unsigned char checksum = 0 ;
    checksum = 0;
    for(int i=1 ; i < nSize - 5 ; i++)
    {
        checksum ^= pReadData[i];
    }

    return checksum == checksumtemp ? TRUE : FALSE;

}

bool CuGPSDevice::_writeFileCheck(CuFile &File, void *pData, int nSize)
{
    static int nWriteFileResult = 0;
    nWriteFileResult = File.Write(pData, nSize);
    
    bool bResult = (nWriteFileResult == nSize);

    if( !bResult )
    {
        MessageBox(NULL, L"WriteFileError", L"WriteFileError", MB_OK);
        m_Error_Message_File.Write(pData, nSize);
    }
    return bResult;
}

沒有留言:

張貼留言