Beispiel #1
0
CSerial::EError CSerial::GetError (void)
{
	// Reset error state
	m_lLastError = ERROR_SUCCESS;

	// Check if the device is open
	if (m_hFile == 0)
	{
		// Set the internal error code
		m_lLastError = ERROR_INVALID_HANDLE;

		// Issue an error and quit
		_RPTF0(_CRT_WARN,"CSerial::GetError - Device is not opened\n");
		return EErrorUnknown;
	}

	// Obtain COM status
	DWORD dwErrors = 0;
	if (!::ClearCommError(m_hFile,&dwErrors,0))
	{
		// Set the internal error code
		m_lLastError = ::GetLastError();

		// Issue an error and quit
		_RPTF0(_CRT_WARN,"CSerial::GetError - Unable to obtain COM status\n");
		return EErrorUnknown;
	}

	// Return the error
	return EError(dwErrors);
}
Beispiel #2
0
DWORD CSerialEx::ThreadProc (void)
{
	// Use overlapped structure
	LPOVERLAPPED lpOverlapped = 0;

	// Keep looping
	do
	{
#ifndef SERIAL_NO_OVERLAPPED
		// Reset the event handle
		::ResetEvent(m_hevtOverlappedWorkerThread);

		// Initialize the overlapped structure
		OVERLAPPED ovInternal = {0};
		ovInternal.hEvent = m_hevtOverlappedWorkerThread;

		// Start the WaitEvent (use our own overlapped structure)
		if (WaitEvent(&ovInternal) != ERROR_SUCCESS)
			return m_lLastError;

		// Wait for the overlapped operation to complete
		if (::WaitForSingleObject(m_hevtOverlappedWorkerThread,INFINITE) != WAIT_OBJECT_0)
		{
			// Set the internal error code
			m_lLastError = ::GetLastError();

			// Issue an error and quit
			_RPTF0(_CRT_WARN,"CSerialEx::ThreadProc - Unable to wait until COM event has arrived\n");
			return m_lLastError;
		}
#else
		// Start the WaitEvent (don't need to specify an overlapped structure)
		if (WaitEvent() != ERROR_SUCCESS)
			return m_lLastError;
#endif

		// Wait until one of the events happens
		if (!m_fStopping)
		{
			// Determine the event
			EEvent eEvent = GetEventType();

			// Obtain the error status during this event
			DWORD dwErrors = 0;
			if (!::ClearCommError(m_hFile,&dwErrors,0))
			{
				// Set the internal error code
				m_lLastError = ::GetLastError();

				// Issue an error and quit
				_RPTF0(_CRT_WARN, "CSerialEx::ThreadProc - Unable to obtain COM status\n");
			}

			// Convert the error
			EError eError = EError(dwErrors);

			// There was a COMM event, which needs handling. We'll call the
			// event handler. We can receive a "zero" event, when the
			// mask or event character has been set. We won't pass this
			// down to the window.
			if (eEvent)
				OnEvent(eEvent,eError);
		}
	}
	while (!m_fStopping);

	// Bye bye
	return 0;
}