//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: SocketVM.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of SocketVM.cpp //---------------------------------------------------------------------------- // SocketVM.cpp: implementation of the CSocketVM class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" //#include "chicago.h" #include "SocketVM.h" #include "SensorArray.h" #include "..\..\LgLib\Version.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CSocketVM::CSocketVM() { m_nType = SOCKET_VM; for( int i = 0; i < MAX_DEVICE; i++ ) m_ppDevice[ i ] = NULL; m_nDeviceCount = 0; m_nDeviceCurrent = 0; m_bRunning = TRUE; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketVM::CSocketVM") ); } CSocketVM::~CSocketVM() { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketVM::~CSocketVM") ); } int CSocketVM::OnCommand() { BOOL bWait = FALSE; for( int i = 0; i < m_nDeviceCount; i++ ) { if( GetTickCount() > m_pTime[ m_nDeviceCurrent ] ) { BOOL bPause = FALSE; DWORD dwTime; if( m_ppDevice[ m_nDeviceCurrent ]->GetCommand( m_pBuffer, dwTime, bPause ) ) { if( !bPause ) { m_pTime[ m_nDeviceCurrent ] = 0UL; return CMD_WRITE; } else { m_pTime[ m_nDeviceCurrent ] = GetTickCount() + dwTime; // m_nRCount is pause time bWait = TRUE; } } else m_nDeviceCurrent = (m_nDeviceCurrent + 1) % m_nDeviceCount; } else { m_nDeviceCurrent = (m_nDeviceCurrent + 1) % m_nDeviceCount; bWait = TRUE; } } if( bWait ) return CMD_PAUSE; return CMD_STOP; /* for( int i = 0; i < m_nDeviceCount; i++ ) { if( m_ppDevice[ m_nDeviceCurrent ]->GetCommand( m_pBuffer ) ) { return CMD_WRITE; } else m_nDeviceCurrent = (m_nDeviceCurrent + 1) % m_nDeviceCount; } return CMD_STOP; */ } void CSocketVM::OnBeforeWrite( LPBYTE pBuffer, DWORD& nCount ) { nCount = MSG_LENGTH; ::CopyMemory( pBuffer, m_pBuffer, MSG_LENGTH ); ::EscapeCommFunction( m_hCommPort, SETRTS ); } int CSocketVM::OnAfterWrite() { return CMD_READ; } void CSocketVM::OnBeforeRead( DWORD& nCount, DWORD& dwTime ) { nCount = MSG_LENGTH; dwTime = 50UL; ::EscapeCommFunction( m_hCommPort, CLRRTS ); } int CSocketVM::OnAfterRead( LPBYTE pBuffer, DWORD nCount ) { if( m_ppDevice[ m_nDeviceCurrent ]->SetAnswer( pBuffer, nCount ) ) m_nCountSuccess++; else m_nCountFailure++; return CMD_COMMAND; } BOOL CSocketVM::DeviceAttach( CDeviceVMs *pDevice ) { if( m_nDeviceCount < MAX_DEVICE ) { for( int i = 0; i < m_nDeviceCount; i++ ) if( m_ppDevice[ i ] == pDevice ) return FALSE; m_pTime[ m_nDeviceCount ] = 0UL; m_ppDevice[ m_nDeviceCount++ ] = pDevice; pDevice->SetSocket( this ); pDevice->SetLogicalID( m_nDeviceCount ); return TRUE; } return FALSE; } BOOL CSocketVM::DeviceDetach( CDeviceVMs *pDevice ) { for( int i = 0; i < m_nDeviceCount; i++ ) { if( m_ppDevice[ i ] == pDevice ) { m_ppDevice[ i ] = m_ppDevice[ --m_nDeviceCount ]; if( m_nDeviceCurrent >= m_nDeviceCount ) m_nDeviceCurrent = 0; return TRUE; } } return FALSE; } int CSocketVM::OnPause( DWORD &dwTime ) { dwTime = 1UL; return CMD_COMMAND; } void CSocketVM::CommandLoopStart() { if( !m_bRunning ) { m_bRunning = TRUE; CSocketRS232::Start(); } } void CSocketVM::CommandLoopStop() { if( m_bRunning ) m_bRunning = FALSE; } BOOL CSocketVM::Create(CSensorArray *pSensorArray, LPCTSTR pPort) { if( !CSocketRS232::Create( pPort, 9600, 8, NOPARITY, ONESTOPBIT ) ) { pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, FALSE, m_nType, m_nNumber, RGB( 0xFF, 0, 0 ), "Can't create video matrix line" ); return m_bInitiated = FALSE; } else pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, TRUE, m_nType, m_nNumber, RGB( 0, 0xFF, 0 ), "Video matrix line created" ); return TRUE; }