OPC Exercises
OPC Exercises
Table of Contents
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
Objectives
In Visual Basic 6, using OPC DA Automation objects and the PI OPC DA/HDA Server:
o Write PI values
Problem Description
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 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
2. Double-click on the "Connect to OPC Server" button on the form in order to go in the
cmdConnect_Click event.
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
7. First add code to create an OPC group with the specified name:
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
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.
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.
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
17. Double-click on the "Write Value" button to get in the cmdWrite_Click event.
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
Take a look at the declaration of the module-level OPCGroup object, at the top of the code.
20. In the Form_Load event, add the following code to subscribe the group for new
values:
m_opcGroup.IsSubscribed = True
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
Objectives
In Visual Basic 6, using OPC HDA Automation objects and the PI OPC DA/HDA Server:
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 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
2. Double-click on the "Connect to OPC Server" button on the form in order to go in the
cmdConnect_Click event.
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
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.
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:
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.
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.
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:
14. Now examine how the For..Next loop goes through the array of attributes to display
them.
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:
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.
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:
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:
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
31. Add the code to update the item with the selected action and the specified value:
34. Add the code to delete the values within the specified time frame:
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