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

📄 cmdinterpret.cpp

📁 PDA串口通讯程序,实现收发功能并根据协议解析等功能
💻 CPP
字号:
// CmdInterpret.cpp: implementation of the CCmdInterpret class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RddDbg.h"
#include "CmdInterpret.h"

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

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

CCmdInterpret::CCmdInterpret()
{
	m_CmdArray.SetSize(0,10);
}

CCmdInterpret::~CCmdInterpret()
{
	for(int u=0;u<=m_CmdArray.GetUpperBound();u++)
	{
		if(m_CmdArray[u].pCmdCode!=NULL)
		{
			delete m_CmdArray[u].pCmdCode;
			m_CmdArray[u].pCmdCode=NULL;
		}
	}
}

int CCmdInterpret::AddCmdCode(LPCTSTR pCmd)//增加命令,返回命令所在的数组序号
{
	OneCmd cmd;
	cmd.pCmdCode=new TCHAR[wcslen(pCmd)+1];
	memset(cmd.pCmdCode,0,wcslen(pCmd)+1);
	wcscpy(cmd.pCmdCode, pCmd);
	cmd.uLength=wcslen(pCmd);
	cmd.uPosition=0;
	return m_CmdArray.Add(cmd)+1;
}

BOOL CCmdInterpret::ReadCmd(int iNo,CString& sCmd)//通过序号返回命令
{
	iNo--;
	ASSERT(iNo<=m_CmdArray.GetUpperBound());
	sCmd.Format(_T("%s"),m_CmdArray[iNo].pCmdCode);
	return TRUE;
}

UINT CCmdInterpret::ReadAChar(TCHAR cChar)//读入一个字符,如果正好组成一个命令,返回命令的序号
{
	UINT uRet=0;
	OneCmd cmd;
	for(int i=0;i<=m_CmdArray.GetUpperBound();i++)
	{
		cmd=m_CmdArray.GetAt(i);
		//等于?号,或者等于字符
		if((cmd.pCmdCode[cmd.uPosition]=='?')
			||(cChar==cmd.pCmdCode[cmd.uPosition]))
		{
			cmd.uPosition++;
			if(cmd.uPosition==cmd.uLength)
			{
				uRet=i+1;
				goto exit;
			}
		}
		else if(cmd.pCmdCode[cmd.uPosition]=='*')
		{
			if(cmd.uPosition+1==cmd.uLength) //说明*结尾
			{
				uRet=i+1;
				goto exit;
			}
			if(cChar==cmd.pCmdCode[cmd.uPosition+1])//判断*号的下一个字符
			{
				cmd.uPosition+=2;//包括*的长度和接下来的字符长度
				if(cmd.uPosition==cmd.uLength) 
				{
					uRet=i+1;
					goto exit;
					
				}
			}
		}
		else 
		{
			cmd.uPosition=0;
		}
		m_CmdArray[i]=cmd;
	}
exit:
	if(uRet!=0)
	{
		//所有位移清零
		for(i=0;i<=m_CmdArray.GetUpperBound();i++)
		{
			m_CmdArray[i].uPosition=0;
		}
	}
	return uRet;
}

⌨️ 快捷键说明

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