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

📄 comm.cpp

📁 一个VC++编写的串口通信程序
💻 CPP
字号:
// Comm.cpp: implementation of the CComm class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ComDbg.h"
#include "Comm.h"

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

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

BYTE	_PARITY[8]=
{
	NOPARITY,
	ODDPARITY,
	EVENPARITY,
	MARKPARITY,
	SPACEPARITY,
	NOPARITY,
	NOPARITY,
	NOPARITY,
};
BYTE	_STOPBITS[4]=
{
	ONESTOPBIT,
	ONE5STOPBITS,
	TWOSTOPBITS,
	ONESTOPBIT,
};

CComm::CComm()
{
	m_hCom		=INVALID_HANDLE_VALUE;
	m_nCom		=1;
	m_nbps		=19200;
	m_parity	=NOPARITY;
	m_bytesize	=8;
	m_stopbits	=ONESTOPBIT;
	m_hWnd		=NULL;
	m_nMask		=EV_RXCHAR;
}

CComm::~CComm()
{
	Close();
}

void CComm::SetWnd(HWND hWnd)
{
	m_hWnd=hWnd;
}
void CComm::SetCom(int nCom)
{
	ASSERT(nCom>0 && nCom<=16);
	m_nCom=nCom;
}
void CComm::SetBps(UINT bps)
{
	ASSERT( bps==110 || bps==300 || 
			bps==1200 || bps==2400 ||
			bps==4800 || bps==9600 ||
			bps==19200 || bps==38400 ||
			bps==56000 || bps==14400 ||
			bps==57600 || bps==115200 ||
			bps==128000 || bps==256000
		);
	m_nbps=bps;
}
void CComm::SetParity(BYTE parity)
{
	ASSERT(	parity == EVENPARITY ||
			parity == MARKPARITY ||
			parity == NOPARITY ||
			parity == ODDPARITY ||
			parity == SPACEPARITY);
	m_parity=parity;
}
void CComm::SetByteSize(BYTE bytesize)
{
	ASSERT(	bytesize>1 && bytesize<=16);
	m_bytesize=bytesize;
}
void CComm::SetStopBits(BYTE stopbits)
{
	ASSERT(	stopbits == ONESTOPBIT ||
			stopbits == ONE5STOPBITS ||
			stopbits == TWOSTOPBITS);
	m_stopbits=stopbits;
}
void CComm::SetMask(DWORD dwMask)
{
	m_nMask=dwMask;
}

BOOL CComm::Open()
{
	Close();
	char	szcom[8];
	sprintf(szcom,"COM%d",m_nCom);
	m_hCom = CreateFile(szcom,							// communication port string (COMX)
					     GENERIC_READ|GENERIC_WRITE,	// read/write types
					     0,								// comm devices must be opened with exclusive access
					     NULL,							// no security attributes
					     OPEN_EXISTING,					// comm devices must use OPEN_EXISTING
					     NULL,							// 同步通讯
					     NULL);							// template must be 0 for comm devices

	if (m_hCom == INVALID_HANDLE_VALUE)
	{
		TRACE("CComm->CreateFile(): Error %lu\n",GetLastError());
		return FALSE;
	}
	//======================================================
	DCB dcb;
	if(!GetCommState(m_hCom, &dcb))
	{
		TRACE("CComm->GetCommState(): Error %lu\n",GetLastError());
		Close();
		return FALSE;
	}
	dcb.BaudRate = m_nbps;
	dcb.ByteSize = m_bytesize;
	dcb.Parity = m_parity;
	dcb.StopBits = m_stopbits;
	if(!SetCommState(m_hCom, &dcb))
	{
		TRACE("CComm->SetCommState(): Error %lu\n",GetLastError());
		Close();
		return FALSE;
	}
	COMMTIMEOUTS	tmo={0};
	if(!GetCommTimeouts(m_hCom,&tmo))
	{
		TRACE("CComm->GetCommTimeouts(): Error %lu\n",GetLastError());
		Close();
		return FALSE;
	}
	tmo.ReadIntervalTimeout			=100;	//每个字符的超时
	tmo.ReadTotalTimeoutConstant	=500;	//读超时
	tmo.ReadTotalTimeoutMultiplier	=20;
	tmo.WriteTotalTimeoutConstant	=500;	//写超时
	tmo.WriteTotalTimeoutMultiplier	=20;
	if(!SetCommTimeouts(m_hCom,&tmo))
	{
		TRACE("CComm->SetCommTimeouts(): Error %lu\n",GetLastError());
		Close();
		return FALSE;
	}
	//======================================================
	if(!SetCommMask(m_hCom, m_nMask))
	{
		TRACE("CComm->SetCommMask(): Error %lu\n",GetLastError());
		Close();
		return FALSE;
	}
	//======================================================
	if(!SetupComm(m_hCom,512,512))
	{
		TRACE("CComm->SetupComm(): Error %lu\n",GetLastError());
		Close();
		return FALSE;
	}
	//======================================================
	// 刷新端口
	if(!PurgeComm(m_hCom,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_TXABORT))
	{
		TRACE("CComm->PurgeComm(): Error %lu\n",GetLastError());
		Close();
		return FALSE;
	}
	TRACE("Open COM%d completed.\n", m_nCom);
	//================================================
	return TRUE;
}
void CComm::Close()
{
	CSingleLock l(&m_crit,TRUE);
	if(m_hCom != INVALID_HANDLE_VALUE)
	{
		::CloseHandle(m_hCom);
		m_hCom=INVALID_HANDLE_VALUE;
	}
}
BOOL CComm::Send(void* pbuf,DWORD nbytes)
{
	CSingleLock l(&m_crit,TRUE);
	BOOL	ret		=FALSE;
	DWORD	nsend	=0;
	ASSERT(nbytes>=0);
	if(pbuf == NULL) return TRUE;
	if(m_hCom == INVALID_HANDLE_VALUE)	return FALSE;
	//清空缓冲区
	PurgeComm(m_hCom,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_TXABORT);
	ret=WriteFile(	m_hCom,		// Handle to COMM Port
					pbuf,		// Pointer to message buffer in calling finction
					nbytes,		// Length of message to send
					&nsend,		// Where to store the number of bytes sent
					NULL);		// Overlapped structure
	return (ret && nsend == nbytes);
}
BOOL CComm::Recv(void* pbuf,DWORD nbytes)
{
	CSingleLock l(&m_crit,TRUE);
	BOOL	ret		=FALSE;
	DWORD	nread	=0;
	ASSERT(pbuf != NULL);
	if(m_hCom == INVALID_HANDLE_VALUE)	return FALSE;
	ret=ReadFile(	m_hCom,		// Handle to COMM port 
					pbuf,		// RX Buffer Pointer
					nbytes,		// Read one byte
					&nread,		// Stores number of bytes read
					NULL);		// pointer to the m_ov structure
	return (ret && nread == nbytes);
}
BOOL CComm::IsOpened()
{
	CSingleLock l(&m_crit,TRUE);
	return (m_hCom != INVALID_HANDLE_VALUE);
}

⌨️ 快捷键说明

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