You are on page 1of 19

OPC UA Client Library

for Microsoft Excel


Siemens
S7-1500 / OPC UA / .NET / C# / MS Excel VBA Industry
Online
https://support.industry.siemens.com/cs/ww/en/view/109748892 Support
Legal information

Legal information
Use of application examples
Application examples illustrate the solution of automation tasks through an interaction of several
components in the form of text, graphics and/or software modules. The application examples are
a free service by Siemens AG and/or a subsidiary of Siemens AG (“Siemens”). They are non-
binding and make no claim to completeness or functionality regarding configuration and
equipment. The application examples merely offer help with typical tasks; they do not constitute
customer-specific solutions. You yourself are responsible for the proper and safe operation of the
products in accordance with applicable regulations and must also check the function of the
respective application example and customize it for your system.
Siemens grants you the non-exclusive, non-sublicensable and non-transferable right to have the
application examples used by technically trained personnel. Any change to the application
examples is your responsibility. Sharing the application examples with third parties or copying the
application examples or excerpts thereof is permitted only in combination with your own products.
The application examples are not required to undergo the customary tests and quality inspections
of a chargeable product; they may have functional and performance defects as well as errors. It is
your responsibility to use them in such a manner that any malfunctions that may occur do not
result in property damage or injury to persons.

Disclaimer of liability
Siemens shall not assume any liability, for any legal reason whatsoever, including, without
limitation, liability for the usability, availability, completeness and freedom from defects of the
application examples as well as for related information, configuration and performance data and
any damage caused thereby. This shall not apply in cases of mandatory liability, for example
under the German Product Liability Act, or in cases of intent, gross negligence, or culpable loss of
life, bodily injury or damage to health, non-compliance with a guarantee, fraudulent
non-disclosure of a defect, or culpable breach of material contractual obligations. Claims for
damages arising from a breach of material contractual obligations shall however be limited to the
© Siemens AG 2017 All rights reserved

foreseeable damage typical of the type of agreement, unless liability arises from intent or gross
negligence or is based on loss of life, bodily injury or damage to health. The foregoing provisions
do not imply any change in the burden of proof to your detriment. You shall indemnify Siemens
against existing or future claims of third parties in this connection except where Siemens is
mandatorily liable.
By using the application examples, you acknowledge that Siemens cannot be held liable for any
damage beyond the liability provisions described.

Other information
Siemens reserves the right to make changes to the application examples at any time without
notice. In case of discrepancies between the suggestions in the application examples and other
Siemens publications such as catalogs, the content of the other documentation shall have
precedence.
The Siemens terms of use (https://support.industry.siemens.com) shall also apply.

Security information
Siemens provides products and solutions with industrial security functions that support the secure
operation of plants, systems, machines and networks.
In order to protect plants, systems, machines and networks against cyber threats, it is necessary
to implement – and continuously maintain – a holistic, state-of-the-art industrial security concept.
Siemens’ products and solutions constitute one element of such a concept.
Customers are responsible for preventing unauthorized access to their plants, systems, machines
and networks. Such systems, machines and components should only be connected to an
enterprise network or the Internet if and to the extent such a connection is necessary and only
when appropriate security measures (e.g. firewalls and/or network segmentation) are in place.
For additional information on industrial security measures that may be implemented, please visit
https://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them more secure.
Siemens strongly recommends that product updates are applied as soon as they are available
and that the latest product versions are used. Use of product versions that are no longer
supported, and failure to apply the latest updates may increase customer’s exposure to cyber
threats.
To stay informed about product updates, subscribe to the Siemens Industrial Security RSS Feed
at: https://www.siemens.com/industrialsecurity.

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 2
Table of Contents

Table of Contents
Legal information ......................................................................................................... 2
1 Introduction ........................................................................................................ 4
1.1 Overview............................................................................................... 4
1.2 Mode of operation ................................................................................ 4
1.3 Components used ................................................................................ 5
2 Commissioning and operation ......................................................................... 6
2.1 Configuring the OPC UA server of the S7-1500 .................................. 6
2.2 Installing the OPC UA Client Library .................................................... 6
2.3 Operation .............................................................................................. 8
2.3.1 Description of the user interface and operating the sheets .................. 8
3 Creating the OPC UA Excel Client ................................................................. 14
4 OPC UA Client Library ..................................................................................... 15
5 Worth knowing ................................................................................................. 17
5.1 Certificate handling in the example program...................................... 17
5.2 Information about the license model of the software ......................... 17
6 Appendix .......................................................................................................... 18
6.1 Service & support ............................................................................... 18
© Siemens AG 2017 All rights reserved

6.2 Links and literature ............................................................................. 19


6.3 Change documentation ...................................................................... 19

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 3
1 Introduction

1 Introduction
1.1 Overview
As of firmware V2.0 of SIMATIC S7-1500 an integrated OPC UA Server has been
added to the control system. This enables an additional option of direct process
data exchange of the SIMATIC S7-1500 with a wide variety of other systems that
support OPC UA.
In this application example, you can find the OPC UA Client Library with which you
can create your own OPC UA client applications in Excel VBA to interchange data
between Excel and the SIMATIC S7-1500.

Assumed knowledge
The following basic knowledge is required by the user:
• Basics of programming in C#/.NET and Excel VBA
• Basics of configuring in the TIA Portal
• Basics of OPC

1.2 Mode of operation


© Siemens AG 2017 All rights reserved

The "OPC UA Client Library" is a "Dynamic Link Library" (DLL) that can be
integrated into Microsoft Excel in order to program OPC UA client functions in
Excel via VBA OPC.
The library internally uses the C# class “UAClientHelperAPI”. This class
summarizes the most important functions of the underlying .NET stack of the OPC
Foundation. The OPC UA Excel Client communicates with the OPC UA server of a
SIMATIC S7-1500.
The SIMATIC S7-1500 OPC UA server is planned and configured via the TIA
Portal. Client and server are connected via Ethernet and communicate through
OPC UA via TCP/IP.
For more information, please refer to the entry \3\.

Figure 1-1

OPC UA Excel Client OPC UA Server

Industrial Ethernet
OPC UA

OPC UA Client Library

UAClientHelperAPI

OPC UA .NET Stack

Windows PC/PG SIMATIC S7-1500

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 4
1 Introduction

Following functions are supported by the "OPC UA Client Library":


• Connect to an OPC UA server
• Read and write of variables
• Read struct/UDT
• Subscription of variables
• Call of methods

NOTE Not all data types can be interpreted by the "OPC UA Client Library". Adapt the
source code if necessary.

1.3 Components used


This application example was created with the following components:
Table 1-1
Component Number Article number Note
S7-1500 1 6ES7 516-3AN01-0AB0 Firmware 2.0 or higher
CPU 1516-3 PN/DP
© Siemens AG 2017 All rights reserved

TIA Portal 1 6ES7822-1..05-.. V14 or higher


Visual Studio 2017 1 - Community version also
possible
OPC UA .Net Stack 1 - V1.03.341.0
Download: Links &
Literature in item \4\
Microsoft Office 2016 1 -

This application example consists of the following components:


Table 1-2
Components File name Note
Visual Studio 109748892_OPC_UA_ClientLibrary_ Project with open
project and Excel CODE_V2_1.zip sources, pre-compiled
example library and Excel sheet
Documentation 109748892_OPC_UA_ClientLibrary_DOC_ This document
V2_1_en.pdf

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 5
2 Commissioning and operation

2 Commissioning and operation


2.1 Configuring the OPC UA server of the S7-1500
For information on how to configure the OPC UA server of the S7-1500, please
refer to the entry \3\. The Excel sheet in this example refers to the OPC UA Server
of the above example project. Alternatively, you have the possibility to use any
other OPC UA server.

2.2 Installing the OPC UA Client Library


To use the OPC UA Client Library, you need to register the library on your system.
For this, you can use the Microsoft .NET Framework Assembly Registration Utility.
The Utility (regasm.exe) is contained in the Microsoft .NET Framework 4.7.2.
The instructions below show you the required steps:
1. Install the .NET Framework 4.7.2 (https://www.microsoft.com)
2. The path, in which the .NET Framework has been installed, is:
“C:\Windows\Microsoft.NET\Framework\v4.0.30319”.
3. Download the “109748892_OPC_UA_ClientLibrary_CODE_V2_1.zip” file.
4. Unzip this file on the same drive on which your operating system is installed. In
this example, the following was chosen:
“C:\109748892_OPC_UA_ClientLibrary_CODE_V2_1”.
© Siemens AG 2017 All rights reserved

5. Start the command prompt as administrator.

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 6
2 Commissioning and operation

6. Register the library on your system. Enter the following string into the
command line interface
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm
C:\109748892_OPC_UA_ClientLibrary_CODE_V2_1\OPC_UA_ExcelClient\
Application\Opc.Ua.ExcelClient.dll /tlb /codebase
and acknowledge with Enter.

7. After the successful registration, you can use the library in Excel.
© Siemens AG 2017 All rights reserved

NOTE The above instructions are valid for Microsoft Office 32-bit. If you have Microsoft
Office x64 installed, use the following string for "regasm":
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe <Pfad zu
Opc.Ua.ExcelClient.dll> /tlb /codebase

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 7
2 Commissioning and operation

2.3 Operation
The following step-by-step instructions show you how you can commission the
application example and how you can operate it.
The following prerequisites apply:
• The OPC UA Client Library has been registered on your system.
• An OPC UA Server is activated and connected to your PC/PG.
• The worksheet "109748892_OPC_UA_ClientLibrary_Sample_V2_1.xlsm" has
been downloaded and opened.

2.3.1 Description of the user interface and operating the sheets

The user interface of the “OPC UA Excel Client” example client is divided in four
sheets: “Connect”, “ReadWrite”, “Subscribe”, “Method” and "ReadStructUdt".

Connect
The following figure shows the “Connect” interface:
Figure 2-1
© Siemens AG 2017 All rights reserved

1 2

The following table describes the functions of the interface of the previous figure:
Table 2-1
No. Description
1. Text field to enter an OPC UA server URL.
2. Button to search OPC UA endpoints with the URL (1).
3. List of the OPC UA endpoints found.
4. Settings for user authentication.
5. Button to establish a connection to a selected endpoint of the list (3) and
disconnection.

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 8
2 Commissioning and operation

The following steps describe how to connect the client to a server:


1. Enter the server URL in the field "OPC Server" (1).
2. Click on the button "GetEndpoints" (2).
3. Select an endpoint of the server from the Endpoints list (3).
4. (Opional) Enter a user name and password in the User and Password fields (4)
if your server requires this authentication method.
5. Click on the "Connect" button (5). You will then be informed about the status of
the connection setup via a pop-up.

ReadWrite
The following figure shows the “Read/Write” interface:
Figure 2-2

1 2 3

4 5 6

9 10
© Siemens AG 2017 All rights reserved

7 8

11 12

13 14

The following table describes the functions of the interface of the previous figure:
Table 2-2
No. Description
1. Button to read an entered node ID from (2).
2. Text field to enter a node ID to be read.
3. Text field to output the value of the read node ID from (2).
4. Button to write an entered node ID from (5).
5. Text field to enter a node ID to be written.
6. Text field to enter the value of the node ID to be written from (5).
7. Button to register an entered node ID from (9).
8. Button to cancel a registration of an entered node ID from (9).
9. Text field to enter a node ID to be registered.
10. Text field to output a registered node ID.
11. Button to read a registered node ID.
12. Text field to output the value of the read, registered node ID from text field.
13. Button to write a registered node ID.
14. Text field to enter the value of the registered node ID to be written.

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 9
2 Commissioning and operation

The following steps describe how to read a variable of the server:


1. Enter the Node Id of a variable to be read in the field "Node Id" (2).
2. Click on the "Read" button (1) to read the variable. The read result can be
found in the "Read value" field (3).

The following steps describe how to write a variable from the server:
1. Enter the Node Id of a variable to be written in the field "Node Id" (5).
2. Enter the value to be written in the field "Value to write" (6).
3. Click on the "Write" button (4) to write the variable.

The following steps describe how to register a server variable and how to read and
write the registered variable:
1. Enter the Node Id of a server variable to be registered in the Node Id field (9).
2. Click on the "Register" button (7). You will get back the handle of the registered
variable in the field "Registered Node Id" (10).
3. Click on the "Read" button (11) to read the registered variable. The read result
can be found in the "Read value" field (12).
4. Enter the value to be written in the "Value to write" field (14).
5. Click on the "Write" button (13) to write the variable.
© Siemens AG 2017 All rights reserved

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 10
2 Commissioning and operation

"Subscribe"
The following figure shows the “Subscribe” interface:
Figure 2-3

1 2

4
© Siemens AG 2017 All rights reserved

The following table describes the functions of the interface of the previous figure:
Table 2-3
No. Description
1. Button to start a subscription on an OPC UA server and to create a
MonitoredItem via the node ID from text field (3).
2. Button to end a subscription and to delete the MonitoredItem.
3. Text field to enter a node ID that is added to the subscription as MonitoredItem.
4. Text field to output the value of the MonitoredItem of the subscription with time
stamp and status.

The following steps describe how to subscribe to a variable of the server:


1. Enter the Node Id of the variable to be subscribed into the field "Node Id" (3).
2. Click on the "Subscribe" button (1) to subscribe to the variable.
3. In the "Reported value" area (4), the new values of the subscribed variable are
displayed.

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 11
2 Commissioning and operation

Method
The following figure shows the “Method” interface:
Abbildung 2-4

1 2

3 4

5 6

The following table describes the functions of the interface of the previous figure:
Tabelle 2-4
© Siemens AG 2017 All rights reserved

Nr. Beschreibung
1. Button to retrieve the input and output parameters.
2. Button to call the method.
3. Enter the node ID of the parent object in which the method is located.
4. Enter the node ID of the method.
5. List of the input parameters of the method (enter the values of the input
parameters in the "Value" column).
6. List of the output parameters of the method.

The following steps describe how to call a method of the server:


1. Enter the Node Id of the method in the "Method Node Id" field (4). In addition,
enter the Node Id of the node under which the method is to be found in the
"Parent Object Node Id" field (3).
2. Click on the "Get Method Arguments" button (1). The input arguments of the
method are displayed in the "Input Arguments" area (5) and the output
arguments in the "Output Arguments" area (6).
3. (Optional) In the "Input Arguments" area (5), enter the values of the input
arguments in the "Value" column, if they exist.
4. Click the Call Method button (2) to pass the input parameters to the server and
call the method.
5. (Optional) After calling the method, you will see the values of the output
parameters in the "Output Arguments" area (6) in the "Value" column, if output
parameters exist.

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 12
2 Commissioning and operation

ReadStructUdt
The following figure shows the “ReadStrucUdt” interface:
Figure 2-5

3
© Siemens AG 2017 All rights reserved

The following table describes the functions of the interface of the previous figure:
Tabelle 2-5
Nr. Beschreibung
1. Button to read an entered node ID from (2).
2. Text field to enter a node ID to be read.
3. Text field to output the value of the struct/UDT with variable names and data
types.

The following steps describe how to read a struct/UDT of the server:


1. Enter the Node Id of a variable to be read in the field "Node Id" (2).
2. Click on the "Read" button (1) to read the variable. The read result can be
found in the output text field (3).

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 13
3 Creating the OPC UA Excel Client

3 Creating the OPC UA Excel Client

Integrating the OPC UA Client Library

Note Requirement: OPC UA Client Library is registered on your system.

The following step-by-step instructions show you how to create your own OPC UA
Excel Client.
1. Start Excel with a new file.
2. In the "Developer" menu, select the Microsoft Basic Editor.
© Siemens AG 2017 All rights reserved

3. Refer to the OPC UA Client Library.

4. You can now use the OPC UA Client Library in your client. Please refer to VBA
implementation of the Microsoft Basic Editor in the delivered example sheet for
further information.

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 14
4 OPC UA Client Library

4 OPC UA Client Library


The library internally uses the "UAClientHelperAPI" and the "OPC UA .NET" stack
from OPC Foundation which are described in the entry \3\.
The following classes are contained in the library:

OpcUaClient
This class represents an OPC UA client.
Table 4-1
Method Description

OpcUaEndpoint[] GetEndpoints(String strURL); Determines the available endpoints on


a server via which a connection can be
established.
bool Connect(OpcUaEndpoint Establishes a connection to a server
opcUaEndpoint, bool bUserAuth, String and creates a secure channel and a
session to the server.
strUserName, String strUserPassword);
void Disconnect(); Ends an existing session and
disconnects the connection to the
server.
String[] ReadValues(ref String[] nodeIdStrings); Reads the values of a tag node.
void WriteValues(ref String[] values, ref String[] Writes values to tag nodes.
nodeIdStrings);
© Siemens AG 2017 All rights reserved

String[] ReadStructUdt(String nodeIdString); Reads structs and UDTs.


String[] RegisterNodeIds(ref String[] Registers node IDs at the server for an
nodeIdStrings); optimized access to the nodes.
void UnregisterNodeIds(ref String[] Deletes the registration of already
nodeIdStrings); registered node IDs.
OpcUaSubscription Subscribe(int Creates a subscription on the server.
publishingInterval);
void RemoveSubscription(OpcUaSubscription Deletes a specific subscription from the
subscription); server.
OpcUaMonitoredItem Adds a MonitoredItem for monitoring
AddMonitoredItem(OpcUaSubscription sub, an existing subscription.
String nodeIdString, String itemName, int
samplingInterval);
void RemoveMonitoredItem(OpcUaSubscription Deletes an existing MonitoredItem of a
subscription, OpcUaMonitoredItem subscription.
monitoredItem);
String[] GetMethodArguments(String Requests input and output parameters
nodeIdString) of a method and their data types
aswell.
String[] CallMethod(String methodIdString, Calls a method of the server.
String objectIdString, String[] inputData, String[]
inputType)

OpcUaSubscription
This class represents a subscription. The subscription is created and returned by
the OpcUaClient in the Subscribe(...) method. The subscription is needed in the
AddMonitoredItem(…) method and in the RemoveSubscription(…) method.

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 15
4 OPC UA Client Library

OpcUaMonitoredItem
This class represents a MonitoredItem. The MonitoredItem is created and returned
by the OpcUaClient in the AddMonitoredItem(...) method. The MonitoredItem is
needed in the RemoveMonitoredItem(...) for its removal.

OpcUaEndpoint
This class represents an endpoint of an OPC UA server. In the GetEndpoints
method, the OpcUaClient provides all endpoints for the supplied URL of the OPC
UA server. The endpoint is required in the Connect(...) method to establish a
connection.
© Siemens AG 2017 All rights reserved

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 16
5 Worth knowing

5 Worth knowing
5.1 Certificate handling in the example program
The client certificate of the sample program is created the first time it is started and
stored in the Windows certificate store. The certificate is required for encrypted and
signed connections and has the name "UA Client 1500". The certificate is self-
signed by the application.
The certificate store can be found under "Start > Run > mmc > Enter > File >
Add/Remove Snap-In > Certificates > Add > Select My Account > OK" in the "My
Certificates" folder.
To re-create the certificate, delete it from the store and then restart the application.

Hinweis The sample client automatically accepts all server certificates unchecked for
each session but does not save them.

5.2 Information about the license model of the software


The "OPC UA .NET Stack" used in this example was created by the OPC
Foundation and will be continued as a joint project. The GitHub community is
© Siemens AG 2017 All rights reserved

responsible for the further development and maintenance of the stack.


Depending on your membership in the OPC Foundation, the stack is subject to two
different license models:
1. the "RCL" model applies to members of the OPC Foundation.
2. the "GPL 2.0" model applies to non-members of the OPC Foundation.

Hinweis Before you start developing your own application, check out the appropriate
licensing models based on your membership of the OPC Foundation.

Hinweis The crypto component "BouncyCastle.Crypto.dll" and the implementations of


Siemens AG are subject to the open source license "MIT".

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 17
6 Appendix

6 Appendix
6.1 Service & support
Industry Online Support
Do you have any questions or need assistance?
Siemens Industry Online Support offers round the clock access to our entire
service and support know-how and portfolio.
The Industry Online Support is the central address for information about our
products, solutions and services.
Product information, manuals, downloads, FAQs, application examples and videos
– all information is accessible with just a few mouse clicks:
support.industry.siemens.com

Technical Support
The Technical Support of Siemens Industry provides you fast and competent
support regarding all technical queries with numerous tailor-made offers
– ranging from basic support to individual support contracts. Please send queries
to Technical Support via Web form:
www.siemens.com/industry/supportrequest
© Siemens AG 2017 All rights reserved

SITRAIN – Training for Industry


We support you with our globally available training courses for industry with
practical experience, innovative learning methods and a concept that’s tailored to
the customer’s specific needs.
For more information on our offered trainings and courses, as well as their
locations and dates, refer to our web page:
www.siemens.com/sitrain

Service offer
Our range of services includes the following:
• Plant data services
• Spare parts services
• Repair services
• On-site and maintenance services
• Retrofitting and modernization services
• Service programs and contracts
You can find detailed information on our range of services in the service catalog
web page:
support.industry.siemens.com/cs/sc

Industry Online Support app


You will receive optimum support wherever you are with the "Siemens Industry
Online Support" app. The app is available for Apple iOS and Android:
support.industry.siemens.com/cs/ww/en/sc/2067

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 18
6 Appendix

6.2 Links and literature


Table 6-1
No. Topic
\1\ Siemens Industry Online Support
https://support.industry.siemens.com
\2\ Link to the entry page of the application example
https://support.industry.siemens.com/cs/ww/en/view/109748892
\3\ OPC UA .NET Client for the SIMATIC S7-1500 OPC UA Server
https://support.industry.siemens.com/cs/ww/en/view/109737901
\4\ OPC UA .NET stack from GitHub
https://github.com/OPCFoundation/UA-.NETStandard

6.3 Change documentation


Table 6-2
Version Date Modifications
V1.0 09/2017 First version
© Siemens AG 2017 All rights reserved

V2.0 09/2019 Update to OPC UA 1.04


Added method calls
V2.1 05/2020 Added reading of struct/UDT

OPC UA Client Library


Entry ID: 109748892, V2.1, 05/2020 19

You might also like