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.
732 lines
20 KiB
732 lines
20 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: RS232.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of Rs232.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "StdAfx.h"
|
|
#include "..\Version.h"
|
|
#include "RS232.h"
|
|
|
|
CConnectionRS232::CConnectionRS232()
|
|
{
|
|
m_hCommPort = INVALID_HANDLE_VALUE;
|
|
m_dwCommMask = 0;
|
|
m_hEventQuit = NULL;
|
|
|
|
::ZeroMemory( &m_ovWrite, sizeof( OVERLAPPED ) );
|
|
::ZeroMemory( &m_ovWriteWait, sizeof( OVERLAPPED ) );
|
|
::ZeroMemory( &m_ovRead, sizeof( OVERLAPPED ) );
|
|
::ZeroMemory( &m_ovReadWait, sizeof( OVERLAPPED ) );
|
|
}
|
|
|
|
CConnectionRS232::~CConnectionRS232()
|
|
{
|
|
Destroy();
|
|
}
|
|
|
|
BOOL CConnectionRS232::Create( LPCTSTR pPort, INT nBaudRate, INT nBitCount, INT nParity, INT nStopBit, DWORD dwReadBufferSize, DWORD dwWriteBufferSize )
|
|
{
|
|
Destroy();
|
|
|
|
m_CreateParams.dwReadBufferSize = dwReadBufferSize;
|
|
m_CreateParams.dwWriteBufferSize = dwWriteBufferSize;
|
|
m_CreateParams.nBaudRate = nBaudRate;
|
|
m_CreateParams.nBitCount = nBitCount;
|
|
m_CreateParams.nParity = nParity;
|
|
m_CreateParams.nStopBit = nStopBit;
|
|
_tcscpy( m_CreateParams.pPort, pPort );
|
|
|
|
m_hEventQuit = ::CreateEvent( NULL, FALSE, FALSE, NULL );
|
|
if( !m_hEventQuit )
|
|
return FALSE;
|
|
|
|
m_ovReadWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
if( !m_ovReadWait.hEvent )
|
|
return FALSE;
|
|
|
|
m_ovWriteWait.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
if( !m_ovWriteWait.hEvent )
|
|
return FALSE;
|
|
|
|
m_ovRead.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
if( !m_ovRead.hEvent )
|
|
return FALSE;
|
|
|
|
m_ovWrite.hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL );
|
|
if( !m_ovWrite.hEvent )
|
|
return FALSE;
|
|
|
|
return Create();
|
|
}
|
|
|
|
BOOL CConnectionRS232::Create()
|
|
{
|
|
// close previous communication port handle
|
|
if( m_hCommPort != INVALID_HANDLE_VALUE )
|
|
{
|
|
::CloseHandle( m_hCommPort );
|
|
m_hCommPort = INVALID_HANDLE_VALUE;
|
|
}
|
|
|
|
// open new one
|
|
m_hCommPort = ::CreateFile( m_CreateParams.pPort, GENERIC_READ | GENERIC_WRITE,
|
|
0UL, NULL, OPEN_EXISTING,
|
|
FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, NULL );
|
|
|
|
if( m_hCommPort == INVALID_HANDLE_VALUE )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CConnectionRS232::Create"), _T("CreateFile(%s) failed with 0x%08X"), m_CreateParams.pPort, GetLastError() );
|
|
return FALSE;
|
|
}
|
|
|
|
DCB dcb;
|
|
::ZeroMemory( &dcb, sizeof( DCB ) );
|
|
dcb.DCBlength = sizeof( DCB );
|
|
::GetCommState( m_hCommPort, &dcb );
|
|
|
|
dcb.BaudRate = m_CreateParams.nBaudRate;
|
|
dcb.ByteSize = m_CreateParams.nBitCount;
|
|
dcb.Parity = m_CreateParams.nParity;
|
|
dcb.StopBits = m_CreateParams.nStopBit;
|
|
dcb.EvtChar = 0;
|
|
dcb.fParity = ( m_CreateParams.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.fRtsControl = RTS_CONTROL_ENABLE;
|
|
dcb.fRtsControl = RTS_CONTROL_TOGGLE; // specially for PROMAG USB->RS485 converter
|
|
dcb.fAbortOnError = FALSE;
|
|
dcb.fBinary = TRUE;
|
|
dcb.fTXContinueOnXoff = TRUE;
|
|
|
|
// setup communication port buffers
|
|
if( !::SetupComm( m_hCommPort, m_CreateParams.dwReadBufferSize, m_CreateParams.dwWriteBufferSize ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::Create"), _T("SetupComm failed with 0x%08X"), GetLastError() );
|
|
return FALSE;
|
|
}
|
|
|
|
// setup communication port parameters
|
|
if( !::SetCommState( m_hCommPort, &dcb ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::Create"), _T("SetCommState failed with 0x%08X"), GetLastError() );
|
|
return FALSE;
|
|
}
|
|
|
|
COMMTIMEOUTS ct;
|
|
ct.ReadIntervalTimeout = 0UL;
|
|
ct.ReadTotalTimeoutMultiplier = 0UL;
|
|
ct.ReadTotalTimeoutConstant = 0UL;
|
|
ct.WriteTotalTimeoutMultiplier = 0UL;
|
|
ct.WriteTotalTimeoutConstant = 0UL;
|
|
|
|
// setup communication port timeouts
|
|
if( !::SetCommTimeouts( m_hCommPort, &ct ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::Create"), _T("SetCommTimeouts failed with 0x%08X"), GetLastError() );
|
|
return FALSE;
|
|
}
|
|
|
|
// clear communication port
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT );
|
|
|
|
// setup communication port timeouts
|
|
// ::EscapeCommFunction( m_hCommPort, SETRTS );
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
void CConnectionRS232::Destroy()
|
|
{
|
|
if( m_hEventQuit )
|
|
{
|
|
::CloseHandle( m_hEventQuit );
|
|
m_hEventQuit = NULL;
|
|
}
|
|
|
|
if( m_hCommPort != INVALID_HANDLE_VALUE )
|
|
{
|
|
::CloseHandle( m_hCommPort );
|
|
m_hCommPort = INVALID_HANDLE_VALUE;
|
|
}
|
|
|
|
if( m_ovReadWait.hEvent )
|
|
{
|
|
::CloseHandle( m_ovReadWait.hEvent );
|
|
m_ovReadWait.hEvent = NULL;
|
|
}
|
|
|
|
if( m_ovWriteWait.hEvent )
|
|
{
|
|
::CloseHandle( m_ovWriteWait.hEvent );
|
|
m_ovWriteWait.hEvent = NULL;
|
|
}
|
|
|
|
if( m_ovWrite.hEvent )
|
|
{
|
|
::CloseHandle( m_ovWrite.hEvent );
|
|
m_ovWrite.hEvent = NULL;
|
|
}
|
|
|
|
if( m_ovRead.hEvent )
|
|
{
|
|
::CloseHandle( m_ovRead.hEvent );
|
|
m_ovRead.hEvent = NULL;
|
|
}
|
|
}
|
|
|
|
BOOL CConnectionRS232::SetCommState( LPDCB pDCB )
|
|
{
|
|
return ::SetCommState( m_hCommPort, pDCB );
|
|
}
|
|
|
|
BOOL CConnectionRS232::GetCommState( LPDCB pDCB )
|
|
{
|
|
return ::GetCommState( m_hCommPort, pDCB );
|
|
}
|
|
|
|
BOOL CConnectionRS232::ClearCommError( LPDWORD lpErrors, LPCOMSTAT lpStat )
|
|
{
|
|
return ::ClearCommError( m_hCommPort, lpErrors, lpStat );
|
|
}
|
|
|
|
DWORD CConnectionRS232::Read( PBYTE pBuffer, DWORD dwSize, DWORD dwTimeout )
|
|
{
|
|
return CommRead( pBuffer, dwSize, dwTimeout );;
|
|
}
|
|
|
|
DWORD CConnectionRS232::Write( PBYTE pBuffer, DWORD dwSize, DWORD dwTimeout )
|
|
{
|
|
return CommWrite( pBuffer, dwSize, dwTimeout );
|
|
}
|
|
|
|
BOOL CConnectionRS232::SetRTS( BOOL bOn )
|
|
{
|
|
::EscapeCommFunction( m_hCommPort, bOn ? SETRTS : CLRRTS );
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CConnectionRS232::SetDTR( BOOL bOn )
|
|
{
|
|
::EscapeCommFunction( m_hCommPort, bOn ? SETDTR : CLRDTR );
|
|
return TRUE;
|
|
}
|
|
|
|
DWORD CConnectionRS232::CommRead( PBYTE pBuffer, DWORD dwSize, DWORD dwTimeout )
|
|
{
|
|
__try
|
|
{
|
|
// used for retrieve port information
|
|
COMSTAT comstat;
|
|
// placeholder for result
|
|
DWORD dwRead = 0UL;
|
|
// event array useable in Wait... functions
|
|
HANDLE hEventsWait[] = { m_hEventQuit, m_ovReadWait.hEvent };
|
|
HANDLE hEventsRead[] = { m_hEventQuit, m_ovRead.hEvent };
|
|
|
|
m_ovReadWait.Internal = m_ovReadWait.InternalHigh = m_ovReadWait.Offset = m_ovReadWait.OffsetHigh = 0;
|
|
m_ovRead.Internal = m_ovRead.InternalHigh = m_ovRead.Offset = m_ovRead.OffsetHigh = 0;
|
|
|
|
// if previous operation was not read then re-set event mask
|
|
if( m_dwCommMask != EV_RXCHAR )
|
|
::SetCommMask( m_hCommPort, m_dwCommMask = EV_RXCHAR );
|
|
|
|
// remember read start time
|
|
DWORD dwStartTime = ::GetTickCount();
|
|
|
|
while( TRUE )
|
|
{
|
|
DWORD dwErrors;
|
|
|
|
// check communication port for errors
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & CE_RXOVER )
|
|
::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK )
|
|
::ClearCommBreak( m_hCommPort );
|
|
}
|
|
else
|
|
{
|
|
// log error
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::CommRead"), _T("ClearCommError failed") );
|
|
// recreate commport and continue reading
|
|
Create();
|
|
}
|
|
|
|
// calculate time eplaced since start of read
|
|
DWORD dwSubTime = ::GetTickCount() - dwStartTime;
|
|
|
|
// check amount of bytes in input buffer and remaining time
|
|
if( (comstat.cbInQue < dwSize) && (dwSubTime < dwTimeout) )
|
|
{
|
|
DWORD dwEvtMask = 0UL;
|
|
DWORD dwWaitResult;
|
|
|
|
// wait for event EV_RXCHAR
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &m_ovReadWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
// wait for overlapped completion
|
|
switch( ::WaitForMultipleObjects( 2, hEventsWait, FALSE, dwTimeout - dwSubTime ) )
|
|
{
|
|
// request to abort
|
|
case WAIT_OBJECT_0 + 0:
|
|
// abort waiting
|
|
::CancelIo( m_hCommPort );
|
|
// check result (nessessary by default)
|
|
::GetOverlappedResult( m_hCommPort, &m_ovReadWait, &dwWaitResult, FALSE );
|
|
goto READ_ABORT;
|
|
|
|
// timeout eplaced but event did not received
|
|
case WAIT_TIMEOUT:
|
|
::CancelIo( m_hCommPort );
|
|
|
|
// event received (or timeout eplaced - see above)
|
|
case WAIT_OBJECT_0 + 1:
|
|
::GetOverlappedResult( m_hCommPort, &m_ovReadWait, &dwWaitResult, FALSE );
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
// log error
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::CommRead"), _T("WaitCommEvent failed with 0x%08X"), dwError );
|
|
// recreate commport and continue reading
|
|
Create();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else break;
|
|
}
|
|
|
|
// actual data reading (from internal buffers)
|
|
if( !::ReadFile( m_hCommPort, pBuffer, min( dwSize, comstat.cbInQue), &dwRead, &m_ovRead ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
// wait for overlapped completion
|
|
switch( ::WaitForMultipleObjects( 2, hEventsRead, FALSE, 0 ) )
|
|
{
|
|
// request to abort
|
|
case WAIT_OBJECT_0 + 0:
|
|
// stop read operation
|
|
::PurgeComm( m_hCommPort, PURGE_RXABORT );
|
|
// abort reading
|
|
::CancelIo( m_hCommPort );
|
|
// check result for receive number of bytes have readed to this time
|
|
::GetOverlappedResult( m_hCommPort, &m_ovRead, &dwRead, FALSE );
|
|
goto READ_ABORT;
|
|
|
|
// timeout eplaced but event did not received
|
|
case WAIT_TIMEOUT:
|
|
::CancelIo( m_hCommPort );
|
|
|
|
// event received (or timeout eplaced - see above)
|
|
case WAIT_OBJECT_0 + 1:
|
|
// check result of operation
|
|
if( !::GetOverlappedResult( m_hCommPort, &m_ovRead, &dwRead, FALSE ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
// stop read operation
|
|
case ERROR_IO_INCOMPLETE:
|
|
::PurgeComm( m_hCommPort, PURGE_RXABORT );
|
|
break;
|
|
// operation was aborted by something
|
|
case ERROR_OPERATION_ABORTED:
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
// log error
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::CommRead"), _T("ReadFile failed with 0x%08X"), dwError );
|
|
// recreate commport and continue reading
|
|
Create();
|
|
break;
|
|
}
|
|
}
|
|
|
|
READ_ABORT:
|
|
return dwRead;
|
|
}
|
|
__except( EXCEPTION_EXECUTE_HANDLER )
|
|
{
|
|
// log error
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CConnectionRS232::CommRead"), _T("Exception 0x%08X"), _exception_code() );
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
DWORD CConnectionRS232::CommWrite( PBYTE pBuffer, DWORD dwSize, DWORD dwTimeout )
|
|
/*
|
|
{
|
|
__try
|
|
{
|
|
// used for retrieve port information
|
|
COMSTAT comstat;
|
|
DWORD dwErrors;
|
|
// placeholder for result
|
|
DWORD dwWrite = 0UL;
|
|
// placeholder for event mask
|
|
DWORD dwEvtMask = 0UL;
|
|
// event array useable in Wait... functions
|
|
// HANDLE hEventsWait[] = { m_hEventQuit, m_ovWriteWait.hEvent };
|
|
// HANDLE hEventsWrite[] = { m_hEventQuit, m_ovWrite.hEvent };
|
|
|
|
m_ovWriteWait.Internal = m_ovWriteWait.InternalHigh = m_ovWriteWait.Offset = m_ovWriteWait.OffsetHigh = 0;
|
|
m_ovWrite.Internal = m_ovWrite.InternalHigh = m_ovWrite.Offset = m_ovWrite.OffsetHigh = 0;
|
|
|
|
// check communication port for errors
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
}
|
|
else
|
|
{
|
|
// recreate commport and continue reading
|
|
Create();
|
|
}
|
|
|
|
// if previous operation was not Write then re-set event mask
|
|
if( m_dwCommMask != EV_TXEMPTY )
|
|
{
|
|
m_dwCommMask = EV_TXEMPTY;
|
|
::SetCommMask( m_hCommPort, EV_TXEMPTY );
|
|
}
|
|
|
|
DWORD dwStartTime = ::GetTickCount();
|
|
DWORD dwWaitTime;
|
|
DWORD dwEplacedTime;
|
|
BOOL bWaitEstablished = FALSE;
|
|
while( !bWaitEstablished )
|
|
{
|
|
// wait for event EV_TXEMPTY
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &m_ovWriteWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
bWaitEstablished = TRUE;
|
|
break;
|
|
|
|
default:
|
|
Create();
|
|
break;
|
|
}
|
|
}
|
|
|
|
// calc eplaced time
|
|
dwEplacedTime = ::GetTickCount() - dwStartTime;
|
|
if( dwEplacedTime > dwTimeout )
|
|
break;
|
|
}
|
|
|
|
// can't begin write
|
|
if( !bWaitEstablished )
|
|
return 0;
|
|
|
|
// actual data writing
|
|
if( !::WriteFile( m_hCommPort, pBuffer, dwSize, &dwWrite, &m_ovWrite ) )
|
|
{
|
|
// HANDLE hEventsWait[] = { m_hEventQuit, m_ovWriteWait.hEvent };
|
|
HANDLE hEventsWrite[] = { m_hEventQuit, m_ovWrite.hEvent };
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_IO_PENDING:
|
|
// store current time
|
|
dwEplacedTime = GetTickCount() - dwStartTime;
|
|
|
|
// maximum wait timeout
|
|
dwWaitTime = ( dwTimeout >= dwEplacedTime ) ? dwTimeout - dwEplacedTime : 0UL;
|
|
|
|
// wait for overlapped completion
|
|
switch( ::WaitForMultipleObjects( 2, hEventsWrite, FALSE, dwWaitTime ) )
|
|
{
|
|
default:
|
|
LGTRACE( "Write pending wait error\n" );
|
|
break;
|
|
|
|
// request to abort
|
|
case WAIT_OBJECT_0 + 0:
|
|
// stop write operation
|
|
::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_TXCLEAR );
|
|
// abort writing
|
|
::CancelIo( m_hCommPort );
|
|
// check result for receive number of bytes have Writeed to this time
|
|
::GetOverlappedResult( m_hCommPort, &m_ovWrite, &dwWrite, FALSE );
|
|
goto WRITE_ABORT;
|
|
|
|
// timeout eplaced but event did not received
|
|
case WAIT_TIMEOUT:
|
|
LGTRACE( "Write timeout\n" );
|
|
::CancelIo( m_hCommPort );
|
|
|
|
// event received (or timeout eplaced - see above)
|
|
case WAIT_OBJECT_0 + 1:
|
|
// check result of operation
|
|
if( !::GetOverlappedResult( m_hCommPort, &m_ovWrite, &dwWrite, FALSE ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
// stop write operation
|
|
case ERROR_IO_INCOMPLETE:
|
|
// stop write operation
|
|
::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_TXCLEAR );
|
|
// abort writing
|
|
::CancelIo( m_hCommPort );
|
|
break;
|
|
// operation was aborted by something
|
|
case ERROR_OPERATION_ABORTED:
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
LGTRACE( "Error Write, %d (%p, %p, %p) ", GetLastError(), m_hEventQuit, hEventsWrite[0], hEventsWrite[1] );
|
|
// recreate commport and continue writeing
|
|
Create();
|
|
break;
|
|
}
|
|
}
|
|
|
|
WRITE_ABORT:
|
|
// store current time
|
|
dwEplacedTime = GetTickCount() - dwStartTime;
|
|
|
|
// maximum wait timeout
|
|
dwWaitTime = ( dwTimeout >= dwEplacedTime ) ? dwTimeout - dwEplacedTime : 0UL;
|
|
|
|
DWORD dwWaitResult;
|
|
|
|
HANDLE hEventsWait[] = { m_hEventQuit, m_ovWriteWait.hEvent };
|
|
// HANDLE hEventsWrite[] = { m_hEventQuit, m_ovWrite.hEvent };
|
|
// wait for end of writing
|
|
switch( ::WaitForMultipleObjects( 2, hEventsWait, FALSE, dwWaitTime ) )
|
|
{
|
|
default:
|
|
LGTRACE( "ErrorWait, %d (%p, %p, %p) ", GetLastError(), m_hEventQuit, hEventsWait[0], hEventsWait[1] );
|
|
break;
|
|
|
|
// request to abort
|
|
case WAIT_OBJECT_0 + 0:
|
|
// timeout eplaced but event did not received
|
|
case WAIT_TIMEOUT:
|
|
// abort waiting
|
|
::CancelIo( m_hCommPort );
|
|
// clear result
|
|
dwWrite = 0UL;
|
|
|
|
// event received (or timeout eplaced - see above)
|
|
case WAIT_OBJECT_0 + 1:
|
|
// check result (nessessary by default)
|
|
if( !::GetOverlappedResult( m_hCommPort, &m_ovWriteWait, &dwWaitResult, FALSE ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
// stop write operation
|
|
case ERROR_IO_INCOMPLETE:
|
|
// stop write operation
|
|
::PurgeComm( m_hCommPort, PURGE_TXABORT | PURGE_TXCLEAR );
|
|
// abort writing
|
|
::CancelIo( m_hCommPort );
|
|
break;
|
|
// operation was aborted by something
|
|
case ERROR_OPERATION_ABORTED:
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
// check write result
|
|
if( ( dwEvtMask & EV_TXEMPTY ) != EV_TXEMPTY )
|
|
{
|
|
LGTRACE( "[%p] NO TXEMPTY EVENT (%d)\n", this, dwWrite );
|
|
dwWrite = 0UL;
|
|
}
|
|
|
|
return dwWrite;
|
|
}
|
|
__except(1)
|
|
{
|
|
MessageBox( NULL, "Error in CConnectionRS232::Write", "Error in CConnectionRS232::Write", MB_OK );
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
*/
|
|
{
|
|
m_dwCommMask = EV_TXEMPTY;
|
|
|
|
// store operation's start time
|
|
DWORD dwStartTime = ::GetTickCount();
|
|
|
|
// set comm wait mask
|
|
if( !::SetCommMask( m_hCommPort, EV_TXEMPTY ) )
|
|
return 0;
|
|
|
|
// check internal buffers and errors
|
|
COMSTAT comstat;
|
|
DWORD dwErrors;
|
|
if( ::ClearCommError( m_hCommPort, &dwErrors, &comstat ) )
|
|
{
|
|
// check for UART errors
|
|
if( dwErrors & CE_RXOVER ) ::PurgeComm( m_hCommPort, PURGE_RXCLEAR );
|
|
if( dwErrors & CE_BREAK ) ::ClearCommBreak( m_hCommPort );
|
|
// if port did not finish to transmit previous packet
|
|
if( comstat.cbOutQue )
|
|
{
|
|
// cancel it
|
|
::CancelIo( m_hCommPort );
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
|
|
}
|
|
}
|
|
else
|
|
// error occured
|
|
return 0;
|
|
|
|
|
|
// establish wait operation
|
|
DWORD dwEvtMask = 0;
|
|
if( !::WaitCommEvent( m_hCommPort, &dwEvtMask, &m_ovWriteWait ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
if( dwError != ERROR_IO_PENDING )
|
|
{
|
|
// error occured
|
|
SetLastError( dwError );
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// write packet
|
|
DWORD dwWritten = 0;
|
|
if( !::WriteFile( m_hCommPort, pBuffer, dwSize, &dwWritten, &m_ovWrite ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
if( dwError != ERROR_IO_PENDING )
|
|
{
|
|
// cancel wait operation
|
|
::SetCommMask( m_hCommPort, 0 );
|
|
// error occured
|
|
::SetLastError( dwError );
|
|
return 0;
|
|
}
|
|
|
|
// wait for completion
|
|
HANDLE hEvents[] = { m_ovWrite.hEvent, m_hEventQuit };
|
|
DWORD dwOverlappedWriteResult = 0;
|
|
switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, dwTimeout ) )
|
|
{
|
|
// if write complette
|
|
case WAIT_OBJECT_0:
|
|
if( !::GetOverlappedResult( m_hCommPort, &m_ovWrite, &dwOverlappedWriteResult, FALSE ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
// cancel write operation
|
|
::CancelIo( m_hCommPort );
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
|
|
// cancel wait operation
|
|
::SetCommMask( m_hCommPort, 0 );
|
|
// error occured
|
|
if( dwError != ERROR_IO_INCOMPLETE )
|
|
::SetLastError( dwError );
|
|
return 0;
|
|
}
|
|
// store amount of written bytes
|
|
dwWritten = dwOverlappedWriteResult;
|
|
break;
|
|
|
|
// if write canceled or error occured
|
|
default:
|
|
case WAIT_OBJECT_0+1:
|
|
// cancel write operation
|
|
::CancelIo( m_hCommPort );
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
|
|
// cancel wait operation
|
|
::SetCommMask( m_hCommPort, 0 );
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// calculate eplaced time
|
|
DWORD dwEplacedTime = ::GetTickCount() - dwStartTime;
|
|
// calculate remaining time
|
|
DWORD dwRemaingTime = ( dwTimeout > dwEplacedTime ) ? ( dwTimeout - dwEplacedTime ) : 0UL;
|
|
|
|
// wait for UART's TX is EMPTY
|
|
HANDLE hEvents[] = { m_ovWriteWait.hEvent, m_hEventQuit };
|
|
DWORD dwOverlappedWaitResult = 0;
|
|
switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, dwRemaingTime ) )
|
|
{
|
|
// if write complette
|
|
case WAIT_OBJECT_0:
|
|
if( !::GetOverlappedResult( m_hCommPort, &m_ovWriteWait, &dwOverlappedWaitResult, FALSE ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
// cancel write operation
|
|
::CancelIo( m_hCommPort );
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
|
|
// cancel wait operation
|
|
::SetCommMask( m_hCommPort, 0 );
|
|
// error occured
|
|
if( dwError != ERROR_IO_INCOMPLETE )
|
|
::SetLastError( dwError );
|
|
return 0;
|
|
}
|
|
break;
|
|
|
|
// if wait canceled or error occured
|
|
default:
|
|
case WAIT_OBJECT_0+1:
|
|
// cancel write operation
|
|
::CancelIo( m_hCommPort );
|
|
::PurgeComm( m_hCommPort, PURGE_TXCLEAR | PURGE_TXABORT );
|
|
// cancel wait operation
|
|
::SetCommMask( m_hCommPort, 0 );
|
|
return 0;
|
|
}
|
|
|
|
return dwWritten;
|
|
}
|
|
|
|
BOOL CConnectionRS232::Flush()
|
|
{
|
|
if( m_hCommPort == INVALID_HANDLE_VALUE )
|
|
return FALSE;
|
|
|
|
return ::FlushFileBuffers( m_hCommPort );
|
|
}
|