//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: PTZPelco.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of PTZPelco.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #ifdef __BORLANDC__ #include "..\LgLib\Comm\PTZPelco.h" #else #include "PTZPelco.h" #endif CProtocolPTZPelco::CProtocolPTZPelco() { m_pConnection = NULL; m_nCount = 0; m_pBufferPtr = m_pBuffer; m_bAddress = 1; #ifdef DEBUG_PELCO_PROTO logFID = NULL; logFID = fopen( "pelco_bin.log", "w" ); #endif } CProtocolPTZPelco::~CProtocolPTZPelco() { #ifdef DEBUG_PELCO_PROTO if( logFID ) fclose( logFID ); #endif Destroy(); } BOOL CProtocolPTZPelco::Create( CConnection *pConnection ) { Destroy(); m_pConnection = pConnection; m_pBufferPtr = m_pBuffer; m_nCount = 0; return TRUE; } void CProtocolPTZPelco::Destroy() { m_pConnection = NULL; m_pBufferPtr = m_pBuffer; m_nCount = 0; } void CProtocolPTZPelco::SetAddress( BYTE bAddress ) { m_bAddress = bAddress; } BOOL CProtocolPTZPelco::Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost ) { bHost; BOOL bResult = FALSE; // copy remaining part if( ( m_pBuffer != m_pBufferPtr ) && m_nCount ) memcpy( m_pBuffer, m_pBufferPtr, m_nCount ); // try to read command header if( m_nCount < m_nResponseSize ) { // read new data DWORD nCount = m_nResponseSize - m_nCount; m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nCount, dwTimeout ) ); } if( m_nCount >= m_nResponseSize ){ #ifdef DEBUG_PELCO_PROTO if( logFID ){ // prepare data&time SYSTEMTIME st; GetLocalTime( &st ); // prepare full string fprintf( logFID, "[%02d/%02d/%02d %02d:%02d:%02d:%03d]", st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds ); // output fprintf( logFID, ">" ); for( INT index=0; index < m_nResponseSize; index++ ) fprintf( logFID, "%02X", m_pBufferPtr[index]); fprintf( logFID, "\r\n" ); }; #endif // store length (*pCount) = (INT)m_nResponseSize; (*ppBuffer) = m_pBufferPtr; // shift pointers m_pBufferPtr += m_nResponseSize; m_nCount -= m_nResponseSize; // at least one command received bResult = TRUE; } return bResult; } BOOL CProtocolPTZPelco::Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost ) { bHost; // validate data length if( nCount > MAX_COMMAND_SIZE ) return FALSE; // clear receiver m_nCount = 0; m_pBufferPtr = m_pBuffer; // write data DWORD dwDataSize = nCount; #ifdef DEBUG_PELCO_PROTO if( logFID ){ // prepare data&time SYSTEMTIME st; GetLocalTime( &st ); // prepare full string fprintf( logFID, "[%02d/%02d/%02d %02d:%02d:%02d:%03d]", st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds ); // output fprintf( logFID, "<" ); for( INT index=0; index < nCount; index++ ) fprintf( logFID, "%02X", pBuffer[index]); fprintf( logFID, "\r\n" ); }; #endif return (BOOL)(m_pConnection->Write( pBuffer, dwDataSize, dwTimeout ) == dwDataSize); }