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

📄 impiopcsyncio.cpp

📁 基于Intellution开发包的开发的OPC服务器
💻 CPP
字号:
// ImpIOPCSyncIO.cpp
//
//  This file contains the implementation of 
//  the IOPCSyncIO interface for groups in the XXX server.
//
//	(c) COPYRIGHT 1996-1998, INTELLUTION INC.
// ALL RIGHTS RESERVED
//
//
//	Functions defined in this module:
//
//			CImpIOPCSyncIO::CImpIOPCAsyncIO()
//			CImpIOPCSyncIO::~CImpIOPCAsyncIO()
//			CImpIOPCSyncIO::AddRef()
//			CImpIOPCSyncIO::Release()
//			CImpIOPCSyncIO::QueryInterface()
//			CImpIOPCSyncIO::Read()
//			CImpIOPCSyncIO::Write()
//
//
//
// Modification Log:
//	Vers	Date     By		Notes
//	----	-------- ---	-----
//	1.0		08/26/97 jra	Created
//	1.3		03/10/98 jra	Modified to be wizard generated and driver specific.
//
//

#define WIN32_LEAN_AND_MEAN

#include "OpcStdAfx.h"

#include "OPC.h"

#include "OPCError.h"


/////////////////////////////////////////////////////////////////////////////
// Constructor /Destructor functions
//

////////////////////////////////////////////////////////////////
// CImpIOPCSyncIO()
//
// Constructor for this Interface
//
////////////////////////////////////////////////////////////////
CImpIOPCSyncIO::CImpIOPCSyncIO(LPUNKNOWN pUnkOuter)
{
	m_pUnkOuter = pUnkOuter;
	m_pParentGroup = (COPCDrvGroup *)pUnkOuter;
}


////////////////////////////////////////////////////////////////
// ~CImpIOPCSyncIO()
//
// Destructor for this Interface
//
////////////////////////////////////////////////////////////////

CImpIOPCSyncIO::~CImpIOPCSyncIO(void)
{
	m_pParentGroup->m_pCImpISyncIO = NULL;
}


/////////////////////////////////////////////////////////////////////////////
// IUnknown functions Delegate to Parent
//


////////////////////////////////////////////////////////////////
// AddRef()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG) CImpIOPCSyncIO::AddRef(void)
{
	return m_pUnkOuter->AddRef();
}


////////////////////////////////////////////////////////////////
// Release()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG) CImpIOPCSyncIO::Release(void)
{
	return m_pUnkOuter->Release();
}


////////////////////////////////////////////////////////////////
// QueryInterface()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCSyncIO::QueryInterface(REFIID	iid, 
											LPVOID	*ppInterface)
{
	return m_pUnkOuter->QueryInterface(iid, ppInterface);
}



/////////////////////////////////////////////////////////////////////////////
// CImpIOPCSyncIO (IOPCSyncIO) interface functions
//


////////////////////////////////////////////////////////////////
// Read()
//
// This function performs a synchronous read.
//
// Returns:
//	HRESULT	-	S_OK if the function succeeded.
//			-	E_OUTOFMEMORY if a memory allocator failed.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCSyncIO::Read(OPCDATASOURCE		dwSource, 
								  DWORD				dwNumItems, 
								  OPCHANDLE			*phServer, 
								  OPCITEMSTATE		**ppItemValues,
								  HRESULT			**ppErrors)
{
	DWORD			i;
	HRESULT			*hr;
	OPCITEMSTATE	*pItemState;
	OPCHANDLE		OPCHandle;
	COPCDrvItem		*pItem;


	// Defaults in case of error
	//
	*ppErrors		= hr			= NULL;
	*ppItemValues	= pItemState	= NULL;

	// Allocate memory for the item values and hresults
	//
	*ppErrors = hr = (HRESULT*) pIMalloc->Alloc(sizeof(HRESULT) *dwNumItems);
	if(NULL == hr)
	{
		return E_OUTOFMEMORY;
	}
	*ppItemValues = pItemState = 
		(OPCITEMSTATE*) pIMalloc->Alloc(sizeof(OPCITEMSTATE)*dwNumItems);
	if(NULL == pItemState) 
	{
		pIMalloc->Free(hr);
		return E_OUTOFMEMORY;
	}

	// For each item
	//
	for (i = 0; i < dwNumItems; i++)
	{
		// Validate the handle
		//
		OPCHandle = phServer[i];
		if (!m_pParentGroup->IsItemValid(OPCHandle))
		{
			hr[i] = OPC_E_INVALIDHANDLE;
			continue;
		}

		// Find the item that matches the server handle
		//
		pItem = m_pParentGroup->GetItemPtr(OPCHandle);

		// and extract the requested data...
		// GetValue attempts the conversion from Canonical to Requested
		// According the the OPC Spec, AddItems and/or SetDataTypes
		// will have already verified that this is possible.
		//
		pItemState[i].hClient = pItem->m_hClientItemHandle;
		VariantInit(&pItemState[i].vDataValue);

		hr[i] = pItem->GetValue(dwSource, 
								&pItemState[i].vDataValue, 
								&pItemState[i].wQuality, 
								&pItemState[i].ftTimeStamp);

		// For CACHE, If Group not active then quality is bad no matter what
		//
		if((OPC_DS_CACHE == dwSource) && (!m_pParentGroup->m_bActive))
		{
			((QUALITY_STATE *)&pItemState[i].wQuality)->nQuality = QUALITY_BAD;
			((QUALITY_STATE *)&pItemState[i].wQuality)->nSubStatus = SS_OUT_OF_SERVICE;
		}

		//
		// Note that doing a read does not affect OnDataChange
		// so we do NOT clear Changed flag
		//
	}

	return S_OK;
}


////////////////////////////////////////////////////////////////
// Write()
//
// This function performs a synchronous write.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCSyncIO::Write(DWORD		dwNumItems, 
								   OPCHANDLE	*phServer, 
								   VARIANT		*pItemValues, 
								   HRESULT		**ppErrors)
{
	COPCDrvGroup	&ParentGroup	= *m_pParentGroup;
	COPCDrvItem		*pItem			= NULL;
	HRESULT			*pHResultList	= NULL,
					hr;
	OPCHANDLE		OPCHandle;


	// Allocate memory for the hresults
	//
	*ppErrors = pHResultList = (HRESULT *)pIMalloc->Alloc(sizeof(HRESULT) * dwNumItems);
	if(pHResultList == NULL)
	{
		return E_OUTOFMEMORY;
	}

	// For each item
	//
	for (DWORD i = 0; i < dwNumItems; i++)
	{
		// Validate the handle
		//
		OPCHandle = phServer[i];
		if (!m_pParentGroup->IsItemValid(OPCHandle))
		{
			pHResultList[i] = OPC_E_INVALIDHANDLE;
			continue;
		}

		// Find the item that matches the server handle
		//
		pItem = m_pParentGroup->GetItemPtr(OPCHandle);

		// and write the requested data...
		//
		hr = pItem->WriteValue(&pItemValues[i]);
		if(FAILED(hr))
		{
			pHResultList[i] = OPC_E_BADTYPE;
		}
		else
		{
			pHResultList[i] = S_OK;
		}
	}

	return S_OK;
}

⌨️ 快捷键说明

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