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.
514 lines
11 KiB
514 lines
11 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: SocketRS232.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of SocketRS232.cpp
|
|
//----------------------------------------------------------------------------
|
|
// SocketRS232.cpp: implementation of the CSocketRS232 class.
|
|
//
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
#include "stdafx.h"
|
|
#include "SocketRS232.h"
|
|
#include "..\..\LgLib\Version.h"
|
|
|
|
#ifdef _DEBUG
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[]=__FILE__;
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
// Construction/Destruction
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
CSocketRS232::CSocketRS232()
|
|
{
|
|
m_hThread = NULL;
|
|
m_hEventQuit = NULL;
|
|
m_hEventStart = NULL;
|
|
m_hCommPort = INVALID_HANDLE_VALUE;
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketRS232::CSocketRS232") );
|
|
}
|
|
|
|
CSocketRS232::~CSocketRS232()
|
|
{
|
|
Destroy();
|
|
|
|
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CSocketRS232::~CSocketRS232") );
|
|
}
|
|
|
|
BOOL CSocketRS232::Create( LPCTSTR pPort, INT nBaudRate, INT nBitCount, INT nParity, INT nStopBit )
|
|
{
|
|
Destroy();
|
|
|
|
_tcscpy( m_Port.pPort, pPort );
|
|
m_Port.nBaudRate = nBaudRate;
|
|
m_Port.nBitCount = nBitCount;
|
|
m_Port.nParity = nParity;
|
|
m_Port.nStopBit = nStopBit;
|
|
|
|
if( !CreateCommPort() )
|
|
return FALSE;
|
|
|
|
m_hEventStart = ::CreateEvent( NULL, FALSE, FALSE, NULL );
|
|
if( !m_hEventStart )
|
|
return FALSE;
|
|
|
|
m_hEventQuit = ::CreateEvent( NULL, FALSE, FALSE, NULL );
|
|
if( !m_hEventQuit )
|
|
return FALSE;
|
|
|
|
DWORD dwThreadId;
|
|
m_hThread = ::CreateThread( NULL, 0UL, ThreadProcSocketRS232, this, 0UL, &dwThreadId );
|
|
if( !m_hThread )
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
void CSocketRS232::Destroy()
|
|
{
|
|
if( m_hThread )
|
|
{
|
|
::SetEvent( m_hEventQuit );
|
|
::WaitForSingleObject( m_hThread, INFINITE );
|
|
::CloseHandle( m_hThread );
|
|
m_hThread = NULL;
|
|
}
|
|
|
|
if( m_hEventQuit )
|
|
{
|
|
::CloseHandle( m_hEventQuit );
|
|
m_hEventQuit = NULL;
|
|
}
|
|
|
|
if( m_hEventStart )
|
|
{
|
|
::CloseHandle( m_hEventStart );
|
|
m_hEventStart = NULL;
|
|
}
|
|
|
|
if( m_hCommPort != INVALID_HANDLE_VALUE )
|
|
{
|
|
::CloseHandle( m_hCommPort );
|
|
m_hCommPort = INVALID_HANDLE_VALUE;
|
|
}
|
|
}
|
|
|
|
BOOL CSocketRS232::CreateCommPort()
|
|
{
|
|
if( m_hCommPort != INVALID_HANDLE_VALUE )
|
|
::CloseHandle( m_hCommPort );
|
|
|
|
m_hCommPort = ::CreateFile( m_Port.pPort, GENERIC_READ | GENERIC_WRITE,
|
|
0UL, NULL, OPEN_EXISTING,
|
|
FILE_FLAG_OVERLAPPED, NULL );
|
|
|
|
if( m_hCommPort == INVALID_HANDLE_VALUE )
|
|
{
|
|
m_hCommPort = NULL;
|
|
return FALSE;
|
|
}
|
|
|
|
DCB dcb;
|
|
::ZeroMemory( &dcb, sizeof( DCB ) );
|
|
dcb.DCBlength = sizeof( DCB );
|
|
::GetCommState( m_hCommPort, &dcb );
|
|
|
|
dcb.BaudRate = m_Port.nBaudRate;
|
|
dcb.ByteSize = m_Port.nBitCount;
|
|
dcb.Parity = m_Port.nParity;
|
|
dcb.StopBits = m_Port.nStopBit;
|
|
dcb.EvtChar = 0;
|
|
dcb.fParity = ( m_Port.nParity != NOPARITY );
|
|
dcb.fOutxCtsFlow = FALSE;
|
|
dcb.fOutxDsrFlow = FALSE;
|
|
dcb.fDtrControl = DTR_CONTROL_DISABLE;
|
|
dcb.fDsrSensitivity = FALSE;
|
|
dcb.fOutX = FALSE;
|
|
dcb.fInX = FALSE;
|
|
dcb.fErrorChar = FALSE;
|
|
dcb.fNull = FALSE;
|
|
dcb.fRtsControl = RTS_CONTROL_DISABLE;
|
|
dcb.fAbortOnError = FALSE;
|
|
|
|
if( !::SetupComm( m_hCommPort, MAX_BUFFER_SIZE, MAX_BUFFER_SIZE ) )
|
|
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;
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR |
|
|
PURGE_TXABORT | PURGE_RXABORT );
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
DWORD CSocketRS232::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 hEventsStart[] = { m_hEventQuit, m_hEventStart };
|
|
HANDLE hEventsWrite[] = { m_hEventQuit, ovWrite.hEvent };
|
|
HANDLE hEventsWait[] = { m_hEventQuit, ovWait.hEvent };
|
|
HANDLE hEventsRead[] = { m_hEventQuit, ovRead.hEvent };
|
|
|
|
BYTE pBuffer[ MAX_BUFFER_SIZE ];
|
|
DWORD nCount;
|
|
DWORD dwTime;
|
|
DWORD dwStartTime;
|
|
DWORD dwSubTime;
|
|
DWORD dwCommMask = 0;
|
|
DWORD dwWaitTime;
|
|
DWORD dwPauseTime;
|
|
|
|
INT nError;
|
|
|
|
INT nStage = CMD_COMMAND;
|
|
|
|
while( TRUE )
|
|
{
|
|
switch( ::WaitForMultipleObjects( 2, hEventsStart, FALSE, nStage == CMD_STOP ? INFINITE : 0UL ) )
|
|
{
|
|
case WAIT_OBJECT_0:
|
|
|
|
goto THREAD_QUIT;
|
|
|
|
case WAIT_OBJECT_0+1:
|
|
|
|
if( nStage == CMD_STOP )
|
|
nStage = CMD_COMMAND;
|
|
|
|
break;
|
|
}
|
|
|
|
switch( nStage )
|
|
{
|
|
case CMD_COMMAND:
|
|
|
|
switch( ::WaitForSingleObject( m_hEventQuit, 0UL ) )
|
|
{
|
|
case WAIT_OBJECT_0:
|
|
|
|
goto THREAD_QUIT;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
nStage = OnCommand();
|
|
|
|
break;
|
|
}
|
|
|
|
break;
|
|
|
|
case CMD_PAUSE:
|
|
|
|
nStage = OnPause( dwPauseTime );
|
|
|
|
switch( ::WaitForMultipleObjects( 2, hEventsStart, FALSE, dwPauseTime ) )
|
|
{
|
|
case WAIT_OBJECT_0:
|
|
|
|
goto THREAD_QUIT;
|
|
|
|
case WAIT_OBJECT_0+1:
|
|
|
|
nStage = CMD_COMMAND;
|
|
|
|
break;
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
case CMD_WRITE:
|
|
|
|
OnBeforeWrite( pBuffer, nCount );
|
|
|
|
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
|
|
{
|
|
nError = 0;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
if( dwCommMask != EV_TXEMPTY )
|
|
::SetCommMask( m_hCommPort, dwCommMask = EV_TXEMPTY );
|
|
|
|
dwWaitTime = ((DWORD)m_Port.nBaudRate * (DWORD)nCount ) >> 8;
|
|
|
|
if( !::WriteFile( m_hCommPort, pBuffer, nCount, &dwWritten, &ovWrite ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( 2, hEventsWrite, FALSE, dwWaitTime ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0:
|
|
goto THREAD_QUIT;
|
|
case WAIT_OBJECT_0 + 1:
|
|
case WAIT_TIMEOUT:
|
|
::GetOverlappedResult( m_hCommPort, &ovWrite, &dwWritten, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
nError = 1;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
if( nCount != dwWritten )
|
|
{
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( 2, hEventsWait, FALSE, dwWaitTime ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0:
|
|
goto THREAD_QUIT;
|
|
case WAIT_OBJECT_0 + 1:
|
|
case WAIT_TIMEOUT:
|
|
::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
nError = 2;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
if( !( dwEvtMask & EV_TXEMPTY ) )
|
|
{
|
|
nError = 3;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
}
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
|
|
|
|
if( dwWritten != nCount )
|
|
{
|
|
nError = 4;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
nStage = OnAfterWrite();
|
|
|
|
break;
|
|
|
|
case CMD_READ:
|
|
|
|
OnBeforeRead( nCount, dwTime );
|
|
::ZeroMemory( pBuffer, MAX_BUFFER_SIZE );
|
|
|
|
dwStartTime = ::GetTickCount();
|
|
|
|
if( dwCommMask != EV_RXCHAR )
|
|
::SetCommMask( m_hCommPort, dwCommMask = EV_RXCHAR );
|
|
|
|
while( TRUE )
|
|
{
|
|
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
|
|
{
|
|
nError = 5;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
|
|
if( comstat.cbInQue < nCount && ( dwSubTime = ( ::GetTickCount() - dwStartTime ) ) < dwTime )
|
|
{
|
|
dwEvtMask = 0UL;
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &ovWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( 2, hEventsWait, FALSE, dwSubTime ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0:
|
|
goto THREAD_QUIT;
|
|
case WAIT_TIMEOUT:
|
|
::CancelIo( m_hCommPort );
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &ovWait, &dwEvtMask, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
nError = 6;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
}
|
|
else break;
|
|
}
|
|
|
|
if( !::ReadFile( m_hCommPort, pBuffer, comstat.cbInQue, &dwRead, &ovRead ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
switch( ::WaitForMultipleObjects( sizeof(hEventsRead)/sizeof(*hEventsRead), hEventsRead, FALSE, 0 ) )
|
|
{
|
|
case WAIT_OBJECT_0 + 0:
|
|
goto THREAD_QUIT;
|
|
case WAIT_TIMEOUT:
|
|
::CancelIo( m_hCommPort );
|
|
case WAIT_OBJECT_0 + 1:
|
|
if( !::GetOverlappedResult( m_hCommPort, &ovRead, &dwRead, FALSE ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_INCOMPLETE:
|
|
::PurgeComm( m_hCommPort, PURGE_RXABORT );
|
|
break;
|
|
case ERROR_OPERATION_ABORTED:
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
nError = 7;
|
|
goto RECREATE_COMMPORT;
|
|
}
|
|
}
|
|
|
|
nStage = OnAfterRead( pBuffer, dwRead );
|
|
|
|
break;
|
|
|
|
case CMD_WAIT:
|
|
|
|
OnBeforeWait( dwTime, dwEvtMask );
|
|
|
|
nStage = OnAfterWait( dwEvtMask );
|
|
|
|
break;
|
|
}
|
|
|
|
continue;
|
|
|
|
RECREATE_COMMPORT:
|
|
TRACE3( "Recreate port (%d-%d)(%d)\n", nCount, dwWaitTime, nError );
|
|
OnRecreate( nError );
|
|
if( !CreateCommPort() )
|
|
goto THREAD_QUIT;
|
|
}
|
|
|
|
THREAD_QUIT:
|
|
|
|
::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
|
|
|
|
::CloseHandle( ovWrite.hEvent );
|
|
::CloseHandle( ovWait.hEvent );
|
|
::CloseHandle( ovRead.hEvent );
|
|
|
|
return 0;
|
|
}
|
|
|
|
int CSocketRS232::OnCommand()
|
|
{
|
|
return CMD_COMMAND;
|
|
}
|
|
|
|
void CSocketRS232::OnRecreate( int nError )
|
|
{
|
|
}
|
|
|
|
void CSocketRS232::OnBeforeWrite( LPBYTE pBuffer, DWORD& nCount )
|
|
{
|
|
nCount = 0UL;
|
|
}
|
|
|
|
int CSocketRS232::OnAfterWrite()
|
|
{
|
|
return CMD_COMMAND;
|
|
}
|
|
|
|
void CSocketRS232::OnBeforeWait( DWORD& dwTime, DWORD& dwEvent )
|
|
{
|
|
dwTime = 0UL;
|
|
dwEvent = 0UL;
|
|
}
|
|
|
|
int CSocketRS232::OnAfterWait( DWORD dwEvent )
|
|
{
|
|
return CMD_COMMAND;
|
|
}
|
|
|
|
void CSocketRS232::OnBeforeRead( DWORD& nCount, DWORD& dwTime )
|
|
{
|
|
nCount = 0UL;
|
|
dwTime = 0UL;
|
|
}
|
|
|
|
int CSocketRS232::OnAfterRead( LPBYTE pBuffer, DWORD nCount )
|
|
{
|
|
return CMD_COMMAND;
|
|
}
|
|
|
|
int CSocketRS232::OnPause( DWORD &dwTime )
|
|
{
|
|
dwTime = 0UL;
|
|
return CMD_COMMAND;
|
|
}
|