Exemplo n.º 1
0
bool CSerial::Open(QString portName, DWORD dwInQueue, DWORD dwOutQueue, bool fOverlapped)
{
	// Reset error state
	m_lLastError = ERROR_SUCCESS;

	// Check if the port isn't already opened
	if (m_hFile)
		return true;

	// Open the device
	m_hFile = CreateFileA(portName.toAscii(),
						   GENERIC_READ|GENERIC_WRITE,
						   0,
						   0,
						   OPEN_EXISTING,
						   fOverlapped?FILE_FLAG_OVERLAPPED:0,
						   0);

	// Check if the opening was successful.
	if (m_hFile == INVALID_HANDLE_VALUE)
    {
		m_hFile = 0;
		return false;
	}

#ifndef SERIAL_NO_OVERLAPPED
	// We cannot have an event handle yet
	//_ASSERTE(m_hevtOverlapped == 0);

	// Create the event handle for internal overlapped operations (manual reset)
	if (fOverlapped)
	{
		m_hevtOverlapped = ::CreateEvent(0,true,false,0);
		if (m_hevtOverlapped == 0)
		{
			// Obtain the error information
			m_lLastError = ::GetLastError();
			qDebug() << ("CSerial::Open - Unable to create event\n");

			// Close the port
			CloseHandle(m_hFile);
			m_hFile = 0;

			// Return the error
			//return m_lLastError;
			return false;
		}
	}
#else

	// Overlapped flag shouldn't be specified
	//_ASSERTE(!fOverlapped);

#endif

	// Setup the COM-port
	if (dwInQueue || dwOutQueue)
	{
		// Make sure the queue-sizes are reasonable sized. Win9X systems crash
		// if the input queue-size is zero. Both queues need to be at least
		// 16 bytes large.
		//_ASSERTE(dwInQueue >= 16);
		//_ASSERTE(dwOutQueue >= 16);

		if (!SetupComm(m_hFile,dwInQueue,dwOutQueue))
		{
			// Display a warning
			long lLastError = ::GetLastError();
			qDebug() << ("CSerial::Open - Unable to setup the COM-port\n");

			// Close the port
			close();

			// Save last error from SetupComm
			m_lLastError = lLastError;
			//return m_lLastError;
			return false;
		}
	}

	// Setup the default communication mask
	SetMask();

	// Non-blocking reads is default
	SetupReadTimeouts(EReadTimeoutNonblocking);

//
//	// Setup the device for default settings
// 	COMMCONFIG commConfig = {0};
//	DWORD dwSize = sizeof(commConfig);
//	commConfig.dwSize = dwSize;
//	if (GetDefaultCommConfig(lpszDevice,&commConfig,&dwSize))
//	{
//		// Set the default communication configuration
//		if (!SetCommConfig(m_hFile,&commConfig,dwSize))
//		{
//			// Display a warning
//			qDebug() << ("CSerial::Open - Unable to set default communication configuration.\n");
//		}
//	}
//	else
//	{
//		// Display a warning
//		qDebug() << ("CSerial::Open - Unable to obtain default communication configuration.\n");
//	}

	// Return successful
	//return m_lLastError;
	return true;
}
Exemplo n.º 2
0
LONG CSerial::Open (LPCTSTR lpszDevice, DWORD dwInQueue, DWORD dwOutQueue, bool fOverlapped)
{
	// Reset error state
	m_lLastError = ERROR_SUCCESS;

	// Check if the port isn't already opened
	if (m_hFile)
	{
		m_lLastError = ERROR_ALREADY_INITIALIZED;
		_RPTF0(_CRT_WARN,"CSerial::Open - Port already opened\n");
		return m_lLastError;
	}

	// Open the device
	m_hFile = ::CreateFile(lpszDevice,
						   GENERIC_READ|GENERIC_WRITE,
						   0,
						   0,
						   OPEN_EXISTING,
						   fOverlapped?FILE_FLAG_OVERLAPPED:0,
						   0);
	if (m_hFile == INVALID_HANDLE_VALUE)
	{
		// Reset file handle
		m_hFile = 0;

		// Display error
		m_lLastError = ::GetLastError();
		_RPTF0(_CRT_WARN, "CSerial::Open - Unable to open port\n");
		return m_lLastError;
	}

#ifndef SERIAL_NO_OVERLAPPED
	// We cannot have an event handle yet
	_ASSERTE(m_hevtOverlapped == 0);

	// Create the event handle for internal overlapped operations (manual reset)
	if (fOverlapped)
	{
		m_hevtOverlapped = ::CreateEvent(0,true,false,0);
		if (m_hevtOverlapped == 0)
		{
			// Obtain the error information
			m_lLastError = ::GetLastError();
			_RPTF0(_CRT_WARN,"CSerial::Open - Unable to create event\n");

			// Close the port
			::CloseHandle(m_hFile);
			m_hFile = 0;

			// Return the error
			return m_lLastError;
		}
	}
#else
	
	// Overlapped flag shouldn't be specified
	_ASSERTE(!fOverlapped);

#endif

	// Setup the COM-port
	if (dwInQueue || dwOutQueue)
	{
		// Make sure the queue-sizes are reasonable sized. Win9X systems crash
		// if the input queue-size is zero. Both queues need to be at least
		// 16 bytes large.
		_ASSERTE(dwInQueue >= 16);
		_ASSERTE(dwOutQueue >= 16);

		if (!::SetupComm(m_hFile,dwInQueue,dwOutQueue))
		{
			// Display a warning
			long lLastError = ::GetLastError();
			_RPTF0(_CRT_WARN,"CSerial::Open - Unable to setup the COM-port\n");

			// Close the port
			Close();

			// Save last error from SetupComm
			m_lLastError = lLastError;
			return m_lLastError;	
		}
	}

	// Setup the default communication mask
	SetMask();

	// Non-blocking reads is default
	SetupReadTimeouts(EReadTimeoutNonblocking);

	// Setup the device for default settings
 	COMMCONFIG commConfig = {0};
	DWORD dwSize = sizeof(commConfig);
	commConfig.dwSize = dwSize;
	if (::GetDefaultCommConfig(lpszDevice,&commConfig,&dwSize))
	{
		// Set the default communication configuration
		if (!::SetCommConfig(m_hFile,&commConfig,dwSize))
		{
			// Display a warning
			_RPTF0(_CRT_WARN,"CSerial::Open - Unable to set default communication configuration.\n");
		}
	}
	else
	{
		// Display a warning
		_RPTF0(_CRT_WARN,"CSerial::Open - Unable to obtain default communication configuration.\n");
	}

	// Return successful
	return m_lLastError;
}
Exemplo n.º 3
0
LONG CNTSerial::Open (LPCTSTR lpszDevice, DWORD dwInQueue, DWORD dwOutQueue)
{
    // Reset error state
    m_lLastError = ERROR_SUCCESS;

    // Check if the port isn't already opened
    if (m_hFile)
    {
        m_lLastError = ERROR_ALREADY_INITIALIZED;
        _RPTF0(_CRT_WARN,"CNTSerial::Open - Port already opened\n");
        return m_lLastError;
    }

    // Open the device
    m_hFile = ::CreateFile(lpszDevice,
                           GENERIC_READ|GENERIC_WRITE,
                           0,
                           0,
                           OPEN_EXISTING,
                           FILE_FLAG_OVERLAPPED,
                           0);
    if (m_hFile == INVALID_HANDLE_VALUE)
    {
        // Reset file handle
        m_hFile = 0;

        // Display error
        m_lLastError = ::GetLastError();
        _RPTF0(_CRT_WARN, "CNTSerial::Open - Unable to open port\n");
        return m_lLastError;
    }

    // We cannot have an event handle yet
    _ASSERTE(m_hevtOverlapped == 0);

    // Create the event handle for internal overlapped operations (manual reset)
    m_hevtOverlapped = ::CreateEvent(0,true,false,0);
    if (m_hevtOverlapped == 0)
    {
        // Obtain the error information
        m_lLastError = ::GetLastError();
        _RPTF0(_CRT_WARN,"CNTSerial::Open - Unable to create event\n");

        // Close the port
        ::CloseHandle(m_hFile);
        m_hFile = 0;

        // Return the error
        return m_lLastError;
    }

    // Setup the COM-port
    if (!::SetupComm(m_hFile,dwInQueue,dwOutQueue))
    {
        // Display a warning
        long lLastError = ::GetLastError();
        _RPTF0(_CRT_WARN,"CNTSerial::Open - Unable to setup the COM-port\n");

        // Close the port
        ClosePort();

        // Save last error from SetupComm
        m_lLastError = lLastError;
        return m_lLastError;
    }

    // Setup the default communication mask
    SetMask();

    // Setup the device for default settings
    Setup();

    // Non-blocking reads is default
    SetupReadTimeouts(EReadTimeoutNonblocking);

    // Default is no handshaking
    SetupHandshaking(EHandshakeOff);

    // Return successful
    return m_lLastError;
}