C++实现上位机6:网络通讯类CCommnication

CCommnication头文件:

#pragma once

//#include "CCreateHex.h"
#include "CComminicationTool.h"
//#include "CEthernetTCP.h"
#include "CSerialPort.h"
#include "../Tool/CRC.h"
#include "../stdafx.h"

#define INSTRUCTION_SIZE 8
#define INSTRUCTION_LEGNTH 8
#define ONLINE_INSTRUCT_LEGNTH 16
#define DEBUG_INSTRUCT_LEGNTH 16
#define TCP_MODE 0
#define SERIAL_MODE 1

class CCommnication{
public:
	CCommnication();
	~CCommnication();

	//网络相关
	void SeletMode(int mode);//设置模式
	bool IsConnect();//返回是否连接
	
	bool ConnectDSPBySERIAL(std::string com = "COM3");//与DSP进行连接
	void DisConnection();
	bool SendInstruction();//发送单条指令

	//下载相关
		
	//网络数据操作相关
	char* ReadChannelData(int& countSize);//读数据
	char* ReadChannelData();//读数据
	bool SendData(void* data, DWORD size);//发送数据
	char* GetResult();

public:
	//CCreateHex m_createHex;//生成十六进制文件
	//CEthernetTCP m_ethernetTCP; //TCP通信
	CSerialPort m_serialPort;//串口通信
	std::string m_IP;
	int m_Port;
	int m_mode;//0 为TCP/IP通信 ; 1为 串口通信
	unsigned char m_blockSection;//要烧写的区域
	short m_current_address;//当前开展地址
	int m_instructSize;//指令条目
	//int m_OnlineInstructSize;//调试记录上次的发送的条数


};

CCommnication.cpp


#include "CCommnication.h"


CCommnication::CCommnication() :m_mode(TCP_MODE), m_current_address(0x0000), m_blockSection(0x0000)
, m_IP("192.168.2.40"), m_Port(5000){
	//BlockSection((unsigned char)m_createHex.GetBeginAddress(), 0x00);
}

CCommnication::~CCommnication(){
	
}

/***************************************************************************************************/
//网络相关

void CCommnication::SeletMode(int mode){
	if (mode == 0 || mode == 1){
		m_mode = mode;
	}
	else{
		throw ("SeletMode()未知模式选择");
	}	
}

bool CCommnication::IsConnect(){
	switch (m_mode){
	//case 0:return m_ethernetTCP.IsConnect(); break;
	case 1:return m_serialPort.IsConnect(); break;
	default:return false; break;
	}
}

bool CCommnication::ConnectDSPBySERIAL(std::string com){
	return m_serialPort.Connect(com);
}

void CCommnication::DisConnection(){
	switch (m_mode){
	//case 0:m_ethernetTCP.Disconnect(); break;
	case 1:m_serialPort.Disconnect(); break;
	default:break;
	}
}

/***************************************************************************************************/


/***************************************************************************************************/
//下载相关


/***************************************************************************************************/
//解析sHexPack数据包相关函数







/***************************************************************************************************/
//网络数据操作相关
char* CCommnication::ReadChannelData(int& countSize){
	char* ret;
	DWORD count;
	int l_TimeCount = 0;
	do{
		switch (m_mode){
		//case 0:ret = m_ethernetTCP.ReadData(count); break;
		case 1:ret = m_serialPort.ReadData(count); break;
		default:ret = NULL;
			break;
		}
		if (ret == NULL)return NULL;
		l_TimeCount++;
		if (l_TimeCount == 4){
			return NULL;
		}
	} while (count == 0);
	countSize = count;
	return ret;
}

char* CCommnication::ReadChannelData(){
	char* ret;
	DWORD count;
	int l_TimeCount = 0;
	do{
		switch (m_mode){
		//case 0:ret = m_ethernetTCP.ReadData(count); break;
		case 1:ret = m_serialPort.ReadData(count); break;
		default:ret = NULL;
			break;
		}
		if (ret == NULL)return NULL;
		l_TimeCount++;
		if (l_TimeCount == 20){
			return NULL;
		}
	} while (count == 0);
	return ret;
}

bool CCommnication::SendData(void* data, DWORD size){
	switch (m_mode){
	case 0:{
			  // m_ethernetTCP.SendData((char *)data, size);
	}break;
	case 1:{		   
			   if (m_serialPort.SendData((char *)data, size) == -1){
				   return false;
			   }
	}break;
	default:break;
	}
	return true;
}

char* CCommnication::GetResult(){
	switch (m_mode){
	//case 0:return m_ethernetTCP.m_rectDataBuff; break;
	case 1:return m_serialPort.m_resver_str; break;
	default:return NULL; break;
	}
}

/***************************************************************************************************/
//网络数据操作相关

aaa

你可能感兴趣的:(Visual,C++开发,Visual,C++开发)