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

📄 opcclientdlg.cpp

📁 Visual C++通信编程工程实例精解(附盘)
💻 CPP
字号:
// OPCClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "OPCClient.h"
#include "OPCClientDlg.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()

/////////////////////////////////////////////////////////////////////////////
// COPCClientDlg dialog

COPCClientDlg::COPCClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COPCClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COPCClientDlg)
	m_kuangshi = _T("");
	m_jianfen = _T("");
	b_enable=true;
	//bcondition=1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void COPCClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COPCClientDlg)
	DDX_Text(pDX, IDC_KUANGSHI, m_kuangshi);
	DDX_Text(pDX, IDC_JIANFEN, m_jianfen);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COPCClientDlg, CDialog)
	//{{AFX_MSG_MAP(COPCClientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_READ, OnRead)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COPCClientDlg message handlers

BOOL COPCClientDlg::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

	
//######################开始初始化OPC代码###############

	if(CoInitialize(NULL)!=S_OK)//初始COM库
		return TRUE;	
    typedef HRESULT (STDAPICALLTYPE *INTERFACE_FINDER)(void *pThis, DWORD dwData, REFIID riid, void **ppv);
	INTERFACE_FINDER a=INTERFACE_FINDER(-1);
	HRESULT hRet=ConnectToServer(L"RSI.RSView32OPCTagServer",FALSE,&ppUnknown);
	
	if(hRet!=S_OK)
	{
		MessageBox("您可能没启动RSView!您必须首先启动RSView!");
		{
			b_enable=false;
			return true;
		}
	
	}
	
	//初始化组的参数
    fTemp = 0.0f;
	lTimeBias = 0;
	dwActualRate = 0;
	ItemNumber=4;//数据项的数
	hRet=ppUnknown->QueryInterface(IID_IOPCServer,(void **)&pServer);
	hRet=pServer->AddGroup(L"",TRUE,500,1235,&lTimeBias,&fTemp,0,&hOPCServerGroup,
		                     &dwActualRate,IID_IOPCItemMgt,(LPUNKNOWN*)&pOPCItemMgt);
	//if(hRet==S_OK) MessageBox("AddGroupSuccess");	
	
    USES_CONVERSION;//使用T2OLE()
	for(int i=0;i<4;i++)
	{
	   ItemArray[i].dwBlobSize = 0;
       ItemArray[i].pBlob = NULL;
       ItemArray[i].bActive = TRUE;
       ItemArray[i].hClient =i;  // pointer to item is its "handle"
       ItemArray[i].szAccessPath =T2OLE("RSViewProjectName");
       ItemArray[i].vtRequestedDataType =VT_R4;
	}
    ItemArray[0].szItemID =T2OLE("AI\\KUANGSHI");
    ItemArray[1].szItemID =T2OLE("AI\\JIANFEN");   
    ItemArray[2].szItemID =T2OLE("AO\\KUANGSHI");   
    ItemArray[3].szItemID =T2OLE("AO\\JIANFEN");   

    if(hRet==S_OK)
		hRet=pOPCItemMgt->AddItems(ItemNumber,ItemArray,
		(OPCITEMRESULT**)&pItemResult,(HRESULT **)&pErrors);
	//if(hRet==S_OK) MessageBox("AddItemsSuccess");
	
	for(i=0;i<2;i++)
	    hServerAI[i]=pItemResult[i].hServer;
	for(i=0;i<2;i++)
	    hServerAO[i]=pItemResult[i+2].hServer;
	hRet=ppUnknown->QueryInterface(IID_IOPCSyncIO,(void **)&pOPCSync);
			    
//#####################初始通信结束##############
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void COPCClientDlg::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 COPCClientDlg::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 COPCClientDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void COPCClientDlg::OnRead() 
{
	// TODO: Add your control notification handler code here
	if(b_enable)
	{
		int ItemNumber=2;
		float fkuangshi,fjianfen;
		HRESULT hRet=pOPCSync->Read(OPC_DS_CACHE,ItemNumber,hServerAI,&pItemValue,&pErrors);
		fkuangshi=V_R4(&pItemValue[0].vDataValue);
		fjianfen=V_R4(&pItemValue[1].vDataValue);
		m_kuangshi.Format("%3.1f",fkuangshi);
		m_jianfen.Format("%3.1f",fjianfen);
		UpdateData(false);
	}
 	else
	{
		MessageBox("OPC服务器连接失败,无法读取数据!");
	}

}

void COPCClientDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	if(b_enable)
	{
		UpdateData();
		int ItemNumber=2;
		COleVariant WriteValue[2];
		WriteValue[0]=m_kuangshi;
		WriteValue[1]=m_jianfen;
		for(int i=0;i<2;i++)
		{
			WriteValue[i].ChangeType(VT_R4);
		}
		HRESULT hRet=pOPCSync->Write(ItemNumber,hServerAO,WriteValue,&pErrors);
	}
	else
	{
		MessageBox("OPC服务器连接失败,无法发送数据!");
	}

}

void COPCClientDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	if(b_enable)
	{
		pOPCItemMgt->RemoveItems(2,hServerAI,&pErrors);
		pOPCItemMgt->RemoveItems(2,hServerAO,&pErrors);
		pServer->RemoveGroup(hOPCServerGroup,TRUE);
		pOPCSync->Release();
		pOPCItemMgt->Release();
		pServer->Release();
		ppUnknown->Release();
	}

	CDialog::OnClose();
}
HRESULT COPCClientDlg::ConnectToServer(/*in */LPOLESTR ProgID,/*in*/ BOOL IsRemote,/*out */ IUnknown **ppUnknown)
{
	CLSID OPCCLSID;
	HRESULT hRet=CLSIDFromProgID(ProgID,&OPCCLSID);
	//if(hRet==S_OK)
		//MessageBox("convert success");
	hRet=CoCreateInstance(OPCCLSID,NULL,CLSCTX_LOCAL_SERVER,IID_IUnknown,
			(void **)ppUnknown);
	return hRet;
}

⌨️ 快捷键说明

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