It's like when technology gets into industry , It has a high value , We're all in silence , Nobody breaks this window paper , A lot of ideas and source code should be obtained from foreign websites , There are always conditions at home , Limited -- Just don't tell you , Fear of church disciples , Starve master .

Studied N day , Based on my configuration background OPC Client driver , Considering the particularity of driving , So only synchronous interface operation is developed . After a period of testing , just so so . Now let's make the core code public , somewhat VC Basic people can use it directly .

//*************************************************************************
// letter number name :ConnectOPC
// Class name :COPCClien
// transport     enter :CString SvrName
// transport     Out :
// Function description : connect OPC The server
// global variable :
// Call module :
// do     person : Leaf sail
// day     stage :2005 year 12 month 1 day
// repair change people :
// day     stage :
// edition     book :
//*************************************************************************
long COPCClient::ConnectOPC(CString cSvrName)
{
    HRESULT hr;
 CLSID clsid;
 WCHAR wszProgID [512];

 //----------------------------------
 // initialization COM
 hr =CoInitialize(NULL);
 if (FAILED(hr))  return 8;           //com initialization failed
   
 //-----------------------------------
 if(strSvrName!="")        // Determine if it is connected OPC
 {
  if (strSvrName==cSvrName) return 2;  //OPC Server connected
  else                      return 4;  // Only one can be connected OPC The server
 }
    //-----------------------------------

 try
 {
     
  //----------------------------------
  // Convert the object ID in string form to CLSID structural style
  _mbstowcsz (wszProgID, cSvrName, sizeof (wszProgID) / sizeof (WCHAR));
  hr= CLSIDFromProgID(wszProgID,    // [in]
       &clsid);      // [out]
  if(FAILED(hr))
  {
   CoTaskMemFree(&clsid);        //COM Memory release function
   CoUninitialize();             // termination COM Library function service
   return 16;                    // obtain clsid fail
  }
       
  //--------------------------------
  // establish Server example
  hr=CoCreateInstance(clsid,       //[in]
     NULL,                //[in]
     CLSCTX_SERVER,       //[in]
     IID_IUnknown,        //[in]
     (void**)&pUNK);      //[out]

  if(FAILED(hr))
  {
   CoTaskMemFree(&clsid);
      if(pUNK) pUNK->Release();
   pUNK=NULL;
   CoUninitialize();
   return 32;                   // establish Server Instance failed
  }
 
  //------------------------------------
  // query pOPC Interface
  hr=pUNK->QueryInterface(IID_IOPCServer,// [in]
        (void**)&pOPC);// [out]
  if(FAILED(hr))
  {
   CoTaskMemFree(&clsid);
   if(pOPC) pOPC->Release();
   if(pUNK) pUNK->Release();
   pOPC=NULL;
   pUNK=NULL;
   return 64;                   // query pOPC Interface failed
  }

  CoTaskMemFree(&clsid);

        //---------------------------
  strSvrName=cSvrName;             // Assign current OPC Service name

 
 }
 catch(...)
 {
  return 128;                      // Critical error connecting to server
 }
 return 0;                            // success
}

//*************************************************************************
// letter number name :DisconnectOPC
// Class name :COPCClient
// transport     enter :
// transport     Out :long
// Function description : to break off OPC The server
// global variable :
// Call module :
// do     person : Leaf sail
// day     stage :2005 year 12 month 1 day
// repair change people :
// day     stage :
// edition     book :
//*************************************************************************
long COPCClient::DisconnectOPC()
{
    if(strSvrName=="")  return 1;   //OPC The server is not connected

 HRESULT *pErrors = NULL;
 DWORD dwCount=mIOMDevice->mItem.GetSize(),i;

 strSvrName="";                  // Server name empty

 //---------------
 OPCHANDLE *phServer = NULL;
 // Stop asynchronous operation
 pIAsync2->SetEnable(false);
            
 // Remove Tag
 phServer = (OPCHANDLE *) CoTaskMemAlloc (dwCount * sizeof (OPCHANDLE));
 for(i=0;i<dwCount;i++) phServer[i] = (OPCHANDLE)arrhServer.GetAt(i);

 pIItemMgt->RemoveItems(dwCount,   // [in]
         phServer,   // [in]
        &pErrors);  // [out]

 arrhServer.RemoveAll();
 arrhServer.FreeExtra();
 arrItemType.RemoveAll();
 arrItemType.FreeExtra();
 
 // delete group
 pOPC->RemoveGroup(hServer,   //[in]
      true);      //[in]
  
 //---------------
 CoTaskMemFree(&hServer);
 CoTaskMemFree(&hGroup);

 if(pErrors) CoTaskMemFree(pErrors);
    if(pResults) CoTaskMemFree(pResults);

 if(pIAsync2) pIAsync2->Release();
 if(pISync) pISync->Release();
 if(pIItemMgt) pIItemMgt->Release();
 if(pOPC) pOPC->Release();
 if(pUNK) pUNK->Release();

 pOPC=NULL;
 pUNK=NULL;
 pIItemMgt=NULL;
 pIAsync2=NULL;
 pISync=NULL;
 hGroup=0;
 hServer=0;
 
 CoUninitialize();                // close COM

 return 0;
}

//*************************************************************************
// letter number name :AddGroup
// Class name :COPCClient
// transport     enter :
// transport     Out :long
// Function description : add group
// global variable :
// Call module :
// do     person : Leaf sail
// day     stage :2005 year 12 month 1 day
// repair change people :
// day     stage :
// edition     book :
//*************************************************************************
long COPCClient::AddGroup()
{
 HRESULT hr;
    WCHAR wchBuffer[255];
 long lBias=0;
 float fDeadband=0;
 DWORD dwRevUpdateRate=0;
 IUnknown *pUNKgroup;        // Group interface pointer

 MultiByteToWideChar(CP_ACP, 0, mIOMDevice->ConfigMessage, -1, wchBuffer, 255);
   
 // add group
 hr = pOPC->AddGroup (
   wchBuffer,     // [in] group name
   TRUE,                  // [in] active state
   mIOMDevice->Scantime,       // [in] requested update rate
   hGroup,                  // [in] our handle to this group
   &lBias,      // [unique,in] time bias
   &fDeadband,     // [in] percent deadband
   1033,                     // [in] requested language ID
   &hServer,     // [out] server handle to this group
   &dwRevUpdateRate,   // [out] revised update rate
   IID_IUnknown,               // [in] REFIID riid,
   (LPUNKNOWN*)&pUNKgroup);    // [out, iid_is(riid)] LPUNKNOWN *pUNKgroup

 if(FAILED(hr))  // Failed to join group
 {
  CoTaskMemFree(&hServer);
  CoTaskMemFree(&dwRevUpdateRate);
  if(pUNKgroup) pUNKgroup->Release();
  pUNKgroup=NULL;
  return 1;
 }

 //--------------------------------------
 // query pIItemMgt
 hr=pUNKgroup->QueryInterface(IID_IOPCItemMgt,     // [in]
        (void**)&pIItemMgt);  // [out]

    // Query failed
    if(FAILED(hr))
 {
  CoTaskMemFree(&hServer);
  CoTaskMemFree(&dwRevUpdateRate);
  if(pUNKgroup) pUNKgroup->Release();
  pUNKgroup=NULL;
  if(pIItemMgt) pIItemMgt->Release();
  pIItemMgt=NULL;
  return 2;  
 }

 if(pUNKgroup) pUNKgroup->Release();
    pUNKgroup=NULL;
 CoTaskMemFree(&dwRevUpdateRate);
    return 0;
}

//*************************************************************************
// letter number name :AddItem
// Class name :COPCClient
// transport     enter :
// transport     Out :long
// Function description : Add in
// global variable :
// Call module :
// do     person : Leaf sail
// day     stage :2005 year 12 month 1 day
// repair change people :
// day     stage :
// edition     book :
//*************************************************************************
long COPCClient::AddItem()
{
 HRESULT hr;
 OPCITEMDEF *pItemArray = NULL;

 HRESULT *pErrors = NULL;
 DWORD dwCount,dwLen,i;
   
    //--------------------------------------
    // Add label
    dwCount=mIOMDevice->mItem.GetSize();
  pItemArray = (OPCITEMDEF *) CoTaskMemAlloc (dwCount * sizeof (OPCITEMDEF));
// Allocate memory
  
 for(i=0;i<dwCount;i++)
 {
  dwLen = lstrlen (mIOMDevice->mItem[i].strTab);
  pItemArray [i].szItemID = (WCHAR *) CoTaskMemAlloc ((dwLen + 1) * sizeof
(WCHAR));
  MultiByteToWideChar (CP_ACP, 0, mIOMDevice->mItem[i].strTab, -1, pItemArray
[i].szItemID, dwLen + 1);
  pItemArray [i].szAccessPath=NULL;
  pItemArray [i].bActive = true;                     // active state
  pItemArray [i].hClient = (OPCHANDLE)i;             // our handle to item
  pItemArray [i].dwBlobSize = 0;            // no blob support
  pItemArray [i].pBlob = NULL;
  pItemArray [i].vtRequestedDataType =VT_EMPTY;       // Requested data type
  pItemArray [i].wReserved=0;
 }
   
    // add to
 hr = pIItemMgt->AddItems (dwCount,   //[in] DWORD dwCount,Item count
       pItemArray,  //[in, size_is(dwCount)] OPCITEMDEF * pItemArray, Array of
item definition structures
       &pResults,   //[out, size_is(,dwCount)] OPCITEMRESULT ** ppAddResults,
Result array
       &pErrors);   //[out, size_is(,dwCount)] HRESULT ** ppErrors Error array
 // Add failed
 if(FAILED(hr))
 {
     if(pResults) CoTaskMemFree(pResults);
  if(pErrors) CoTaskMemFree(pErrors);
  CoTaskMemFree(&hServer);
  CoTaskMemFree(pItemArray);
        return 2;
 }
   
 // Synchronization interface
 hr=pIItemMgt->QueryInterface(IID_IOPCSyncIO,  // [in]
        (void**)&pISync); // [out]

 if(FAILED (hr))
 {
  CoTaskMemFree(&hServer);
  if(pISync) pISync->Release();
  if(pResults) CoTaskMemFree(pResults);
        if(pErrors) CoTaskMemFree(pErrors);
  pISync=NULL;
  return 3;          // query pISync Interface failed
 }
   
 // Asynchronous interface
 hr=pIItemMgt->QueryInterface(IID_IOPCAsyncIO2,(void**)&pIAsync2);
 if(FAILED (hr))
 {
  CoTaskMemFree(&hServer);
  if(pIAsync2) pIAsync2->Release();
  if(pResults) CoTaskMemFree(pResults);
        if(pErrors) CoTaskMemFree(pErrors);
  pIAsync2=NULL;
  return 4;         // query pIAsync2 Interface failed
 }
   
 //---------------------
 arrhServer.SetSize(dwCount);
 arrItemType.SetSize(dwCount);

 for(i=0;i<dwCount;i++)
 {
  arrhServer.SetAt(i,pResults[i].hServer );
  arrItemType.SetAt(i,(WORD)pResults[i].vtCanonicalDataType);
 }

 //---------------------

    if(pErrors) CoTaskMemFree(pErrors);
 CoTaskMemFree(pItemArray);
    return 0;
}

//*************************************************************************
// letter number name :SyncRead
// Class name :COPCClient
// transport     enter :long lngNo,
//   CString strData
// transport     Out :long
// Function description : Read data synchronously
// global variable :
// Call module :
// do     person : Leaf sail
// day     stage :2005 year 12 month 1 day
// repair change people :
// day     stage :
// edition     book :
//*************************************************************************
long COPCClient::SyncRead(long lngNo,CString &strData)
{
    HRESULT hr = E_FAIL;
 DWORD dwCount=1;
 OPCDATASOURCE dwSource = OPC_DS_CACHE;
 OPCHANDLE *phServer  = NULL;
 OPCITEMSTATE *pValues = NULL;
 HRESULT *pErrors  = NULL;

 ::Sleep(0);

 try
 {
  phServer = (OPCHANDLE *) CoTaskMemAlloc (dwCount * sizeof (OPCHANDLE));
  if (phServer == NULL)
  {
   return 1;   // error has occurred allocating memory
  }

  phServer[0] =(OPCHANDLE)arrhServer.GetAt(lngNo);
  hr=pISync->Read(dwSource,   //[in]
      dwCount,    //[in]
      phServer,   //[in]
      &pValues,   //[out]
      &pErrors);  //[out]
  if(FAILED(hr))
  {
   if (phServer) CoTaskMemFree (phServer);
      if (pValues) CoTaskMemFree (pValues); 
      if (pErrors) CoTaskMemFree (pErrors);
   VariantClear (&pValues[0].vDataValue);
   return 2;   // Error reading data synchronously
  }

  // data conversion
  Variant2Str(pValues[0].vDataValue,strData);
  VariantClear (&pValues[0].vDataValue);

  if (phServer) CoTaskMemFree (phServer);
  if (pValues) CoTaskMemFree (pValues); 
  if (pErrors) CoTaskMemFree (pErrors);
 }
    catch (...)
 {
     return 3;
 }

 return 0;
}

//*************************************************************************
// letter number name :SyncWrite
// Class name :COPCClient
// transport     enter :long lngNo,
//   CString &strData
// transport     Out :long
// Function description : Synchronous write
// global variable :
// Call module :
// do     person : Leaf sail
// day     stage :2005 year 12 month 1 day
// repair change people :
// day     stage :
// edition     book :
//*************************************************************************
long COPCClient::SyncWrite(long lngNo,CString strData)
{  

 DWORD dwIndex  = 0;
 OPCHANDLE *phServer = NULL;
 VARIANT *pValues = NULL;
 HRESULT *pErrors = NULL;
 HRESULT hr   = E_FAIL;
 DWORD cdwItems=1;

 phServer = (OPCHANDLE *) CoTaskMemAlloc (cdwItems * sizeof (OPCHANDLE));
 pValues = (VARIANT *) CoTaskMemAlloc (cdwItems * sizeof (VARIANT));

 if (phServer == NULL || pValues == NULL)
 {
    return 1;
 }
   
 ::Sleep(0);
 
 try
 {
        // Write data synchronously
  phServer[0] =(OPCHANDLE)arrhServer.GetAt(lngNo);
       
  // data conversion
  Str2Variant(strData,pValues[0],arrItemType.GetAt(lngNo));

  hr = pISync->Write (
    1,             // Item count
    phServer,  // Array of server handles for items
    pValues,  // Array of values
    &pErrors);  // Array of errors

  if(FAILED(hr))
  {
   if (phServer) CoTaskMemFree (phServer);
      if (pValues) CoTaskMemFree (pValues); 
      if (pErrors) CoTaskMemFree (pErrors);
   VariantClear (&pValues[0]);
   return 2;   // Error reading data synchronously
  }
 
  VariantClear (&pValues[0]);
 }
 
 catch (...)
 {
     return 3;
 }

 if(phServer)CoTaskMemFree (phServer);
 if (pValues)CoTaskMemFree (pValues);
 if (pErrors) CoTaskMemFree (pErrors);
   
 return 0;
}

 

 

Technology
©2019-2020 Toolsou All rights reserved,
[AndroidO] [RK3399] -- GPIO Drive and control mode PowerShell Use in WebClient Download the file and get the download progress TypeScript Data types in is enough LED Scrolling text python in switch_to_alert Usage of ( Essence )2020 year 6 month 26 day C# Class library Exception handling help class mybatis Return result mapping of series element-ui Of el-date-picker Component get value Digital rolling lottery program ( Essence )2020 year 8 month 9 day C# Basic knowledge reflex