C++实现上位机3:实现串口控制类之抽象基类设计

前面文章讲解了运用输出流打印日志,本文中不再详细讲解,如果想了解详细实现可以查看:

C++实现上位机2:运用C++输出文件流ofstream打印日志

__AFX_H__是windows系统定义的宏,它包含在afx.h文件中,如果定义了它则说明使用了MFC,如果没有定义则没有使用MFC.

如果定义了MFC弹出对话框,如果没有打印消息到txt文件中。

#ifdef __AFX_H__
	void PrintMessage(std::string message){
		AfxMessageBox(CString(message.c_str()));
	}
#else
	void PrintMessage(std::string message){
		//std::cout << message << std::endl;
		if (p_COperationLog == NULL){
			p_COperationLog = new COperationLog();
		}
		p_COperationLog->AddLog(message);
	}
#endif

虚函数与纯虚函数:

 

串口控制类基类源代码:

#pragma once
#include "../stdafx.h"

extern COperationLog *p_COperationLog;

class CComminicationTool{
// Construction
public:
	CComminicationTool() : m_isTEXT(false) , dwError(0){};//初始化
	virtual ~CComminicationTool(){};

// Attributes:
protected:
	bool m_isTEXT;//ture 为使用文本ascII进行传输 ;false 为使用十六进行传输
	DWORD dwError;//获取错误码

// Overrides
public:
	virtual bool Connect() = 0;//连接
	virtual bool Disconnect() = 0;//不连接
	virtual DWORD SendData(void* data, DWORD size) = 0;//发送数据 (指向数据的地址,数据大小(单位为字节))
	virtual char* ReadData(DWORD& wCount) = 0;//读取数据 (数据长度的引用 (单位为字节))
	

// Implementation
protected:
	DWORD GetError(){ return dwError; }//获取错误码
#ifdef __AFX_H__
	void PrintMessage(std::string message){
		AfxMessageBox(CString(message.c_str()));
	}
#else
	void PrintMessage(std::string message){
		//std::cout << message << std::endl;
		if (p_COperationLog == NULL){
			p_COperationLog = new COperationLog();
		}
		p_COperationLog->AddLog(message);
	}
#endif
};

 

你可能感兴趣的:(C,上位机,MFC,虚函数,Visual,C++开发,Visual,C++开发)