📄 opc clientdoc.cpp
字号:
//**************************************************************************
//
// Copyright (c) FactorySoft, INC. 1996-1998, All Rights Reserved
//
//**************************************************************************
//
// Filename : OPC ClientDoc.cpp
// $Author : Jim Hansen
//
// Description: The OPC actions are mostly handled by this Doc.
//
//
//**************************************************************************
#include "stdafx.h"
#include "OPC ClientDoc.h"
#include "WriteItemDlg.h"
#include "OPCServerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// The OPC data formats
UINT OPCSTMFORMATDATA = RegisterClipboardFormat(_T("OPCSTMFORMATDATA"));
UINT OPCSTMFORMATDATATIME = RegisterClipboardFormat(_T("OPCSTMFORMATDATATIME"));
UINT OPCSTMFORMATWRITECOMPLETE = RegisterClipboardFormat(_T("OPCSTMFORMATWRITECOMPLETE"));
#define FULL_TEST 1 // define this to test more interfaces
/////////////////////////////////////////////////////////////////////////////
// OPCClientDoc
IMPLEMENT_DYNCREATE(OPCClientDoc, CDocument)
BEGIN_MESSAGE_MAP(OPCClientDoc, CDocument)
//{{AFX_MSG_MAP(OPCClientDoc)
/*ON_COMMAND(ID_OPC_CONNECT, OnOpcConnect)
ON_UPDATE_COMMAND_UI(ID_OPC_CONNECT, OnUpdateOpcConnect)
ON_COMMAND(ID_OPC_DISCONNECT, OnOpcDisconnect)
ON_UPDATE_COMMAND_UI(ID_OPC_DISCONNECT, OnUpdateOpcDisconnect)
ON_COMMAND(ID_OPC_SERVERSTATUS, OnOpcServerstatus)
ON_UPDATE_COMMAND_UI(ID_OPC_SERVERSTATUS, OnUpdateOpcServerstatus)
ON_COMMAND(ID_OPC_GROUPPARAMETERS, OnOpcGroupparameters)
ON_UPDATE_COMMAND_UI(ID_OPC_GROUPPARAMETERS, OnUpdateOpcGroupparameters)
ON_COMMAND(ID_OPC_ADDITEM, OnOpcAdditem)
ON_UPDATE_COMMAND_UI(ID_OPC_ADDITEM, OnUpdateOpcAdditem)
ON_COMMAND(ID_OPC_ITEMPARAMETERS, OnOpcItemProperties)
ON_UPDATE_COMMAND_UI(ID_OPC_ITEMPARAMETERS, OnUpdateOpcItemProperties)
ON_COMMAND(ID_OPC_ITEMATTRIBUTES, OnOpcItemattributes)
ON_UPDATE_COMMAND_UI(ID_OPC_ITEMATTRIBUTES, OnUpdateOpcItemattributes)
ON_COMMAND(ID_OPC_WRITEVALUETOITEM, OnOpcWritevaluetoitem)
ON_UPDATE_COMMAND_UI(ID_OPC_WRITEVALUETOITEM, OnUpdateOpcWritevaluetoitem)
ON_COMMAND(ID_OPC_READITEM, OnOpcReaditem)
ON_UPDATE_COMMAND_UI(ID_OPC_READITEM, OnUpdateOpcReaditem)
ON_COMMAND(ID_OPC_REFRESH, OnOpcRefresh)
ON_UPDATE_COMMAND_UI(ID_OPC_REFRESH, OnUpdateOpcRefresh)
ON_COMMAND(ID_OPC_REMOVEITEM, OnOpcRemoveitem)
ON_UPDATE_COMMAND_UI(ID_OPC_REMOVEITEM, OnUpdateOpcRemoveitem)
ON_COMMAND(ID_OPC_TEST_SYNC_READ, OnOpcTestSyncRead)
ON_UPDATE_COMMAND_UI(ID_OPC_TEST_SYNC_READ, OnUpdateOpcTestSyncRead)
ON_COMMAND(ID_OPC_MISC, OnOpcMisc)*/
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//**************************************************************************
// OPCClientDoc construction/destruction
OPCClientDoc::OPCClientDoc()
{
groupHandle = 0;
pCurrentItem = NULL;
hView = NULL;
transactionID = 0;
dwConnection1 = 0;
dwConnection2 = 0;
testSink = new CAdviseSink; // create the advise sink for notifications
testSink->AddRef();
callbackCP = new OPCCallbackObject; // create the ConnectionPoint for notifications
callbackCP->AddRef();
shutdownCP = new OPCShutdownObject; // create the ConnectionPoint for notifications
shutdownCP->AddRef();
dwShutdownConnection = 0;
usingCP = FALSE;
AfxOleLockApp();
// Everyone can connect back to IAdviseSink
HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
if (FAILED(hr))
{
TRACE(_T("CoInitializeSecurity failed, %lx"), hr);
}
}
OPCClientDoc::~OPCClientDoc()
{
AfxOleUnlockApp();
if( opcServer.IsOk() )
OnOpcDisconnect();
testSink->Release(); // OLE should clean this up, but may not have time!
callbackCP->Release();
shutdownCP->Release();
Sleep( 100 );
}
BOOL OPCClientDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
return TRUE;
}
//**************************************************************************
// OPCClientDoc serialization
void OPCClientDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
//**************************************************************************
// OPCClientDoc diagnostics
#ifdef _DEBUG
void OPCClientDoc::AssertValid() const
{
CDocument::AssertValid();
}
void OPCClientDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
//**************************************************************************
// OnOpcConnect()
// Called to find a server name from the prompt dialog box,
// and connect to that server.
// The node name is used to connect to servers on remote nodes.
//
//**************************************************************************
void OPCClientDoc::OnOpcConnect()
{
USES_CONVERSION;
ASSERT( !opcServer.IsOk() );
HRESULT hr = S_OK;
// Prompt user for the server name (from list of installed servers)
OPCServerDlg dlg;
dlg.m_Server = lastServer;
dlg.m_Node = lastNode;
if( dlg.DoModal() != IDOK )
return;
CWaitCursor wait;
lastServer = dlg.m_Server;
lastNode = dlg.m_Node;
// Create a running object from the class ID
// (CLSCTX_ALL will allow in-proc, local and remote)
LPUNKNOWN pUnkn = NULL;
if( dlg.m_Node.IsEmpty() )
{
hr = CoCreateInstance(dlg.m_clsid, NULL, CLSCTX_ALL, IID_IUnknown, (LPVOID *)&pUnkn);
if( FAILED(hr) || pUnkn == NULL)
{
}
}
else // use the node name
{
COSERVERINFO si;
MULTI_QI qi;
si.dwReserved1 = 0;
si.pwszName = T2OLE(dlg.m_Node.GetBuffer(0));
si.pAuthInfo = NULL;
si.dwReserved2 = 0;
qi.pIID = &IID_IOPCServer;
qi.pItf = NULL;
qi.hr = 0;
hr = CoCreateInstanceEx(dlg.m_clsid, NULL, CLSCTX_ALL, &si, 1, &qi);
if (FAILED(hr))
{
/* CString format( (LPCSTR)IDS_CONNECT_ERROR );
CString msg;
msg.Format( format, dlg.m_Server );
msg += _T("CoCreateInstance: ");
ReportError( msg, hr );
return;*/
}
if (FAILED(qi.hr))
{
ReportError( _T("MultiQI: "), qi.hr );
return;
}
pUnkn = qi.pItf;
}
// Get the IOPCServer interface.
hr = opcServer.Attach( pUnkn );
pUnkn->Release(); // Don't need this anymore.
pUnkn = NULL;
if( FAILED(hr) )
{
CString appName((LPCSTR)AFX_IDS_APP_TITLE);
MessageBox(0, _T("You may not have registered the OPC Proxy dll!\n"), appName, MB_OK);
return;
}
SetTitle( dlg.m_Server ); // display the server name
// OPC 2.0 Server shutdown ConnectionPoint
{
IConnectionPointContainer *pCPC = 0;
hr = opcServer.QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC);
if( SUCCEEDED(hr) ) // This server supports 2.0
{
IConnectionPoint *pCallbackCP = 0;
hr = pCPC->FindConnectionPoint(IID_IOPCShutdown, &pCallbackCP);
pCPC->Release();
if( SUCCEEDED(hr) )
{
hr = pCallbackCP->Advise(shutdownCP, &dwShutdownConnection);
pCallbackCP->Release();
}
}
}
// Create a single group that will contain all the items
FLOAT deadband = 0.0;
DWORD rate;
hr = opcServer.AddGroup( L"Fred", TRUE, 1000, // name, active, rate
1324, NULL, &deadband, // handle, bias, band
0, &groupHandle, &rate,
IID_IOPCGroupStateMgt, // interface to return
opcGroup ); // this holds the group ptr
if( FAILED(hr) )
{
ReportError( _T("AddGroup: "), hr );
return;
}
// Test GetGroupByName and SetName
#ifdef FULL_TEST
IOPCGroupStateMgt* pTest=NULL;
hr = opcServer.GetGroupByName( L"Fred", IID_IOPCGroupStateMgt, (LPUNKNOWN*)&pTest );
if( SUCCEEDED(hr) )
{
ASSERT( pTest == (IOPCGroupStateMgt*)opcGroup ); // should get the same
hr = pTest->SetName( L"Group one" ); // set new name
pTest->Release();
if( FAILED(hr) )
{
ReportError( _T("IOPCGroupStateMgt::SetName: "), hr );
}
else
{
// should now go by this new name
hr = opcServer.GetGroupByName( L"Group one", IID_IOPCGroupStateMgt, (LPUNKNOWN*)&pTest );
if( SUCCEEDED(hr) )
{
ASSERT( pTest == (IOPCGroupStateMgt*)opcGroup );
pTest->Release();
}
}
}
#endif // FULL_TEST
// OPC 2.0 ConnectionPoints
IConnectionPointContainer *pCPC = 0;
hr = opcGroup.QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC);
if( SUCCEEDED(hr) ) // This server supports 2.0
{
usingCP = TRUE;
IConnectionPoint *pCallbackCP = 0;
hr = pCPC->FindConnectionPoint(IID_IOPCDataCallback, &pCallbackCP);
pCPC->Release();
if( FAILED(hr) )
{
ReportError( _T("FindConnectionPoint: "), hr );
usingCP = FALSE; // Try old style
}
hr = pCallbackCP->Advise(callbackCP, &dwConnection1);
pCallbackCP->Release();
if( FAILED(hr) )
{
ReportError( _T("Advise ConnectionPoint: "), hr );
dwConnection1 = 0;
usingCP = FALSE; // Try old style
}
}
if( !usingCP )
{
// OPC 1.0 data advise format
FORMATETC formatEtc ;
formatEtc.tymed = TYMED_HGLOBAL;
formatEtc.ptd = NULL;
formatEtc.dwAspect = DVASPECT_CONTENT;
formatEtc.lindex = -1;
// IAdviseSink is an interface on OUR object that is passed to
// the server for callbacks
IAdviseSink *pAdviseSink = NULL;
hr = testSink->QueryInterface(IID_IAdviseSink, (LPVOID *)&pAdviseSink);
if( FAILED(hr) )
{
ReportError( _T("IAdviseSink: "), hr );
opcGroup.Detach();
opcServer.Detach();
return;
}
// Get an IDataObject interface on the group
DataObject dataObject;
hr = dataObject.Attach( opcGroup );
if(FAILED(hr) || !dataObject.IsOk() )
{
// some servers don't do this, so don't quit altogether
MessageBox( 0, _T("IDataObject not supported by this server\nNo data notifications will take place"), _T("FactorySoft Client"), MB_OK );
return;
}
// Register our IAdvise with the group
// Need to register both formats: data change, and write complete
formatEtc.cfFormat = OPCSTMFORMATWRITECOMPLETE ;
hr = dataObject.DAdvise(&formatEtc,
ADVF_PRIMEFIRST, // ADVF flag
pAdviseSink,
&dwConnection2);
if( FAILED(hr) )
{
ReportError( _T("IDataObject::DAdvise: "), hr );
return;
}
#ifdef DATATIMEFORMAT
formatEtc.cfFormat = OPCSTMFORMATDATATIME ;
#else
formatEtc.cfFormat = OPCSTMFORMATDATA ;
#endif // DATATIMEFORMAT
hr = dataObject.DAdvise(&formatEtc,
ADVF_PRIMEFIRST, // ADVF flag
pAdviseSink,
&dwConnection1);
pAdviseSink->Release();
if( FAILED(hr) )
{
ReportError( _T("IDataObject::DAdvise: "), hr );
return;
}
}
}
void OPCClientDoc::OnUpdateOpcConnect(CCmdUI* pCmdUI)
{
pCmdUI->Enable( !opcServer.IsOk() );
}
//*******************************************************************
void OPCClientDoc::OnOpcDisconnect()
{
CWaitCursor wait;
HRESULT hr = S_OK;
if( opcServer.IsOk() && opcGroup.IsOk() )
{
if( dwShutdownConnection )
{
IConnectionPointContainer *pCPC = 0;
hr = opcServer.QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC);
if( SUCCEEDED(hr) )
{
IConnectionPoint *pCallbackCP = 0;
hr = pCPC->FindConnectionPoint(IID_IOPCShutdown, &pCallbackCP);
if( SUCCEEDED(hr) )
{
hr = pCallbackCP->Unadvise(dwShutdownConnection);
pCallbackCP->Release();
}
pCPC->Release();
}
}
if( usingCP )
{
// OPC 2.0 ConnectionPoints
IConnectionPointContainer *pCPC = 0;
hr = opcGroup.QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC);
if( SUCCEEDED(hr) && dwConnection1 ) // This server supports 2.0
{
IConnectionPoint *pCallbackCP = 0;
hr = pCPC->FindConnectionPoint(IID_IOPCDataCallback, &pCallbackCP);
if( SUCCEEDED(hr) )
{
hr = pCallbackCP->Unadvise(dwConnection1);
pCallbackCP->Release();
}
pCPC->Release();
}
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -