Sei sulla pagina 1di 14

Demo: Excel as OPC-Client

Demo: Excel as
OPC-Client

Topic: Demo:Demo: Excel as OPC-Client


Author: Ralf Eden
Issued: 09.01.02
File name: 350014551.doc

PC based Automation

350014551.doc 09.01.02 Page: 1 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

Contents
1 How to operate Excel as OPC Client......................................................................3
2 Setup, Software Requirements...............................................................................3
3 Start-up and Operation............................................................................................3
3.1 Starting Excel OPC-Client....................................................................................3
3.2 Operating the Excel User Interface......................................................................4
3.3 Settings in Visual Basic for Applications (VBA)...................................................5
3.4 Sample Configuration...........................................................................................6
3.4.1 Developing the Excel User Interface.......................................................................6

350014551.doc 09.01.02 Page: 2 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

1 How to operate Excel as OPC Client


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

2 Setup, Software Requirements


Table 1: Components
No Component
1 PC with Pentium II or higher with 128 MB, WinNT 4.0 SP6 or Win2000
2 WinAC Basis V3.0 SP1
3 SIMATIC Computing V3.0 SP1 ( contains WinAC-OPC-Server)
4 Microsoft Excel (Office 97)

3 Start-up and Operation


The following sections describe starting Excel-OPC-Client as well as operating the
Excel user interface during runtime of the example.

3.1 Starting Excel OPC-Client


Excel OPC-Client is started by double-clicking on the Excel file
ExcelClientDemoCD.xls (Figure 1). This file is located in the directory:
\PCbADemoOPC\Programme\Microsoft_Excel\Source\
For starting the application it must be ensured, that the development system is not in
design mode. Pressing the Connect button creates a connection with the OPC server.
All further functions of the user interface are described in the chapter below.

350014551.doc 09.01.02 Page: 3 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

Figure 1: Excel OPC-Client in Runtime mode

3.2 Operating the Excel User Interface


The Excel table contains the following functions and elements:
The column Item contains the variable names of the items.

In the column ItemID the corresponding item-IDs of the variables (absolute addresses)
are entered by the program for identifying the variables in the OPC server address space.

In the column Value the current values are entered.

The column Quality contains the state of the read or written variable values.

The column Time Stamp contains the time stamp at which time the variable value was
read or written by the OPC server.

For writing values set-point values can be entered in the column Writing Values.

Describing the OPC functions and buttons in the Excel example:


The connection with the OPC server is created with the Connect button.

The Read button is used for reading the current values of Temperature, Niveau [Level],
Temperature_Out, Temperature_Max, Niveau_1 und Niveau_2 synchronously from the
OPC server.

350014551.doc 09.01.02 Page: 4 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

The values for Temperature, Niveau, Temperature_Out, Temperature_Max, Niveau_1 and


Niveau_2 are read asynchronously from the OPC server by pressing Connect and are
automatically updated in the column Value (OnDataChange).

The Write button is used for writing the set-point values (Temperature_Out,
Temperature_Max, Niveau_1 und Niveau_2), which were entered in the cells of Writing
Values, synchronous to the OPC server. The set-point values are automatically updated
in the Value cells according to the given set-point values.

The Disconnect button disconnects the connection to the OPC server.

3.3 Settings in Visual Basic for Applications (VBA)


A macro is required for reading out data from an OPC server with Excel. After starting
Excel the macro can be created with Visual Basic for Applications (VBA). To do this,
the following settings are necessary:
Activate the Visual Basic toolbar in Excel with Ansicht [View] > Symbolleisten [Toolbars] >
Visual Basic.

In the now activated toolbar the development environment for Visual Basic for
Applications can be started with the symbol.

Settings in Visual Basic:


To be able to use OPC in Visual Basic, the OPC interface must be introduced in
Visual Basic. To do this the following settings are necessary:
The dialog Verweise [References] is accessed via the menu Extras > Verweise. There
you select the entry Siemens OPCDAAutomation 2.0 (Figure 2).

Figure 2: Verweise [References] - DemoCD Siemens OPC DAAutomation 2.0

350014551.doc 09.01.02 Page: 5 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

Objektkatalog [Object catalog] is opened with Ansicht [View] > Objektkatalog. Here
information is available on the object functions of the OPC user interface. In the upper
left dropdown box the library OPCSiemensDAAutomation can be selected and then all
objects of the OPC interface are displayed. (Figure 3)

Figure 3: View of OPC objects in the object catalog

3.4 Sample Configuration


The integration of control elements and the integration of OPC functionality is
described below by means of a selected configuration step.

3.4.1 Developing the Excel User Interface


Before the OPC functionality can be used, the user interface must first be created. It
can be integrated directly into an Excel table. Figure 4 shows the complete Excel user
interface of the Excel OPC Client sample project.

350014551.doc 09.01.02 Page: 6 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

Figure 4: Complete Excel user interface of the sample project

The required cells are cells for displaying the server name, cells for Item Ids (absolute
addresses of the variables), which are to be read or written, cells for output of the
read values (Value), cells for the quality state of the item values (Quality), the Time
Stamp and cells for value input (Writing Values).
Buttons must be inserted into the user interface in order to be able to trigger actions.
A step by step description of integrating the Connect button and the subsequent OPC
functionality is given below.
Activate the display of the toolbox control elements in the Excel menu Ansicht [View] >
Symbolleisten [Toolbar] > Steuerelement [Control element] -Toolbox.

A control element (in this case a button) is inserted by selecting it in the toolbox
Steuerelemente [Control elements] and blowing it up as a rectangle in the desired table
(Figure 5).

350014551.doc 09.01.02 Page: 7 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

Figure 5: Inserting a Microsoft Excel control element


The properties of the buttons must be changed. The properties dialog (Figure 6) is
accessed by right-clicking on Steuerelement [Control element] > Eigenschaften
[Properties].

Figure 6: Properties of the buttons

350014551.doc 09.01.02 Page: 8 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

The following properties of the button (CommandButton1) must be changed:


(Name): cmdConnect
Caption: Connect
Enable: True
(Name): This name operates a button in the macro.
Caption: This is the labelling of the button.
Enable: This property defines whether the button triggers a function when
starting Excel. At the beginning this only makes sense with the Connect button.
The macro can now be created.
In order to avoid errors in the interaction between Excel and VisualVasic, the active
VB code (With Callbacks or With Events) should not be directly embedded in the
tables, but be located in a class module. A class module is created as follows:
Select your project in ProjectExplorer, and create a class module in Einfgen [Insert] >
Klassenmodul [Class module].

Figure 7: Creating a class module


In the first row of the displayed properties window (Figure 7), the name of the class
module can be adapted. In this case: ExcelOPCClass

The larger part of the code is then created in the class module, the button Events
however are displayed in Sheet1. The Events buttons call functions in the class
module which helps controlling the program procedure.
A function body for a button is created as follows:

350014551.doc 09.01.02 Page: 9 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client
A function is created by double-clicking on the button. This provides automatic access to
the Visual Basic window (Figure 8), where the function body is displayed. This function is
called when pressing the button.

Figure 8: Function body Connect button

After creating the function body the following program rows must be supplemented in
the program head (Figure 9). An object of the class object type is defined in the
program head. After initialisation, this object can then be used for accessing the
functions of the class module. A global string variable is created for the server name.
The following recommendations should also be observed:
Inserting Option Base 1, as the OPC arrays all start with 1

Further recommendation: inserting Option Explicit, in order to avoid creating new


variables due to input errors and thus using wrong variables.

Figure 9: Program head Sheet1

Now the OPC functionality can be supplemented in the class module.

350014551.doc 09.01.02 Page: 10 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

Figure 10: Program head class module ExcelOPCClass

In order to read values from the OPC server, the following objects must be created:
Object OPCServer for managing the connection.

Object OPCGroup for acknowledging the items.

Before creating these objects, variables must be created for these objects.
Private MyOPCServer As OPCServer for the OPCServer object
Private With Events MyOPCGroup As OPCGroup for the OPCGroup object

The keyword With Events is necessary for receiving feedback (e.g. changes of Item
values) from group objects. Furthermore, Option Base 1 and Option Explicit should be
inserted again (Figure 10).
The function body is created with Public Sub Connect(ServerName) and
acknowledging with Enter. The function must be public for allowing outside access
(Sheet1). The string variable ServerName is a transfer parameter, which is transferred
when calling the function.
To avoid a program crash with possible errors in the program code or other errors, an
internal error handling should be written within each function body. At the beginning of
the code a jump label (On Error GoTo ErrorHandler) must be set to which the program
code jumps in case of a fault.

This jump label is marked at the end of the code by the jump label: (ErrorHandler:). 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.

350014551.doc 09.01.02 Page: 11 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client
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
11).

Figure 11: Method Connect in ExcelOPCClass with Error-Handling

After defining the OPC objects in the class module, the method cmdConnect_Click()
can be implemented in Sheet1.
First an entity of the class module must be created. This OPCModule object then
provides access to the OPC functionality.
Creating class objects:
Set OPCModule = New ExcelOPCClass

The server name is assigned to the variable SName.


SName = OPCServer.WinAC

Furthermore, the function Connect is called in the class module. Thereby the server name
is transferred.
OPCModule.Connect SName

To avoid wrong operation the button operation options are set. After acknowledging the
buttons the Connect button is deactivated.
cmdConnect.Enabled = False

350014551.doc 09.01.02 Page: 12 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

Figure 12: Implementing the function CmdConnect_Click()

Establishing server connection:


The function Connect (ServerName as String) of the class module (Figure 13) creates
the server object and the connection to the server ba menas of the following
commands:
Crate new Server Object:

Set MyOPCServer = New OPCServer

Connect with server:

MyOPCServer.Connect ServerName

Establishing Remote Server Connection:


For establishing a remote connection of an Excel-OPC-Client beyond the computer
limitations (DCOM required) an additional parameter for computer address (e.g. IP-
address or computer name) on which the OPC server is located must be specified
when pressing Connect. The remote connection is distinguished from the local
connection of an OPC server by the following program commands.

350014551.doc 09.01.02 Page: 13 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.
Demo: Excel as OPC-Client

Creating a variable for the computer name

Private NodeName as String

Calling Connect for a remote connection to the OPC server

MyOPCServer.Connect ServerName NodeName

Caution: for accessing an Excel 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..

Figure 13: Implementing the function Connect in the class module

This completes the sample configuration of the Connect button. All further functions of
the Excel-OPC-Client must be supplemented according to the source code of the
sample project.

350014551.doc 09.01.02 Page: 14 / 14


Copyright (C) Siemens AG 2001 All Rights Reserved.

Potrebbero piacerti anche