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

📄 atcommands.cpp

📁 串口开发的另一种方式.rar
💻 CPP
字号:
#include "stdafx.h"
#include "ATCommands.h"
#include "superSP.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

CATCommand::CATCommand()
: m_pWin(NULL)
{
}

CATCommand::~CATCommand()
{
	Release();
}

// 加载文件
BOOL CATCommand::LoadFile(TCHAR* FileName)
{
	int counter = 0;
	string TmpCurrentLine;
	ifstream iFile(FileName);
	
	if (iFile.fail())
	{
		return FALSE;
	}
	while (!iFile.eof())
	{
		STCommand *pTmpCommand = new STCommand;
		iFile >> pTmpCommand->iCommandCode;
		iFile >> pTmpCommand->strCommandName;
		iFile >> pTmpCommand->strExplain;

		Vcommands.push_back(pTmpCommand);
		counter++;
	}

	iTotalLines = counter;

	return TRUE;
}

// 添加一条命令
UINT CATCommand::AddCommand(STCommand *pCommand)
{
	vector<pSTCommand>::iterator p;
	for (p=Vcommands.begin(); p != Vcommands.end(); p++)
	{
		if ((*p)->iCommandCode == pCommand->iCommandCode)
		{
			AfxMessageBox("The command is exist!");	
			return 0;
		}
	}

	Vcommands.push_back(pCommand);
                                                                                                                                                         
	return pCommand->iCommandCode;
}

// 删除一条命令
BOOL CATCommand::DeleteCommand(UINT commandCode)
{
	vector<pSTCommand>::iterator p;
	for (p=Vcommands.begin(); p != Vcommands.end(); p++)
	{
		if ((*p)->iCommandCode == commandCode)
		{
			delete *p;
			Vcommands.erase(p);
			return TRUE;
			
		}
	}

	return FALSE;
}

// 保存文件
void CATCommand::SaveFile(TCHAR* FileName)
{
	ofstream oFile(FileName);

	for (int i=0; i<Vcommands.size(); i++)
	{
		oFile << Vcommands[i]->iCommandCode;
		oFile << ' ';
		oFile << Vcommands[i]->strCommandName;
		oFile << ' ';
		oFile << Vcommands[i]->strExplain;
		if (i != (Vcommands.size()-1) )
		{// 最后一行不敲回车,否则计算的文件的行数就会多一
			oFile << '\n';
		}
	}

	oFile << flush;
	oFile.close();
}

// 执行一条命令
void CATCommand::ExecuteCommand(const TCHAR* strCommand)
{
	int    size = strlen(strCommand);

	PACKAGE* pData = new PACKAGE;
	pData->iLen = size;
	strcpy((char *)pData->pData, strCommand);

	if( NULL != m_pWin)
	{
		m_pWin->SendMessage(m_winMsg, (WPARAM)pData);
	}
}

// 根据命令号执行一条命令
void CATCommand::ExecuteCommand(UINT commandCode)
{
	for (int i=0; i<Vcommands.size(); i++)
	{
		if (Vcommands[i]->iCommandCode == commandCode)
		{
			ExecuteCommand(Vcommands[i]->strCommandName.c_str());
		}
	}
}

// 释放资源: vector
void CATCommand::Release()
{
	vector<pSTCommand>::iterator p;
	for (p=Vcommands.begin(); p != Vcommands.end(); p++)
	{
		delete *p;
	}

	Vcommands.clear();
}

⌨️ 快捷键说明

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