void CSerialBuffer::Flush()
{
	LockBuffer();
	m_szInternalBuffer.erase ();
	m_alBytesUnRead = 0;
	m_iCurPos  = 0;
	UnLockBuffer();
}
bool CSerialBuffer::Read_Available( std::string &szData,HANDLE & hEventToReset)
{
	ATLTRACE6 (_T("CSerialBuffer : (tid:%d) Read_Upto called "), GetCurrentThreadId ());

	LockBuffer();
  szData += m_szInternalBuffer ;
	
	ClearAndReset ( hEventToReset );
	
	UnLockBuffer();
	
  ATLTRACE6 (_T("CSerialBuffer : (tid:%d) Read_Available returned (data:%s)  "), GetCurrentThreadId (),((szData)).c_str());	
	return ( szData.size () > 0 );
}
Exemple #3
0
int Wavefunction<Rank>::SetActiveBuffer(int bufferName)
{
	int oldActiveBufferName = GetActiveBufferName();

	//Get lock on the new array
	LockBuffer(bufferName);
	if (oldActiveBufferName != -1)
	{
		UnLockBuffer(oldActiveBufferName);
	}

	//Reference the data
	Data.reference(WavefunctionData[bufferName].GetArray());
	ActiveBufferName = bufferName;
	return oldActiveBufferName;
}
bool CSerialBuffer::Read_Upto( std::string &szData,char chTerm,long  &alBytesRead ,HANDLE & hEventToReset)
{
	return Read_Upto_FIX(szData,chTerm,alBytesRead ,hEventToReset);
	
	ATLTRACE6 (_T("CSerialBuffer : (tid:%d) Read_Upto called "), GetCurrentThreadId ());

	LockBuffer();
   	
	alBytesRead = 0 ;
	bool abFound = false;
	if ( m_alBytesUnRead > 0 ) 
	{//if there are some bytes un-read...
				
			int iActualSize = GetSize ();
			

			for ( int i = m_iCurPos ; i < iActualSize; ++i )
			{
				alBytesRead++;
				szData .append ( m_szInternalBuffer,i,1);
				m_alBytesUnRead -= 1;
				if ( m_szInternalBuffer[i] == 	chTerm) 
				{
					abFound = true;
					break;
				}
			}
			if ( m_alBytesUnRead == 0 ) 
			{
				ClearAndReset ( hEventToReset );
			}
			else 
			{ 
				//if we are here it means that there is still some data in the local buffer and
				//we have already found what we want... maybe this is ok but we want to catch this
				//scenario --- fix is in TCP/ip SocketBuffer.
				ASSERT(0); 
			} 
	}

	UnLockBuffer();
  ATLTRACE6 (_T("CSerialBuffer : (tid:%d) Read_Upto returned %d bytes (data:%s)  "), GetCurrentThreadId (),alBytesRead,((szData)).c_str());	
	return abFound;
}
long	 CSerialBuffer::Read_N( std::string &szData,long  alCount ,HANDLE & hEventToReset)
{
	ATLTRACE6 (_T("CSerialBuffer : (tid:%d) Read_N() called "), GetCurrentThreadId ());	
	ASSERT ( hEventToReset != INVALID_HANDLE_VALUE ) ;
	
	
	LockBuffer();
	long alTempCount	= min( alCount ,  m_alBytesUnRead) ;
	long alActualSize	= GetSize();
   	
	szData.append (m_szInternalBuffer,m_iCurPos ,alTempCount);
	
	m_iCurPos +=  alTempCount ;
	
	m_alBytesUnRead -= alTempCount;
	if (m_alBytesUnRead == 0 )
	{
				ClearAndReset ( hEventToReset );
	}
 
	UnLockBuffer();
	ATLTRACE6 (_T("CSerialBuffer : (tid:%d) Read_N returned %d bytes (data:%s)  "), GetCurrentThreadId (),alTempCount,((szData)).c_str());	
	return alTempCount;
}
bool CSerialBuffer::Read_Upto_FIX( std::string &szData,char chTerm,long  &alBytesRead ,HANDLE & hEventToReset)
{
	ATLTRACE6 (_T("CSerialBuffer : (tid:%d) Read_Upto called "), GetCurrentThreadId ());

	LockBuffer();
		alBytesRead = 0 ;

   	
 	bool abFound = false;
	if ( m_alBytesUnRead > 0 ) 
	{//if there are some bytes un-read...
		
		int iActualSize = GetSize ();
 		int iIncrementPos = 0;
		for ( int i = m_iCurPos ; i < iActualSize; ++i )
		{
			//szData .append ( m_szInternalBuffer,i,1);
			szData+=m_szInternalBuffer[i];
			m_alBytesUnRead -= 1;
			if ( m_szInternalBuffer[i] == 	chTerm) 
			{
				iIncrementPos++;
				abFound = true;
				break;
			}
			iIncrementPos++;
		}
		m_iCurPos += iIncrementPos;
		if ( m_alBytesUnRead == 0 ) 
		{
			ClearAndReset ( hEventToReset );
		}
	}
	UnLockBuffer();	
	return abFound;
}
Exemple #7
0
unsigned CDll_RS232::ThreadFunction( ) 
{
	DWORD dwEventMask = 0 , dwWait ;
	BOOL	bContinue = TRUE ;
	OVERLAPPED ov ;
	memset( &ov , 0 , sizeof( ov ) ) ;
	ov.hEvent = CreateEvent( NULL , TRUE , NULL , NULL ) ;

	HANDLE arHandles[ 2 ] = { m_hTerminate , ov.hEvent } ;
	
	SetEvent( m_hThreadStarted ) ;
	while( bContinue ) 
	{
		BOOL bRet = WaitCommEvent( m_hCommPort , &dwEventMask , &ov ) ;

		if ( !bRet ){
			ATLASSERT( GetLastError() == ERROR_IO_PENDING ) ;
		}

		dwWait = WaitForMultipleObjects ( 2 , arHandles , FALSE , INFINITE ) ;
		switch( dwWait ){
		case WAIT_OBJECT_0 :
			ATLTRACE( "RS232 thread terminate\r\n" ) ;
			_endthreadex( 1 ) ;
			break ;
		case WAIT_OBJECT_0 + 1 :
			{
				try{
					OVERLAPPED ovRead ;
					memset( &ovRead , 0 , sizeof( ovRead ) ) ;
					ovRead.hEvent = CreateEvent( NULL , TRUE , NULL , NULL ) ;
					COMSTAT cs ; 
					memset( &cs , 0 , sizeof( cs ) ) ;
					DWORD dwError ; 
					if ( !ClearCommError( m_hCommPort , &dwError , &cs ) ){
						throw ;
					}
					DWORD dwReadAvailable = cs.cbInQue , dwRead ;
					if ( !dwReadAvailable ) {
						CloseHandle( ovRead.hEvent ) ;
						continue ;
					}
					CAtlArray<BYTE> buf ;
					buf.SetCount( dwReadAvailable ) ;
					BOOL bOk = ReadFile( m_hCommPort , buf.GetData() , dwReadAvailable , &dwRead , &ovRead ) ;
					if ( !bOk ){
						if ( GetLastError() == ERROR_IO_PENDING ){
							dwWait = WaitForSingleObject( ovRead.hEvent , INFINITE ) ;
							ATLASSERT( dwWait == WAIT_OBJECT_0 ) ;
						}else{
							CloseHandle( ovRead.hEvent ) ;
							continue ;
						}
					}
					LockBuffer() ;
					m_arBuffer.Append( buf ) ;
//#ifdef _DEBUG
					char _dbg[ 1024 ] ;
					sprintf( _dbg , "Receive bytes : " ) ;
					for ( int i = 0 ; i < buf.GetCount() ; i++ ) 
						sprintf( _dbg , "%s %02X" , _dbg , buf[ i ] ) ;
					sprintf( _dbg , "%s Tick = %d\r\n" , _dbg , GetTickCount( ) ) ;
					ATLTRACE( _dbg ) ;
					fprintf( stream , _dbg ) ;
					fflush( stream ) ;
//#endif
					UnLockBuffer ( ) ;
					
					m_SignalDataReceive ( ) ;
					
					CloseHandle( ovRead.hEvent ) ;
					ResetEvent( ov.hEvent ) ;
				}catch(...){
					ATLASSERT( 0 ) ;
				}
			break ;
			}
		}
	}
	return 1 ;
}
Exemple #8
0
void Wavefunction<Rank>::FreeData(int bufferName) 
{
	LockBuffer(bufferName);
	WavefunctionData[bufferName].FreeArray();
	UnLockBuffer(bufferName);
}