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

📄 serialport.cpp

📁 这是我仿照串口助手(龚建伟)作的一个例子并修正了其中的一些bug
💻 CPP
字号:
// SerialPort.cpp: implementation of the CSerialPort class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "serialas.h"
#include "SerialPort.h"

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

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

CSerialPort::CSerialPort()
{
   m_hCom = NULL;
   m_sError.Empty();
}

CSerialPort::~CSerialPort()
{

}

HANDLE CSerialPort::OpenPort(CString sPort,DWORD dwBaudRate,BYTE byDataBits,
				BYTE byParity,BYTE byStopBits,BOOL bSynchronization)
{
	CString str;
	str = "\\\\.\\" + sPort;
	if(bSynchronization)
	{
		m_hCom = CreateFile(str,
							GENERIC_READ|GENERIC_WRITE,
							0,						//共享模式
							NULL,
							OPEN_EXISTING,
							0,						//同步
							NULL);
		
	}
	else
	{
		m_hCom = CreateFile(str,
							GENERIC_READ|GENERIC_WRITE,
							0,						//共享模式
							NULL,
							OPEN_EXISTING,
							FILE_FLAG_OVERLAPPED,    //异步
							NULL);	
	}

	if(m_hCom==INVALID_HANDLE_VALUE)
	{
		m_sError = GetErrorDesc(GetLastError());
		::CloseHandle(m_hCom);
		return NULL;
	}

	if(SetupComm(m_hCom,2048,1024)==0)
	{
		m_sError = GetErrorDesc(GetLastError());
		::CloseHandle(m_hCom);
		return NULL;
	}

//	SetCommMask(m_hCom, EV_RXCHAR);//设置事件驱动的类型

	if(PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR 

		| PURGE_RXCLEAR )==0)
	{
		m_sError = GetErrorDesc(GetLastError());
		::CloseHandle(m_hCom);
		return NULL;
	}
		
	DCB dcb;                         //定义dcb对象

	if(!GetCommState(m_hCom,&dcb))
	{
		m_sError = GetErrorDesc(GetLastError());
		::CloseHandle(m_hCom);
		return NULL;
		
	}
	
	COMMTIMEOUTS ct;
	GetCommTimeouts(m_hCom, &ct);
	ct.ReadIntervalTimeout = 500;
	ct.ReadTotalTimeoutConstant = 1000;
	ct.ReadTotalTimeoutMultiplier = 1000;
	ct.WriteTotalTimeoutConstant = 0;
	ct.WriteTotalTimeoutMultiplier = 0;

	if(!SetCommTimeouts(m_hCom, &ct))
	{
		m_sError = GetErrorDesc(GetLastError());
		CloseHandle(m_hCom);
		return FALSE;
	}

	dcb.Parity = byParity;				//获得校验方式

	if(byParity!=NOPARITY)
	{
		dcb.fBinary = TRUE;				//允许校验
	}

	dcb.BaudRate = dwBaudRate;			//获得通信速率

	dcb.ByteSize = byDataBits;			//数据位数

	dcb.StopBits = byStopBits;			//停止位

	dcb.fOutxCtsFlow = FALSE;			//流量控制
	dcb.fOutxDsrFlow = FALSE;
	dcb.fOutX = FALSE;
	dcb.fInX = FALSE;

	// 将设定的参数值用于该串口
	if(!SetCommState(m_hCom,&dcb))
	{
		m_sError = GetErrorDesc(GetLastError());
		::CloseHandle(m_hCom);
		return NULL;
	}
	return m_hCom;
}


BOOL  CSerialPort::ClosePort()
{
	if(CloseHandle(m_hCom))
	{
		m_hCom = NULL;
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

CString CSerialPort::GetError()
{
	return m_sError;
}

CString CSerialPort::GetErrorDesc(DWORD dwError)
{
	CString strResult;
	HLOCAL hlocal = NULL; //malloc a result buffer
	BOOL fOk = FormatMessage
	(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 
	NULL, dwError, 
	MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), //simple chinese
	(PTSTR) &hlocal, 0, NULL
	);
	if (!fOk) 
	{
	//check if it is netError
	//remember do not load the dll or module the dll loaded
	HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
	if (hDll != NULL) 
	{
	fOk =FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM,
	hDll, dwError, 
	MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), 
	(PTSTR) &hlocal, 0, NULL);
	FreeLibrary(hDll);
	}
	}
//	if(!fOk)
//	return GetSocketErrorDesc(dwError);
	strResult.Format(_T("错误代码 : %ld\n错误消息: %s"),dwError,hlocal);
	LocalFree(hlocal);
	return strResult;
}

⌨️ 快捷键说明

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