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.
 
 
 
 
 

346 lines
10 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: LgAdvantechPCI1762.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of LgAdvantechPCI1762.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "LgAdvantechPCI1762.h"
CLgAdvantechPCI1762::CLgAdvantechPCI1762()
{
m_hDevice = NULL;
m_bEvent = FALSE;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgAdvantechPCI1762::CLgAdvantechPCI1762") );
}
CLgAdvantechPCI1762::~CLgAdvantechPCI1762()
{
Destroy();
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgAdvantechPCI1762::~CLgAdvantechPCI1762") );
}
BOOL CLgAdvantechPCI1762::Create( INT nDevice )
{
Destroy();
__try
{
// open device
DWORD dwResult = DRV_DeviceOpen( nDevice, &m_hDevice );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::Create"), _T("DRV_DeviceOpen failed with <%s>"), pErrorMessage );
return FALSE;
}
// enable event handling
PT_EnableEvent EventSetting;
EventSetting.EventType = ADS_EVT_INTERRUPT;
EventSetting.Enabled = TRUE;
EventSetting.Count = 1;
dwResult = DRV_EnableEvent( m_hDevice, &EventSetting );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::Create"), _T("DRV_EnableEvent failed with <%s>"), pErrorMessage );
return FALSE;
}
m_bEvent = TRUE;
}
__except( EXCEPTION_EXECUTE_HANDLER )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::Create"), _T("Exception 0x%08X"), _exception_code() );
return FALSE;
}
return TRUE;
}
void CLgAdvantechPCI1762::Destroy()
{
if( m_hDevice != NULL )
{
if( m_bEvent )
{
PT_EnableEvent EventSetting;
EventSetting.EventType = ADS_EVT_INTERRUPT;
EventSetting.Enabled = FALSE;
EventSetting.Count = 1;
DWORD dwResult = DRV_EnableEvent( m_hDevice, &EventSetting );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::Destroy"), _T("DRV_EnableEvent failed with <%s>"), pErrorMessage );
}
m_bEvent = FALSE;
}
DRV_DeviceClose( &m_hDevice );
m_hDevice = NULL;
}
}
BOOL CLgAdvantechPCI1762::WaitForEvent( DWORD dwTimeout )
{
if( !m_hDevice )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::WaitForEvent"), _T("Device handle is not valid") );
return FALSE;
}
USHORT usEventType = 0;
PT_CheckEvent ptCheckEvent;
ptCheckEvent.EventType = &usEventType;
ptCheckEvent.Milliseconds = dwTimeout;
if( DRV_CheckEvent( m_hDevice, &ptCheckEvent ) != SUCCESS )
return FALSE;
return TRUE;
}
BOOL CLgAdvantechPCI1762::SetDirectOutput( INT nBit, BOOL bOn )
{
if( !m_hDevice )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::SetDirectOutput"), _T("Device handle is not valid") );
return FALSE;
}
PT_DioWriteBit dioWriteBit;
dioWriteBit.port = nBit / 8;
dioWriteBit.bit = nBit % 8;
dioWriteBit.state = bOn;
DWORD dwResult = DRV_DioWriteBit( m_hDevice, &dioWriteBit );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::SetDirectOutput"), _T("DRV_DioWriteBit failed with <%s>"), pErrorMessage );
return FALSE;
}
return TRUE;
}
BOOL CLgAdvantechPCI1762::SetDirectOutputAll( DWORD dwMask, DWORD dwState )
{
if( !m_hDevice )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::SetDirectOutputAll"), _T("Device handle is not valid") );
return FALSE;
}
// write first part
PT_DioWritePortByte dioWritePortByte;
dioWritePortByte.port = 0;
dioWritePortByte.mask = (SHORT)(dwMask & 0x00FF);
dioWritePortByte.state = (SHORT)(dwState & 0x00FF);
DWORD dwResult = DRV_DioWritePortByte( m_hDevice, &dioWritePortByte );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::SetDirectOutputAll"), _T("DRV_DioWritePortByte for first part failed with <%s>"), pErrorMessage );
return FALSE;
}
// write second part
dioWritePortByte.port = 1;
dioWritePortByte.mask = (SHORT)( ( dwMask >> 8 ) & 0x00FF);
dioWritePortByte.state = (SHORT)( ( dwState >> 8 ) & 0x00FF);
dwResult = DRV_DioWritePortByte( m_hDevice, &dioWritePortByte );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::SetDirectOutputAll"), _T("DRV_DioWritePortByte for second part failed with <%s>"), pErrorMessage );
return FALSE;
}
return TRUE;
}
BOOL CLgAdvantechPCI1762::GetDirectOutput( INT nBit, PBOOL pOn )
{
if( !m_hDevice )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::GetDirectOutput"), _T("Device handle is not valid") );
return FALSE;
}
if( !pOn )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::GetDirectOutput"), _T("Passed NULL pointer for output") );
return FALSE;
}
USHORT state;
PT_DioGetCurrentDOBit dioGetCurrentDOBit;
dioGetCurrentDOBit.port = nBit / 8;
dioGetCurrentDOBit.bit = nBit % 8;
dioGetCurrentDOBit.state = &state;
DWORD dwResult = DRV_DioGetCurrentDOBit( m_hDevice, &dioGetCurrentDOBit );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::GetDirectOutput"), _T("DRV_DioGetCurrentDOBit failed with <%s>"), pErrorMessage );
return FALSE;
}
*pOn = state;
return TRUE;
}
BOOL CLgAdvantechPCI1762::GetDirectOutputAll( DWORD dwMask, PDWORD pState )
{
if( !m_hDevice )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::GetDirectOutputAll"), _T("Device handle is not valid") );
return FALSE;
}
if( !pState )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::GetDirectOutputAll"), _T("Passed NULL pointer for output") );
return FALSE;
}
USHORT state;
PT_DioGetCurrentDOByte dioGetCurrentDOByte;
dioGetCurrentDOByte.port = 0;
dioGetCurrentDOByte.value = &state;
DWORD dwResult = DRV_DioGetCurrentDOByte( m_hDevice, &dioGetCurrentDOByte );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::GetDirectOutputAll"), _T("DRV_DioGetCurrentDOByte for first part failed with <%s>"), pErrorMessage );
return FALSE;
}
*pState = (DWORD)state;
dioGetCurrentDOByte.port = 1;
dioGetCurrentDOByte.value = &state;
dwResult = DRV_DioGetCurrentDOByte( m_hDevice, &dioGetCurrentDOByte );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::GetDirectOutput2All"), _T("DRV_DioGetCurrentDOByte for second part failed with <%s>"), pErrorMessage );
return FALSE;
}
*pState |= (DWORD)(state << 8);
return TRUE;
}
BOOL CLgAdvantechPCI1762::GetDirectInput( INT nBit, PBOOL pOn )
{
if( !m_hDevice )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::GetDirectInput"), _T("Device handle is not valid") );
return FALSE;
}
if( !pOn )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::GetDirectInput"), _T("Passed NULL pointer for output") );
return FALSE;
}
USHORT state;
PT_DioReadBit dioReadBit;
dioReadBit.port = nBit / 8;
dioReadBit.bit = nBit % 8;
dioReadBit.state = &state;
DWORD dwResult = DRV_DioReadBit( m_hDevice, &dioReadBit );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::GetDirectInput"), _T("DRV_DioReadBit failed with <%s>"), pErrorMessage );
return FALSE;
}
*pOn = state;
return TRUE;
}
BOOL CLgAdvantechPCI1762::GetDirectInputAll( DWORD dwMask, PDWORD pState )
{
if( !m_hDevice )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::GetDirectInputAll"), _T("Device handle is not valid") );
return FALSE;
}
if( !pState )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgAdvantechPCI1762::GetDirectInputAll"), _T("Passed NULL pointer for output") );
return FALSE;
}
USHORT state;
PT_DioReadPortByte dioReadPortByte;
dioReadPortByte.port = 0;
dioReadPortByte.value = &state;
DWORD dwResult = DRV_DioReadPortByte( m_hDevice, &dioReadPortByte );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::SetDirectOutputAll"), _T("DRV_DioReadPortByte for first part failed with <%s>"), pErrorMessage );
return FALSE;
}
*pState = (DWORD)state;
dioReadPortByte.port = 1;
dioReadPortByte.value = &state;
dwResult = DRV_DioReadPortByte( m_hDevice, &dioReadPortByte );
if( dwResult != SUCCESS )
{
TCHAR pErrorMessage[MAX_PATH];
DRV_GetErrorMessage( dwResult, pErrorMessage );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgAdvantechPCI1762::SetDirectOutputAll"), _T("DRV_DioReadPortByte for second part failed with <%s>"), pErrorMessage );
return FALSE;
}
*pState |= (DWORD)(state << 8);
return TRUE;
}