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

📄 synopcdlg.cpp

📁 最简单的VC编写的OPC客户端程序
💻 CPP
字号:
// SynOpcDlg.cpp : implementation file
//
//--------------------------------------------------------- 
// (c) COPYRIGHT 2003,2004 http://www.opc-china.com INC.
// ALL RIGHTS RESERVED
// Original Author:Jigang Si
// Original Author Email:economic@126.com 
//--------------------------------------------------------- 
#include "stdafx.h"
#include "SynOpc.h"
#include "SynOpcDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CSynOpcDlg dialog

CSynOpcDlg::CSynOpcDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSynOpcDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSynOpcDlg)
	m_readQulitay = _T("");
	m_readTs = _T("");
	m_writeResult = _T("");
	m_readValue = 0.0;
	m_writeValue = 0.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSynOpcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSynOpcDlg)
	DDX_Text(pDX, IDC_QUALITY, m_readQulitay);
	DDX_Text(pDX, IDC_TIMESTAMP, m_readTs);
	DDX_Text(pDX, IDC_WRITERESULT, m_writeResult);
	DDX_Text(pDX, IDC_READVALUE, m_readValue);
	DDX_Text(pDX, IDC_WRITEVALUE, m_writeValue);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSynOpcDlg, CDialog)
	//{{AFX_MSG_MAP(CSynOpcDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_READ, OnRead)
	ON_BN_CLICKED(IDC_WRITE, OnWrite)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSynOpcDlg message handlers

BOOL CSynOpcDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSynOpcDlg::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 CSynOpcDlg::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 CSynOpcDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
/*启动OPC服务器,添加组对象,添加项*/
void CSynOpcDlg::OnStart() 
{
	// TODO: Add your control notification handler code here
	HRESULT		r1;
	CLSID		clsid;
	LONG		TimeBias = 0;
	FLOAT		PercentDeadband = 0.0;
	DWORD		RevisedUpdateRate;
	CString		szErrorText;
	m_ItemResult = NULL;
	//	初始化COM库
	r1 = CoInitialize(NULL);
	if (r1 != S_OK)
	{	if (r1 == S_FALSE)
		{
			AfxMessageBox("COM库已经初始化");
		}
		else
		{
			AfxMessageBox("COM库初始化失败");			
			return;
		}
	}	
	//	通过ProgID,查找注册表中的相关CLSID
	r1 = CLSIDFromProgID(L"Intellution.OPCiFIX", &clsid);
	if (r1 != S_OK)
	{	
		AfxMessageBox("获取CLSID失败");
		CoUninitialize();
		return;
	}
	//创建OPC服务器对象,并查询对象的IID_IOPCServer接口
	r1 = CoCreateInstance (clsid, NULL, CLSCTX_LOCAL_SERVER ,IID_IOPCServer, (void**)&m_IOPCServer);
	if (r1 != S_OK)
	{	
		AfxMessageBox("创建OPC服务器对象失败");
		m_IOPCServer = NULL;
		CoUninitialize();
		return;
	}
	//添加一个group对象,并查询IOPCItemMgt接口
	r1=m_IOPCServer->AddGroup(L"grp1",			//	[in] group name
						TRUE,					//	[in] active
						500,					//	[in] request this Update Rate from Server
						1,						//	[in] Client handle
						&TimeBias,				//	[in] no time interval to system UTC time
						&PercentDeadband,		//	[in] no deadband, so all data changes are reported	
						LOCALE_ID,				//	[in] Server uses English language for text values
						&m_GrpSrvHandle,		//	[out] Server handle to identify this group in later calls
						&RevisedUpdateRate,		//	[out] the answer form the Server to the requested update rate
					    IID_IOPCItemMgt,		//	[in] requested interface type of the group object
						(LPUNKNOWN*)&m_IOPCItemMgt);	//	[out] pointer to the requested interface
	
	if (r1 == OPC_S_UNSUPPORTEDRATE)
	{	
		AfxMessageBox("请求的刷新速率与实际的刷新速率不一致");
	}
	else
		if (FAILED(r1))
		{
			AfxMessageBox("不能为服务器添加group对象");
			m_IOPCServer->Release();
			m_IOPCServer = NULL;
			CoUninitialize();		
			return;
		}

	//	为AddItem定义item表的参数
	m_Items[0].szAccessPath		  = L"";			//	Accesspath not needed
	m_Items[0].szItemID			  = szItemID;		//	ItemID, see above
	m_Items[0].bActive			  = TRUE;			
	m_Items[0].hClient			  = 1;
	m_Items[0].dwBlobSize		  = 0;
	m_Items[0].pBlob			  = NULL;
	m_Items[0].vtRequestedDataType = 0;				//	return values in native (cannonical) datatype 
													//	defined by the item itself

	r1 = m_IOPCItemMgt->AddItems(1,				   //	[in] add one item
							m_Items,				//	[in] see above
							&m_ItemResult,			//	[out] array with additional information about the item
							&m_pErrors);			//	[out] tells which of the items was successfully added.
													//	For any item which failed it provides a reason

	if ( (r1 != S_OK) && (r1 != S_FALSE) )
	{	
		AfxMessageBox("AddItems失败");		
		m_IOPCItemMgt->Release();
		m_IOPCItemMgt = NULL;
		m_GrpSrvHandle = NULL;
		m_IOPCServer->Release();
		m_IOPCServer = NULL;
		CoUninitialize();		
		return;
	}
	else if(r1==S_OK)
	{	
		AfxMessageBox("AddItems()成功");		
	}
	
	//	检测Item的可读写性
	if (m_ItemResult[0].dwAccessRights != (OPC_READABLE + OPC_WRITEABLE))
	{
		AfxMessageBox("Item不可读,也不可写,请检查服务器配置");
	}
	
    //查询group对象的同步接口
	r1 = m_IOPCItemMgt->QueryInterface(IID_IOPCSyncIO, (void**)&m_IOPCSyncIO);
	if (r1 < 0)
	{	
		AfxMessageBox("IOPCSyncIO没有发现,错误的查询!");
		CoTaskMemFree(m_ItemResult);
		m_IOPCItemMgt->Release();
		m_IOPCItemMgt = NULL;
		m_GrpSrvHandle = NULL;
		m_IOPCServer->Release();
		m_IOPCServer = NULL;
		CoUninitialize();
		return;
	}

}
/*同步读,将结果显示在对话框*/
void CSynOpcDlg::OnRead() 
{
	// TODO: Add your control notification handler code here
	OPCHANDLE		*phServer;
	OPCITEMSTATE	*pItemValue;
	HRESULT			*pErrors;
	HRESULT			r1;
	UINT			qnr;	
	if (m_pErrors[0] != S_OK)		//Item不可用
	{	
		AfxMessageBox("OPC Item 不可用,不能用同步读功能!");
		return;
	}	
	//内存分配
	phServer = new OPCHANDLE[1];
	//通过server句柄选择item(由AddItem得到的)
	phServer[0] = m_ItemResult[0].hServer;		
	r1 = m_IOPCSyncIO->Read(OPC_DS_DEVICE, 1, phServer, &pItemValue, &pErrors);	
	//释放内存
	delete [] phServer;
	if (r1 == S_OK) {
		m_readValue = pItemValue[0].vDataValue.fltVal;			
		qnr = pItemValue[0].wQuality;
		switch(qnr)
		{	
		case OPC_QUALITY_BAD:			m_readQulitay = "BAD";
			break;
        case OPC_QUALITY_UNCERTAIN:		m_readQulitay = "UNCERTAIN";
			break;
        case OPC_QUALITY_GOOD:			m_readQulitay = "GOOD";
			break;
		case OPC_QUALITY_NOT_CONNECTED:	m_readQulitay = "NOT_CONNECTED";
			break;
        case OPC_QUALITY_DEVICE_FAILURE:m_readQulitay = "DEVICE_FAILURE";
			break;
        case OPC_QUALITY_SENSOR_FAILURE:m_readQulitay = "SENSOR_FAILURE";
			break;
        case OPC_QUALITY_LAST_KNOWN:    m_readQulitay = "LAST_KNOWN";
			break;
        case OPC_QUALITY_COMM_FAILURE:  m_readQulitay = "COMM_FAILURE";
			break;
        case OPC_QUALITY_OUT_OF_SERVICE:m_readQulitay = "OUT_OF_SERVICE";
			break;
        case OPC_QUALITY_LAST_USABLE:	m_readQulitay = "LAST_USABLE";
			break;
        case OPC_QUALITY_SENSOR_CAL:	m_readQulitay = "SENSOR_CAL";
			break;
        case OPC_QUALITY_EGU_EXCEEDED:  m_readQulitay = "EGU_EXCEEDED";
			break;
        case OPC_QUALITY_SUB_NORMAL:	m_readQulitay = "SUB_NORMAL";
			break;
        case OPC_QUALITY_LOCAL_OVERRIDE:m_readQulitay = "LOCAL_OVERRIDE";
			break;
        default:			
			m_readQulitay = "UNKNOWN ERROR";
		}
		m_readTs = COleDateTime( pItemValue[0].ftTimeStamp ).Format();
		UpdateData(FALSE);
	}
	if (r1 == S_FALSE) 
	{	
		AfxMessageBox("Read()错误");
	}
	if (FAILED(r1)) 
	{
		AfxMessageBox("同步读失败!");
	}
	else 
	{
		//释放内存,如果操作成功	
		CoTaskMemFree(pErrors);
		CoTaskMemFree(pItemValue);
	}
	
}
/*同步写,将写结果显示在对话框*/
void CSynOpcDlg::OnWrite() 
{
	// TODO: Add your control notification handler code here
	OPCHANDLE		*phServer;
	HRESULT			*pErrors;
	VARIANT			values[1];
	HRESULT			r1;
	LPWSTR			ErrorStr;
	CString			szOut;

	if (m_pErrors[0] != S_OK)		//Item not available
	{	
		AfxMessageBox("OPC Item 不可用,不能用同步读功能!");
		return;
	}	
	
	//通过server句柄选择item(由AddItem得到的)
	phServer = new OPCHANDLE[1];
	phServer[0] = m_ItemResult[0].hServer;
	
	//	从对话框获得数据
	UpdateData(TRUE);

	//	设置Variant变量的数据类型和数值
	values[0].vt = VT_R4;
	values[0].fltVal = m_writeValue;

		
	r1 = m_IOPCSyncIO->Write(1, phServer, values, &pErrors);
	
	delete[] phServer;

	if (FAILED(r1))
	{		
		AfxMessageBox("同步写Item错误");
	}
	else 
	{
		m_IOPCServer->GetErrorString(pErrors[0], LOCALE_ID, &ErrorStr);
		m_writeResult = ErrorStr;
		m_writeResult.Remove('\r');
		m_writeResult.Remove('\n');
		UpdateData(FALSE);
		CoTaskMemFree(pErrors);	
	}
	
}
/*停止服务器*/
void CSynOpcDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	HRESULT		r1;
	OPCHANDLE	*phServer;
	HRESULT		*pErrors;

	//	删除Item
	phServer = new OPCHANDLE[1];
	phServer[0] = m_ItemResult[0].hServer;

	r1 = m_IOPCItemMgt->RemoveItems(1,			//	[in] remove one item
							phServer,			//	[in] identified by server handle
							&pErrors);			//	[out] Errors for each item, returned by OPC server
	
	if ( (r1 != S_OK) && (r1 != S_FALSE) )
	{	
		AfxMessageBox("RemoveItems失败!");
	}
	else if(r1==S_OK)
	{
		AfxMessageBox("RemoveItems()成功");
	}

	delete[] phServer; 
	CoTaskMemFree(pErrors);
	CoTaskMemFree(m_ItemResult);
	m_ItemResult=NULL;
	CoTaskMemFree(m_pErrors);
	m_pErrors = NULL;

	//	释放同步接口
	m_IOPCSyncIO->Release();
	m_IOPCSyncIO = NULL;

	//	释放item管理接口
	m_IOPCItemMgt->Release();
	m_IOPCItemMgt = NULL;
	
	//	删除group对象
	r1=m_IOPCServer->RemoveGroup(m_GrpSrvHandle, TRUE);
	if (r1 != S_OK)
	{
		AfxMessageBox("RemoveGroup失败!");
	}
	else
	{
		AfxMessageBox("RemoveGroup成功!");
	}
	m_GrpSrvHandle = NULL;

	//	释放 OPC服务器
	m_IOPCServer->Release();
	m_IOPCServer = NULL;

	//关闭COM库
	CoUninitialize();

	
}

⌨️ 快捷键说明

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