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

📄 operationtooldlg.cpp

📁 采用文档类方法实现的一种串口通讯协议。可以借鉴参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				strInf += "Return is not regular.\n\r";

				mf_DispMsg(strInf);
				break;
			}
		}
	}
	else
	{
		strLast.Format("0X%X", nReturnCMDS);
		strInf  = "The return Command is ";
		strInf += strLast;
		strInf += "\n\r";

		strInf += "Return is not regular.\n\r";
		mf_DispMsg(strInf);
	}
}

//
//Send data and get data 
//
void COperationToolDlg::OnConvert() 
{
	// TODO: Add your control notification handler code here
	BYTE	abyReceiveVerPac[VERPACKETMAX] = "";	
		
	CWaitCursor WaitCursor;			//display working cursor, it disappear when this object is over
	
	mf_ParseCommand(m_nCMDH12, m_nParaCount, m_abyDataH, abyReceiveVerPac);
	
	mf_SplitPacket(abyReceiveVerPac);		//split packet

	((CButton *)GetDlgItem(ID_Convert))->EnableWindow(FALSE);

}

//
//get parameter 
//
void COperationToolDlg::OnNext() 
{
	// TODO: Add your control notification handler code here
	CString		strLabel;	
	static int	nCountClick = 0;
	
	UpdateData(TRUE);
	GetDlgItemText(ID_Next, strLabel);		

	if (strLabel == "Start")
	{				
		m_nParaCount = m_nValue;		//first is the parameter nCount 

		if (m_nParaCount > 0)
		{
			SetDlgItemText(ID_Next, "Next");
		}
		else
		{
			SetDlgItemText(ID_Next, "Stop");

			((CButton *)GetDlgItem(ID_Convert))->EnableWindow(TRUE);
			((CButton *)GetDlgItem(ID_Next))->EnableWindow(FALSE);
			((CEdit *)GetDlgItem(IDC_Value))->EnableWindow(FALSE);
		}

		nCountClick = 0;
		m_nCount = nCountClick + 1;		//m_nCount -- now filled nCount
	}
	else
	{
		if (nCountClick < m_nParaCount)
		{			
			m_abyDataH[nCountClick++] = m_nValue;	//fill value into array			

			m_nCount = nCountClick + 1;
			if (nCountClick == m_nParaCount)	
			{
				nCountClick = 0;
				m_nCount = nCountClick;

				SetDlgItemText(ID_Next, "Stop");

				((CButton *)GetDlgItem(ID_Convert))->EnableWindow(TRUE);
				((CButton *)GetDlgItem(ID_Next))->EnableWindow(FALSE);
				((CEdit *)GetDlgItem(IDC_Value))->EnableWindow(FALSE);
			}	//end nCountClick == m_nParaCount		
		}//end  <
	}		

	m_nValue = 0;
	UpdateData(FALSE);

}

//
//Initial slave unit
//
void COperationToolDlg::OnInitial() 
{
	// TODO: Add your control notification handler code here
	BOOL	bRet;

	BYTE	abyInitPacSend[VERPACKETMAX] = "";
	BYTE	abyInitPacReceive[VERPACKETMAX] = "";
	
	BYTE	bSupportProtocol = 0;
	DWORD	dwSendCount = 0;
	
	CWaitCursor WaitCursor;			//display different cursor.

	mf_DispMsg("", 1);					//clear the result
	dwSendCount = mf_CombineInitPac(abyInitPacSend, 1, 0);		//combine intial packet
	
	//communication
	bRet = m_ComDev.GetData(abyInitPacSend, dwSendCount, abyInitPacReceive);
	if (!bRet)
	{
		return;
	}
		
	bSupportProtocol = mf_DemInitPac( abyInitPacReceive );
	
/*
	SProtocol dlgSP;		//use when need to select diffent protocol 
	switch (SupportProtocol)
	{
	case 0:
		mf_DispMsg("No Protocol Supported");
		return ;		
	case 1:
		mf_DispMsg("AutoRAE Protocol Initial Completed");
		return ;		
	case 2:
		mf_DispMsg("ProRAE Suite Protocol Initial Completed");
		return ;		
	case 4:
		mf_DispMsg("ProRAE Studio Protocol Initial Completed");
		return ;
	case 3:
		mf_DispMsg("Support AutoRAE and ProRAE Suite Protocol");
		dlgSP.cProtocol=3;
		dlgSP.DoModal();			
		break;
	case 5:
		mf_DispMsg("Support AutoRAE and ProRAE studio Protocol");
		dlgSP.cProtocol=5;
		dlgSP.DoModal();
		break;
	case 6:
		mf_DispMsg("SSupport ProRAE Suite and ProRAE Studio Protocol");
		dlgSP.cProtocol=6;
		dlgSP.DoModal();		
		break;
	case 7:
		mf_DispMsg("Supported All Protocols");
		dlgSP.cProtocol=7;
		dlgSP.DoModal();
		break;
	default:
		return ;
	}
*/
	if ((bSupportProtocol & 0x07) == 0x00)
	{
		mf_DispMsg("No Protocol Supported");
	}
	else if ((bSupportProtocol & 0x07) == 0x04)
	{
		mf_DispMsg("ProRAE Studio Protocol Initial Completed");
	}
	else		//send the second step initail protocol
	{
		dwSendCount = mf_CombineInitPac(abyInitPacSend, 1, 1, 2);   //dlgSP.m_ProtocolSel);

		bRet = m_ComDev.GetData(abyInitPacSend, dwSendCount, abyInitPacReceive);
		if (!bRet)
		{
			return;
		}


		bSupportProtocol = mf_DemInitPac( abyInitPacReceive );		
		if ((bSupportProtocol & 0x04) == 0x04)
		{
			mf_DispMsg("ProRAE Studio Protocol Initial Completed.");			
		}
		else
		{
			mf_DispMsg("ProRAE Studio Protocol Initial Failed.");
		}
	}

	mf_SetPCMode();	//set pc mode
		/*switch (SupportProtocol)			//use when need to select diffent protocol 
		{
		case 1:		
			mf_DispMsg("AutoRAE Protocol Initial Completed");	
			break;
		case 2:
			mf_DispMsg("ProRAE Suite Protocol Initial Completed");	
			break;
		case 4:
			mf_DispMsg("ProRAE Studio Protocol Initial Completed");	
			break;
		default:
			mf_DispMsg("NO Protocol Selected");		
			break;
		}
	*/
}

//
//Combine intial packet 
//
UINT COperationToolDlg::mf_CombineInitPac(BYTE *O_pbyInitPac, BYTE I_byWireless, BYTE I_byInit2Flag, int I_nToolVer)
{
	ASSERT(O_pbyInitPac != NULL);

	BYTE byInitPac;
	BYTE byChecksum = 0;;
	UINT unbyteCount = 0;
	
	byInitPac = 0x80;
	O_pbyInitPac[0] = 0x7B;
		
	if (I_byInit2Flag == 1)
	{
		byInitPac |= 0x08;

		switch(I_nToolVer)
		{
		case 0:
			byInitPac |= 0x01;
			break;
		case 1:
			byInitPac |= 0x02;
			break;
		case 2:
			byInitPac |= 0x04;
			break;
		default:
			break;
		}
	}		

	if (I_byWireless == 0)	
	{
		byInitPac |= 0x00;
		O_pbyInitPac[1] = byInitPac;
		O_pbyInitPac[2] = 0x7D;
		unbyteCount = 3;
	}
	else
	{
		byInitPac |= 0x40;
		O_pbyInitPac[1] = byInitPac;
		O_pbyInitPac[2] = 0x04;
		O_pbyInitPac[3] = 0x05;
		O_pbyInitPac[4] = 0x01;
		for (int r = 0; r < 5; r++)
		{
			byChecksum += O_pbyInitPac[r];			
		}
		byChecksum = ~byChecksum + 1;
		O_pbyInitPac[5] = byChecksum;
		O_pbyInitPac[6] = 0x7D;
		unbyteCount = 7;
	}	

	return unbyteCount;
}

//
//Demodem initial packet
//
BYTE COperationToolDlg::mf_DemInitPac(BYTE *I_pbyInitResPac)
{
	ASSERT(I_pbyInitResPac != NULL);

	BYTE	abyInitResPac[INITPACKETMAX] = "";
	BYTE	byInitFlag = 0;
	
	BOOL	bflagRoll = FALSE;
	BOOL	bflag7BFirst = FALSE;
	
	int		nRoll = 0;
	int		nCount = 0;

	while (!bflagRoll)		//get right packet and delete unwanted information 
	{
		if ((I_pbyInitResPac[nRoll] != 0x7B) && (!bflag7BFirst))				
		{
			nRoll ++;
		}
		else
		{
			bflag7BFirst = TRUE;
			abyInitResPac[nCount] = I_pbyInitResPac[nRoll];

			if (I_pbyInitResPac[nRoll] == 0x7D)
			{
				bflagRoll = TRUE;
			}

			nRoll ++;
			nCount ++;
		}		
	}
		
	byInitFlag = abyInitResPac[1];		//get the supported protocol byte
	
	return (byInitFlag & 0x07);		//return 
}

//
//Combine Type III data packet 
//
UINT COperationToolDlg::mf_CombineVerPac(BYTE *O_pbyVerPac, int I_nCMDH, int I_nPacLen, BYTE *I_pbyDataH)
{
	ASSERT(O_pbyVerPac != NULL);
	ASSERT(I_pbyDataH != NULL);

	int		nLength;
	int		nCurrentPosition;
	BYTE	byChecksum = 0;

	O_pbyVerPac[0] = 0x7B;
	O_pbyVerPac[1] = 0x13;

	nLength = 6 + I_nPacLen;	//get packet total length
	O_pbyVerPac[2] = nLength;
	O_pbyVerPac[3] = 0x05;
	O_pbyVerPac[4] = 0x01;
	O_pbyVerPac[5] = (I_nCMDH & 0xFF00) >> 8;
	O_pbyVerPac[6] = (I_nCMDH & 0xFF);

	nCurrentPosition = 7;

	for (int r = 0; r < I_nPacLen; r++)
	{
		if ((I_pbyDataH[r] != 0x7B) && (I_pbyDataH[r] != 0x7D))
		{			
			O_pbyVerPac[nCurrentPosition] = I_pbyDataH[r];		
			nCurrentPosition ++;
		} 		
	}
		
	for (r = 0; r < nCurrentPosition; r++)	
	{
		byChecksum += O_pbyVerPac[r];
	}	
	byChecksum = ~byChecksum + 1;
	O_pbyVerPac[nCurrentPosition++] = byChecksum ;

	O_pbyVerPac[nCurrentPosition++] = 0x7D;

	return static_cast<UINT>(nCurrentPosition);
}

//
//demodem Type III packet; not include every case 
//
void COperationToolDlg::mf_DemVerPac(BYTE *I_pbyVerResPac, short int &O_nCMDS, short int &O_nPacLen, BYTE *O_pbyDataS)
{
	ASSERT(I_pbyVerResPac != NULL);
	ASSERT(O_pbyDataS != NULL);

	BYTE	abyVerResPac[VERPACKETMAX] = "";	
	BYTE	byChecksum = 0;

	BOOL	bflagRoll = FALSE;
	BOOL	bflag7BFirst = FALSE;
	
	short	nLocalCMDS = 0;
	int		nRoll = 0;
	int		nCount = 0;

	for (int r = 0; r < 150; r++)
	{
		abyVerResPac[r] = 0;
	}

	while (!bflagRoll)
	{
		if ((I_pbyVerResPac[nRoll] != 0x7B)  && (!bflag7BFirst))
		{
			nRoll ++;
		}
		else
		{
			bflag7BFirst = TRUE;
			abyVerResPac[nCount] = I_pbyVerResPac[nRoll];

			if ((I_pbyVerResPac[nRoll] == 0x7B) && (I_pbyVerResPac[nRoll + 1] == 0x7B))							
			{
				nRoll ++;			
			}

			if (I_pbyVerResPac[nRoll] == 0x7D) 
			{
				if  (I_pbyVerResPac[nRoll + 1] == 0x7D)	
				{
					nRoll ++;									
				}
				else
				{
					bflagRoll = TRUE;
				}
			}

			nRoll ++;
			nCount ++;
		}		
	}
		
	for (r = 0; r < (nCount-2); r++)	
	{
		byChecksum += abyVerResPac[r];	
	}
	byChecksum = ~byChecksum + 1;
	
	if (byChecksum == abyVerResPac[nCount-2])
	{	
		nLocalCMDS  = (abyVerResPac[5] & 0xFF) << 8;
		nLocalCMDS += abyVerResPac[6] & 0xFF;
		O_nCMDS  = nLocalCMDS;

		O_nPacLen = abyVerResPac[2];
		for (r = 0; r < (O_nPacLen - 6); r++)
		{
			O_pbyDataS[r] = abyVerResPac[7 + r];
		}

		O_nPacLen = O_nPacLen - 6;
	}			
}

//
//enter into com setup
//
void COperationToolDlg::OnComSetup() 
{
	CComSetup *dlg = new CComSetup(this,m_ComDev.GetDCB());	

	char port;
	itoa(m_nComPort, &port, 10);
	dlg->m_strComPort = port;

	if (dlg->DoModal() == IDOK)
	{
		m_nComPort = atoi(dlg->m_strComPort) ;
		m_ComDev.InitPort(this, 
							m_nComPort, 
							atoi(dlg->m_strComBaud),
							dlg->m_strComCheck[0],
							atoi(dlg->m_strComData),
							atoi(dlg->m_strComStop));
	}
	
	delete dlg;
	

}

//
//Display information in richedit and clear the richedit
//
void COperationToolDlg::mf_DispMsg(CString I_strMsg, int I_nClear)
{
	if (I_nClear == 0) 
	{
		m_strSlaveSponse += I_strMsg;			
	}
	else	
	{
		m_strSlaveSponse = "";
	}	

	UpdateData(FALSE);
}

//
//Get Data
//
BOOL COperationToolDlg::mf_RepeatReadData(short &O_nReadDataCount, BYTE *O_pbyReadData)
{
	ASSERT(O_pbyReadData != NULL);

	BYTE	abyReceiveVerPac[VERPACKETMAX] = "";
	BYTE	abyLocalDataS[VERPACKETMAX] = "";

	short	nLocalParaCount = 0;
	short	nReturnCMDS = 0;

	BOOL	bRet;
		
	if (m_abyDataH[1] < 0xFF)		//adjust packet index
	{
		m_abyDataH[1] ++;		
	}
	else
	{
		m_abyDataH[0] ++;
		m_abyDataH[1] = 0;
	}

	bRet = mf_ParseCommand(m_nCMDH12, m_nParaCount, m_abyDataH, abyReceiveVerPac);
	if (!bRet)
	{		
		return FALSE;
	}
					
	mf_DemVerPac(abyReceiveVerPac, nReturnCMDS, nLocalParaCount, abyLocalDataS);
	
	O_nReadDataCount = nLocalParaCount;
	for (int r = 0; r < nLocalParaCount; r++)
	{
		O_pbyReadData[r] = abyLocalDataS[r];
	}

	return TRUE;

}

//
//parse command and get data from comm
//
BOOL COperationToolDlg::mf_ParseCommand(int I_nCMD, int I_nInDataCount, BYTE *I_pbyDataH, BYTE *O_pbyReceivePac)
{
	BYTE	abySendVerPac[VERPACKETMAX] = "";
	DWORD	dwSendCount = 0;
			
	//Combine Type III Packet
	dwSendCount = mf_CombineVerPac(abySendVerPac, I_nCMD, I_nInDataCount, I_pbyDataH);
	//com communication
	BOOL Ret = m_ComDev.GetData(abySendVerPac, dwSendCount, O_pbyReceivePac);
	if (!Ret)
	{
		return FALSE;
	}

	return TRUE;
}

BOOL COperationToolDlg::mf_SetPCMode()
{
	//set communication mode to mode 9
	m_abyDataH[0] = 0x08;
	m_nCMDH12 = 0x228A;
	m_nParaCount = 1;
	BOOL bRet = mf_ParseCommand(m_nCMDH12, m_nParaCount, m_abyDataH, NULL);
	if (!bRet)
	{
		::AfxMessageBox("Set PC mode Failed");
		return FALSE;
	}
	
	//set communication mode to mode 8
	m_abyDataH[0] = 0x09;
	bRet = mf_ParseCommand(m_nCMDH12, m_nParaCount, m_abyDataH, NULL);
	if (!bRet)
	{
		::AfxMessageBox("Set PC mode Failed");
		return FALSE;
	}

	return TRUE;
}

void COperationToolDlg::OnCancel() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();
}

HBRUSH COperationToolDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

BOOL COperationToolDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDialog::PreTranslateMessage(pMsg);
}


void COperationToolDlg::OnOpenFile() 
{
	// TODO: Add your control notification handler code here
	CString strOpenName;
	SHELLEXECUTEINFO ShExecInfo = {0};

	char szFilePath[_MAX_PATH] = "";
	char szParameters[_MAX_PATH] = "";

	if ((_getcwd(szFilePath, _MAX_PATH)) == NULL)
	{
		TRACE("Get File Path Error\n");
	}
	
	strcpy(szParameters, szFilePath);
	strcat(szParameters, "\\DataLog.txt");

	strOpenName = _T("Notepad.exe");

	ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
	ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
	ShExecInfo.hwnd = NULL;
	ShExecInfo.lpVerb = NULL;
	ShExecInfo.lpFile = strOpenName;		
	ShExecInfo.lpParameters = szParameters;	
	ShExecInfo.lpDirectory = NULL;
	ShExecInfo.nShow = SW_SHOW;
	ShExecInfo.hInstApp = NULL;	
	ShellExecuteEx(&ShExecInfo);

}

⌨️ 快捷键说明

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