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

📄 vcsampledlg.cpp

📁 modbus通讯
💻 CPP
📖 第 1 页 / 共 4 页
字号:
case 6:
	//获取诊断的值
	m_cmbVerftyCode.GetWindowText(strVerifyCode);
	m_bListen=false;
	if(strVerifyCode=="0x04")
	{
		m_bListen=true;
	}
	//Hex转换Int begin
	sscanf(strVerifyCode,"0x%x",&nVeritfyCode);            
	//Hex转换Int end 
	//获取回送诊断校验的函数指针begin
	ackReport=(ACKPack)::GetProcAddress(m_hInst,"VerifyPack");
	if(ackReport)
	{
		//调用回送诊断校验的函数
		nResult=ackReport(m_nCommName,nStatioNo,nVeritfyCode,stDataField);
		if(nResult !=0)
		{
			m_EdMsg.GetWindowText(oldMsg);
			msg.Format("\r\n通过COM%d 回送诊断校验失败,错误代码:%d,错误消息:%s.%s",m_nCommName,nResult,getAckMsg(nResult),oldMsg);
			m_EdMsg.SetWindowText(msg);
			if(m_pStartaddr!=NULL)
			{
				delete [] m_pStartaddr;
				m_pStartaddr=NULL;
			}
			return ;
		}
		//获取接收到消息内容 begin
		for(int k=0;k<2;k++)
		{
			reviceMsg.Format("%s%d",strMsg,stDataField[k]);
			if(k < 1)
			{						
				strMsg =reviceMsg+"、";
			}
			else
			{
				strMsg =reviceMsg;
			}
		}
		//获取接收到消息内容 end; 
		m_EdMsg.GetWindowText(oldMsg);
		msg.Format("\r\n通过COM%d 回送诊断校验成功,消息内容:%s.%s",m_nCommName,strMsg,oldMsg);
		m_EdMsg.SetWindowText(msg);
	}
	//获取动态连接库中函数指针begin
	break;
default:
	break;
		}
	}
	if(m_pStartaddr!=NULL)
	{
		delete [] m_pStartaddr;
		m_pStartaddr=NULL;
	}
}

/*
*	拆分字符转换Short
*  输入:拆分的字符
*/
int CVcSampleDlg::SpiltString(CString value,int nNum)
{
	int nStart=0,nEnd=0;//起始位、结束位
	int nValue=0;
	char *chValue=NULL;
	int nIndex=0;
	int nTotal=0;
	CString strMsg="";
	while(value.GetLength()!=0)//查找有没有字符中是否有空格并返回空格的位置
	{
		nEnd=value.Find(" ");//获取空格的位置
		if(nEnd <0)
		{
			chValue=new  char[value.GetLength()+1] ;
			strncpy(chValue,value,value.GetLength());//获取字符的值
			chValue[value.GetLength()]='\0';
			value=value.Mid(value.GetLength(),value.GetLength()); //截取剩余的字符的值 
		} else
		{		  
			chValue=new  char[nEnd+1] ;
			strncpy(chValue,value,nEnd);//获取字符的值
			chValue[nEnd]='\0';
			value=value.Mid(nEnd+1,value.GetLength()); //截取剩余的字符的值 
		}
		sscanf(chValue,"%x",&nValue);//转换为十六进制的数据
		m_shWordValue[nIndex]=nValue;//保存到数组中
		if(nValue >65535)
		{
			strMsg.Format("元件值:%s,超过取值范围0-65535,请输入..",chValue);
			MessageBox(strMsg,"提示框",MB_ICONQUESTION);
			if (chValue!=NULL)
			{
				delete[] chValue;
				chValue=NULL;
			}
			return GREAT_NUM;
		}
		if (chValue!=NULL)
		{
			delete[] chValue;
			chValue=NULL;
		}
		nIndex++;
		nTotal++;
	}
	if(nTotal!=nNum)
	{
		return GREAT_VALUE;
	}
	return SUCCESS_SPINT;
}
/*
*	拆分字符转换Char
*  输入:拆分的字符
*/
int CVcSampleDlg::SpiltChar(CString value,int nNum)
{
	int nStart=0,nEnd=0;//起始位、结束位
	int nValue=0;
	char *chValue=NULL;
	int nIndex=0;
	int nTotal=0;
	CString strMsg="";
	while(value.GetLength()!=0)//查找有没有字符中是否有空格并返回空格的位置
	{
		nEnd=value.Find(" ");//获取空格的位置
		if(nEnd <0)
		{
			chValue=new  char[value.GetLength()+1] ;
			strncpy(chValue,value,value.GetLength());//获取字符的值
			chValue[value.GetLength()]='\0';
			value=value.Mid(value.GetLength(),value.GetLength()); //截取剩余的字符的值 
		} else
		{		  
			chValue=new  char[nEnd+1] ;
			strncpy(chValue,value,nEnd);//获取字符的值
			chValue[nEnd]='\0';
			value=value.Mid(nEnd+1,value.GetLength()); //截取剩余的字符的值 
		}
		sscanf(chValue,"%x",&nValue);//转换为十六进制的数据
		m_chBitValue[nIndex]=nValue;
		if(nValue >65535)
		{
			strMsg.Format("元件值:%s,超过取值范围0-65535,请输入..",chValue);
			MessageBox(strMsg,"提示框",MB_ICONQUESTION);
			if (chValue!=NULL)
			{
				delete[] chValue;
				chValue=NULL;
			}
			return GREAT_NUM;
		}
		if (chValue!=NULL)
		{
			delete[] chValue;
			chValue=NULL;
		}
		nIndex++;
		nTotal++;
	}
	m_chBitValue[nIndex+1]='\0';//加字符的结束位
	if(nTotal!=nNum)
	{
		return GREAT_VALUE;
	}
	return SUCCESS_SPINT;
}

/*
*	控制CEdit输入键盘值
*/
BOOL CVcSampleDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class

	if(WM_KEYDOWN == pMsg->message )
	{
		if(0x39 < (int) pMsg->wParam)//控制0-9
		{
			if((0x47 <= (int) pMsg->wParam))//控制'A'-'F','a'-'f'
			{						
				if (!((0x56<= (int) pMsg->wParam) &&(0x69 >=(int) pMsg->wParam)))//小键盘
				{
					return true;   
				}		       

			}

		} 
	}	
	return CDialog::PreTranslateMessage(pMsg);
}

/*
*	获取应答消息
*/
CString CVcSampleDlg::getAckMsg(int AckID)
{
	CString  msg;
	switch(AckID) {
case 1:
	msg="非法功能码";
	break;
case 2:
	msg="非法寄存器地址";
	break;
case 3:
	msg="数据个数错误";
	break;
case 92:
	msg="CRC校验码不正确";
	break;
case 93:
	msg="响应格式不合法";
	break;
case 94:
	msg="从机无响应";
	break;
case 95:
	msg="内存不足";
	break;
case 96:
	msg="端口不可用";
	break;
case 97:
	msg="无法打开端口";
	break;
case 98:
	msg="函数参数错误";
	break;
case 99:
	msg="尚未实现";
	break;
case 100:
	msg="无法导入动态库";
	break;
default:
	break;
	}
	return msg;
}
BOOL CVcSampleDlg::LoadJPGFile(const char *pPathname)
{
	CFile file;
	if( !file.Open( pPathname, CFile::modeRead) )
		return FALSE;

	m_nFileLen = file.GetLength();

	HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, m_nFileLen );
	LPVOID lpBuf = ::GlobalLock( hMem );
	if( file.Read( lpBuf, m_nFileLen ) != m_nFileLen )
		return FALSE;
	file.Close();

	m_pJPGBuffer = (char *)lpBuf;	
	::GlobalUnlock( hMem );

	if ( CreateStreamOnHGlobal( hMem, TRUE, &pStream ) !=S_OK ) 	
		return FALSE;

	if ( OleLoadPicture( pStream, m_nFileLen, TRUE, IID_IPicture, ( LPVOID * )&pPicture ) !=S_OK )
		return FALSE;

	return TRUE;
}

void CVcSampleDlg::DrawImage(int x, int y, CDC *pDC)
{
	if (pPicture != NULL) 
	{
		long nWidth,nHeight;  
		pPicture->get_Width( &nWidth );   
		pPicture->get_Height( &nHeight ); 		

		//图片原大显示
		CSize sz( nWidth, nHeight );
		pDC->HIMETRICtoDP( &sz );
		pPicture->Render(pDC->m_hDC,x,y,sz.cx,sz.cy,0,nHeight,nWidth-10,-nHeight,NULL);			
		/*
		//按窗口尺寸显示
		CRect rect;
		GetClientRect(&rect);
		pPicture->Render(pDC->m_hDC,x,y,rect.Width(),rect.Height(),0,nHeight,nWidth,-nHeight,NULL);*/

	}
}
/*
*	获取元件的最大个数
*/
int CVcSampleDlg::GetIMaxCount(int nItem)
{	
	int nValue=0;
	int nIndex=0;
	char strValue[4]={0};
	sprintf(strValue,"1%d%d",m_nOperIndex,nItem);
	sscanf(strValue,"%d",&nIndex);
	switch(nIndex) {
case 100:
	nValue=Y_COMPONENT_MAX;
	break;
case 101:
	nValue=X_COMPONENT_MAX;
	break;
case 102:
	nValue=M_COMPONENT_MAX;
	break;
case 103:
	nValue=SM_COMPONENT_MAX;
	break;
case 104:
	nValue=S_COMPONENT_MAX;
	break;
case 105:
	nValue=T_COMPONENT_MAX;
	break;
case 106:
	nValue=C_COMPONENT_MAX;
	break;
case 120:
	nValue=Y_COMPONENT_MAX;
	break;
case 121:
	nValue=X_COMPONENT_MAX;
	break;
case 122:
	nValue=M_COMPONENT_MAX;
	break;
case 123:
	nValue=SM_COMPONENT_MAX;
	break;
case 124:
	nValue=S_COMPONENT_MAX;
	break;
case 125:
	nValue=T_COMPONENT_MAX;
	break;
case 126:
	nValue=C_COMPONENT_MAX;
	break;
case 130:
	nValue=Y_COMPONENT_MAX;
	break;
case 131:
	nValue=X_COMPONENT_MAX;
	break;
case 132:
	nValue=M_COMPONENT_MAX;
	break;
case 133:
	nValue=SM_COMPONENT_MAX;
	break;
case 134:
	nValue=S_COMPONENT_MAX;
	break;
case 135:
	nValue=T_COMPONENT_MAX;
	break;
case 136:
	nValue=C_COMPONENT_MAX;
	break;
case 110:
	nValue=D_COMPONENT_MAX;
	break;
case 111:
	nValue=SD_COMPONENT_MAX;
	break;
case 112:
	nValue=Z_COMPONENT_MAX;
	break;
case 113:
	nValue=T_COMPONENT_CHAR_MAX;
	break;
case 114:
	nValue=C_COMPONENT_CHAR_MAX;
	break;
case 140:
	nValue=D_COMPONENT_MAX;
	break;
case 141:
	nValue=SD_COMPONENT_MAX;
	break;
case 142:
	nValue=Z_COMPONENT_MAX;
	break;
case 143:
	nValue=T_COMPONENT_CHAR_MAX;
	break;
case 144:
	nValue=C_COMPONENT_CHAR_MAX;
	break;
case 150:
	nValue=D_COMPONENT_MAX;
	break;
case 151:
	nValue=SD_COMPONENT_MAX;
	break;
case 152:
	nValue=Z_COMPONENT_MAX;
	break;
case 153:
	nValue=T_COMPONENT_CHAR_MAX;
	break;
case 154:
	nValue=C_COMPONENT_CHAR_MAX;
	break;
default:
	break;
	}
	return nValue;
}
void CVcSampleDlg::OnCbnSelchangeCmbitem()
{
	// TODO: Add your control notification handler code here
	int nTotal=0;
	int nIndex=0;
	nIndex=m_cmbItem.GetCurSel();
	nTotal=GetIMaxCount(nIndex);
	CString strValue="";
	strValue.Format("%d",nTotal);
	switch(m_nOperIndex) {
case 0:
	m_edNum.SetWindowText(strValue);
	break;
case 1:
	if(nTotal >125)
	{
		m_edNum.SetWindowText("125");
	}
	else
	{		
		m_edNum.SetWindowText(strValue);
	}
	break;
case 3:
	if(nTotal >1968)
	{		
		m_edNum.SetWindowText("1968");
	}
	else
	{
		m_edNum.SetWindowText(strValue);
	}
	break;
case 5:
	if(nTotal >120)
	{
		m_edNum.SetWindowText("120");
	}
	else
	{		
		m_edNum.SetWindowText(strValue);
	}
	break;
default:
	break;
	}
}

void CVcSampleDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CVcSampleDlg::OnBnClickedOpertionbtn2()
{
	// TODO: Add your control notification handler code here
	m_EdMsg.SetWindowText("");
}

void CVcSampleDlg::OnBnClickedResetbtn()
{
	// TODO: Add your control notification handler code here
	m_cmbItem.SetCurSel(0);
	m_cmbSationNo.SetCurSel(0);
	m_edStartAddr.SetWindowText("0");
	m_edValue.SetWindowText("");
	m_EdMsg.SetWindowText("");
	int nValue=0;
	int nIndex=0;
	char strValue[4]={0};
	switch(m_nOperIndex) {
case 0:
	m_edNum.SetWindowText("256");
	break;
case 1:
	m_edNum.SetWindowText("125");
	break;
case 2:
	m_edNum.SetWindowText("1");
	break;
case 3:
	m_edNum.SetWindowText("256");
	break;
case 4:
	m_edNum.SetWindowText("1");
	break;
case  5:
	m_edNum.SetWindowText("120");
	break;
case 6:
	m_edNum.SetWindowText("");
	m_cmbVerftyCode.SetCurSel(0);
	break;
case 7:
	m_cmbVerftyCode.SetCurSel(0);
default:
	break;
	}
}

⌨️ 快捷键说明

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