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

📄 datasink20.cpp

📁 KepWare的OPC Client 示例.面向C
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			pItem->UpdateData (pvValues [dwItem], pwQualities [dwItem], pftTimeStamps [dwItem]);
			}
		catch (...)
			{
			// Must have tried to use a bad pItem pointer.  Bump a bad item 
			// handle count and try to process then next item:
			++cdwBadItemHandles;
			continue;
			}

		// Issue a TRACE message if item read resulted in an error. This is 
		// for debugging purposes.  You could call GetErrorString() to get
		// meaning of server specific error codes and log them if you wish.
		if (FAILED (pErrors [dwItem]))
			{
			TRACE (_T("OTC: OnDataChange 2.0 - item '%s' failure %08X\r\n"), 
				pItem->GetItemID (), pErrors [dwItem]);
			}
		}

	// Output any non-zero bad item handle counts if debugging:
#ifdef _DEBUG
	if (cdwBadItemHandles)
		{
		TRACE (_T("OTC: OnDataChange 2.0 - bad item handles received %d\r\n"), cdwBadItemHandles);
		}
#endif

	// Return "success" code.  Note this does not mean that there were no 
	// errors reported by the OPC Server, only that we successfully processed
	// the advisement.
	return (S_OK);
	}

// **************************************************************************
// OnReadComplete ()
//
// Description:
//	This method is provided to handle notifications from an OPC Group on 
//	completion of AsyncIO2 reads of one or more items.
//
// Parameters:
//	DWORD		dwTransID,		Transaction ID returned by the server when
//								  the read was initiated.
//	OPCHANDLE	hGroup,			Client group handle.
//	HRESULT		hrMasterQuality	S_OK if all qualities are GOOD, otherwise S_FALSE.
//	HRESULT		hrMasterError	S_OK if all errors are S_OK, otherwise S_FALSE.
//	DWORD		dwCount			Number of items in the lists that follow.
//								  This may not be the same number as passed in
//								  read request if the server encountered errors.
//	OPCHANDLE	*phClientItems	Item client handles.
//	VARIANT		*pvValues		Item data.
//	WORD		*pwQualities	Item qualities.
//	FILETIME	*pftTimeStamps	Item timestamps.
//	HRESULT		*pErrors		Item errors.
//
// Returns:
//	STDMETHODIMP -
//		S_OK - Processing of advisement successful.
//		E_INVALIDARG - One of the arguments was invalid.
// **************************************************************************
STDMETHODIMP IKDataSink20::OnReadComplete (DWORD dwTransID,
							OPCHANDLE hGroup,
							HRESULT hrMasterQuality,
							HRESULT hrMasterError,
							DWORD dwCount,
							OPCHANDLE *phClientItems,
							VARIANT *pvValues,
							WORD *pwQualities,
							FILETIME *pftTimeStamps,
							HRESULT *pErrors)
	{
	CKGroup *pGroup = NULL;
	CKItem *pItem = NULL;

	// Initialize a bad item handle counter (used for debugging only):
	DWORD cdwBadItemHandles = 0;

	// Validate arguments.  Return with "invalid argument" error code 
	// if any are invalid:
	if (hGroup					== NULL	||
		dwCount					== 0	||
		phClientItems			== NULL	||
		pvValues				== NULL	||
		pwQualities				== NULL	||
		pftTimeStamps			== NULL	||
		pErrors					== NULL)
		return (E_INVALIDARG);

	// The group argument gives us a pointer to the destination CKGroup:
	pGroup = (CKGroup *) hGroup;

	// Post a "read complete" message:
	LogMsg (IDS_ASYNC20_READXACT_COMPLETE, dwTransID, dwCount,
			pGroup->GetName (), hrMasterError);

	// Loop over items:
	for (DWORD dwItem = 0; dwItem < dwCount; dwItem++)
		{
		// The client item handles give us pointers to the destination 
		// CKItem objects:
		pItem = (CKItem *) phClientItems [dwItem];

		// Wrap use of pItem in exception handler in case pointer is bad:
		try
			{
			// Udate the item's value, quality, and timerstamp:
			pItem->UpdateData (pvValues [dwItem], pwQualities [dwItem], pftTimeStamps [dwItem]);
			}
		catch (...)
			{
			// Must have tried to use a bad pItem pointer.  Bump a bad item 
			// handle count and try to process then next item:
			++cdwBadItemHandles;
			continue;
			}

		// Issue a TRACE message if item read resulted in an error. This is 
		// for debugging purposes.  You could call GetErrorString() to get
		// meaning of server specific error codes and log them if you wish.
		if (FAILED (pErrors [dwItem]))
			{
			TRACE (_T("OTC: OnReadComplete 2.0 - item '%s' failure %08X\r\n"), 
				pItem->GetItemID (), pErrors [dwItem]);
			}
		}

	// Output any non-zero bad item handle counts if debugging:
#ifdef _DEBUG
	if (cdwBadItemHandles)
		{
		TRACE (_T("OTC: OnReadComplete 2.0 - bad item handles received %d\r\n"), cdwBadItemHandles);
		}
#endif

	// Return "success" code.  Note this does not mean that there were no 
	// errors reported by the OPC Server, only that we successfully processed
	// the advisement.
	return (S_OK);
	}

// **************************************************************************
// OnWriteComplete ()
//
// Description:
//	This method is provided to handle notifications from an OPC Group on 
//	completion of AsyncIO2 writes to one or more items.
//
// Parameters:
//	DWORD		dwTransID		Transaction ID returned by the server when the 
//								  write was initiated.
//	OPCHANDLE	hGroup			Client group handle.
//	HRESULT		hrMasterError	S_OK if all errors are S_OK, otherwise S_FALSE
//	DWORD		dwCount			Number of items in the lists that follow.
//								  This may not be the same number as passed in
//								  read request if the server encountered errors.
//	OPCHANDLE	*phClientItems	Item client handles.
//	HRESULT		*pErrors		Item errors	.
//
// Returns:
//	STDMETHODIMP -
//		S_OK - Processing of advisement successful.
//		E_INVALIDARG - One of the arguments was invalid.
// **************************************************************************
STDMETHODIMP IKDataSink20::OnWriteComplete (DWORD dwTransID,
							OPCHANDLE hGroup,
							HRESULT hrMasterError,
							DWORD dwCount,
							OPCHANDLE *phClientItems,
							HRESULT *pErrors)
	{
	CKGroup *pGroup = NULL;
	CKItem *pItem = NULL;

	// Initialize a bad item handle counter (used for debugging only):
	DWORD cdwBadItemHandles = 0;

	// Validate arguments.  Return with "invalid argument" error code 
	// if any are invalid:
	if (hGroup					== NULL	||
		dwCount					== 0	||
		phClientItems			== NULL	||
		pErrors					== NULL)
		return (E_INVALIDARG);

	// The group argument gives us a pointer to the destination CKGroup:
	pGroup = (CKGroup *) hGroup;

	// Post a "write complete" message:
	LogMsg (IDS_ASYNC20_WRITE_COMPLETE, dwTransID, dwCount,
		pGroup->GetName (), hrMasterError);

	// Loop over items:
	for (DWORD dwItem = 0; dwItem < dwCount; dwItem++)
		{
		// Post "failed write" messages:
		if (pErrors && FAILED (pErrors [dwItem]))
			{
			// Wrap use of pItem in exception handler in case pointer is bad:
			try
				{
				// The client item handles give us pointers to the destination 
				// CKItem objects:
				pItem = (CKItem *) phClientItems [dwItem];

				// Post the message.  You could call GetErrorString() to get
				// meaning of server specific error codes if you wish.
				LogMsg (IDS_ASYNC20_WRITE_COMPLETE_FAILURE, 
					pItem->GetItemID (), dwTransID, pErrors [dwItem]);
				}
			catch (...)
				{
				// Must have tried to use a bad pItem pointer.  Bump a bad item 
				// handle count and try to process then next item:
				++cdwBadItemHandles;
				continue;
				}
			}
		}

	// Output any non-zero bad item handle counts if debugging:
#ifdef _DEBUG
	if (cdwBadItemHandles)
		{
		TRACE (_T("OTC: OnWriteComplete 2.0 - bad item handles received %d\r\n"), cdwBadItemHandles);
		}
#endif

	// Return "success" code.  Note this does not mean that there were no 
	// errors reported by the OPC Server, only that we successfully processed
	// the advisement.
	return (S_OK);
	}

// **************************************************************************
// OnCancelComplete ()
//
// Description:
//	This method is provided to handle notifications from an OPC Group on
//	successful completion of an AsyncIO2 cancel.  This should not get called
//	if cancel request fails.  We do not need to do anything here except 
//	acknowledge receipt of notification by returning S_OK.
//
// Parameters:
//	DWORD		dwTransID		Transaction ID provided by use when the Read,
//								  Write, or Refresh was initiated.
//	OPCHANDLE	hGroup			Group handle.
//
// Returns:
//	STDMETHODIMP - S_OK
// **************************************************************************
STDMETHODIMP IKDataSink20::OnCancelComplete (DWORD dwTransID, OPCHANDLE hGroup)
	{
	TRACE (_T("OTC: OnCancelComplete20\r\n"));
	return (S_OK);
	}

⌨️ 快捷键说明

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