Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Release 01/2003
Contents
Contents
1 Introduction..................................................................................................................... 3
1.1 Development Environment .................................................................................. 4
2 Requirements.................................................................................................................. 5
2.1 Hardware Requirements...................................................................................... 6
2.2 Software Requirements ....................................................................................... 7
3 Preparing to Use OPC Functionality in Visual Basic.................................................. 8
3.1 Automation Interface............................................................................................ 9
4 Working with the OPC Client....................................................................................... 11
4.1 General .............................................................................................................. 12
4.2 Selecting an OPC Server and Establishing a Connection ................................ 14
4.3 Creating and Removing an OPC Group Object ................................................ 15
4.4 Adding Items to a Group or Removing Them.................................................... 16
4.5 Reading Values ................................................................................................. 17
4.6 Writing Values.................................................................................................... 18
4.7 Closing the Client............................................................................................... 19
5 Description of How the Individual Control Elements Work ..................................... 20
5.1 General Stipulations .......................................................................................... 21
5.2 Global Variables ................................................................................................ 22
5.2.1 OPC Variables ................................................................................................... 23
5.2.2 Variables Independent of OPC.......................................................................... 25
5.3 Basic Structure of a Method .............................................................................. 26
5.4 Method: Form_Load()........................................................................................ 27
5.5 Method: Form_Unload() .................................................................................... 28
5.6 Method: Button_Connect_Click() ...................................................................... 29
5.7 Method: Button_Disconnect_Click() .................................................................. 30
5.8 Method: Button_AddGroup_Click() ................................................................... 31
5.9 Method: Button_RemoveGroup_Click() ............................................................ 32
5.10 Method: Button_AddItems_Click()..................................................................... 33
5.11 Method: Button_RemoveItems_Click().............................................................. 35
5.12 Method: Button_SyncRead_Click() ................................................................... 36
5.13 Method: Button_SyncWrite_Click() ................................................................... 38
Introduction
1
Note
The following chapters describe the OPC client used for the SIMATIC Quick Start
CD in detail. This is not intended to be a programming manual but rather its aim is
to provide you with useful tips to help you when working with OPC in Visual Basic.
Visual Basic
As the development environment, we have used Visual Basic Studio V6.0 for
Microsoft. The samples can, however be reproduced in other development
environments. They have not, however, been tested in other development
environments.
Note
This OPC client is only intended to be a simple example. To make the code easier
to understand, it does not include any form of advanced error handling.
Requirements
2
Note
This chapter lists the hardware and software required to run the sample.
Overview
Below, you will find a list of the hardware components required to access S7
control variables.
After connecting the components, you have a system like the one shown below:
network
Description
The OPC client and OPC Server are installed on one PC (local access). The PC is
connected to an S7 controller (S7-300 or S7-400) via a network adapter (Ethernet
or PROFIBUS). A valid configuration for the PC station and the controller is
assumed.
Overview
SIMATIC NET PC software V6.0.5 (for NT and 2000) or V6.1 (for Windows XP)
Visual Studio 6 with Visual Basic (to create the OPC client)
Note
This chapter describes the preparations necessary to use OPC with Visual Basic.
To use OPC Data Access functionality with Visual Basic, a reference to the
SOPCDAAuto.dll must be added to the existing project by opening the
"References" dialog in Visual Basic from the "Project" menu.
After selecting the "Siemens OPC DAAutomation 2.0" entry, you exit the dialog
with the "OK" button. The reference is created and added to the project.
Object Browser
To gain an overview of the functions of this reference, open the "Object Browser"
dialog from the "View" menu. Here, select the entry "OPCSiemensDAAutomation"
in the combo box to display all the functions.
4.1 General
Overview
The general example consists of a dialog window containing a combo box, several
buttons, and edit boxes. When you start the sample program, all buttons except for
"Connect" are deactivated.
Operation
The table below lists the steps required to read or write values of OPC Items.
Step Description
1 Select an OPC Server from the "select an OPC-Server first" combo box
and establish a connection with the "Connect" button.
2 Create and remove an OPC group object with the "AddGroup" and
"RemoveGroup" buttons respectively.
3 Add items to the group with the "AddItems" button or remove them with
"RemoveItems"
4 Read values with the "SyncRead" button and display the values in the
"Value" display box.
5 Write values with the "SyncWrite" button and enter a value in the input
box.
6 Close the client with "Close".
Connecting
In the combo box, you can select a locally available (OPC server must be
registered on the local PC) OPC Data Access 2.x server with which the OPC client
will connect. Click the "Connect" button to establish a connection to the OPC
Server. The "Disconnect" button closes this connection again. This sample
supports only establishment of one connection between the OPC client and OPC
Server.
Quitting Monitoring
To stop operator control and monitoring, exit the OPC sample client using the
"Close" button. The OPC Server terminates automatically.
Creating a Group
The "AddGroup" button creates a group object with the name from the text box
above ("Group1"). You can select any name for this group.
Removing a Group
Adding Variables
In the input boxes of "Item1" and "Item 2", two ItemIDs can now be entered for
OPC Item that will be read or written synchronously.
To add these two OPC items to be group you created, click the "AddItems" button.
Removing Variables
If the selected ItemIDs originate from the address space of the OPC Server, the
"RemoveItems", "SyncRead", and "SyncWrite" buttons are activated.
The OPC items you added to the group can be deleted again with the
"RemoveItems" button.
If you click the "SyncRead" button, a synchronous read call is sent to the OPC
Server. After the call, the current values of the OPC variables are returned by the
OPC server and displayed in the "Value" text boxes.
To write values of the OPC variables synchronously, the values for the item must
first be entered in the text boxes above the "SyncWrite" button. When you click the
"SyncWrite" button, the values are now sent to the controller and stored in the
corresponding variables.
Note
By repeating the synchronous read, you can check the success of the
synchronous write. Handling errors if writing is unsuccessful is described
separately.
Close
After performing these two tasks you can simply close the connection by clicking
the "Disconnect" button after which, you can establish a new connection to the
same or to a different OPC Server.
If you do not want to test any further connections, you can exit the dialog with the
"Close" button. The required steps (delete items -> remove group -> close
connection) are performed automatically.
Note
This chapter describes every step and the corresponding Visual Basic source
code.
General Declaration
Option Explicit
With "Option Explicit", every variable must be declared before it is used. This
prevents the creation of new variables and their use if you make incorrect input.
Variables must now be created with "Dim".
Option Base 1
Option Base 1 specifies that the lowest index of an array is 1. This simplifies
working with the OPC arrays since all the arrays here start at 1.
General
Private Variables
MyOPCServer
MyOPCGroup corresponds to a group object with the name MyOPCServer. All the
OPC item management tasks are handled using this OPC object.
MyOPCServer
remove items
read synchronous
write synchronous
Private Variables
The ServerName variable contains the name of the selected OPC Server.
This variable contains the name of the group that is added to the OPC Server
object with the "AddGroup" button by copying the content of the corresponding text
box to the variable.
The "ItemIDs" array contains all ItemIDs managed by the OPC group object. This
can be extended as required.
To simplify the handling of the items, the client can assign a ClientHandle for each
item. In this example, this Clienthandle corresponds to a number higher than 0. A
Clienthandle should be unique so that the corresponding ItemID can later be found
faster.
As the response to adding items to the group object, the client receives an array of
Serverhandles. Each ServerHandle is unique and corresponds to exactly one OPC
item object. These ServerHandles are required to read, write, and release the
variables again.
General
On principle, every method used in the OPC functionality should include error
handling to prevent program crashes.
A jump marker should be set at the beginning of every method to which a jump is
made in the program if an error occurs.
Private submethod_XYZ()
....
Exit Sub
ErrorHandler:
MsgBox("Error!" & Err.Description)
End Sub
An error object that is created and written when an error occurs contains not only a
description of the error but also the error code.
General
The Form_Load event is run through at the start of the dialog. This specifies the
basic status of the dialog. This includes the following:
The following call creates a new OPC Server object and assigns it to the
"MyOPCServer" variable.
The GetOPCServer() OPC function of the OPC Server object returns all local OPC
Servers. These are entered and displayed in the combo box.
Since only the "Connect" button should be active at the start of the dialog, the other
buttons and text boxes must be deactivated. This is achieved by setting the
"Enabled" property of each control to "False".
Button_AddGroup.Enabled = False
General
If the dialog is closed, the Form_Unload method is run through. This contains
function calls and releases the existing connections and objects. These include:
At the end of the Form_Unload() method, the "MyOPCServer" OPC Server object
is also deleted.
General
Implementation
The connection between the "MyOPCServer" OPC Server object and the OPC
Server selected in the combo box is established in this method. Following a check
to query whether an OPC Server was selected in the combo box, the "Connect"
method of the OPC Server object is called.
Call MyOPCServer.Connect(ServerName)
The "ServerName" global variable is used as the parameter. This variable was
previously transferred to the selected server.
ServerName = ctrlOPCServerList.Text
Button_AddGroup.Enabled = True
Button_Disconnect.Enabled = True
General
Implementation
To close the connection between the OPC Server and the OPC server object
"MyOPCServer", the OPC "Disconnect" method must be called by the OPC server
object.
Call MyOPCServer.Disconnect
In this method, the "Disconnect" and "AddGroup" buttons are also deactivated and
the "Connect" button activated to allow a new connection to an OPC Server to be
established.
General
Implementation
GroupName = ctrlGroup.Text
If (GroupName = "") Then GroupName = "Group1"
The group name is then transferred as a parameter of the "AddGroup" OPC object
to the "MyOPCServer" OPC server object. The return object of the function is an
OPC group object that is assigned to the global variable "MyOPCGroup".
General
Implementation
The "Remove" OPC method removes a group object from the "MyOPCServer"
OPC server object. The name of the group to be removed is transferred as a
parameter.
Since the OPC Server object can manage several group objects, all the groups are
stored in a list (collection). For this reason, the "Remove" function is also executed
via this list.
Call MyOPCServer.OPCGroups.Remove(GroupName)
If no errors occurred when removing the group, the local group object will now be
deleted.
General
Implementation
To be able to read or write items, these must first be assigned to a group object.
This is achieved using the "AddItems" OPC function of the OPC group object.
Apart from the number of items, the corresponding ItemIDs and Clienthandles
must also be transferred to the function. The return values are two arrays. One
with handles generated by the server and the other an array of error codes. The
Serverhandles are required to be able to write and read items.
If an item could not be added to the group or if an incorrect ItemID was transferred,
the error is returned in the error code array. In this example, however, it is
assumed that all ItemIDs were specified correctly. The ItemIDs are entered in the
two text boxes labeled "ItemID1" and "ItemID2". These are read and checked
before the OPC "AddItems" call.
ItemIds(1) = ctrlItem1.Text
ItemIds(2) = ctrlItem2.Text
Following this, the required Clienthandles are generated. The Clienthandles are
useful to more complex clients in the handling of the items to be managed, but are
of no significance in this example.
ReDim Clienthandles(2)
Clienthandles(1) = 1
Clienthandles(2) = 2
If the preparation was successful, the items can now be added to the group object.
Since the "Error" array is not evaluated in this example, the memory created for it
can be released immediately again.
If no errors occurred when adding the items, the "RemoveItems", "SyncRead", and
"SyncWrite" buttons are activated.
Note
The "Error" array should always be evaluated with group calls and the client should
only release the memory created by the server afterwards.
General
Implementation
In this example, the "RemoveItems" button is used to delete the items from the
"MyOPCGroup" group object. Only the Serverhandles are required to do this.
If an invalid Serverhandle is transferred, the "Errors" array contains the error code.
Erase ItemIds()
Erase Clienthandles()
Erase Serverhandles()
To allow other items to be read and written, the "AddItems" button is activated. The
"RemoveGroup" button is also activated to allow the group to be removed.
To prevent read and write calls be executed as long as null items have been added
to the group, the "SyncRead" and "SyncWrite" buttons are also deactivated.
General
Implementation
A synchronous read can be executed with the "SyncRead" button. To do this, not
only the array of Serverhandles of the items to be read is required, but also the
data source. With OPC data access, you can choose between "OPCCache" and
"OPCDevice".
OPCCache
This corresponds to a data buffer on the OPC Server. This data is updated with
each synchronous read and write job.
OPCDevice
To obtain the most up-to-date data, "OPCDevice" can be specified as the data
source. Here, the values are read directly from the controller. The actual call is
made using the "SyncRead" OPC function of the "MyOPCGroup" OPC group
object.
The values that are read are written to the transferred "Values" array. If a value
could not be obtained, the "Errors" contains the error code. Possible errors could
be, for example, a bad serverhandle or invalid access rights.
If the values could be obtained, they are entered in the corresponding text boxes.
ctrlItemValue1.Text = Values(1)
ctrlItemValue2.Text = Values(2)
At the end of this method, the memory created by the server is released.
Erase Values()
Erase Errors()
General
Implementation
If the individual elements of the array do not contain a value, 0 is used as the
default.
Using the stored Serverhandles and the "WriteValues" array, we can now call the
"SyncWrite" OPC method of the OPC group object.
The "Errors" array contains an error codes if an item with a transfer value could not
be set or if an invalid serverhandle was used.
Erase Errors()
Erase WriteValues()