Вы находитесь на странице: 1из 13

Demo: Visual Basic OPC Client

Demo: Visual Basic


OPC Client

Topic:

Demo: Visual Basic OPC Client

Author:

Ralf Eden

Issued:

09.01.02

File name:

326990659.doc

PC based Automation
326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 1 / 13

Demo: Visual Basic OPC Client

Contents
1

How to operate Visual Basic OPC Client................................................................3

Setup, Software Requirements...............................................................................3

3 Function Principle and Operation...........................................................................3


3.1 Starting VB OPC-Client........................................................................................3
3.1.1 Operating the Visual Basic User Interface...............................................................4
3.2 Settings in Visual Basic (VB)................................................................................5
3.3 Sample Configuration...........................................................................................6
3.3.1 Developing the Visual Basic User Interface............................................................6
3.3.2 Implementing a Program for OPC Functionality.....................................................8

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 2 / 13

Demo: Visual Basic OPC Client

1 How to operate Visual Basic OPC Client


This document describes operating and configuring the Visual Basic OPC Client.
The aim of the exercise described in this document is to start and program a
selected configuration step of the Visual Basic OPC Client for visualising the mixing
process.

2 Setup, Software Requirements


Table 1: Components
N

Component

PC with Pentium II or higher with 128 MB, WinNT 4.0 or Win2000

WinAC Basis V3.0 SP1

SIMATIC Computing V3.0 SP1 ( contains WinAC-OPC-Server)

Visual Basic in Microsoft Visual Studio 6.0

3 Function Principle and Operation


The following sections describe starting VB OPC Client as well as operating the
Visual Basic user interface during runtime of the example.

3.1

Starting VB OPC-Client

The VB OPC-Client is started by double-clicking on the Excel file VBDemo.exe. The


program file is filed in the directory:
\PCbADemoOPC\Programme\Visual_Basic\Source\
Pressing Connect OPC Server establishes a connection to the OPC server. All
further functions of the user interface (Figure 1) are described in more detail in the
next chapter.

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 3 / 13

Demo: Visual Basic OPC Client

Figure 1: Visual Basic OPC-Client user interface

3.1.1 Operating the Visual Basic User Interface


The user interface of the Visual Basic OPC Client contains the following functions
and elements:

The program enters the server name of the WinAC OPC server in the output field
Connect to OPC Server. An input via this edit box is not possible.

The current values for Temperature and Niveau [Level] are automatically updated in the
output boxes Niveau and Temperature. An input of values via the user interface is not
possible.

The current binary states for the Mixer, Cooler, Heater, Inlet_Valve_1, Inlet_Valve_2 and
Outlet_Valve are illustrated in check boxes. For the binary state 1, the corresponding
CheckBox appears as follows:
. It is not possible for the user to activate the
check boxes.

A set-point value for Temperature_Max, Temperature_Out, Niveau_1 and Niveau_2 can


be defined via the Visual Basic user interface.

The Start Process button starts the process and Stop Process button ends the process.

The Schlieen [Close]

button closes the user interface of the Visual Basic OPC-

Client as well as the connection to the OPC server.

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 4 / 13

Demo: Visual Basic OPC Client

Describing the OPC functions and buttons in the VB example:

Operating the Connect OPC Server button creates the connection to the OPC server.

Operating the Write Niveau Settings button writes the defined set-point values of
Niveau_1 and Niveau_2 to the OPC server.

Write Temperature Settings writes the defined set-point values for Temperature_Out and
Temperature_Max to the OPC server.

Automatically updating (asynchronous reading) the analogue values Niveau and


Temperature as well as the binary values of Mixer, Heater, Cooler, Inlet_Valve_1,
Inlet_Valve_2 and Outlet_Valve ber OPC.

Synchronous writing of the Niveau [Level] settings (Niveau_1, Niveau_2) as well as


Temperature settings (Temperature_Max, Temperature_Out) to the OPC server.

3.2

Settings in Visual Basic (VB)

Introducing OPC Automation Interface:

Figure 2: References using Project1 OPC Automation user interface

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 5 / 13

Demo: Visual Basic OPC Client

For using OPC in VB, the correct Dynamic Link Library (Dll) must first be defined.
The menu Project > References opens a dialog box in which all registered COM
servers are listed. In this dialog (Figure 2) Siemens OPC DAAutomation 2.0 must be
selected and acknowledged with OK.
The integration of control elements and the integration of OPC functionality is
described below by means of a selected configuration step.

3.3

Sample Configuration

The integration of control elements and the integration of OPC functionality is


described below by means of a selected configuration step.

3.3.1 Developing the Visual Basic User Interface


Before the OPC functionality can be used, the user interface must first be created in
Visual Basic. Figure 3 shows the complete Visual Basic user interface of the Visual
Basic OPC Client sample project.

Figure 3: Visual Basic User Interface


Output boxes (TextBox) are required for displaying server name, current Niveau and
Temperature values, as well as the Niveau and Temperature set-point values. For
displaying binary states of the mixer for example, the check boxes are inserted into
the user interface.

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 6 / 13

Demo: Visual Basic OPC Client

To be able to trigger actions, buttons (CommandButton) must be inserted into the


user interface. The Start Process and Stop Process buttons start and stop the
process (sequencer). The Write Niveau Settings and Write Temperature Settings
buttons write the set-point values synchronous to the OPC server.
An example of inserting the Connect OPC Server button and well as the subsequent
OPC functionality are described step-by-step as follows.

First a new project must be created in Visual Basic via the menu File > New Project and
selecting Standard Exe. A new Visual Basic Form opens. The project name can be
changed in VBDemo, the name of the form in FormProcess and the caption of the form
in SIEMENS PC based Automation Visual Basic OPC Client. The form will look as
follows (Figure 4).

Figure 4: VBDemo FormProcess

A Command button must now be inserted in this Form via the control elements ToolBox
(menu View > Toolbox) by means of drag&drop.

Figure 5: Connect OPC Server user interface

The following properties must be changed in the dialog Properties-cmdConnect (Figure


6) of the Command button:

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 7 / 13

Demo: Visual Basic OPC Client

Name:

cmdConnect

Caption:

Connect OPC Server

Font:

Arial bold 10

Enable:

TRUE

Brief description of the button properties:


(Name):

This name calls a button in Visual Basic Code.

Caption:

This is the labelling of the button.

Font

Letter size of button

Enable:
This property defines whether the button can trigger a function when
starting Visual Basic Runtime. At the beginning this only makes sense with the
Connect OPC Server button, as this button is used for creating a connection to the
OPC server.

Figure 6: Properties Command Button


3.3.2 Implementing a Program for OPC Functionality

After creating the user interface (button) the code view of Visual Basic must be
changed. There the OPC functionality must now be implemented with Visual Basic.

To connect to an OPC server, a server object must be created. To do this a variable


must first be created with the following command:
Private MyOPCServer As OPCServer
Furthermore, the following recommendations should be observed:

Inserting Option Base 1, as the OPC arrays all start with 1

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 8 / 13

Demo: Visual Basic OPC Client

Further recommendations:

Inserting the Option Explicit, to avoid creating new variables due to input errors, and
thus using wrong variables. Variables must be created with Dim. In addition to that, two
further variables for server name and boolean variables must be created for the
connection status.

Figure 7 shows the respective inserted commands.

Figure 7: Creating variables

The event Form_Load is executed when starting the VB program. The function
Form_Unload at the end of the program. In Form_Load the variables should be
initialised and in Form_Unload the existing connections to the OPC Server be released.
Creating and deleting of objects should always be performed together to ensure
releasing the objects to an OPC server.

A Connect function must be created for the connection to the OPC server, and a
Disconnect function for the release.

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 9 / 13

Demo: Visual Basic OPC Client

Figure 8: Creating functions and Form_Load / Form_Unload

To avoid a program crash at possible program code errors or other errors, a


separate error handling should be written within each function body. A jump label
(On Error GoTo Sprungmarke[Jump Label]) is set at the beginning of the code to
which the program jumps in case of an error.
This jump label is marked at the end of the code by the jump label: (in this case
Error_Connect or Error_Disconnect). In order to avoid passing this jump point at the
end of the function procedure, the function must be exited before the jump point by
means of the Exit Sub command
In case of an error, any specified sequence of commands follows after the jump. In
this case the user receives an error indication by means of a message box (Figure
9).

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 10 / 13

Demo: Visual Basic OPC Client

Figure 9: Error handling in functions

Now the functions cmdConnect_Click() and Form_Unload must be implemented. The


actual function for connecting the OPC server (Connect) must be called in the function
cmdConnect_Click(), which is called after pressing Connect OPC Server. The function
for releasing the OPC server (Disconnect) is called in the function Form_Unload (Figure
10).

Figure 10: Calling the functions Connect and Disconnect

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 11 / 13

Demo: Visual Basic OPC Client

A server object must be created in the function Connect by means of Set MyOPCServer
= New OPCServer. Calling MyOPCServer.Connect(ServerName) calls the OPC function
Connect of the OPC Data Access Automation user interface for connecting the OPC
server.

In

the

Disconnect

function

the

OPC

server

must

be

released

MyOPCServer.Disconnect and the server object must be released with


MyOPCServer=Nothing.

with
Set

Figure 11: Implementing OPC functions


This completes the sample configuration of the Connect OPC Server button. All
further functions (AddGroup, AddItems etc.) of the Visual Basic-OPC-Client must be
supplemented according to the source code of the sample project.
Establishing a remote server connection:
For establishing a remote connection of an OPC Client beyond the computer
boundaries (DCOM required) an additional parameter for the computer address
(computer name) on which the OPC server is located must be specified when calling
Connect. The remote connection differs from the local connection of an OPC server
in the following program commands.

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 12 / 13

Demo: Visual Basic OPC Client

Creating a variable for the computer name

Private NodeName as String

Calling Connect for the remote connection to the OPC server

MyOPCServer.Connect ServerName NodeName


Caution: for accessing an OPC Client on the WinAC OPC Server beyond computer
boundaries (DCOM) the DCOM settings, described in the WinAC manual, must
absolutely be observed and adjusted as required.

326990659.doc
Copyright (C) Siemens AG 2001 All Rights Reserved.

09.01.02

Page: 13 / 13

Вам также может понравиться