0% found this document useful (0 votes)
111 views

OPC Exercises

This document provides step-by-step instructions for accessing PI data using OPC DA Automation in VB6. It describes how to connect to an OPC server, map PI points, read and write PI values. The instructions guide the user through modifying an existing VB6 template project to connect to a PI OPC server, create an OPC group, add and remove PI points, read the current value of points, and write values to points.

Uploaded by

Jerome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

OPC Exercises

This document provides step-by-step instructions for accessing PI data using OPC DA Automation in VB6. It describes how to connect to an OPC server, map PI points, read and write PI values. The instructions guide the user through modifying an existing VB6 template project to connect to a PI OPC server, create an OPC group, add and remove PI points, read the current value of points, and write values to points.

Uploaded by

Jerome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

OPC Exercises

Table of Contents

Exercise 1 - Accessing PI Data Using OPC DA Automation in VB6............................................................3


Objectives....................................................................................................................................... 3
Problem Description........................................................................................................................ 3
Approach......................................................................................................................................... 3
Step by Step Solution...................................................................................................................... 5
Exercise 2 - Accessing PI Data Using OPC HDA Automation in VB6.........................................................9
Objectives....................................................................................................................................... 9
Problem Description........................................................................................................................ 9
Approach......................................................................................................................................... 9
Step by Step Solution.................................................................................................................... 11

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 1
OPC Exercises

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 2
OPC Exercises

Exercise 1 - Accessing PI Data Using OPC DA


Automation in VB6

Objectives

In Visual Basic 6, using OPC DA Automation objects and the PI OPC DA/HDA Server:

o Connect to an OPC Server;

o Map items in the OPC Server (PI Points);

o Read PI values using Poll or Advise;

o Write PI values

Problem Description

Your organization already has a number of home-made applications to read/write data


from/to OPC Servers.

Because PI – using the PI OPC DA/HDA Server – becomes just another OPC Server, you
could simply add a few lines of code to these applications, so data from PI and other data
sources can be used at the same place.

Approach

With the "OSI OPC HDA Automation" Library, you will develop a Visual Basic 6 application
that performs the main tasks in regards with OPC DA:
a. Connect to an OPC Server;
b. Map items in the OPC Server (PI Points);
c. Read PI values using Poll or Advise;
d. Write PI values.

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 3
OPC Exercises

You can build the application from scratch, but it is strongly recommended you start from
the proposed template: c:\class\OPC\DA Console\Template\OPCDAConsole.vpb.

The following step-by-step solution will guide you through typing the missing lines of code;
make sure you understand the rest of the code and the way item handles are handled.

The missing lines are identified by the following comment: 'TYPE CODE HERE

You should consider taking some time to look at the modGeneric module, which contains a
number of global declarations.

Also note that "Option Base 1" is stated at the top of every file in the project; this is strongly
recommended when using the OPC Foundation's OPC Automation.

The complete solution is available in the c:\class\OPC\DA Console\Solution directory.

The completed application should look like that:

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 4
OPC Exercises

Step by Step Solution

1. Open the template project in Visual Studio 6: C:\class\OPC\DA


Console\Template\OPCDAConsole.vpb

2. Double-click on the "Connect to OPC Server" button on the form in order to go in the
cmdConnect_Click event.

3. Connect to the specified OPC server, on the specified node:

g_opcServer.Connect cmbServer.Text, txtNode.Text

At this point, you may want to check out how the OPCEnum service is used to list available
servers on the specified node. This is done in the txtNode_LostFocus event.

4. Back on the Design view of the form, double-click on the "Connect to OPC Server"
button to get back in the cmdConnect_Click event.

5. In the Else part of the If..Then..Else statement, disconnect from the current OPC
server:

g_opcServer.Disconnect

6. Double-click on the "Create Group" button to get in the cmdCreateGroup_Click


event.

7. First add code to create an OPC group with the specified name:

Set g_opcGroup = g_opcServer.OPCGroups.Add(txtGroupName.Text)

8. Then the code to remove the current OPC group:

g_opcServer.OPCGroups.Remove g_opcGroup.Name

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 5
OPC Exercises

9. Double-click on the "Add" button to get in the cmdAddItem_Click event

10. Add the specified ItemID to the OPC group:


Set itmNewItem = g_opcGroup.OPCItems.AddItem(txtItemID.Text, g_lngNextClientHandle)

The code that follows this line in the cmdAddItem_Click event is to manage item handles.
Notice the array that contains the associations between client handles and server handles.

11. Double-click on the "Remove" button to get in the cmdRemoveItem_Click event.

12. Remove the selected item from the OPC group:

g_opcGroup.OPCItems.Remove 1, lngSrvHandles, lngErrors

At this point, you may want to explore the code in the frmBrowseItems form.

This form presents how to use the OPCBrowser object to browse through items in the OPC
server; this would prevent users to type items manually.

13. Open the Design view of the frmReadCurrent form.

14. Double-click on the form's background to get in the Form_Load event.

15. Examine the For..Each loop that is used, and type the following code inside, to read
the item's current value:

itmTempItem.Read OPCCache

16. Open the Design view of the frmWriteCurrentValues form.

17. Double-click on the "Write Value" button to get in the cmdWrite_Click event.

18. Write the specified value to the selected OPC item:

g_opcGroup.OPCItems.Item(cmbItem.Text).Write txtValue.Text

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 6
OPC Exercises

19. Open the Code view of the frmAdviseGroup form.

Take a look at the declaration of the module-level OPCGroup object, at the top of the code.

This is a WithEvents declaration, giving access to the OPCGroup's DataChange event.

20. In the Form_Load event, add the following code to subscribe the group for new
values:

m_opcGroup.IsSubscribed = True

21. Examine the information provided by the m_opcGroup_DataChange event, and


how it is used in the routine, to display new values for items in the group:
 TransactionID As Long: the specified transaction ID, when using the
Asynchronous "AsyncRefresh" method.
 NumItems As Long: the number of items that received a new value
 ClientHandles() As Long: the client handles of the affected items
 ItemValues() As Variant: the new values of the affected items
 Qualities() As Long: the qualities of the new values
 TimeStamps() As Date: the timestamp of the new values

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 7
OPC Exercises

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 8
OPC Exercises

Exercise 2 - Accessing PI Data Using OPC HDA


Automation in VB6

Objectives

In Visual Basic 6, using OPC HDA Automation objects and the PI OPC DA/HDA Server:

o Connect to an OPC Server;

o Map items in the OPC Server (PI Points);

o Read Raw, Calculated and Timed PI values;

o Write PI values

Problem Description

Because the OPC DA specification is limited to reading and writing data at the current time,
the next logical step is to develop a client application that uses OPC HDA.

With this specification, one can retrieve or send data in the past, as well as get calculated
data.

Approach

With the "OSI OPC HDA Automation" Library, you will develop a Visual Basic 6 application
that performs the main tasks in regards with OPC HDA:
a. Connect to an OPC Server;
b. Map items in the OPC Server (PI Points);
c. Read Raw, Calculated and Timed PI values;
d. Write PI values.

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 9
OPC Exercises

You can build the application from scratch, but it is strongly recommended you start from
the proposed template: c:\class\OPC\HDA Console\Template\OPCHDAConsole.vpb.

The following step-by-step solution will guide you through typing the missing lines of code;
make sure you understand the rest of the code and the way item handles are handled.

The missing lines are identified by the following comment: 'TYPE CODE HERE

You should consider taking some time to look at the modGeneric module, which contains a
number of global declarations.

Also note that "Option Base 1" is stated at the top of every file in the project; this is strongly
recommended when using the OPC Foundation's OPC Automation.

The complete solution is available in the c:\class\OPC\HDA Console\Solution directory.

The completed application should look like that:

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 10
OPC Exercises

Step by Step Solution

1. Open the template project in Visual Studio 6: C:\class\OPC\HDA


Console\Template\OPCHDAConsole.vpb

2. Double-click on the "Connect to OPC Server" button on the form in order to go in the
cmdConnect_Click event.

3. Connect to the specified OPC server, on the specified node:

g_opcHDAServer.Connect cmbServer.Text, txtNode.Text

At this point, you may want to check out how the OPCEnum service is used to list available
servers on the specified node. This is done in the txtNode_LostFocus event.

4. Back on the Design view of the form, double-click on the "Disconnect from Server"
button to get back in the cmdConnect_Click event.

5. In the Else part of the If..Then..Else statement, disconnect from the current OPC
server:

g_opcServer.Disconnect

6. Double-click on the "Add" button to get in the cmdAddItem_Click event

7. Add the specified ItemID to client-side list of OPC items:

Set itmNewItem = g_opcHDASrv.OPCHDAItems.AddItem(txtItemID.Text, g_lngNextClientHandle)

The code that follows this line in the cmdAddItem_Click event is to manage item handles.
Notice the array that contains the associations between client handles and server handles.

8. Double-click on the "Remove" button to get in the cmdRemoveItem_Click event.

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 11
OPC Exercises

9. Remove the selected item from the client-side list of OPC items:

g_opcHDASrv.OPCHDAItems.Remove 1, lngSrvHandles, lngErrors

At this point, you may want to explore the code in the frmBrowseItems form.

This form presents how to use the OPCBrowser object to browse through items in the OPC
server; this would prevent users to type items manually.

10. Open the Design view of the frmGetItemAttributes form.

11. Double-click on the form's background to get in the Form_Load event.

12. You first have to get the list of attributes supported by the connected OPC Server;
this is because some attributes are generic to all OPC servers, and some may be
implemented by vendor-specific (OSIsoft, for example) OPC servers.

g_opcHDASrv.GetItemAttributes lngNbrAttributes, lngAttribIDs, strNames, strDescriptions, intDataTypes

13. Examine the For..Each loop that is used to loop through OPC Items, and type the
following code inside, to read the item's attributes:

g_opcHDASrv.OPCHDAItems.SyncReadAttribute Now(), Now(), itmTempItem.ServerHandle, lngNbrAttributes,


lngAttribIDs, varAttribValues, lngErrors

14. Now examine how the For..Next loop goes through the array of attributes to display
them.

15. Open the Design view of the frmGetRawValues form.

16. Double-click on the "OK" button to get in the cmdOK_Click event.

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 12
OPC Exercises

17. Examine the way the dates are handled; this is very important since the OPC
Automation may throw errors if not handled properly.

18. Examine the outer For..Each loop that is used, and type the following code inside,
to read the item's values for the specified time frame:

Set hstValues = itmTempItem.ReadRaw(varStartTime, varEndTime)

19. Examine the inner For..Each loop – the one that displays values on the ListBox –
and see how the OPCHDAValue is used, out of the OPCHDAHistory collection.

20. Open the Design view of the frmGetAggregatedValues form.

21. Double-click on the "OK" button to get in the cmdOK_Click event.

22. Note that the dates are handled similarly to the ones in the frmGetRawValues form.

23. Examine the way the interval is handled; like the dates, the interval has to be sent
in this specific format for the OPC Automation to accept it.

You may want to examine the fnValueOfTimeInterval function, at the bottom of the
code.

24. Request the aggregated (calculated) values for the current item:

Set hstValues = itmTempItem.ReadProcessed(varStartTime, varEndTime, dtInterval,


lstAggregates.ItemData(lstAggregates.ListIndex))

25. Open the Design view of the frmGetTimedValues form.

26. Double-click on the "OK" button to get in the cmdOK_Click event.

27. Once the array of timestamps is built – which is already done in the template – you
simply request these timed values from the OPC server:

Set hstValues = itmTempItem.ReadAtTime(lstTimestamps.ListCount, dtTimestamps)

28. Open the Design view of the frmUpdateDeleteValues form.

29. Double-click on the "Update Value" button to get in the cmdUpdate_Click event.

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 13
OPC Exercises

30. Add the code to make a reference to the selected item:

Set opcItem = g_opcHDASrv.OPCHDAItems(cmbItem.Text)

31. Add the code to update the item with the selected action and the specified value:

opcItem.Update CDate(txtTimestamp.Text), txtValue.Text, OPCHDAQualityRaw + OPCQualityGood,


cmbEditType.ItemData(cmbEditType.ListIndex)

32. Then go in the cmdDelete_Click event.

33. Add the code to make a reference to the selected item:

Set opcItem = g_opcHDASrv.OPCHDAItems(cmbItem.Text)

34. Add the code to delete the values within the specified time frame:

opcItem.DeleteRaw CDate(txtStartTime.Text), CDate(txtEndTime.Text)

The information contained in this guide is confidential and may be subject to revision. No part of this
publication may be reproduced or transmitted, in any form or by any means without prior permission
of OSIsoft.
All rights reserved, OSIsoft Inc.  2008
Page 14

You might also like