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

📄 serialcommunication.cpp

📁 シリアル通信のプログラム、初学者により、いいです。
💻 CPP
字号:
// SerialCommunication.cpp: implementation of the CSerialCommunication class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "EcoPowerMeter.h"
#include "SerialCommunication.h"

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

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

CSerialCommunication::CSerialCommunication(IReceiveEvent *receiveEvent)
{
	m_hComm = NULL;
	m_receiveThread = NULL;
	m_receiveEvent = receiveEvent;
	//m_pApp = (CEcoPowerMeterApp *)AfxGetApp();
	
}

CSerialCommunication::~CSerialCommunication(void)
{
	Close();
}

void CSerialCommunication::Close()
{
	m_isReceiving = false;
	if (m_receiveThread != NULL) {
		WaitForSingleObject(m_receiveThread, 4000);
		m_receiveThread = NULL;
	}

    if(m_hComm != NULL){
		CloseHandle(m_hComm);
		m_hComm = NULL;
    }
}

void CSerialCommunication::Send(LPCSTR str,DWORD strLength)
{
	if (m_hComm == NULL) {
		AfxMessageBox("CONN'T OPEN THE COM\n");
		return;
	}

	DWORD	dwWrite; 
	::WriteFile(m_hComm, str, (DWORD) strlen(str), &dwWrite, NULL);
}

void CSerialCommunication::FireOnReceive(LPCSTR str,DWORD strLength)
{ 
	static CString strTmp("");

	CString strWork(str);
	if(!strWork.GetLength())
		return;

	strTmp += strWork;
	if(strTmp.Right(1)!="\r")
//	if(strTmp.Right(1)!="\n")
		return;

	if (m_receiveEvent != NULL) {
		m_receiveEvent->OnReceive(strTmp,strLength);
		strTmp="";
	}
}

UINT CSerialCommunication::ReceiveRs232cFunc(LPVOID pParam)
{
	CSerialCommunication *pInst = (CSerialCommunication *) pParam; 
	CString str; 

	DWORD	dwErrors; 
	COMSTAT	ComStat; 
	DWORD	dwCount; 
	char	pszBuf[256];
	DWORD	dwRead; 

	while (pInst->m_isReceiving) {

		ClearCommError(pInst->m_hComm, &dwErrors, &ComStat);
		dwCount = ComStat.cbInQue;

		if (dwCount != 0) {
			if (dwCount >= 256) {
				dwCount = 255;
			}
			ZeroMemory(pszBuf, 256);
			ReadFile(pInst->m_hComm, pszBuf, dwCount, &dwRead, NULL); 
			//ZeroMemory(pszBuf, 256);
			pszBuf[dwRead] = '\0'; 

			if (dwRead != 0) {
				str = pszBuf;
			} else {
				Sleep(10);
				continue;
			}
		} else { 
			Sleep(10);
			continue;
		}

		pInst->FireOnReceive(str,dwRead);
		str = "";
	}

	return 0;
}

bool CSerialCommunication::Open(LPCSTR strPort)
{
	m_isReceiving = false;
	if (m_receiveThread != NULL) {
		WaitForSingleObject(m_receiveThread, 4000);
		m_receiveThread = NULL;
	}
 
	if (m_hComm != NULL) {
		CloseHandle(m_hComm);
	}

    m_hComm = CreateFile(strPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    
	if(m_hComm == INVALID_HANDLE_VALUE){
        AfxMessageBox("CONN'T OPEN THE COM\n");
		return false;
	}

	DCB dcb;
	GetCommState(m_hComm, &dcb);	
	dcb.BaudRate = 9600;		
	dcb.ByteSize = 8;			
	dcb.Parity = NOPARITY;		
	dcb.fParity = FALSE;		
	dcb.StopBits = ONESTOPBIT;
	
	SetCommState(m_hComm, &dcb);

	m_isReceiving = true;
	m_receiveThread = AfxBeginThread(ReceiveRs232cFunc, this);

	return true;
}

⌨️ 快捷键说明

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