Sei sulla pagina 1di 39

SIMATIC NET Contents

Quick Start CD: Introduction 1


Description of the OPC Client for
Put/Get Requirements 2

Manual Preparing to Use OPC 3


Functionality in Visual
Basic

Working with the OPC 4


Client

Description of How the 5


Individual Control
Elements Work

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

Quick Start CD:


Description of the OPC Client for Put/Get
2
Introduction

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.

Quick Start CD:


Description of the OPC Client for Put/Get
3
Introduction

1.1 Development Environment

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.

Quick Start CD:


Description of the OPC Client for Put/Get
4
Requirements

Requirements
2
Note
This chapter lists the hardware and software required to run the sample.

Quick Start CD:


Description of the OPC Client for Put/Get
5
Requirements

2.1 Hardware Requirements

Overview

Below, you will find a list of the hardware components required to access S7
control variables.

Desktop PC (PIII, at least 400 MHz)

Network adapter (Ethernet or PROFIBUS)

Network cable (crossover cable for Ethernet, PROFIBUS cable)

S7 controller (S7-300 or S7-400)

Schematic of the System

After connecting the components, you have a system like the one shown below:

PC/ PG station with network card S7- 400 or S7 300


(OPC client and OPC server) (communications
processor)

network

Figure 2-1 Layout of the System

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.

Quick Start CD:


Description of the OPC Client for Put/Get
6
Requirements

2.2 Software Requirements

Overview

The following software must be installed on the PC station:

Windows NT 4.0 6a, Windows 2000 SP2 or Windows XP SP1

SIMATIC NET PC software V6.0.5 (for NT and 2000) or V6.1 (for Windows XP)

STEP 7 V5.2 (to program the S7 controller, for online monitoring)

Visual Studio 6 with Visual Basic (to create the OPC client)

Quick Start CD:


Description of the OPC Client for Put/Get
7
Preparing to Use OPC Functionality in Visual Basic

Preparing to Use OPC Functionality in


Visual Basic 3

Note
This chapter describes the preparations necessary to use OPC with Visual Basic.

Quick Start CD:


Description of the OPC Client for Put/Get
8
Preparing to Use OPC Functionality in Visual Basic

3.1 Automation Interface

Referencing the Library

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.

Figure 3-1 Referencing the Automation Interface

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.

Quick Start CD:


Description of the OPC Client for Put/Get
9
Preparing to Use OPC Functionality in Visual Basic

Figure 3-2 Object Browser

Quick Start CD:


Description of the OPC Client for Put/Get
10
Working with the OPC Client

Working with the OPC Client


4
Note
This chapter describes the user interface and working with the OPC client.

Quick Start CD:


Description of the OPC Client for Put/Get
11
Working with the OPC Client

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.

Figure 4-1 OPC Client

Quick Start CD:


Description of the OPC Client for Put/Get
12
Working with the OPC Client

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".

Quick Start CD:


Description of the OPC Client for Put/Get
13
Working with the OPC Client

4.2 Selecting an OPC Server and Establishing a Connection

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.

Figure 4-2 Connecting to the OPC Server

Quitting Monitoring

To stop operator control and monitoring, exit the OPC sample client using the
"Close" button. The OPC Server terminates automatically.

Quick Start CD:


Description of the OPC Client for Put/Get
14
Working with the OPC Client

4.3 Creating and Removing an OPC Group Object

Creating a Group

If a connection could be established, the "Disconnect" and "AddGroup" buttons are


activated.

The "AddGroup" button creates a group object with the name from the text box
above ("Group1"). You can select any name for this group.

Figure 4-3 Creating a Group

Removing a Group

The "RemoveGroup" button deletes the group you have created.

Quick Start CD:


Description of the OPC Client for Put/Get
15
Working with the OPC Client

4.4 Adding Items to a Group or Removing Them

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.

Figure 4-4 Adding OPC Items

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.

Quick Start CD:


Description of the OPC Client for Put/Get
16
Working with the OPC Client

4.5 Reading Values

Reading Values of the Variables

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.

Figure 4-5 Reading Variables

Quick Start CD:


Description of the OPC Client for Put/Get
17
Working with the OPC Client

4.6 Writing Values

Writing Values of the Variables

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.

Figure 4-6 Writing Values of the 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.

Quick Start CD:


Description of the OPC Client for Put/Get
18
Working with the OPC Client

4.7 Closing the Client

Close

Remember the following points before closing the OPC client:

Delete the items from the group object ("RemoveItems" button)

Remove the group ("RemoveGroup" button)

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.

Quick Start CD:


Description of the OPC Client for Put/Get
19
Description of How the Individual Control Elements Work

Description of How the Individual Control


Elements Work 5

Note
This chapter describes every step and the corresponding Visual Basic source
code.

Quick Start CD:


Description of the OPC Client for Put/Get
20
Description of How the Individual Control Elements Work

5.1 General Stipulations

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.

Quick Start CD:


Description of the OPC Client for Put/Get
21
Description of How the Individual Control Elements Work

5.2 Global Variables

General

No global variables are used.

Quick Start CD:


Description of the OPC Client for Put/Get
22
Description of How the Individual Control Elements Work

5.2.1 OPC Variables

Private Variables

Private MyOPCServer As OPCServer

The MyOPCServer variable is of the type OPCServer and therefore corresponds to


the OPC server object. It manages the connection and contains all created group
objects.

OPC server object

MyOPCServer

OPC group object OPC group object

Figure 5-1 OPC Server Object

Private MyOPCGroup As OPCGroup

MyOPCGroup corresponds to a group object with the name MyOPCServer. All the
OPC item management tasks are handled using this OPC object.

Quick Start CD:


Description of the OPC Client for Put/Get
23
Description of How the Individual Control Elements Work

OPC server object

MyOPCServer

OPC group object

add items MyOPCGroup

remove items

read synchronous

write synchronous

Figure 5-2 OPC Group Object

Quick Start CD:


Description of the OPC Client for Put/Get
24
Description of How the Individual Control Elements Work

5.2.2 Variables Independent of OPC

Private Variables

Private ServerName As String

The ServerName variable contains the name of the selected OPC Server.

Private GroupName As String

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.

Private ItemCount As Long

The number of OPC items to be managed is stored in the "ItemCount" variable.

Private ItemIds() As String

The "ItemIDs" array contains all ItemIDs managed by the OPC group object. This
can be extended as required.

Private Clienthandles() As Long

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.

Private Serverhandles() As Long

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.

Quick Start CD:


Description of the OPC Client for Put/Get
25
Description of How the Individual Control Elements Work

5.3 Basic Structure of a Method

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()

On Error GoTo ErrorHandler

....
Exit Sub
ErrorHandler:
MsgBox("Error!" & Err.Description)
End Sub

In the ErrorHandler, a message box with an error description is displayed.

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.

Quick Start CD:


Description of the OPC Client for Put/Get
26
Description of How the Individual Control Elements Work

5.4 Method: Form_Load()

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:

Create an OPC Server object

The following call creates a new OPC Server object and assigns it to the
"MyOPCServer" variable.

Set MyOPCServer = New OPCServer

An instance of the OPC Server is created.

Figure 5-3 COM Object

Fill in the Combo Box with the Local OPC Servers

The GetOPCServer() OPC function of the OPC Server object returns all local OPC
Servers. These are entered and displayed in the combo box.

Deactivate Buttons and Text Boxes.

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

Quick Start CD:


Description of the OPC Client for Put/Get
27
Description of How the Individual Control Elements Work

5.5 Method: Form_Unload()

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:

Remove OPC item object from the group object

If the "RemoveItems" button is still active, it is assumed that the "MyOPCGroup"


group object still contains and manages OPC items. If this is the case, the
"Button_RemoveItems_Click()" method is called to remove the items from the
group object.

Remove group object

If the "RemoveGroup" button is still active, the Button_RemoveGroup_Click()


method is called to remove and delete the "MyOPCGroup" object from the
"MyOPCServer" OPC server object.

Disconnect from the OPC Server

If the "Disconnect" button is still active, the Button_Disconnect_Click() method is


called to disconnect the "MyOPCServer" object from the connected OPC Server.

Delete OPC Server object

At the end of the Form_Unload() method, the "MyOPCServer" OPC Server object
is also deleted.

Quick Start CD:


Description of the OPC Client for Put/Get
28
Description of How the Individual Control Elements Work

5.6 Method: Button_Connect_Click()

General

This function is always executed when the "Connect" button is clicked.

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

If a connection was established successfully, the "AddGroup" button for creating a


group object and the "Disconnect" button for closing the connection are activated.

Button_AddGroup.Enabled = True
Button_Disconnect.Enabled = True

Quick Start CD:


Description of the OPC Client for Put/Get
29
Description of How the Individual Control Elements Work

5.7 Method: Button_Disconnect_Click()

General

This function is always executed when the "Disconnect" button is clicked.

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.

Quick Start CD:


Description of the OPC Client for Put/Get
30
Description of How the Individual Control Elements Work

5.8 Method: Button_AddGroup_Click()

General

This function is always executed when the "AddGroup" button is clicked.

Implementation

If there is a connection to an OPC Server, an OPC group object is created in this


step. To do this, a group name is required that, in this case, can be taken from the
text box above. If the text box does not contain a name, a default value is used.

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".

Set MyOPCGroup = MyOPCServer.OPCGroups.Add(GroupName)

If a new group object was created successfully, the "AddItems" and


"RemoveGroup" buttons are activated. Since in this example, we only want to
create one group, the "AddGroup" button will be deactivated.

Quick Start CD:


Description of the OPC Client for Put/Get
31
Description of How the Individual Control Elements Work

5.9 Method: Button_RemoveGroup_Click()

General

This function is always executed when the "RemoveGroup" button is clicked.

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.

Set MyOPCGroup = Nothing

The "AddGroup" button is then activated to allow a new group to be created an


added.

Quick Start CD:


Description of the OPC Client for Put/Get
32
Description of How the Individual Control Elements Work

5.10 Method: Button_AddItems_Click()

General

This function is always executed when the "AddItems" button is clicked.

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

If ItemIds(1) = "" Or ItemIds(2) = "" Then


Call MsgBox("ItemID missing!", vbExclamation)
Exit Sub
End If

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.

Quick Start CD:


Description of the OPC Client for Put/Get
33
Description of How the Individual Control Elements Work

Call MyOPCGroup.OPCItems.AddItems(ItemCount, ItemIds, _


Clienthandles, Serverhandles, Errors)

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.

Quick Start CD:


Description of the OPC Client for Put/Get
34
Description of How the Individual Control Elements Work

5.11 Method: Button_RemoveItems_Click()

General

This function is always executed when the "RemoveItems" button is clicked.

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.

Call MyOPCGroup.OPCItems.Remove(ItemCount, Serverhandles, _


Errors)

If an invalid Serverhandle is transferred, the "Errors" array contains the error code.

If the "Remove" function was executed successfully, the array of Serverhandles,


array of Clienthandles, and array of ItemIDs can then be deleted.

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.

Quick Start CD:


Description of the OPC Client for Put/Get
35
Description of How the Individual Control Elements Work

5.12 Method: Button_SyncRead_Click()

General

This function is always executed when the "SyncRead" button is clicked.

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.

Call MyOPCGroup.SyncRead(OPCCache, ItemCount, _


Serverhandles, Values, Errors)

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.

Quick Start CD:


Description of the OPC Client for Put/Get
36
Description of How the Individual Control Elements Work

Erase Values()
Erase Errors()

Quick Start CD:


Description of the OPC Client for Put/Get
37
Description of How the Individual Control Elements Work

5.13 Method: Button_SyncWrite_Click()

General

This function is always executed when the "SyncWrite" button is clicked.

Implementation

In this example, the "Button_SyncWrite_Click()" method contains a synchronous


write job. The values to be written are first read out of the text boxes and entered in
an array.

Dim WriteValues(2) As Variant


WriteValues(1) = ctrlWriteValue1.Text
WriteValues(2) = ctrlWriteValue2.Text

If the individual elements of the array do not contain a value, 0 is used as the
default.

If WriteValues(1) = "" Then WriteValues(1) = 0


If WriteValues(2) = "" Then WriteValues(2) = 0

Using the stored Serverhandles and the "WriteValues" array, we can now call the
"SyncWrite" OPC method of the OPC group object.

Call MyOPCGroup.SyncWrite(ItemCount, Serverhandles, _


WriteValues, Errors)

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.

Finally, the memory is released.

Quick Start CD:


Description of the OPC Client for Put/Get
38
Description of How the Individual Control Elements Work

Erase Errors()
Erase WriteValues()

Quick Start CD:


Description of the OPC Client for Put/Get
39

Potrebbero piacerti anche