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

📄 plc_protocol_testdlg.cpp

📁 本源代码演示了通过串口利用MPI协议与西门子PLC S7-300系统通讯的实现方式。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	case 2:								//M
		m_iErrorCode = PLC_W_M(m_iBitDeviceAdd,m_iBitNo,false);
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}	
	
	    break;
	case 3:								//DB
		m_iErrorCode = PLC_W_DB(m_iBitDBblock,m_iBitDeviceAdd,m_iBitNo,false);
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}	
	    break;
	default:
	    break;
	}
	UpdateData(FALSE);
}

void CPLC_Protocol_testDlg::OnBtnOn()							//Set bit with 1
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_csCommunicationStatus = _T("");
//	int result;
	int index = m_cmbBitDevice.GetCurSel();
	switch(index)
	{
	case 0:								// I
		break;
	case 1:								//Q
		m_iErrorCode = PLC_W_Q(m_iBitDeviceAdd,m_iBitNo,true);	//Set Q bit as 1
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}	
		break;
	case 2:								//M
		m_iErrorCode = PLC_W_M(m_iBitDeviceAdd,m_iBitNo,true);
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}	
	
	    break;
	case 3:								//DB
		m_iErrorCode = PLC_W_DB(m_iBitDBblock,m_iBitDeviceAdd,m_iBitNo,true);
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}	
	    break;
	default:
	    break;
	}
	UpdateData(FALSE);
}

void CPLC_Protocol_testDlg::OnSelchangeCmbWordDevice()			//select device of word test
{
	// TODO: Add your control notification handler code here
	m_csCommunicationStatus = _T("");
	//0 I,1 Q,2 M,3 DB,4 C
	int index = m_cmbWordDevice.GetCurSel();					//get the selected index
	switch(index)
	{
	case 0:									//I
		GetDlgItem(IDC_EDIT_WORD_DBBLOCK)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_WORDWRITE)->EnableWindow(FALSE);
		break;
	case 1:									//Q
	case 2:									//M
	case 4:									//C
		GetDlgItem(IDC_EDIT_WORD_DBBLOCK)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_WORDWRITE)->EnableWindow(TRUE);
		break;
	case 3:									//DB
		GetDlgItem(IDC_EDIT_WORD_DBBLOCK)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_WORDWRITE)->EnableWindow(TRUE);
		break;
	default:
		break;
	}
}


void CPLC_Protocol_testDlg::OnBtnWordread() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_csCommunicationStatus = _T("");
	int index = m_cmbWordDevice.GetCurSel();	//get the index of device
	WORD result;								//store the read result
	short temp = 0; 
	switch(index)
	{
	case 0:									//IW
		m_iErrorCode = PLC_R_IW(m_iWordDeviceAdd,1,&result);	//read I
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
		break;
	case 1:									//QW
		m_iErrorCode = PLC_R_QW(m_iWordDeviceAdd,1,&result);	//read Q
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);		
		}
		break;
	case 2:								 //MW
		m_iErrorCode = PLC_R_MW(m_iWordDeviceAdd,1,&result); //read M
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
	    break;
	case 3:								//DBW
		m_iErrorCode = PLC_R_DBW(m_iWordDBblock,m_iWordDeviceAdd,1,&result); //read DB
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
	    break;
	case 4:								//CW
		m_iErrorCode = PLC_R_C(m_iWordDeviceAdd,1,&result);
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
		temp = (short)result;
		SwapShort(temp);
		result = (unsigned short)temp;
		break;
	default:
	    break;
	}
	m_csWordReadResult.Format("%d",result);
	UpdateData(FALSE);
}

void CPLC_Protocol_testDlg::OnBtnWordwrite()					//write word
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	WORD sWordWriteValue;									// store the write value
	sWordWriteValue = (short)atol(m_csWordWriteValue);
	m_csCommunicationStatus = _T("");
	int index = m_cmbWordDevice.GetCurSel();
	switch(index)
	{
	case 0:									//IW
	case 4:									//CW
		break;
	case 1:									//QW
		m_iErrorCode = PLC_W_QW(m_iWordDeviceAdd,1,&sWordWriteValue);
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
		break;
	case 2:									//MW
		m_iErrorCode = PLC_W_MW(m_iWordDeviceAdd,1,&sWordWriteValue);
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}	
	    break;
	case 3:									//DBW
		m_iErrorCode = PLC_W_DBW(m_iWordDBblock,m_iWordDeviceAdd,1,&sWordWriteValue);
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}	
	    break;
	default:
	    break;
	}
	UpdateData(FALSE);

}

void CPLC_Protocol_testDlg::OnSelchangeCmbDwordDevice() 
{
	// TODO: Add your control notification handler code here
	m_csCommunicationStatus = _T("");
		//0 I,1 Q,2 M,3 DB,4 T
	int index = m_cmbDWordDevice.GetCurSel();
	switch(index)
	{
	case 0:									//I
		GetDlgItem(IDC_EDIT_DWORD_DBBLOCK)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_DWORDWRITE)->EnableWindow(FALSE);
		break;
	case 1:									//Q
	case 2:									//M
	case 4:									//C
		GetDlgItem(IDC_EDIT_DWORD_DBBLOCK)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_DWORDWRITE)->EnableWindow(TRUE);
		break;
	case 3:									//DB
		GetDlgItem(IDC_EDIT_DWORD_DBBLOCK)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_DWORDWRITE)->EnableWindow(TRUE);
		break;
	default:
		break;
	}
}

//////////////////////////////////////////////////////////////////////////
// If we want to read or write a float or long values which use 4 bytes,we
// can use a float or long type value or a char[4] array. Then we can change
// the value as a long or float type. If it is a float type, we should use
// Gp_to_Float to get the value, Float_to_Gp to write the value. Use SwapLong
// to set the value if we need a long type.
void CPLC_Protocol_testDlg::OnBtnDwordread() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CString str = _T(" ");
	short sValue = 0;
	m_csCommunicationStatus = _T("");
	int index = m_cmbDWordDevice.GetCurSel();
// 	float result;							//store the PLC type float
	char result[4];
	switch(index)
	{
	case 0:									//ID
//		m_iErrorCode = PLC_R_ID(m_iDWordDeviceAdd,1,&result);	//read I
		m_iErrorCode = PLC_R_ID(m_iDWordDeviceAdd,1,result);	//read I

		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
//		Gp_to_Float(&result,&m_fDWordReadResult);	//change the gp type to IEEE type float store in m_fWordReadRedult
		Gp_to_Float(&result,&m_fDWordReadResult);	//change the gp type to IEEE type float store in m_fWordReadRedult
		break;
	case 1:									//QD
//		m_iErrorCode = PLC_R_QD(m_iDWordDeviceAdd,1,&result);	//read Q
		m_iErrorCode = PLC_R_QD(m_iDWordDeviceAdd,1,result);	//read Q

		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
//		Gp_to_Float(&result,&m_fDWordReadResult); //change the gp type to IEEE type float store in m_fWordReadRedult	
		Gp_to_Float(result,&m_fDWordReadResult); //change the gp type to IEEE type float store in m_fWordReadRedult	
		break;
	case 2:								 //MD
//		m_iErrorCode = PLC_R_MD(m_iDWordDeviceAdd,1,&result);  //read M
		m_iErrorCode = PLC_R_MD(m_iDWordDeviceAdd,1,&result);  //read M
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
//		Gp_to_Float(&result,&m_fDWordReadResult);	//change the gp type to IEEE type float store in m_fWordReadRedult
		Gp_to_Float(result,&m_fDWordReadResult);	//change the gp type to IEEE type float store in m_fWordReadRedult
	    break;
	case 3:								//DBD
//		m_iErrorCode = PLC_R_DBD(m_iDWordDBblock,m_iDWordDeviceAdd,1,&result); //read DB
		m_iErrorCode = PLC_R_DBD(m_iDWordDBblock,m_iDWordDeviceAdd,1,result); //read DB
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
//		Gp_to_Float(&result,&m_fDWordReadResult); //change the gp type to IEEE type float store in m_fWordReadRedult
		Gp_to_Float(result,&m_fDWordReadResult); //change the gp type to IEEE type float store in m_fWordReadRedult

	    break;
	case 4:								//TD
		m_iErrorCode = PLC_R_T(m_iDWordDeviceAdd,1,result); //read Time
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
		sValue = *(short*)result;
		SwapShort(sValue);
		//When we set the time greater than 1 hour,Then the return value will be a number greater than 3000
		//if greater than 1 minute and less than 1 hour,the number will be greater than 2000
		//if less than a minute, the number will be greater than 1000
		switch(sValue/1000)
		{
		case 1:
			sValue -= 1000;
			str.Format("时间为%d秒,%d毫秒",sValue/10,sValue%10*100);
			break;
		case 2:
			sValue -= 2000;
			str.Format("时间为%d分,%d秒",sValue/60,sValue%60);
			break;
		case 3:
			sValue -= 3000;
			str.Format("时间为%d小时,%d分,%d秒",sValue/360,(sValue - sValue/360*360)/6,sValue%6*10);
			break;
		default:
			break;
		}
		MessageBox(str);
	    break;
	default:
	    break;
	}
	UpdateData(FALSE);	
}

void CPLC_Protocol_testDlg::OnBtnDwordwrite()	//write dword
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_csCommunicationStatus = _T("");
	int index = m_cmbDWordDevice.GetCurSel();  //get the index of selected device
//	float fValue;							  //to store the gp type with to be written into PLC
	char fValue[4];
	Float_to_Gp(&m_fDWordWriteValue,fValue); //change IEEE type to S7 type
	switch(index)
	{
	case 0:									//ID	can't write I, I is readonly
		break;
	case 1:									//QD
		m_iErrorCode = PLC_W_QD(m_iDWordDeviceAdd,1,fValue);  //write Q
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}
		break;
	case 2:									//MD
		m_iErrorCode = PLC_W_MD(m_iDWordDeviceAdd,1,fValue); //Write M
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}	
		break;
	case 3:									//DBD
		m_iErrorCode = PLC_W_DBD(m_iDWordDBblock,m_iDWordDeviceAdd,1,fValue); //write DB
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}	
		break;
	case 4:									//TD is read only
		break;	
	default:
		break;
	}
	UpdateData(FALSE);	
}

⌨️ 快捷键说明

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