⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comportio.cpp

📁 读取GPS数据
💻 CPP
字号:
// COMPortIO.cpp: implementation of the CCOMPortIO class.
//
//////////////////////////////////////////////////////////////////////

#include <stdlib.h>
#include "stdafx.h"
#include "COMPortIO.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCOMPortIO::CCOMPortIO()
{
	m_hFile = INVALID_HANDLE_VALUE;
}

CCOMPortIO::~CCOMPortIO()
{
	CloseHandle(m_hFile);
}

BOOL CCOMPortIO::InitComm(const UINT nPort, const UINT nBaud, const UINT nParity, 
						  const UINT nData, const UINT nStop, const BOOL bNoError)
{
	TCHAR s[MAX_PATH];
	swprintf(s, _T("COM%d:"), nPort);
	int err;
	DCB dcb;
	
	m_nBaud = nBaud;
	m_nPort = nPort;

	m_hFile = CreateFile(
		s,							// pointer to name of the file 
	    GENERIC_READ|GENERIC_WRITE,	// access (read-write) mode 
	    0,							// share mode 
	    NULL,						// pointer to security descriptor 
	    OPEN_EXISTING,				// how to create 
		FILE_ATTRIBUTE_NORMAL,		// file attributes 
		NULL 						// handle to file with attributes to copy  
	);

	if(m_hFile == INVALID_HANDLE_VALUE)
	{
		DWORD dwLastError = GetLastError();
		CString sError;
		switch(dwLastError)
		{
			///////////////////////////////////////////////////////////////////
			case ERROR_ACCESS_DENIED: 
				sError = "Access Denied!\n\n The communications port may be in use"\
					" at this time. Please choose another port or close the"\
					" application using the desired port.";
			break;

			///////////////////////////////////////////////////////////////////
			case ERROR_FILE_NOT_FOUND: 
				sError = "Communication Port Not Found!\n\n";
				sError = sError + "The communications port does not exist."\
					" Check your system configuration on the status of this"\
					" Communication port";
			break;

			///////////////////////////////////////////////////////////////////
			default: 
				sError = "Comm Open Error!";
				sError = sError + _ltow(dwLastError,s,10) + "]";
				sError = sError + "\n\n The communications port may be in use"\
					" at this time. Please choose another port or close the"\
					" application using the desired port.";
			break;
		}

		AFX_MANAGE_STATE(AfxGetStaticModuleState())
		if(bNoError == FALSE) AfxMessageBox(sError,MB_ICONSTOP|MB_OK);
		
		return FALSE;
	}
	
	err = GetCommState(m_hFile,&dcb);

	if (err < 0)
	{
		if(bNoError == FALSE) MessageBeep(MB_ICONSTOP);
		AFX_MANAGE_STATE(AfxGetStaticModuleState())
	    if(bNoError == FALSE) AfxMessageBox(CString("Could not get comm state"),
			MB_OK|MB_ICONSTOP);
	    return FALSE;
	}


	///////////////////////////////////////////////////////////////////////////
	// Need to se te baud seperately
	dcb.BaudRate = (DWORD)nBaud;
	dcb.Parity = (BYTE)nParity;
	dcb.StopBits = (BYTE)nStop;
	dcb.ByteSize = (BYTE)nData;

	dcb.fBinary = TRUE;						// binary mode, no EOF check 
	dcb.fOutxCtsFlow = FALSE;				// CTS output flow control 
	dcb.fOutxDsrFlow = FALSE;				// DSR output flow control 
	dcb.fDtrControl = DTR_CONTROL_ENABLE;  // DTR flow control type 
	dcb.fDsrSensitivity = FALSE;			// DSR sensitivity 
	dcb.fOutX = FALSE;						// XON/XOFF out flow control 
	dcb.fInX =	FALSE;						// XON/XOFF in flow control 
	dcb.fNull = FALSE;						// enable null stripping 
	dcb.fRtsControl=RTS_CONTROL_ENABLE;	// RTS flow control 
	dcb.fAbortOnError = FALSE;				// abort reads/writes on error 

	err = SetCommState(m_hFile,&dcb);

	if (err < 0) 
	{
		if(bNoError == FALSE) MessageBeep(MB_ICONSTOP);
		AFX_MANAGE_STATE(AfxGetStaticModuleState())
	    if(bNoError == FALSE)
			AfxMessageBox(_T("Could not set communication state"),MB_OK|MB_ICONSTOP);
	    return FALSE;
	}

	///////////////////////////////////////////////////////////////////////////
	// Setup the comm timeouts
	COMMTIMEOUTS CommTimeOuts;
	CommTimeOuts.ReadIntervalTimeout = MAXDWORD ;
	CommTimeOuts.ReadTotalTimeoutMultiplier = 0 ;
	CommTimeOuts.ReadTotalTimeoutConstant = 0 ;
	CommTimeOuts.WriteTotalTimeoutMultiplier = 0 ;
	CommTimeOuts.WriteTotalTimeoutConstant = 1000 ;
	SetCommTimeouts( m_hFile, &CommTimeOuts ) ;

	DWORD dwErrors;
	COMSTAT ComStat;
	ClearCommError(m_hFile,&dwErrors,&ComStat);

	SetupComm( m_hFile, 16384, 16384 ); 

	return TRUE;
}

DWORD CCOMPortIO::Write(const void *pBuff, DWORD dwLen)
{
	DWORD dwBytesWrittern;
	
	BOOL bResult = WriteFile(
	    m_hFile,				// handle to file to write to 
		pBuff,					// pointer to data to write to file 
		dwLen,					// number of bytes to write 
		&dwBytesWrittern,		// pointer to number of bytes written 
		NULL				 	// addr. of structure needed for overlapped I/O  
	);

	return dwBytesWrittern;
}

DWORD CCOMPortIO::Read(void *pData, DWORD dwLen)
{
	DWORD dwBytesRead;

	if(m_hFile == INVALID_HANDLE_VALUE)
		return 0;

	ReadFile(
		m_hFile,			// handle of file to read 
		pData,				// address of buffer that receives data  
		dwLen,				// number of bytes to read 
		&dwBytesRead,		// address of number of bytes read 
		NULL				// address of structure for data 
	);

	DWORD dwLastError = GetLastError();
	DWORD dwErrors;
	COMSTAT ComStat;
	ClearCommError(m_hFile,&dwErrors,&ComStat);

	return dwBytesRead;
}

BOOL CCOMPortIO::Close()
{
	BOOL bVal = CloseHandle(m_hFile);
	m_hFile = INVALID_HANDLE_VALUE;
	return bVal;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -