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
};

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haimianjie2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值