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.
 
 
 
 
 

213 lines
4.8 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: ProtLGSm.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of ProtLGSm.cpp
//----------------------------------------------------------------------------
#include "StdAfx.h"
#ifdef __BORLANDC__
#include "..\LgLib\Version.h"
#include "..\LgLib\Comm\ProtLGSm.h"
#else
#include "..\Version.h"
#include "ProtLGSm.h"
#endif
CProtocolLGS::CProtocolLGS()
{
m_pConnection = NULL;
m_nCount = 0;
m_pBufferPtr = m_pBuffer;
}
CProtocolLGS::~CProtocolLGS()
{
Destroy();
}
BOOL CProtocolLGS::Create( CConnection *pConnection )
{
Destroy();
m_pConnection = pConnection;
m_pBufferPtr = m_pBuffer;
m_nCount = 0;
return TRUE;
}
void CProtocolLGS::Destroy()
{
m_pConnection = NULL;
m_pBufferPtr = m_pBuffer;
m_nCount = 0;
}
BOOL CProtocolLGS::Read( PBYTE *ppBuffer, PINT pCount, DWORD dwTimeout, BOOL bHost )
{
BOOL bResult = FALSE;
#ifdef _MSC_VER
DWORD dwStartTime = ::GetTickCount();
#endif
while( TRUE )
{
// assume without read repeating
BOOL bRepeat = FALSE;
// copy remaining part
if( m_pBuffer != m_pBufferPtr )
memcpy( m_pBuffer, m_pBufferPtr, m_nCount );
// read data
while( TRUE )
{
if( m_nCount < HEADER_SIZE )
{
// try to read command header
m_nCount += m_pConnection->Read( m_pBuffer + m_nCount, HEADER_SIZE - m_nCount, dwTimeout );
if( m_nCount < HEADER_SIZE )
{
break;
}
}
// check header tag
if( m_pBuffer[ HEADER_POS ] == HEADER_TAG )
{
// validate possibility of lenght
BYTE bLength = ( m_pBuffer[ LENGTH_POS ] & LENGTH_MASK );
if( bLength <= MAX_DATA_LENGTH )
{
// read remaining part
DWORD nReadCount = bLength - (m_nCount - HEADER_SIZE) + 1;
m_nCount += m_pConnection->Read( m_pBuffer + m_nCount, nReadCount, dwTimeout );
break;
}
else
memmove( m_pBuffer, m_pBuffer + 1, --m_nCount );
}
else
memmove( m_pBuffer, m_pBuffer + 1, --m_nCount );
#ifdef _MSC_VER
if( ::GetTickCount() - dwStartTime > dwTimeout )
break;
#endif
}
// initialize pointer
m_pBufferPtr = m_pBuffer;
// parse input bytes
while( m_nCount >= MIN_COMMAND_SIZE )
{
// check header tag
if( m_pBufferPtr[ HEADER_POS ] == HEADER_TAG )
{
// validate possibility of lenght
BYTE bLength = ( m_pBufferPtr[ LENGTH_POS ] & LENGTH_MASK );
if( bLength <= MAX_DATA_LENGTH )
{
// check current lenght
if( ( bLength + MIN_COMMAND_SIZE ) > m_nCount )
return FALSE;
// length without checksum
INT bSubLength = bLength + HEADER_SIZE;
// test checksum
BYTE bCheckSum = 0;
for( INT i = 0; i < bSubLength; i++ )
bCheckSum += m_pBufferPtr[ i ];
// if checksum ok
if( bCheckSum == m_pBufferPtr[ i ] )
{
BOOL bFromHost = ( ( m_pBufferPtr[ LENGTH_POS ] & HOST_TAG ) == HOST_TAG );
// if right direction
if( (bFromHost && !bHost) || (bHost && !bFromHost) )
{
// store length
*pCount = bLength;
*ppBuffer = m_pBufferPtr + DATA_POS;
// at least one command received
bResult = TRUE;
}
else
{
// probably received echo - repeat reading
bRepeat = TRUE;
}
// shift pointers
m_pBufferPtr += bSubLength;
m_nCount -= bSubLength;
}
}
}
// shift pointers
m_pBufferPtr ++;
m_nCount --;
}
// if not need to repeat reading
if( !bRepeat )
break;
}
return bResult;
}
BOOL CProtocolLGS::Write( PBYTE pBuffer, INT nCount, DWORD dwTimeout, BOOL bHost )
{
// validate data length
if( nCount > MAX_DATA_LENGTH )
return FALSE;
// clear receiver
m_nCount = 0;
m_pBufferPtr = m_pBuffer;
// make pointer to write buffer
PBYTE pBufferPtr = m_pBuffer;
#ifdef LGS_PROTOCOL_TRANSMIT_DELAY
// add small delay during transmition
for( INT j = 0; j < LGS_PROTOCOL_TRANSMIT_DELAY; j++ )
*pBufferPtr++ = 0;
#endif
// write header tag
BYTE bCheckSum = (*pBufferPtr++ = HEADER_TAG);
// write data length
bCheckSum += (*pBufferPtr++ = ( nCount | ( bHost ? HOST_TAG : 0 ) ) );
// write data
for( INT i = 0; i < nCount; i++ )
bCheckSum += (*pBufferPtr++ = *pBuffer++);
// write check sum
*pBufferPtr = bCheckSum;
// write data
#ifdef LGS_PROTOCOL_TRANSMIT_DELAY
DWORD dwBytesToWrite = nCount + MIN_COMMAND_SIZE + LGS_PROTOCOL_TRANSMIT_DELAY;
#else
DWORD dwBytesToWrite = nCount + MIN_COMMAND_SIZE;
#endif
return m_pConnection->Write( m_pBuffer, dwBytesToWrite, dwTimeout ) == dwBytesToWrite;
}