How to dump OPC server's address space

QuickOPC-COM
Product Version 5.0
Application Note
How to dump OPC server's address space
recursively to the console in C++
Following C++ code dumps all branches in leaves in OPC server's address space to the console, with the use of
QuickOPC-COM. It has been tested in Visual Studio 2008. Note that the code does not use any MFC or ATL.
// Dumps OPC server's address space recursively to the console.
//
#include "stdafx.h"
#import "libid:FAB7A1E3-3B79-4292-9C3A-DF39A6F65EC1" version(5.0) //
EasyOpcDALib
#import "scrrun.dll" rename("DeleteFile", "DeleteFileX") rename("MoveFile",
"MoveFileX") rename("CopyFile", "CopyFileX") \
exclude("IDrive", "IDriveCollection", "IFileSystem", "IFileSystem3",
"IFolder", "ITextStream", "IFolderCollection", "IFile", \
"IFileCollection")
using namespace EasyOpcDALib;
using namespace Scripting;
//
void DumpBranch(IEasyDAClientPtr EasyDAClientPtr, LPCTSTR ParentItemId, int
Level)
{
// Browse all nodes (branches and leaves) under ParentItemId
IDictionaryPtr NodeElementDictionaryPtr(EasyDAClientPtr>BrowseNodes(_T(""), _T("OPCLabs.KitServer.2"), ParentItemId));
variant_t vKeys(NodeElementDictionaryPtr->Keys());
variant_t vItems(NodeElementDictionaryPtr->Items());
LONG Ubound;
SafeArrayGetUBound(vKeys.parray, 1, &Ubound);
for (LONG i = 0; i <= Ubound; i++)
{
_variant_t vKey;
SafeArrayGetElement(vKeys.parray, &i, &vKey);
_variant_t vItem;
SafeArrayGetElement(vItems.parray, &i, &vItem);
IDANodeElementPtr NodeElementPtr(vItem);
for (int j = 0; j < Level; j++) _tprintf(_T("
"));
_tprintf(_T("%s: HasChildren %d, IsBranch %d, IsLeaf %d, ItemId '%s',
Name '%s'\n"),
(TCHAR*)_bstr_t(vKey),
NodeElementPtr->HasChildren,
NodeElementPtr->IsBranch,
NodeElementPtr->IsLeaf,
(TCHAR*)NodeElementPtr->ItemId,
(TCHAR*)NodeElementPtr->Name);
CODE Consulting and Development, s.r.o. , Bolevecka naves 27, 323 00 Plzen, Czech Republic
e-mail: [email protected], Web: www.opclabs.com, tel. +420 603 214 412, fax +420 378 600 795
QuickOPC-COM-2-How to dump OPC server's address space recursively to the console in C++.docx; 2013-11-21
Page 1 of 3
QuickOPC-COM
Product Version 5.0
Application Note
// If this node is a branch, dump its children recursively
if (NodeElementPtr->IsBranch)
DumpBranch(EasyDAClientPtr, NodeElementPtr->ItemId, Level + 1);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize the COM library
CoInitialize(NULL);
// Instatiate the EasyOPC-DA client object
IEasyDAClientPtr EasyDAClientPtr(__uuidof(EasyDAClient));
// Dump the OPC address space from the root branch
DumpBranch(EasyDAClientPtr, _T(""), 0);
// Release all interface pointers BEFORE calling CoUninitialize()
EasyDAClientPtr = NULL;
CoUninitialize();
TCHAR line[80];
_putts(_T("Press Enter to continue..."));
_fgetts(line, sizeof(line), stdin);
return 0;
}
This example has been added to QuickOPC-COM Bonus pack and will be available with its next build.
The output is indented according to the hierarchical structure of the address space. Here is how the actual output
may look like:
Demo: HasChildren -1, IsBranch -1, IsLeaf 0, ItemId 'Demo', Name 'Demo'
Greenhouse: HasChildren -1, IsBranch -1, IsLeaf 0, ItemId 'Greenhouse', Name
'Greenhouse'
Sprinklers: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Greenhouse.Sprinklers', Name 'Sprinklers'
Temperature: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Greenhouse.Temperature', Name 'Temperature'
RoofOpen: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Greenhouse.RoofOpen', Name 'RoofOpen'
Humidity: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Greenhouse.Humidity', Name 'Humidity'
$ServerControl: HasChildren -1, IsBranch -1, IsLeaf 0, ItemId
'$ServerControl', Name '$ServerControl'
$Mode: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'$ServerControl.$Mode', Name '$Mode'
Command: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'$ServerControl.Command', Name 'Command'
Trends: HasChildren -1, IsBranch -1, IsLeaf 0, ItemId 'Trends', Name 'Trends'
Ramp (1 s): HasChildren 0, IsBranch 0, IsLeaf -1, ItemId 'Trends.Ramp (1
s)', Name 'Ramp (1 s)'
Sine (10 s): HasChildren 0, IsBranch 0, IsLeaf -1, ItemId 'Trends.Sine
(10 s)', Name 'Sine (10 s)'
Ramp (10 s): HasChildren 0, IsBranch 0, IsLeaf -1, ItemId 'Trends.Ramp
(10 s)', Name 'Ramp (10 s)'
Sine (1 s): HasChildren 0, IsBranch 0, IsLeaf -1, ItemId 'Trends.Sine (1
s)', Name 'Sine (1 s)'
CODE Consulting and Development, s.r.o. , Bolevecka naves 27, 323 00 Plzen, Czech Republic
e-mail: [email protected], Web: www.opclabs.com, tel. +420 603 214 412, fax +420 378 600 795
QuickOPC-COM-2-How to dump OPC server's address space recursively to the console in C++.docx; 2013-11-21
Page 2 of 3
QuickOPC-COM
Product Version 5.0
Application Note
Ramp (1 min): HasChildren 0, IsBranch 0, IsLeaf -1, ItemId 'Trends.Ramp
(1 min)', Name 'Ramp (1 min)'
Sine (1 min): HasChildren 0, IsBranch 0, IsLeaf -1, ItemId 'Trends.Sine
(1 min)', Name 'Sine (1 min)'
Ramp (10 min): HasChildren 0, IsBranch 0, IsLeaf -1, ItemId 'Trends.Ramp
(10 min)', Name 'Ramp (10 min)'
Sine (10 min): HasChildren 0, IsBranch 0, IsLeaf -1, ItemId 'Trends.Sine
(10 min)', Name 'Sine (10 min)'
Simulation: HasChildren -1, IsBranch -1, IsLeaf 0, ItemId 'Simulation', Name
'Simulation'
RegisterSet_2: HasChildren -1, IsBranch -1, IsLeaf 0, ItemId
'Simulation.RegisterSet_2', Name 'RegisterSet_2'
ArrayOfUI2: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.RegisterSet_2.ArrayOfUI2', Name 'ArrayOfUI2'
I4: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.RegisterSet_2.I4', Name 'I4'
Register_RECORD: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.Register_RECORD', Name 'Register_RECORD'
RegisterSet_3: HasChildren -1, IsBranch -1, IsLeaf 0, ItemId
'Simulation.RegisterSet_3', Name 'RegisterSet_3'
ArrayOfUI2: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.RegisterSet_3.ArrayOfUI2', Name 'ArrayOfUI2'
I4: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.RegisterSet_3.I4', Name 'I4'
Register_I4: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.Register_I4', Name 'Register_I4'
Register_BOOL: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.Register_BOOL', Name 'Register_BOOL'
Register_UI4: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.Register_UI4', Name 'Register_UI4'
RegisterSet_4: HasChildren -1, IsBranch -1, IsLeaf 0, ItemId
'Simulation.RegisterSet_4', Name 'RegisterSet_4'
ArrayOfUI2: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.RegisterSet_4.ArrayOfUI2', Name 'ArrayOfUI2'
I4: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.RegisterSet_4.I4', Name 'I4'
Register_ERROR: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.Register_ERROR', Name 'Register_ERROR'
Register_UINT: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.Register_UINT', Name 'Register_UINT'
RegisterSet_5: HasChildren -1, IsBranch -1, IsLeaf 0, ItemId
'Simulation.RegisterSet_5', Name 'RegisterSet_5'
ArrayOfUI2: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.RegisterSet_5.ArrayOfUI2', Name 'ArrayOfUI2'
I4: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.RegisterSet_5.I4', Name 'I4'
Register_DISPATCH: HasChildren 0, IsBranch 0, IsLeaf -1, ItemId
'Simulation.Register_DISPATCH', Name 'Register_DISPATCH'
...
CODE Consulting and Development, s.r.o. , Bolevecka naves 27, 323 00 Plzen, Czech Republic
e-mail: [email protected], Web: www.opclabs.com, tel. +420 603 214 412, fax +420 378 600 795
QuickOPC-COM-2-How to dump OPC server's address space recursively to the console in C++.docx; 2013-11-21
Page 3 of 3