Knowledge Base Nr: 00032 OPCClient.h - http://www.swe-kaiser.de

Downloads:

OPC: übersichtliche C++ Wrapperklasse für OPC

  
// erlaubt das starten/stopen eines opc servers, das anlegen von gruppen und items,
// das synchrone und asynchrone lesen/schreiben von items.
// virtuelle funktionen für die ereignisbehandlung können überschrieben werden
// nachdem eine eigene klasse abgeleitet wurde.
// (es wird der OPC Client Toolkit von FactorySoft benötigt)

#if !defined(AFX_OPCCLIENT_H__90B262F7_1FEB_11D3_B382_0000E83A0185__INCLUDED_)
#define AFX_OPCCLIENT_H__90B262F7_1FEB_11D3_B382_0000E83A0185__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CItem : public CObject
{
public:
CItem() : m_bIsGood(FALSE) { }
virtual ~CItem() { }

CItem& operator= (const CItem& source)
{
if( this == &source )
return *this;

m_strName = source.m_strName;
m_varValue = source.m_varValue;
m_bIsGood = source.m_bIsGood;
m_timestamp = source.m_timestamp;

return *this;
}

CString m_strName;
COleVariant m_varValue;
BOOL m_bIsGood;
FILETIME m_timestamp;
};

class OPCServer;
class OPCCallback;
class CAdviseSink;
class Item;
class CGroup;

#include <afxtempl.h>

typedef CTypedPtrList<CObList, Item*> ItemList;


class COPCClient : public CObject
{
private:
OPCServer* m_opcServer; // the connected server

//used for GetLastError()
HRESULT m_hr;
CString m_strLastError;

// OPC 2.0 call back (one for all groups)
OPCCallback* m_callbackCP;

// OPC 1.0 call back (one for all groups)
CAdviseSink* m_testSink;

//the list of all items (for internal usage only!)
ItemList m_items;

//the list of all groups (for internal usage only!)
CObList m_groups;

protected:
CString GetErrorString(LPCTSTR description, HRESULT error, LPCTSTR addText = NULL);
void ReportError(LPCTSTR description, HRESULT error, LPCTSTR addText = NULL);

Item* GetItemInfo(LPCSTR lpszGroupName, LPCSTR lpszItemName);
CGroup* GetGroupInfo(LPCSTR lpszGroupName);

public:
COPCClient();
virtual ~COPCClient();

BOOL Start(LPCOLESTR lpszProgID, LPCSTR lpszMachineName = NULL); //f.e. L"OPC.SimaticNET"
BOOL Stop();

void GetLastError(CString& strError, HRESULT& hr) { strError = m_strLastError; hr = m_hr; }

//the Siemens OPC server have problems with adding items to active groups
//so the group is created as inactive and must be activated later
//nRefreshRate returns the advised refresh rate
BOOL AddGroup(LPCSTR lpszGroupName, DWORD& nRefreshRate, FLOAT deadband = 0.0);

BOOL ActivateGroup(LPCSTR lpszGroupName, DWORD& nRefreshRate, FLOAT deadband, BOOL bActivate = TRUE);

BOOL AddItem(LPCSTR lpszGroupName, CItem& cItem);

BOOL WriteItem(LPCSTR lpszGroupName, CItem& cItem, BOOL bAsync = FALSE);
BOOL WriteItems(LPCSTR lpszGroupName, int nItemCount, CItem cItems[], BOOL bAsync = FALSE);

BOOL ReadItem(LPCSTR lpszGroupName, CItem& cItem, BOOL bFromDevice = FALSE);
BOOL ReadItems(LPCSTR lpszGroupName, int nItemCount, CItem cItems[], BOOL bFromDevice = FALSE);

//overwrite this method to receive changes
virtual void OnValueChange(CItem& cItem);

//overwrite this method to receive trace messages
virtual void OnMessage(LPCSTR lpszMsg, HRESULT hr);
};

#endif // !defined(AFX_OPCCLIENT_H__90B262F7_1FEB_11D3_B382_0000E83A0185__INCLUDED_)