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

📄 unit1.cpp

📁 用C++ 实现的OPC SERVER的原代码
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#include <assert.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;


//---------------------------------------------------------------------------
 
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner),
   m_vbActive(VARIANT_FALSE),
   m_hGroup(0),
   m_dwRate(1000),
   m_fDeadBand(0.0f),
   m_hItem(0),
   m_dwID(0),
   m_dwCancelID(0)
{
   CoGetMalloc(MEMCTX_TASK, &m_ptrMalloc);
   assert(m_ptrMalloc != NULL);
}

//---------------------------------------------------------------------------
 void __fastcall TForm1::ConnectToServer()
{
   HRESULT  Hret2= CoInitialize(NULL);
   //assert(!m_vbActive);
  // assert(m_ptrMalloc != NULL);
   // Create the OPC Server:
   CLSID clsid;
  CLSIDFromProgID(L"OPC.SimaticNET", &clsid);
  CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_IOPCServer,reinterpret_cast<LPVOID*>(&m_ptrServer));
  DWORD dwRevisedRate = 0;
  m_ptrServer->AddGroup(L"", TRUE, m_dwRate, 0, 0, &m_fDeadBand,0, &m_hGroup, &dwRevisedRate, IID_IUnknown,reinterpret_cast<LPUNKNOWN*>(&m_ptrGroup));
  m_dwRate = dwRevisedRate;
  //assert(m_ptrGroup != NULL);
  m_ptrGroup->QueryInterface(IID_IOPCSyncIO,reinterpret_cast<LPVOID*>(&m_ptrSyncIO));

 }
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

 {
   try
   {
     // assert(m_ptrMalloc != NULL);
    //  assert(m_ptrSyncIO != NULL);
      phResult = NULL;
      OPCITEMSTATE* pItemState;
      m_ptrSyncIO->Read(OPC_DS_DEVICE, 1, &m_hItem, &pItemState, &phResult);
   //   assert(phResult != NULL);
      if (SUCCEEDED(phResult[0]))
      {
      //   assert(pItemState != NULL);
         float fkuangshi = V_R4(&pItemState[0].vDataValue);
     
         String ckuangshi = ckuangshi.FormatFloat("%3.1f",fkuangshi);
          Edit1->Text =  ckuangshi;
       //  Edit1->Text = Format(_T('%s'),ARRAYOFCONST((Variant2Str(pItemState[0].vDataValue)));

         // Free results
         m_ptrMalloc->Free(pItemState);
         m_ptrMalloc->Free(phResult);
      }
      else
      {


	      // Free the returned results
	      //
         m_ptrMalloc->Free(phResult);
	      m_ptrMalloc->Free(pItemState);

      
      }
   }
   catch(...)
   {
   }

}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormShow(TObject *Sender)
{
ConnectToServer();
 GetItems();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{

   // Only integers, by now:
  // CComVariant vValue = Sysutils::StrToInt(Edit2->Text);

   assert(m_ptrMalloc != NULL);
   assert(m_ptrSyncIO != NULL);

   HRESULT *phResult = NULL;
  // m_ptrSyncIO->Write(1, &m_hItem, &vValue, &phResult);

   // Check the item result for errors
   assert(phResult != NULL);

   m_ptrMalloc->Free(phResult);
 
}

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


void __fastcall TForm1::FormDestroy(TObject *Sender)
{
      CComPtr<IOPCItemMgt> ptrItMgm;
      m_ptrGroup->QueryInterface(IID_IOPCItemMgt,reinterpret_cast<LPVOID*>(&ptrItMgm));
      HRESULT* phResult = NULL;
      ptrItMgm->RemoveItems(1, &m_hItem, &phResult);
      // Check the item result for errors
      assert(phResult != NULL);
      HRESULT hr = phResult[0];
      m_ptrMalloc->Free(phResult);
      m_hItem = 0;
      m_ptrSyncIO.Release();
      m_ptrGroup.Release();
      m_ptrServer->RemoveGroup(m_hGroup, FALSE);
      m_hGroup = 0;
      m_ptrServer.Release();
      m_vbActive = VARIANT_FALSE;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GetItems()
{
	// loop until all items are added
	char sz2[200];
	TCHAR szBuffer[256];
	HRESULT hr = 0;
	int nTestItem = 0; // how many items there are
	IEnumString* pEnumString = NULL;
	int nCount = 0;
	USES_CONVERSION;
        ListBox1->Items->Clear();
        m_ptrBrowse->BrowseOPCItemIDs(OPC_FLAT, L"", VT_EMPTY, 0, &pEnumString);
	LPOLESTR pszName = NULL;
        ULONG count = 0;
        while((hr = pEnumString->Next(1, &pszName, &count)) == S_OK)
        {
            ListBox1->Items->Add(OLE2T(pszName));
            ::CoTaskMemFree(pszName);
        }
        pEnumString->Release();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
  //edItem->Text=ListBox2->Items->Strings[ListBox2->ItemIndex];
   m_bstrItem.Empty();
   m_bstrItem = WideString(ListBox1->Items->Strings[ListBox1->ItemIndex]);
   ConnectToItem();

}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

void __fastcall TForm1::ConnectToItem()
{
 m_ptrGroup->QueryInterface(IID_IOPCItemMgt,reinterpret_cast<LPVOID*>(&ptrItMgm));
  OPCITEMDEF itemdef;
  HRESULT *phResult = NULL;
  OPCITEMRESULT *pItemState = NULL;
  // Define one item
  USES_CONVERSION;
	itemdef.szItemID = m_bstrItem;
	itemdef.szAccessPath = T2OLE(_T(""));
	itemdef.bActive = TRUE;
	itemdef.hClient = reinterpret_cast<DWORD>(Handle);
	itemdef.dwBlobSize = 0;
	itemdef.pBlob = NULL;
	itemdef.vtRequestedDataType = 0;
  // Add then items and check the hresults
  ptrItMgm->AddItems(1, &itemdef, &pItemState, &phResult);
 // Check the item result for errors
 //  assert(phResult != NULL);
   if (SUCCEEDED(phResult[0]))
   {
      // Store item server handle for future use
   //   assert(pItemState != NULL);
      m_hItem = pItemState[0].hServer;
      if (pItemState[0].pBlob != NULL)
      {
         m_ptrMalloc->Free(pItemState[0].pBlob);
      }
      // Free the returned results
      m_ptrMalloc->Free(phResult);
      m_ptrMalloc->Free(pItemState);
      // If it gets here, the server is active
      m_vbActive = VARIANT_TRUE;
   }
   else
   {
    // Free the returned results
      m_ptrMalloc->Free(phResult);
      m_ptrMalloc->Free(pItemState);
   }
}

⌨️ 快捷键说明

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