Sunteți pe pagina 1din 16

OPC Examples for ProTool/Pro

OPC Server – OPC Clients (EXCEL, VB program)

Connection of a ProTool/Pro OPC server to


OPC clients (Excel/VB-EXE)

Configuration
ProTool/Pro V6.0
EXCEL 2000 or Visual Basic development environment, V.6 for VB program

Windows NT 4.0 SP6a or Windows 2000 SP2

1. Scope of delivery
File Contents Prerequisites
Dokumentation.doc This document Microsoft Word 97/2000
OPCServer.fwd ProTool/Pro OPC Server Siemens ProTool/Pro Runtime 6.0
OPC-Client.xls Excel OPC Client Microsoft Excel 2000
OPCClient.exe Visual Basic OPC Client

2. Operating system settings


 Minimum requirements: Windows NT 4.0 SP6a or Windows 2000 SP2
 ProTool/Pro must be installed on the Client computer!
 The COM/DCOM configuration is set up in the operating system. It is configured for communication
both on the server side and on the client side using 'dcomcnfg' (additional information is found in the
Appendix).
 The “Siemens OPC DLL” is required for using OPC with ProTool/Pro and OPC clients. It is
automatically installed and registered during the standard installation of ProTool/Pro. However, the
following procedure must be followed if it is not present.
Register the 'sopcdaauto.dll' library in the system. Call up the following command in Execute:
'regsvr32 sopcdaauto.dll'.

Tips:
If communication does not function, the following steps can be taken in an attempt to correct the problem:
- Unregister and re-register the library. (Unregister the library with the command: 'regsvr32 -u
sopcdaauto.dll')
- Reinstall Excel 2000 or the complete Office package (Office 2000).
- It will certainly help if as few programs as possible are installed on the computer.

13.11.2019, SW/GG Page 1 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)

3. Program description

3.1. General information


Three programs are included: the ProTool/Pro OPC server and the OPC clients, i.e. an Excel file and a
VisualBasic program.

A so-called node name has to be entered for the clients. It can be either an IP address (e.g. 192.168.0.80) or
a computer name (e.g. ‘NB1207’ or ‘NB1207.siemens.de’). It is the address or the name of the computer on
which the server is running.

3.2. ProToolPro OPC server

ProTool/Pro Runtime, Version 6.0, is required for executing the OPC server.
The OPC server is automatically started when the program is called up (OPCServer.fwd).
There are six variables (VAR_1 to VAR_6). The first four are integer values and VAR_5 and VAR_6 are texts.
The slide control represents VAR_1.
If an OPC client is connected, the variables at the client automatically change as soon as an entry in the server is
completed with the Enter key (or changed with the slide control).

13.11.2019, SW/GG Page 2 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)

3.3. EXCEL document with a connection to ProTool/Pro (EXCEL client)


Excel 2000 is required to execute the OPC client (OPC-Client.xls).
After opening the Excel document, the user is asked whether he wants to activate macros. This must be answered
affirmatively.
The necessary steps are briefly described in the EXCEL document itself. The colored cells should be checked or
modified as required. The green/red cell contains the current status of the connection.

After the node name (see also 3.1.General information) has been entered, the client is “connected” to the server
(which, of course, must have been started) by pressing the “Start Client” button. If you now change one of the
values in the lower half of the worksheet (cells C13 – C18), this value is automatically transmitted to the OPC
server. Of course, this communication also operates in the opposite direction. Before exiting the Excel document
and the server, exit the client by pressing the “Stop Client” button.

13.11.2019, SW/GG Page 3 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)

3.4. Visual Basic client


The node name (see also 3.1.General information) has to be entered after starting the program (OPC-
Client.xls). Press the “Start” button to start the client (of course, the server must be running). Now the

value of the variable can be changed; synchronization with the OPC server takes place automatically.
Before closing the client or exiting the server, the client is stopped by pressing the “Stop” button.

13.11.2019, SW/GG Page 4 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)

4.0. Programming

4.1. ProTool/Pro
Select the 'OPC-Server' option in the 'Target system' -> 'Settings'.

4.2. Excel 2000


The “programming language”, Visual Basic for Applications, is used in Excel.
A description of the Macros used is found in the Appendix, as are several notes on Error Handling.
To be able to use the OPC functions, a program library must be linked into the programming environment
(Alt-F11) under Extras -> Links. The library is named “Siemens OPC DAAutomation 2.0” and its file name
is “SOPCDaAuto.dll”.
(OPC-Client.xls)

4.3. Visual Basic 6.0


Excel Macros are largely transferable for the routines under Visual Basic. The field entries must be replaced
by variables and the graphics for the current data display are implemented using VB capabilities.
A program library must be linked under Project -> Links to be able to use the OPC functions. The library is
named “Siemens OPC DAAutomation 2.0” and its file name is “SOPCDaAuto.dll”.
The remarks on Error Handling also apply to Visual Basic.
The sources are found in the VisualBasic directory (OPCClient.vbw)

13.11.2019, SW/GG Page 5 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)

Appendix

Microsoft Excel as Client  Macros


 Sample code for macros:

Option Explicit 'Variable names must be explicitly defined

Option Base 1 'Indices begin at 1 (not 0)

Dim WithEvents MyOPCServer As OPCServer 'OPCServer object is defined

Dim MyOPCGroupColl As OPCGroups 'OPCGroups object is defined

Dim WithEvents MyOPCGroup As OPCGroup 'OPCGroup object is defined

Dim MyOPCItemColl As OPCItems 'OPCItems object is defined

Dim MyOPCItem As OPCItem 'OPCItem object is defined

Dim ClientHandles(1 To 6) As Long

Dim ServerHandles() As Long

Dim Values(1 To 6) As Variant

Dim Errors() As Long

Dim ItemIDs(1 To 6) As String

Dim ServerName As String

Dim NodeName As String

Dim GroupName As String

These are the declarations for the required variables.


The required number of variables to be synchronized must be set for the ClientHandles, Values and
ItemIDs (in this example: six variables).
OPC is hierarchically structured as follows:
OPCServer -> OPCGroups -> OPCGroup -> OPCItems -> OPCItem

13.11.2019, SW/GG Page 6 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)

Sub StartClient()

'Connect to Server, create Groups, items etc.

'On Error GoTo ErrorHandler 'should really be used!

ClientHandles(1) = 1
ClientHandles(2) = 2
ClientHandles(3) = 3
ClientHandles(4) = 4
ClientHandles(5) = 5
ClientHandles(6) = 6

ServerName = Range("A2").Value

GroupName = Range("C2").Value

NodeName = Range("B2").Value

ItemIDs(1) = Range("A6").Value
ItemIDs(2) = Range("A7").Value
ItemIDs(3) = Range("A8").Value
ItemIDs(4) = Range("A9").Value
ItemIDs(5) = Range("A10").Value
ItemIDs(6) = Range("A11").Value

'----------- Get an instance of the OPC server


Set MyOPCServer = New OPCServer

MyOPCServer.Connect ServerName, NodeName

Set MyOPCGroupColl = MyOPCServer.OPCGroups

'----------- Set the default active state for adding groups


MyOPCGroupColl.DefaultGroupIsActive = True

'----------- Add our group to the Collection


Set MyOPCGroup = MyOPCGroupColl.Add(GroupName)

Set MyOPCItemColl = MyOPCGroup.OPCItems

'----------- Add one item, ServerHandles are returned


MyOPCItemColl.AddItems 6, ItemIDs, ClientHandles, ServerHandles, Errors

'----------- A group that is subscribed receives asynchronous notifications


MyOPCGroup.IsSubscribed = True

Range("E2").Value = "Client on"


Range("F2").Value = CStr(MyOPCServer.LastUpdateTime)
Range("G2").Value = MyOPCItemColl.Count
Range("H2").Value = MyOPCServer.ServerState

Exit Sub

ErrorHandler:

MsgBox "Error: " & Err.Description, vbCritical, "ERROR"

End Sub

13.11.2019, SW/GG Page 7 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)

Sub StopClient()

'----------- Release the Group and Server objects

If (Range("H2").Value <> 1) Then Exit Sub

MyOPCGroupColl.RemoveAll

'----------- Disconnect from the server and clean up

MyOPCServer.Disconnect

Set MyOPCItemColl = Nothing

Set MyOPCGroup = Nothing

Set MyOPCGroupColl = Nothing

Set MyOPCServer = Nothing

Range("E2").Value = "Client off"


Range("H2").Value = 0

End Sub

Private Sub MyOPCGroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles()


As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)

'------------ Event is triggered when data on the server change


'only one item is queried at a time. The assumption is made that only one is changed simultaneously
If ClientHandles(1) = 1 Then Range("B6").Value = ItemValues(1)
If ClientHandles(1) = 2 Then Range("B7").Value = ItemValues(1) 'if only the second item has changed
If ClientHandles(1) = 3 Then Range("B8").Value = ItemValues(1) 'etc.
If ClientHandles(1) = 4 Then Range("B9").Value = ItemValues(1)
If ClientHandles(1) = 5 Then Range("B10").Value = ItemValues(1)
If ClientHandles(1) = 6 Then Range("B11").Value = ItemValues(1)

'If NumItems > 1 Then Range("B7").Value = ItemValues(2) 'Condition if first item changes is that the second value is
unknown

Range("F2").Value = CStr(MyOPCServer.LastUpdateTime)

End Sub

The MyOPCGroup_DataChange event is activated if ‘something’ in the 'MyOPCGroup' group has changed.
The condition that a query only takes place when a variable changes was implemented for user-related
reasons.

13.11.2019, SW/GG Page 8 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

'------------ Event is triggered if data in the worksheet change


If Range("H2").Value = 0 Then Exit Sub 'Exit if not connected
'Exit if none of the variable values has changed
If (Target <> Range("B6")) And (Target <> Range("B7")) And (Target <> Range("B8")) And (Target <>
Range("B9")) And (Target <> Range("B10")) And (Target <> Range("B11")) Then Exit Sub ' if values have not
changed -> exit

Values(1) = Range("B6").Value
Values(2) = Range("B7").Value
Values(3) = Range("B8").Value
Values(4) = Range("B9").Value
Values(5) = Range("B10").Value
Values(6) = Range("B11").Value

MyOPCGroup.SyncWrite 6, ServerHandles, Values, Errors 'Write values in synchronous mode on the server
End Sub

The Worksheet_Change event is immediately activated when there is a change in the Excel worksheet. All
variables are then transferred to the OPC server.
 While the program can be extended by a wide range of error checks, this example is sufficient to
demonstrate the basic features of the OPC.
 There are two basic methods of reading or writing the values:
synchronous and
asynchronous.
In the example, writing is synchronous and reading is asynchronous.

Error Handling (Excel & Visual Basic)


 To start with, an overall error handling procedure can generally be implemented..
To do so, an “On Error GoTo ErrorHandler” is set in each routine (Sub).
The “ErrorHandler:” jump label is inserted at the end of the routine. In most cases, the output of a message
box with the error message is sufficient.
(see example under Excel macros)

 An additional option is available if special errors need to be intercepted for separate handling. For example,
all methods of the OPCServer object return an array of “Errors”. These are values of the long type that can
then be converted to text using the GetErrorString function. This can also be realized using other objects
(OPCGroup,OPCGroup,OPCItems etc.).

 The following is a list of all errors that can occur in the OPCServer:
OPCInvalidHandle, OPCBadType, OPCPublic, OPCBadRights, OPCUnknownItemID, OPCInvalidItemID,
OPCInvalidFilter, OPCUnknownPath, OPCRange, OPCDuplicateName, OPCUnsupportedRate,
OPCClamp, OPCInuse, OPCInvalidConfig, OPCNotFound, OPCInvalidPID

 An exact description is found in the “Data Access Automation Interface Standard“ description of the OPC
Foundation (document included).

13.11.2019, SW/GG Page 9 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)

DCOM settings

A detailed description of how to set up the DCOM interface with the “dmcnfg” tool of Windows NT is contained
in the SIMATIC HMI Communication manual for windows based Systems.

The figures illustrate the settings that must be made for both partners.

Important!!
The settings of the rights should either be assigned for “everyone” (as shown in the figures), or explicitly for the
user on the local and remote computer.
This applies to users in “workgroups” as well as to users in domains.

The most important settings are summarized here.

Standard properties:

13.11.2019, SW/GG Page 10 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)
Selection of the OPC server: “OPC.SimaticHMI.PTPro”

General settings

13.11.2019, SW/GG Page 11 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)
Location settings:

User identity:

13.11.2019, SW/GG Page 12 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)
Security settings:

13.11.2019, SW/GG Page 13 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)
End points:

13.11.2019, SW/GG Page 14 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)
General standard security rights:

13.11.2019, SW/GG Page 15 of 16


OPC Examples for ProTool/Pro
OPC Server – OPC Clients (EXCEL, VB program)

DCOM security functions

The settings shown up to this point offer only a limited degree of security. Security can be tightened as follows:
 Instead of setting up the rights for “everyone”, two users can be set up on the server side and the client side.
The users must have identical names and passwords on both computers. The security settings are then made
for these two users only (on the client and on the server!)
 A domain can be used instead of a workgroup. This heightens security since the users are exactly the same.
Of course, it is then no longer necessary to set the users up locally.
 The authentification level (on the “General” page of the OPC server) can be set to “connect” or higher.
 A user group for an OPC server can be set up in a domain. This group is then integrated into the security
settings of the corresponding “dcomcnfg” settings for the OPC server. In this way, the users of different
client computers can access the server.

DCOM error handling

DCOM also features an error handling procedure. The following notes and example pertain to this procedure:

Error Handling (from the OPC DCOM White Paper by R. C. Harrison)


When OPC Client server calls are remoted, all of the RPC_E_* error codes must be considered possible returns
from method calls. The following may be done to indicate the need for a client to completely reconnect to a
server:

// Check if the facility code of the returned HRESULT is of type RPC


if (FACILITY_RPC == HRESULT_FACILITY(hr))
{
if (RPC_E_DISCONNECTED == hr)
{
// Need to reconnect (i.e. Call CoCreateInstance)
}
}

Question: What is the proper way for a client to determine that a server has failed and what should it do
when this does happen?
Background: The failure can occur at any time (i.e. during any method). If such a failure does occur the
client will need to reconnect to the server.
Resolution: For each interface method call, its return value should be checked for error as indicated above.
Note that Release() does not have to be called on all interface pointers in this case, as (a) the connection has been
lost and such calls will not go through and (b) the server object will be garbage collected eventually (six
minutes) as mentioned earlier.
Note: do not use QueryInterface() as a 'ping' mechanism because a QueryInterface() for the same IID is cached
and never goes remote, and proxies are not automatically updated of server status. Instead, use a call such as
IOPCServer->GetStatus().

13.11.2019, SW/GG Page 16 of 16

S-ar putea să vă placă și