Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

212 lines
5.2 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: PMic1300.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of PMic1300.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#ifdef __BORLANDC__
#include "..\LgLib\Version.h"
#include "..\LgLib\Comm\PMic1300.h"
#include "..\LgLib\Comm\Mic1300m.h"
#else
#include "..\Version.h"
#include "PMic1300.h"
#include "Mic1300m.h"
#endif
CProtocolMic1300PTZ::CProtocolMic1300PTZ()
{
m_pConnection = NULL;
m_nCount = 0;
m_pBufferPtr = m_pBuffer;
m_pAddress[0] = m_pAddress[1] = '0';
m_bReadAnyAddress = FALSE;
}
CProtocolMic1300PTZ::~CProtocolMic1300PTZ()
{
Destroy();
}
BOOL CProtocolMic1300PTZ::Create( CConnection *pConnection )
{
Destroy();
m_pConnection = pConnection;
m_pBufferPtr = m_pBuffer;
m_nCount = 0;
return TRUE;
}
void CProtocolMic1300PTZ::Destroy()
{
m_pConnection = NULL;
m_pBufferPtr = m_pBuffer;
m_nCount = 0;
}
void CProtocolMic1300PTZ::SetAddress( BYTE bAddress )
{
m_pAddress[0] = PTZ_MIC_1_300::ToAscii( bAddress, 1 );
m_pAddress[1] = PTZ_MIC_1_300::ToAscii( bAddress, 0 );
}
BOOL CProtocolMic1300PTZ::Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost )
{
bHost;
BOOL bResult = FALSE;
while( TRUE )
{
// assume without read repeating
BOOL bRepeat = FALSE;
// copy remaining part
if( ( m_pBuffer != m_pBufferPtr ) && m_nCount )
memcpy( m_pBuffer, m_pBufferPtr, m_nCount );
DWORD nReadCount = 0;
// try to read command header
if( m_nCount < HEADER_SIZE )
{
// read new data
DWORD nCount = HEADER_SIZE - m_nCount;
m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nCount, dwTimeout ) );
}
if( m_nCount >= HEADER_SIZE )
{
// check header tag
if( ( m_pBuffer[ HEADER_POS ] == HEADER_TAG ) )
{
if( m_bReadAnyAddress ||
( m_pBuffer[ ADDRESS_POS ] == '0' && m_pBuffer[ ADDRESS_POS+1 ] == '0' ) ||
( m_pBuffer[ ADDRESS_POS ] == m_pAddress[0] && m_pBuffer[ ADDRESS_POS+1 ] == m_pAddress[1] ) )
{
// validate possibility of lenght
WORD bLength = PTZ_MIC_1_300::ToValue( m_pBuffer + LENGTH_POS, 2 );
if( bLength <= MAX_COMMAND_SIZE )
{
nReadCount = bLength - m_nCount;
}
else
nReadCount = MAX_COMMAND_SIZE - m_nCount;
}
else
nReadCount = MAX_COMMAND_SIZE - m_nCount;
}
else
nReadCount = MAX_COMMAND_SIZE - m_nCount;
}
else
nReadCount = MAX_COMMAND_SIZE - m_nCount;
// read new data
m_nCount += ( m_pConnection->Read( m_pBuffer + m_nCount, nReadCount, dwTimeout ) );
// initialize pointer
m_pBufferPtr = m_pBuffer;
// parse input bytes
while( m_nCount >= MIN_COMMAND_SIZE )
{
// check header tag
if( ( m_pBuffer[ HEADER_POS ] == HEADER_TAG ) )
{
if( m_bReadAnyAddress ||
( m_pBuffer[ ADDRESS_POS ] == '0' && m_pBuffer[ ADDRESS_POS+1 ] == '0' ) ||
( m_pBuffer[ ADDRESS_POS ] == m_pAddress[0] && m_pBuffer[ ADDRESS_POS+1 ] == m_pAddress[1] ) )
{
// validate possibility of lenght
WORD bLength = PTZ_MIC_1_300::ToValue( m_pBuffer + LENGTH_POS, 2 );
if( bLength <= MAX_COMMAND_SIZE )
{
// check current lenght
if( bLength > m_nCount )
return FALSE;
// length without checksum
BYTE bSubLength = bLength - 1;
// test checksum
BYTE bCheckSum = m_pBufferPtr[0];
for( INT i = 1; i < bSubLength; i++ )
bCheckSum ^= m_pBufferPtr[ i ];
bCheckSum |= 0x80;
// if checksum ok
if( bCheckSum == m_pBufferPtr[ i ] )
{
BOOL bFromCamera = m_pBufferPtr[ CFLAG_POS ] != PTZ_MIC_1_300::CF_FULL_DUPLEX &&
m_pBufferPtr[ CFLAG_POS ] != PTZ_MIC_1_300::CF_HALF_DUPLEX &&
m_pBufferPtr[ CFLAG_POS ] != PTZ_MIC_1_300::CF_SIMPLEX;
// if right direction
if( bFromCamera )
{
// store length
(*pCount) = (INT)bLength;
(*ppBuffer) = m_pBufferPtr;
// at least one command received
bResult = TRUE;
}
else
{
// probably received echo - repeat reading
bRepeat = TRUE;
}
// shift pointers
m_pBufferPtr += bSubLength;
m_nCount -= bSubLength;
// at least one command received
bResult = TRUE;
}
}
}
}
// shift pointers
m_pBufferPtr ++;
m_nCount --;
}
// if not need to repeat reading
if( !bRepeat )
break;
}
return bResult;
}
BOOL CProtocolMic1300PTZ::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;
return (BOOL)(m_pConnection->Write( pBuffer, dwDataSize, dwTimeout ) == dwDataSize);
}