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.
 
 
 
 
 

235 lines
5.5 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 19/04/2000
// Last modification: 08/10/2003
// Project Intruder Position Indicator
// SUBSYSTEM: Fence
// FILE: Fence.cpp
// AUTHOR: Chizhevskiy Sergey
// OVERVIEW
// ~~~~~~~~
// Source file of class CFence
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "..\LgLib\Version.h"
#include "Application.h"
#include "Fence.h"
CFence::CFence()
{
ZeroMemory(this, sizeof(*this));
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CFence::CFence" );
}
CFence::~CFence()
{
if(m_hThread)
{
::SetEvent(m_hEventClose);
::WaitForSingleObject(m_hThread, 1000UL);
::CloseHandle(m_hThread);
}
if(m_hEventClose)
::CloseHandle(m_hEventClose);
if(m_hCommPort)
::CloseHandle(m_hCommPort);
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CFence::~CFence" );
}
BOOL CFence::Create(CApplication *pApplication, LPCTSTR pCommPort)
{
COMMTIMEOUTS ct;
DCB dcb;
m_pApplication = pApplication;
m_hCommPort = CreateFile(pCommPort, GENERIC_READ | GENERIC_WRITE,
0UL, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, NULL);
if(m_hCommPort == INVALID_HANDLE_VALUE)
return FALSE;
::ZeroMemory(&dcb, sizeof(dcb));
dcb.DCBlength = sizeof( dcb );
::GetCommState(m_hCommPort, &dcb);
dcb.BaudRate = CBR_1200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = TRUE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.fDsrSensitivity = FALSE;
if(!::SetCommState(m_hCommPort, &dcb))
{
return FALSE;
}
ZeroMemory(&ct, sizeof(ct));
::SetCommTimeouts(m_hCommPort, &ct);
::SetCommMask(m_hCommPort, EV_CTS | EV_DSR);
::EscapeCommFunction(m_hCommPort, SETRTS);
m_hEventClose = ::CreateEvent(NULL, FALSE, FALSE, NULL);
DWORD dwThreadId;
m_hThread = ::CreateThread(NULL, 0UL, ThreadProcFence, (LPVOID)this, 0UL, &dwThreadId);
if(!m_hThread)
return FALSE;
return TRUE;
}
/*
DWORD CFence::ThreadProc()
{
COMSTAT comStat;
DWORD dwErrors;
DWORD dwEvtMask;
DWORD dwStatus;
OVERLAPPED waitOV = { 0UL, 0UL, 0UL, 0UL, ::CreateEvent(NULL, TRUE, FALSE, NULL) };
HANDLE phHandle[] = { m_hEventClose, waitOV.hEvent };
while(TRUE)
{
::SetCommMask(m_hCommPort, EV_CTS | EV_DSR);
if(!::ClearCommError(m_hCommPort, &dwErrors, &comStat))
{
CMessageBox::MessageBox(NULL, "CFence::ClearCommError: ", NULL, MB_OK);
continue;
}
if(!::WaitCommEvent(m_hCommPort, &dwEvtMask, &waitOV))
{
switch(::GetLastError())
{
case ERROR_IO_PENDING:
switch(::WaitForMultipleObjects(2, phHandle, FALSE, INFINITE))
{
case WAIT_TIMEOUT:
continue;
case WAIT_OBJECT_0 + 0:
goto THREAD_EXIT;
case WAIT_OBJECT_0 + 1:
if(!::GetOverlappedResult(m_hCommPort, &waitOV, &dwEvtMask, FALSE) )
{
CMessageBox::MessageBox(NULL, "CFence::WaitCommEvent: ", NULL, MB_OK);
continue;
}
break;
}
break;
default:
CMessageBox::MessageBox(NULL, "CFence::WaitCommEvent: ", NULL, MB_OK);
continue;
} // end of switch(::GetLastError())
} // end of if(!::WaitCommEvent(m_hCommPort, &dwEvtMask, &waitOV))
::GetCommModemStatus(m_hCommPort, &dwStatus);
if((dwEvtMask & EV_DSR) && (!(dwStatus & MS_DSR_ON)))
if(m_bEnable)
m_pApplication->AddFenceEvent(0);
if((dwEvtMask & EV_CTS) && (dwStatus & MS_CTS_ON))
if(m_bEnable)
m_pApplication->AddFenceEvent(1);
} // end of while(TRUE)
THREAD_EXIT:
::CloseHandle(waitOV.hEvent);
return 70UL;
}
*/
DWORD CALLBACK ThreadProcFence(LPVOID lpvParam)
{
COMSTAT comStat;
DWORD dwErrors;
DWORD dwEvtMask;
DWORD dwStatus;
OVERLAPPED waitOV = { 0UL, 0UL, 0UL, 0UL, ::CreateEvent(NULL, TRUE, FALSE, NULL) };
CFence* ptr = (CFence*)lpvParam;
HANDLE phHandle[] = { ptr->m_hEventClose, waitOV.hEvent };
while(TRUE)
{
::SetCommMask(ptr->m_hCommPort, EV_CTS | EV_DSR);
if(!::ClearCommError(ptr->m_hCommPort, &dwErrors, &comStat))
{
CMessageBox::MessageBox(NULL, "CFence::ClearCommError: ", NULL, MB_OK);
continue;
}
if(!::WaitCommEvent(ptr->m_hCommPort, &dwEvtMask, &waitOV))
{
switch(::GetLastError())
{
case ERROR_IO_PENDING:
switch(::WaitForMultipleObjects(2, phHandle, FALSE, INFINITE))
{
case WAIT_TIMEOUT:
continue;
case WAIT_OBJECT_0 + 0:
goto THREAD_EXIT;
case WAIT_OBJECT_0 + 1:
if(!::GetOverlappedResult(ptr->m_hCommPort, &waitOV, &dwEvtMask, FALSE) )
{
CMessageBox::MessageBox(NULL, "CFence::WaitCommEvent: ", NULL, MB_OK);
continue;
}
break;
}
break;
default:
CMessageBox::MessageBox(NULL, "CFence::WaitCommEvent: ", NULL, MB_OK);
continue;
} // end of switch(::GetLastError())
} // end of if(!::WaitCommEvent(m_hCommPort, &dwEvtMask, &waitOV))
::GetCommModemStatus(ptr->m_hCommPort, &dwStatus);
if((dwEvtMask & EV_DSR) && (!(dwStatus & MS_DSR_ON)))
if(ptr->m_bEnable)
ptr->m_pApplication->AddFenceEvent(0);
if((dwEvtMask & EV_CTS) && (dwStatus & MS_CTS_ON))
if(ptr->m_bEnable)
ptr->m_pApplication->AddFenceEvent(1);
} // end of while(TRUE)
THREAD_EXIT:
::CloseHandle(waitOV.hEvent);
return 70UL;
}