Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Demo: Excel as
OPC-Client
PC based Automation
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
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.
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.
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.
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.
In the now activated toolbar the development environment for Visual Basic for
Applications can be started with the symbol.
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)
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).
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:
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
In order to read values from the OPC server, the following objects must be created:
Object OPCServer for managing the connection.
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.
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
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
MyOPCServer.Connect ServerName
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..
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.