CSerialCommHelper::CSerialCommHelper() { InvalidateHandle( ThreadTerm ); InvalidateHandle( Thread ); InvalidateHandle( ThreadStarted ); InvalidateHandle( CommPort ); InvalidateHandle( DataRx ); InitLock(); state = SS_UnInit; }
CDll_RS232::CDll_RS232( void ):m_bIsConnection( FALSE ) , m_SignalDataReceive( NULL ) { InvalidateHandle( m_hThread ) ; InvalidateHandle( m_hCommPort ) ; InvalidateHandle( m_hTerminate ) ; InvalidateHandle( m_hThreadStarted ) ; InitLock() ; char szPath[ MAX_PATH ] ; ZeroMemory( szPath , MAX_PATH ) ; GetModuleFileName( NULL , szPath , MAX_PATH ) ; char *p = strrchr( szPath , '.' ) ; strcpy( p + 1 , "log" ) ; stream = fopen( szPath, "w" ); }
void CSerialCommHelper::CloseAndCleanHandle(HANDLE& Handle) { BOOL res = CloseHandle( Handle ) ; if ( !res ) { g_assert(0); MTXDBG(CRITICAL,_( "CSerialCommHelper : Failed to open Close Handle %d :Last Error: %d"),Handle,GetLastError()); } InvalidateHandle ( Handle ); }
HRESULT CSerialCommHelper:: Init(std::string PortName, DWORD BaudRate,BYTE byParity,BYTE byStopBits,BYTE byByteSize) { HRESULT hr = S_OK; try { DataRx = CreateEvent(0,0,0,0); //open the COM Port CommPort = ::CreateFile(PortName.c_str (), GENERIC_READ|GENERIC_WRITE,//access ( read and write) 0, //(share) 0:cannot share the COM port 0, //security (None) OPEN_EXISTING,// creation : open_existing FILE_FLAG_OVERLAPPED,// we want overlapped operation 0// no templates file for COM port... ); if ( CommPort == INVALID_HANDLE_VALUE ) { MTXDBG(CRITICAL,_( "CSerialCommHelper : Failed to open COM Port Reason: %d"),GetLastError()); g_assert ( 0 ); return E_FAIL; } MTXDBG(CRITICAL,_( "CSerialCommHelper : COM port opened successfully")); //now start to read but first we need to set the COM port settings and the timeouts if (! ::SetCommMask(CommPort,EV_RXCHAR|EV_TXEMPTY) ) { g_assert(0); MTXDBG(CRITICAL,_("CSerialCommHelper : Failed to Set Comm Mask Reason: %d"),GetLastError()); return E_FAIL; } MTXDBG(CRITICAL,_( "CSerialCommHelper : SetCommMask() success")); //now we need to set baud rate etc, DCB dcb = {0}; dcb.DCBlength = sizeof(DCB); if (!::GetCommState (CommPort,&dcb)) { MTXDBG(CRITICAL,_("CSerialCommHelper : Failed to Get Comm State Reason: %d"),GetLastError()); return E_FAIL; } dcb.BaudRate = BaudRate; dcb.ByteSize = byByteSize; dcb.Parity = byParity; if ( byStopBits == 1 ) dcb.StopBits = ONESTOPBIT; else if (byStopBits == 2 ) dcb.StopBits = TWOSTOPBITS; else dcb.StopBits = ONE5STOPBITS; dcb.fDsrSensitivity = 0; // dcb.fDtrControl = DTR_CONTROL_ENABLE; dcb.fOutxDsrFlow = 0; if (!::SetCommState (CommPort,&dcb)) { g_assert(0); MTXDBG(CRITICAL,_("CSerialCommHelper : Failed to Set Comm State Reason: %d"),GetLastError()); return E_FAIL; } MTXDBG(CRITICAL,_("CSerialCommHelper : Current Settings, (Baud Rate %d; Parity %d; Byte Size %d; Stop Bits %d"), dcb.BaudRate, dcb.Parity,dcb.ByteSize,dcb.StopBits); //now set the timeouts ( we control the timeout overselves using WaitForXXX() COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = MAXDWORD; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts(CommPort, &timeouts)) { g_assert(0); MTXDBG(CRITICAL,_("CSerialCommHelper : Error setting time-outs. %d"),GetLastError()); return E_FAIL; } //create thread terminator event... ThreadTerm = CreateEvent(0,0,0,0); ThreadStarted = CreateEvent(0,0,0,0); Thread = (HANDLE)_beginthreadex(0,0,CSerialCommHelper::ThreadFn,(void*)this,0,0 ); DWORD Wait = WaitForSingleObject ( ThreadStarted , INFINITE ); g_assert ( Wait == WAIT_OBJECT_0 ); CloseHandle(ThreadStarted); InvalidateHandle(ThreadStarted ); connected = true; } catch(...) { g_assert(0); hr = E_FAIL; } if ( SUCCEEDED(hr) ) { state = SS_Init; } return hr; }
/** Release current resource. Next request will require lookup. */ void InvalidateCurrent() { InvalidateHandle(handle_); }