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

📄 plc_protocol_testdlg.cpp

📁 本源代码演示了通过串口利用MPI协议与西门子PLC S7-300系统通讯的实现方式。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// PLC_Protocol_testDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PLC_Protocol_test.h"
#include "PLC_Protocol_testDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPLC_Protocol_testDlg dialog

CPLC_Protocol_testDlg::CPLC_Protocol_testDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPLC_Protocol_testDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPLC_Protocol_testDlg)
	m_iBitNo = 0;									//bit number of bit test
	m_iBitDBblock = 0;								//block number of bit test
	m_iBitDeviceAdd = 0;							//device address of bit test
	m_iBitReadResult = 0;							//result of bit test,0 or1
	m_csCommunicationStatus = _T("");				//to show information
	m_iDWordDBblock = 0;							//DB block number of DWord test
	m_iDWordDeviceAdd = 0;							//device address of DWord test
	//m_iSlaveID = 0;									
	m_iWordDBblock = 0;								//DB block number of word test
	m_iWordDeviceAdd = 0;							//device address of word test
	m_fDWordReadResult = 0.0f;						//read result of dword test,float type
	m_fDWordWriteValue = 0.0f;						//write value of dword test,float type
	m_csWordReadResult = _T("");					//to show the word read result,because there is no USHORT type with EditBox control
	m_csWordWriteValue = _T("");					//to show write value of word test
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPLC_Protocol_testDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPLC_Protocol_testDlg)
	DDX_Control(pDX, IDC_CMB_BIT_DEVICE, m_cmbBitDevice);
	DDX_Control(pDX, IDC_CMB_WORD_DEVICE, m_cmbWordDevice);
	DDX_Control(pDX, IDC_CMB_DWORD_DEVICE, m_cmbDWordDevice);
	DDX_Text(pDX, IDC_EDIT_BIT_BITNO, m_iBitNo);
	DDX_Text(pDX, IDC_EDIT_BIT_DBBLOCK, m_iBitDBblock);
	DDX_Text(pDX, IDC_EDIT_BIT_DEVICEADD, m_iBitDeviceAdd);
	DDX_Text(pDX, IDC_EDIT_BIT_READRESULT, m_iBitReadResult);
	DDX_Text(pDX, IDC_EDIT_COMMUNICATION_STATUS, m_csCommunicationStatus);
	DDX_Text(pDX, IDC_EDIT_DWORD_DBBLOCK, m_iDWordDBblock);
	DDX_Text(pDX, IDC_EDIT_DWORD_DEVICEADD, m_iDWordDeviceAdd);
	DDX_Text(pDX, IDC_EDIT_WORD_DBBLOCK, m_iWordDBblock);
	DDX_Text(pDX, IDC_EDIT_WORD_DEVICEADD, m_iWordDeviceAdd);
	DDX_Text(pDX, IDC_EDIT_DWORD_READRESULT, m_fDWordReadResult);
	DDX_Text(pDX, IDC_EDIT_DWORD_WRITEVALUE, m_fDWordWriteValue);
	DDX_Text(pDX, IDC_EDIT_WORD_READRESULT, m_csWordReadResult);
	DDX_Text(pDX, IDC_EDIT_WORD_WRITEVALUE, m_csWordWriteValue);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPLC_Protocol_testDlg, CDialog)
	//{{AFX_MSG_MAP(CPLC_Protocol_testDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_OPEN, OnBtnOpen)
	ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
	ON_BN_CLICKED(IDC_BTN_EXIT, OnBtnExit)
	ON_CBN_SELCHANGE(IDC_CMB_BIT_DEVICE, OnSelchangeCmbBitDevice)
	ON_BN_CLICKED(IDC_BTN_BITREAD, OnBtnBitread)
	ON_BN_CLICKED(IDC_BTN_OFF, OnBtnOff)
	ON_BN_CLICKED(IDC_BTN_ON, OnBtnOn)
	ON_CBN_SELCHANGE(IDC_CMB_WORD_DEVICE, OnSelchangeCmbWordDevice)
	ON_BN_CLICKED(IDC_BTN_WORDREAD, OnBtnWordread)
	ON_BN_CLICKED(IDC_BTN_WORDWRITE, OnBtnWordwrite)
	ON_CBN_SELCHANGE(IDC_CMB_DWORD_DEVICE, OnSelchangeCmbDwordDevice)
	ON_BN_CLICKED(IDC_BTN_DWORDREAD, OnBtnDwordread)
	ON_BN_CLICKED(IDC_BTN_DWORDWRITE, OnBtnDwordwrite)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPLC_Protocol_testDlg message handlers

BOOL CPLC_Protocol_testDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_cmbBitDevice.SetCurSel(1);		//Q is selected
	m_cmbWordDevice.SetCurSel(1);		//Q is selected
	m_cmbDWordDevice.SetCurSel(1);		//Q is selected
	
	GetDlgItem(IDC_EDIT_BIT_DBBLOCK)->EnableWindow(FALSE);	//Make sure the DB block EditBox is disable.
	GetDlgItem(IDC_EDIT_WORD_DBBLOCK)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_DWORD_DBBLOCK)->EnableWindow(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPLC_Protocol_testDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPLC_Protocol_testDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPLC_Protocol_testDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPLC_Protocol_testDlg::OnBtnOpen() 
{
	// TODO: Add your control notification handler code here
	// To fill the address struct of PLC. if you want to open more than one PLC,
	// You have to define a array with a greater number than the PLCs. For instance,
	//if you want to link two PLCs,you have to define a adr_table_type add[3];
	//the third address's adr have to be 0. The same as one plc
	adr_table_type addr[2];
	addr[0].adr = 2;
	addr[0].slotno = 2;
	addr[0].rackno = 0;
	addr[0].segmentid = 0;
	addr[1].adr = 0;		//it is said there is no PLC below
	//if you want to link more than one PLCs,you have to use New_ss(no) function to activate the PLCs
	m_iErrorCode = OpenPLC(1,"S7ONLINE",addr);			//Link to PLC
	if (m_iErrorCode != 0)								//if error 
	{
		ErrorMessage(m_iErrorCode,m_buffer);
		m_csCommunicationStatus.Format("%s",m_buffer);
	}
	else
	{
		m_csCommunicationStatus = _T("PLC load successful!");	//show message
	}
	UpdateData(FALSE);
}



void CPLC_Protocol_testDlg::OnBtnClose() 
{
	// TODO: Add your control notification handler code here
	m_csCommunicationStatus = _T("");						//Make sure the message is empty
	m_iErrorCode = ClosePLC();								//end the link
	if (m_iErrorCode)
	{
		ErrorMessage(m_iErrorCode,m_buffer);
		m_csCommunicationStatus.Format("%s",m_buffer);
	}
	else
	{
		m_csCommunicationStatus = _T("PLC is closed");
	}
	UpdateData(FALSE);
}

void CPLC_Protocol_testDlg::OnBtnExit()						// application exit
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CPLC_Protocol_testDlg::OnSelchangeCmbBitDevice()		//select the device type of bit test
{
	// TODO: Add your control notification handler code here
	m_csCommunicationStatus = _T("");
	//0 I, 1 Q, 2 M, 3 DB
	int index = m_cmbBitDevice.GetCurSel();					//to get the selected index
	switch(index)
	{
	case 0:			//I,read only
		(GetDlgItem(IDC_BTN_ON))->EnableWindow(FALSE);
		(GetDlgItem(IDC_BTN_OFF))->EnableWindow(FALSE);
		(GetDlgItem(IDC_EDIT_BIT_DBBLOCK))->EnableWindow(FALSE);
		break;
	case 1:
	case 2:
		(GetDlgItem(IDC_BTN_ON))->EnableWindow(TRUE);
		(GetDlgItem(IDC_BTN_OFF))->EnableWindow(TRUE);
		(GetDlgItem(IDC_EDIT_BIT_DBBLOCK))->EnableWindow(FALSE);
		break;
	case 3:
		(GetDlgItem(IDC_BTN_ON))->EnableWindow(TRUE);
		(GetDlgItem(IDC_BTN_OFF))->EnableWindow(TRUE);
		(GetDlgItem(IDC_EDIT_BIT_DBBLOCK))->EnableWindow(TRUE);
		break;
	default:
		break;
	}
}

void CPLC_Protocol_testDlg::OnBtnBitread()						//Read bit
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_csCommunicationStatus = _T("");
	//0 I, 1 Q, 2 M, 3 DB
	int result;
	int index = m_cmbBitDevice.GetCurSel();
	switch(index)
	{
	case 0:									//I
		m_iErrorCode = PLC_R_I(m_iBitDeviceAdd,m_iBitNo,&result);		//read I bit
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);	
		}
		m_iBitReadResult = result;
		break;
	case 1:									//Q
		m_iErrorCode = PLC_R_Q(m_iBitDeviceAdd,m_iBitNo,&result);	 //read Q bit
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);	
		}
		m_iBitReadResult = result;	
		break;
	case 2:
		m_iErrorCode = PLC_R_M(m_iBitDeviceAdd,m_iBitNo,&result);	//read M  bit
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);	
		}
		m_iBitReadResult = result;	
	    break;
	case 3:
		m_iErrorCode = PLC_R_DB(m_iBitDBblock,m_iBitDeviceAdd,m_iBitNo,&result);	//read DB bit
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);	
		}
		m_iBitReadResult = result;	
	    break;
	default:
	    break;
	}
	UpdateData(FALSE);
}

void CPLC_Protocol_testDlg::OnBtnOff()								//set bit with 0
{
	// TODO: Add your control notification handler code here
	//0 I, 1 Q, 2 M, 3 DB
	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,false);		//set Q bit 0
		if (m_iErrorCode)
		{
			ErrorMessage(m_iErrorCode,m_buffer);
			m_csCommunicationStatus.Format("%s",m_buffer);
		}	
		break;

⌨️ 快捷键说明

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