For DOS Protected Mode version of sensor core software
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.
|
|
//----------------------------------------------------------------------------
// 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
uint8_t 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
uint8_t 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
int16_t bSubLength = bLength + HEADER_SIZE;
// test checksum
uint8_t bCheckSum = 0; int16_t i; for( 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, int16_t 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( int16_t j = 0; j < LGS_PROTOCOL_TRANSMIT_DELAY; j++ ) *pBufferPtr++ = 0;#endif
// write header tag
uint8_t bCheckSum = (*pBufferPtr++ = HEADER_TAG);
// write data length
bCheckSum += (*pBufferPtr++ = ( nCount | ( bHost ? HOST_TAG : 0 ) ) );
// write data
for( int16_t 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;}
|