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

📄 opcclient.cpp

📁 VC++实现的预测控制
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// OPCClient.cpp : implementation file
//

#include "stdafx.h"
#include "MPCTest.h"
#include "OPCClient.h"
#include "OPCSET.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UINT OPCSTMFORMATDATA = RegisterClipboardFormat(_T("OPCSTMFORMATDATA"));
UINT OPCSTMFORMATDATATIME = RegisterClipboardFormat(_T("OPCSTMFORMATDATATIME"));
UINT OPCSTMFORMATWRITECOMPLETE = RegisterClipboardFormat(_T("OPCSTMFORMATWRITECOMPLETE"));
COPCClient* theOpcDoc;
extern CArray<CString,CString> AllItemList;
CArray<CString,CString> ReadItemList;
CArray<CString,CString> WriteItemList;
CArray<CString,CString> PIDParas;
 
extern CArray<mytag*,mytag*>* AOTagList;
/////////////////////////////////////////////////////////////////////////////
// COPCClient

IMPLEMENT_DYNCREATE(COPCClient, CCmdTarget)

COPCClient::COPCClient()
{
   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;
   m_async =FALSE;
   m_connect = FALSE;
   m_PID = 0;
   AfxOleLockApp();
   theOpcDoc = this;
   // 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);
   }
}

COPCClient::~COPCClient()
{
   AfxOleUnlockApp();
   if( opcServer.IsOk() )
      DisConnect();
   testSink->Release(); // OLE should clean this up, but may not have time!
   callbackCP->Release();
   shutdownCP->Release();
   Sleep( 100 );
}


BEGIN_MESSAGE_MAP(COPCClient, CCmdTarget)
	//{{AFX_MSG_MAP(COPCClient)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COPCClient message handlers

BOOL COPCClient::connect()
{
	 USES_CONVERSION;
     ASSERT( !opcServer.IsOk() );
     HRESULT hr = S_OK;

	 LPUNKNOWN pUnkn = NULL;
   if( m_connect )
   {
      hr = CoCreateInstance(m_clsid, NULL, CLSCTX_ALL, IID_IUnknown, (LPVOID *)&pUnkn);
      if( FAILED(hr) || pUnkn == NULL)
      {
         CString format("Error connecting to OPC server '%s' \n");// (LPCSTR)IDS_CONNECT_ERROR );
         CString msg;
         msg.Format( format, lastServer );
         msg += _T("CoCreateInstance: ");
         AfxMessageBox( msg);
         return 0;
      }
   }
   else  // use the node name
   {
      COSERVERINFO si;
      MULTI_QI  qi;

      si.dwReserved1 = 0;
      si.pwszName = T2OLE(lastNode.GetBuffer(0));
      si.pAuthInfo = NULL;
      si.dwReserved2 = 0;

      qi.pIID = &IID_IOPCServer;
      qi.pItf = NULL;
      qi.hr = 0;

      hr = CoCreateInstanceEx(m_clsid, NULL, CLSCTX_ALL, &si, 1, &qi);
      if (FAILED(hr))
      {
         CString format("Error connecting to OPC server '%s' \n");// (LPCSTR)IDS_CONNECT_ERROR );
         CString msg;
         msg.Format( format, lastServer );
         msg += _T("CoCreateInstance: ");
         //ReportError( msg, hr );
		 AfxMessageBox( msg );
         return 0;
      }
      if (FAILED(qi.hr))
      {
         //ReportError( _T("MultiQI: "), qi.hr );
		  AfxMessageBox(_T("MultiQI: "));
         return 0;
      }
      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 0;
   }
   
   //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 );
	   AfxMessageBox(_T("AddGroup: "));
      return 0;
   }

   // 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 );
		 AfxMessageBox(_T("FindConnectionPoint: "));
         usingCP = FALSE;  // Try old style
      }

      hr = pCallbackCP->Advise(callbackCP, &dwConnection1);
      pCallbackCP->Release();
      if( FAILED(hr) )
      {
         //ReportError( _T("Advise ConnectionPoint: "), hr );
		 AfxMessageBox(_T("Advise ConnectionPoint: ERROR "));
         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 );
		 AfxMessageBox(_T("IAdviseSink: "));
         opcGroup.Detach();
         opcServer.Detach();
         return 0;
      }

      // 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 0;
      }

      // 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 );
		 AfxMessageBox(_T("IDataObject::DAdvise: "));
         return 0;
      }

#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 );
		 AfxMessageBox(_T("IDataObject::DAdvise: "));
         return 0;
      }
   }
     return 1;
}

BOOL COPCClient::Read()
{
     HRESULT *pErrors;
     CWaitCursor wait;
	 {  // also test sync read since data is returned differently
      OPCSyncIO opcSyncIO;
      if( opcSyncIO.Attach( opcGroup ) == S_OK )
      {
         OPCITEMSTATE* pItemState;
         HRESULT hr = opcSyncIO.Read(OPC_DS_DEVICE, 1, &pCurrentItem->hServerHandle, &pItemState, &pErrors);
         if( SUCCEEDED(hr) )
         {
            if( FAILED(pErrors[0]) )
            {
               //ReportError( _T("Sync Read: "), pErrors[0] );
			   AfxMessageBox("Sync Read failed");
            }
            else
            {
               pCurrentItem->quality = pItemState->wQuality;
               pCurrentItem->value = pItemState->vDataValue;
            }
            VariantClear( &pItemState->vDataValue );
            CoTaskMemFree( pItemState );
            CoTaskMemFree( pErrors );
         }
         else
         {
             //ReportError( _T("Sync Read: "), hr );
			 AfxMessageBox("Sync Read failed");
			 return 0;
         }
      }
   }
	 if( usingCP )
   {
      OPCAsyncIO2 opcAsyncIO2;
      if( opcAsyncIO2.Attach( opcGroup ) == S_OK )
      {
         transactionID = 3;
         HRESULT hr = opcAsyncIO2.Read(1, &pCurrentItem->hServerHandle, transactionID, &transactionID, &pErrors);
         if( SUCCEEDED(hr) )
         {
            if( FAILED(pErrors[0]) )
            {
               //ReportError( _T("Async Read: "), pErrors[0] );
				AfxMessageBox("Sync Read failed");
            }
            CoTaskMemFree( pErrors );
         }
         else
         {
            //ReportError( _T("Async Read: "), hr );
			 AfxMessageBox("Sync Read failed");
         }
#ifdef FULL_TEST
         transactionID = 3;
         hr = opcAsyncIO2.Read(1, &pCurrentItem->hServerHandle, transactionID, &transactionID, &pErrors);
         if( SUCCEEDED(hr) )
         {
            CoTaskMemFree( pErrors );
         }
         transactionID = 3;
         hr = opcAsyncIO2.Read(1, &pCurrentItem->hServerHandle, transactionID, &transactionID, &pErrors);
         if( SUCCEEDED(hr) )
         {
            CoTaskMemFree( pErrors );
         }
         // ask for a few this time (it doesn't matter that its the same one)
         transactionID = 3;
         OPCHANDLE serverHandles[3];
         for( int i=0; i<3; i++ )
            serverHandles[i] = pCurrentItem->hServerHandle;
         hr = opcAsyncIO2.Read(3, serverHandles, transactionID, &transactionID, &pErrors);
         if( SUCCEEDED(hr) )
         {
            CoTaskMemFree( pErrors );
         }
#endif // FULL_TEST
      }
   }
   else
   {
      OPCAsyncIO opcAsyncIO;

⌨️ 快捷键说明

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