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.
825 lines
22 KiB
825 lines
22 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: SocketECS.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of SocketECS.cpp
|
|
//----------------------------------------------------------------------------
|
|
// SocketECS.cpp: implementation of the CSocketECS class.
|
|
//
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
#include "stdafx.h"
|
|
//#include "chicago.h"
|
|
#include "SocketECS.h"
|
|
#include "..\..\LgLib\Version.h"
|
|
|
|
#ifdef _DEBUG
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[]=__FILE__;
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
|
|
#include "SensorArray.h"
|
|
#include "SensorSPRU.h"
|
|
//////////////////////////////////////////////////////////////////////
|
|
// Construction/Destruction
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
CSocketECS::CSocketECS()
|
|
{
|
|
m_nType = SOCKET_ECS;
|
|
m_hCommPort = NULL;
|
|
m_hEventQuit = NULL;
|
|
m_hEventLoop = NULL;
|
|
m_hEventLoopStop = NULL;
|
|
m_hEventCommand = NULL;
|
|
m_hThread = NULL;
|
|
m_dwThreadWriteId = 0;
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketECS::CSocketECS") );
|
|
}
|
|
|
|
CSocketECS::~CSocketECS()
|
|
{
|
|
if( m_hThread )
|
|
{
|
|
::SetEvent( m_hEventQuit );
|
|
::WaitForSingleObject( m_hThread, INFINITE );
|
|
::CloseHandle( m_hThread );
|
|
}
|
|
|
|
if( m_hEventQuit ) ::CloseHandle( m_hEventQuit );
|
|
if( m_hEventLoop ) ::CloseHandle( m_hEventLoop );
|
|
if( m_hEventLoopStop ) ::CloseHandle( m_hEventLoopStop );
|
|
if( m_hEventCommand ) ::CloseHandle( m_hEventCommand );
|
|
|
|
if( m_hCommPort )
|
|
{
|
|
::CloseHandle( m_hCommPort );
|
|
}
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketECS::~CSocketECS") );
|
|
}
|
|
|
|
void CSocketECS::CommandLoopStart()
|
|
{
|
|
::SetEvent( m_hEventLoop );
|
|
}
|
|
|
|
void CSocketECS::CommandLoopStop()
|
|
{
|
|
if( ::WaitForSingleObject( m_hEventLoop, 0 ) == WAIT_OBJECT_0 )
|
|
::SetEvent( m_hEventLoopStop );
|
|
}
|
|
|
|
BOOL CSocketECS::Create(CSensorArray *pSensorArray, LPCTSTR pCommName)
|
|
{
|
|
m_pSensorArray = pSensorArray;
|
|
|
|
if( !CreateCommPort( pCommName ) )
|
|
{
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, FALSE, m_nType, m_nNumber, RGB( 0xFF, 0, 0 ), "Can't create ECS line" );
|
|
return m_bInitiated = FALSE;
|
|
}
|
|
else
|
|
m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_SOCKET_COMMUNICATION, TRUE, m_nType, m_nNumber, RGB( 0, 0xFF, 0 ), "ECS line created" );
|
|
|
|
return m_bInitiated = TRUE;
|
|
}
|
|
|
|
BOOL CSocketECS::CreateCommPort(LPCTSTR pCommName)
|
|
{
|
|
m_strName = pCommName;
|
|
|
|
m_hCommPort = ::CreateFile( pCommName, GENERIC_READ | GENERIC_WRITE,
|
|
0UL, NULL, OPEN_EXISTING,
|
|
FILE_FLAG_OVERLAPPED|FILE_FLAG_NO_BUFFERING, NULL );
|
|
|
|
if( m_hCommPort == INVALID_HANDLE_VALUE )
|
|
return FALSE;
|
|
|
|
DCB dcb;
|
|
::ZeroMemory(&dcb, sizeof(dcb));
|
|
dcb.DCBlength = sizeof( dcb );
|
|
::GetCommState( m_hCommPort, &dcb );
|
|
|
|
dcb.BaudRate = CBR_1200;
|
|
dcb.ByteSize = 8;
|
|
dcb.Parity = EVENPARITY;
|
|
dcb.StopBits = ONESTOPBIT;
|
|
dcb.EvtChar = 0;
|
|
dcb.fParity = TRUE;
|
|
dcb.fAbortOnError = FALSE;
|
|
|
|
m_dwMaxReadTime = ( MESSAGE_SIZE * CBR_9600 ) / dcb.BaudRate;
|
|
if( !m_dwMaxReadTime )
|
|
m_dwMaxReadTime = 1;
|
|
m_dwMaxWaitTime = m_dwMaxReadTime + MAX_PROCESSING_TIME;
|
|
|
|
if( !::SetupComm( m_hCommPort, MESSAGE_SIZE*5, MESSAGE_SIZE*5 ) )
|
|
return FALSE;
|
|
|
|
if( !::SetCommState( m_hCommPort, &dcb ) )
|
|
return FALSE;
|
|
|
|
COMMTIMEOUTS ct;
|
|
ct.ReadIntervalTimeout = 0UL;
|
|
ct.ReadTotalTimeoutMultiplier = 0UL;
|
|
ct.ReadTotalTimeoutConstant = 0UL;
|
|
ct.WriteTotalTimeoutMultiplier = 0UL;
|
|
ct.WriteTotalTimeoutConstant = 0UL;
|
|
|
|
if( !::SetCommTimeouts( m_hCommPort, &ct ) )
|
|
return FALSE;
|
|
|
|
m_hEventQuit = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
m_hEventLoop = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
m_hEventLoopStop = ::CreateEvent( NULL, FALSE, FALSE, NULL );
|
|
m_hEventCommand = ::CreateEvent( NULL, FALSE, FALSE, NULL );
|
|
|
|
m_hThread = ::CreateThread( NULL, 0UL, ThreadProcSocketECS, this, 0UL, &m_dwThreadWriteId );
|
|
|
|
// ::SetThreadPriority( m_hThread, THREAD_PRIORITY_BELOW_NORMAL );
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CSocketECS::PostEvent( EVENT& event )
|
|
{
|
|
BOOL bResult = CSocket::PostEvent( event );
|
|
|
|
::SetEvent( m_hEventCommand );
|
|
|
|
return bResult;
|
|
}
|
|
|
|
DWORD CSocketECS::ThreadProc()
|
|
{
|
|
DWORD dwErrors;
|
|
COMSTAT comstat;
|
|
|
|
DWORD dwWritten;
|
|
OVERLAPPED ovWrite;
|
|
ovWrite.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
ovWrite.Internal = ovWrite.InternalHigh = ovWrite.Offset = ovWrite.OffsetHigh = 0UL;
|
|
|
|
DWORD dwEvtMask = 0;
|
|
OVERLAPPED ovWait;
|
|
ovWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
ovWait.Internal = ovWait.InternalHigh = ovWait.Offset = ovWait.OffsetHigh = 0UL;
|
|
|
|
DWORD dwRead;
|
|
OVERLAPPED ovRead;
|
|
ovRead.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
ovRead.Internal = ovRead.InternalHigh = ovRead.Offset = ovRead.OffsetHigh = 0UL;
|
|
|
|
HANDLE hEventsCommand[] = { m_hEventQuit, m_hEventLoopStop, m_hEventLoop };
|
|
HANDLE hEventsWrite[] = { m_hEventQuit, ovWrite.hEvent };
|
|
HANDLE hEventsWait[] = { m_hEventQuit, ovWait.hEvent };
|
|
HANDLE hEventsRead[] = { m_hEventQuit, ovRead.hEvent };
|
|
|
|
BYTE pBuffer[ MESSAGE_SIZE ];
|
|
|
|
DWORD dwTime = 0;
|
|
DWORD dwLoopCount = 0;
|
|
|
|
WORD wCriticalError;
|
|
|
|
CEventQueue *pEventQueue = m_pSensorArray->GetEventQueue();
|
|
|
|
CString str;
|
|
/*
|
|
HANDLE hEventTimer = ::CreateWaitableTimer( NULL, FALSE, NULL );
|
|
LARGE_INTEGER li;
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&li );
|
|
::SetWaitableTimer( hEventTimer, &li, 30000, NULL, NULL, FALSE );
|
|
HANDLE hEvents[] = { m_hEventQuit, hEventTimer, m_hEventCommand };
|
|
*/
|
|
HANDLE hEvents[] = { m_hEventQuit, m_hEventCommand };
|
|
|
|
EVENT event;
|
|
|
|
DWORD nBufferCount;
|
|
BOOL bWaitResponse = FALSE;
|
|
BOOL bTransmitAlarm = FALSE;
|
|
BOOL bTransmitHeart = FALSE;
|
|
BYTE pAlarmBuffer[ 6 ] = { 0xFE, 0, 0, 0, 0, 0xFF };
|
|
BYTE pHeartBuffer[ 3 ] = { 0xFE, 0xEE, 0xFF };
|
|
|
|
LARGE_INTEGER nextHeartTime, currentTime;
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&nextHeartTime );
|
|
|
|
while( TRUE )
|
|
{
|
|
// Wait for command or exit
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsCommand)/sizeof(*hEventsCommand), hEventsCommand, FALSE, INFINITE ) )
|
|
{
|
|
case WAIT_OBJECT_0+0: goto END_THREAD;
|
|
case WAIT_OBJECT_0+1: ::ResetEvent( m_hEventLoop ); continue;
|
|
case WAIT_OBJECT_0+2: break;
|
|
}
|
|
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)¤tTime );
|
|
|
|
DWORD dwTime = 0;
|
|
__int64 diff = nextHeartTime.QuadPart - currentTime.QuadPart;
|
|
if( diff > 0I64 )
|
|
dwTime = (DWORD)( diff / 10000I64 );
|
|
|
|
switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, dwTime ) )
|
|
{
|
|
case WAIT_OBJECT_0+0: goto END_THREAD;
|
|
|
|
case WAIT_OBJECT_0+1:
|
|
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)¤tTime );
|
|
|
|
if( nextHeartTime.QuadPart > currentTime.QuadPart )
|
|
{
|
|
if( !bWaitResponse )
|
|
{
|
|
if( m_EventQueue.GetEvent( event ) )
|
|
{
|
|
CSensorSPRU *pSensor = (CSensorSPRU*)m_pSensorArray->GetSensor( event.data16 );
|
|
|
|
pAlarmBuffer[1] = pSensor->GetSocket();
|
|
pAlarmBuffer[2] = pSensor->GetAddress();
|
|
switch( event.id )
|
|
{
|
|
case LGS_EVENT_ALARM_SHOW: pAlarmBuffer[3] = 0x02; break;
|
|
case LGS_EVENT_ALARM_HIDE: pAlarmBuffer[3] = 0x00; break;
|
|
case LGS_EVENT_TIMEOUT_APPEAR: pAlarmBuffer[3] = 0x01; break;
|
|
case LGS_EVENT_TIMEOUT_DISAPPEAR: pAlarmBuffer[3] = 0x03; break;
|
|
}
|
|
pAlarmBuffer[4] = (BYTE)event.data32;
|
|
|
|
bWaitResponse = TRUE;
|
|
bTransmitAlarm = TRUE;
|
|
::SetEvent( m_hEventCommand );
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case WAIT_TIMEOUT:
|
|
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)¤tTime );
|
|
|
|
nextHeartTime.QuadPart = currentTime.QuadPart + 300000000I64;
|
|
|
|
bTransmitHeart = TRUE;
|
|
break;
|
|
}
|
|
|
|
if( bTransmitAlarm )
|
|
{
|
|
nBufferCount = 6;
|
|
::CopyMemory( pBuffer, pAlarmBuffer, nBufferCount );
|
|
}
|
|
else
|
|
if( bTransmitHeart )
|
|
{
|
|
nBufferCount = 3;
|
|
::CopyMemory( pBuffer, pHeartBuffer, nBufferCount );
|
|
}
|
|
else
|
|
nBufferCount = 0;
|
|
|
|
// calculate performance
|
|
DWORD dwTempTime = ::GetTickCount();
|
|
if( dwTempTime > dwTime )
|
|
{
|
|
m_nPerformance = dwLoopCount;
|
|
dwLoopCount = 0;
|
|
dwTime = dwTempTime + 1000;
|
|
}
|
|
else dwLoopCount++;
|
|
|
|
if( nBufferCount )
|
|
{
|
|
|
|
// Before writting it needs clear comm errors if they exist
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & (CE_RXOVER|CE_BREAK) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 0;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
|
|
::SetCommMask( m_hCommPort, EV_TXEMPTY );
|
|
|
|
// Asynchronous write data
|
|
if( !::WriteFile( m_hCommPort, pBuffer, nBufferCount, &dwWritten, &ovWrite ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWrite)/sizeof(*hEventsWrite), hEventsWrite, FALSE, 100 ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovWrite, &dwWritten, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 1;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxReadTime ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 2;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
}
|
|
|
|
if( !( dwEvtMask & EV_TXEMPTY ) )
|
|
{
|
|
wCriticalError = 3;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
}
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
|
|
|
|
// If write was not successfull
|
|
if( dwWritten != nBufferCount )
|
|
{
|
|
wCriticalError = 4;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
else
|
|
{
|
|
bTransmitAlarm = FALSE;
|
|
bTransmitHeart = FALSE;
|
|
m_nCountSuccess++;
|
|
}
|
|
}
|
|
|
|
if( bWaitResponse )
|
|
{
|
|
// Before reading it needs clear comm errors if they exist
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & (CE_RXOVER|CE_BREAK) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 5;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
|
|
|
|
BOOL bReason = TRUE;
|
|
|
|
if( !comstat.cbInQue ) // input buffer empty
|
|
{
|
|
::SetCommMask( m_hCommPort, EV_RXCHAR );
|
|
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxWaitTime ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 6;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
}
|
|
|
|
if( !( dwEvtMask & EV_RXCHAR ) )
|
|
bReason = FALSE;
|
|
}
|
|
|
|
if( bReason )
|
|
{
|
|
// Asynchronous read data
|
|
if( !::ReadFile( m_hCommPort, pBuffer, 1, &dwRead, &ovRead ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsRead)/sizeof(*hEventsRead), hEventsRead, FALSE, m_dwMaxReadTime ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovRead, &dwRead, FALSE );
|
|
break;
|
|
case WAIT_TIMEOUT:
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 7;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
}
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_RXCLEAR | PURGE_RXABORT );
|
|
}
|
|
else
|
|
dwRead = 0UL;
|
|
|
|
if( dwRead == 1 ) // reseived response from ECS
|
|
{
|
|
switch( *pBuffer )
|
|
{
|
|
case 0x0F:
|
|
m_nCountSuccess++;
|
|
bWaitResponse = FALSE;
|
|
break;
|
|
case 0xFF:
|
|
m_nCountSuccess++; // need retransmit
|
|
bTransmitAlarm = TRUE;
|
|
break;
|
|
default:
|
|
m_nCountFailure++; // undefined situation
|
|
bWaitResponse = FALSE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
::SetEvent( m_hEventCommand );
|
|
}
|
|
}
|
|
|
|
CRITICAL_END_THREAD:
|
|
str.Format( "ECS error(%d)", wCriticalError );
|
|
pEventQueue->PostEvent( LGS_EVENT_ERROR, 0, m_nNumber, wCriticalError, RGB( 0xFF, 0, 0 ), (LPSTR)(LPCTSTR)str );
|
|
|
|
END_THREAD:
|
|
::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
|
|
/*
|
|
::CancelWaitableTimer( hEventTimer );
|
|
::CloseHandle( hEventTimer );
|
|
*/
|
|
::CloseHandle( ovWrite.hEvent );
|
|
::CloseHandle( ovWait.hEvent );
|
|
::CloseHandle( ovRead.hEvent );
|
|
|
|
TRACE("<SocketECS> thread: ");
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
DWORD CSocketECS::ThreadProc()
|
|
{
|
|
DWORD dwErrors;
|
|
COMSTAT comstat;
|
|
|
|
DWORD dwWritten;
|
|
OVERLAPPED ovWrite;
|
|
ovWrite.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
ovWrite.Internal = ovWrite.InternalHigh = ovWrite.Offset = ovWrite.OffsetHigh = 0UL;
|
|
|
|
DWORD dwEvtMask = 0;
|
|
OVERLAPPED ovWait;
|
|
ovWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
ovWait.Internal = ovWait.InternalHigh = ovWait.Offset = ovWait.OffsetHigh = 0UL;
|
|
|
|
DWORD dwRead;
|
|
OVERLAPPED ovRead;
|
|
ovRead.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
ovRead.Internal = ovRead.InternalHigh = ovRead.Offset = ovRead.OffsetHigh = 0UL;
|
|
|
|
HANDLE hEventsCommand[] = { m_hEventQuit, m_hEventLoop };
|
|
HANDLE hEventsWrite[] = { m_hEventQuit, ovWrite.hEvent };
|
|
HANDLE hEventsWait[] = { m_hEventQuit, ovWait.hEvent };
|
|
HANDLE hEventsRead[] = { m_hEventQuit, ovRead.hEvent };
|
|
|
|
BYTE pBuffer[ MESSAGE_SIZE ];
|
|
|
|
DWORD dwTime = 0;
|
|
DWORD dwLoopCount = 0;
|
|
|
|
WORD wCriticalError;
|
|
|
|
CEventQueue *pEventQueue = m_pSensorArray->GetEventQueue();
|
|
|
|
CString str;
|
|
|
|
HANDLE hEventTimer = ::CreateWaitableTimer( NULL, FALSE, NULL );
|
|
LARGE_INTEGER li;
|
|
::GetSystemTimeAsFileTime( (LPFILETIME)&li );
|
|
::SetWaitableTimer( hEventTimer, &li, 30000, NULL, NULL, FALSE );
|
|
|
|
HANDLE hEvents[] = { m_hEventQuit, hEventTimer, m_hEventCommand };
|
|
|
|
EVENT event;
|
|
|
|
DWORD nBufferCount;
|
|
BOOL bWaitResponse = FALSE;
|
|
BOOL bTransmitAlarm = FALSE;
|
|
BOOL bTransmitHeart = FALSE;
|
|
BYTE pAlarmBuffer[ 6 ] = { 0xFE, 0, 0, 0, 0, 0xFF };
|
|
BYTE pHeartBuffer[ 3 ] = { 0xFE, 0xEE, 0xFF };
|
|
|
|
while( TRUE )
|
|
{
|
|
// Wait for command or exit
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsCommand)/sizeof(*hEventsCommand), hEventsCommand, FALSE, INFINITE ) )
|
|
{
|
|
case WAIT_OBJECT_0+0: goto END_THREAD;
|
|
case WAIT_OBJECT_0+1: break;
|
|
}
|
|
|
|
switch( ::WaitForMultipleObjects( 3, hEvents, FALSE, INFINITE ) )
|
|
{
|
|
case WAIT_OBJECT_0+0: goto END_THREAD;
|
|
case WAIT_OBJECT_0+1:
|
|
bTransmitHeart = TRUE;
|
|
break;
|
|
case WAIT_OBJECT_0+2:
|
|
if( !bWaitResponse )
|
|
{
|
|
if( m_ECSQueue.GetEvent( event ) )
|
|
{
|
|
CSensorDTR *pSensor = (CSensorDTR*)m_pSensorArray->GetSensor( event.data16 );
|
|
|
|
pAlarmBuffer[1] = pSensor->GetSocket();
|
|
pAlarmBuffer[2] = pSensor->GetAddress();
|
|
switch( event.event )
|
|
{
|
|
case LGS_EVENT_ALARM_SHOW: pAlarmBuffer[3] = 0x02; break;
|
|
case LGS_EVENT_ALARM_HIDE: pAlarmBuffer[3] = 0x00; break;
|
|
case LGS_EVENT_TIMEOUT_APPEAR: pAlarmBuffer[3] = 0x01; break;
|
|
case LGS_EVENT_TIMEOUT_DISAPPEAR: pAlarmBuffer[3] = 0x03; break;
|
|
}
|
|
pAlarmBuffer[4] = (BYTE)event.data32;
|
|
|
|
bWaitResponse = TRUE;
|
|
bTransmitAlarm = TRUE;
|
|
::SetEvent( m_hEventCommand );
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
if( bTransmitAlarm )
|
|
{
|
|
nBufferCount = 6;
|
|
::CopyMemory( pBuffer, pAlarmBuffer, nBufferCount );
|
|
}
|
|
else
|
|
if( bTransmitHeart )
|
|
{
|
|
nBufferCount = 3;
|
|
::CopyMemory( pBuffer, pHeartBuffer, nBufferCount );
|
|
}
|
|
else
|
|
nBufferCount = 0;
|
|
|
|
// calculate performance
|
|
DWORD dwTempTime = ::GetTickCount();
|
|
if( dwTempTime > dwTime )
|
|
{
|
|
m_nPerformance = dwLoopCount;
|
|
dwLoopCount = 0;
|
|
dwTime = dwTempTime + 1000;
|
|
}
|
|
else dwLoopCount++;
|
|
|
|
if( nBufferCount )
|
|
{
|
|
|
|
// Before writting it needs clear comm errors if they exist
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & (CE_RXOVER|CE_BREAK) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 0;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
|
|
::SetCommMask( m_hCommPort, EV_TXEMPTY );
|
|
|
|
// Asynchronous write data
|
|
if( !::WriteFile( m_hCommPort, pBuffer, nBufferCount, &dwWritten, &ovWrite ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWrite)/sizeof(*hEventsWrite), hEventsWrite, FALSE, 100 ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovWrite, &dwWritten, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 1;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxReadTime ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 2;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
}
|
|
|
|
if( !( dwEvtMask & EV_TXEMPTY ) )
|
|
{
|
|
wCriticalError = 3;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
}
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
|
|
|
|
// If write was not successfull
|
|
if( dwWritten != nBufferCount )
|
|
{
|
|
wCriticalError = 4;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
else
|
|
{
|
|
bTransmitAlarm = FALSE;
|
|
bTransmitHeart = FALSE;
|
|
m_nCountSuccess++;
|
|
}
|
|
}
|
|
|
|
if( bWaitResponse )
|
|
{
|
|
// Before reading it needs clear comm errors if they exist
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & (CE_RXOVER|CE_BREAK) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wCriticalError = 5;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
|
|
|
|
BOOL bReason = TRUE;
|
|
|
|
if( !comstat.cbInQue ) // input buffer empty
|
|
{
|
|
::SetCommMask( m_hCommPort, EV_RXCHAR );
|
|
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsWait)/sizeof(*hEventsWait), hEventsWait, FALSE, m_dwMaxWaitTime ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 6;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
}
|
|
|
|
if( !( dwEvtMask & EV_RXCHAR ) )
|
|
bReason = FALSE;
|
|
}
|
|
|
|
if( bReason )
|
|
{
|
|
// Asynchronous read data
|
|
if( !::ReadFile( m_hCommPort, pBuffer, 1, &dwRead, &ovRead ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsRead)/sizeof(*hEventsRead), hEventsRead, FALSE, m_dwMaxReadTime ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0: goto END_THREAD;
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovRead, &dwRead, FALSE );
|
|
break;
|
|
case WAIT_TIMEOUT:
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
wCriticalError = 7;
|
|
goto CRITICAL_END_THREAD;
|
|
}
|
|
}
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_RXCLEAR | PURGE_RXABORT );
|
|
}
|
|
else
|
|
dwRead = 0UL;
|
|
|
|
if( dwRead == 1 ) // reseived response from ECS
|
|
{
|
|
switch( *pBuffer )
|
|
{
|
|
case 0x0F:
|
|
m_nCountSuccess++;
|
|
bWaitResponse = FALSE;
|
|
break;
|
|
case 0xFF:
|
|
m_nCountSuccess++; // need retransmit
|
|
bTransmitAlarm = TRUE;
|
|
break;
|
|
default:
|
|
m_nCountFailure++; // undefined situation
|
|
bWaitResponse = FALSE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
::SetEvent( m_hEventCommand );
|
|
}
|
|
}
|
|
|
|
CRITICAL_END_THREAD:
|
|
str.Format( "socket critical error (%d)", wCriticalError );
|
|
pEventQueue->PostEvent( LGS_EVENT_SOCKET_CRITICAL_ERROR, 0, m_nNumber, wCriticalError, RGB( 0xFF, 0, 0 ), (LPSTR)(LPCTSTR)str );
|
|
|
|
END_THREAD:
|
|
::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
|
|
|
|
::CancelWaitableTimer( hEventTimer );
|
|
::CloseHandle( hEventTimer );
|
|
|
|
::CloseHandle( ovWrite.hEvent );
|
|
::CloseHandle( ovWait.hEvent );
|
|
::CloseHandle( ovRead.hEvent );
|
|
|
|
return 0;
|
|
}
|
|
*/
|
|
|