How to Access Siemens Data from a C# or VB .NET Application

How to Access Siemens Data from a C# or VB .NET Application


Open Automation Software can connect directly to Siemens controllers with the built in Siemens Driver Interfaces which support communications over Ethernet to S7-200, S7-300, S7-400, S7-1200, and S7-1500. The OASData component is used for real time and historical data access against a local or remote OAS instance and can be used with .NET 5, .NET Core 2.0 or greater, .NET Framework 4.61 or greater, Xamarin.iOS 10.14, Xamarin.Android 8.0, and UWP 1.0.0.16299. This tutorial walks you though downloading and installing OAS, configuring a Siemens driver, configuring tags and reading and writing to them with the .Net Data Connector. This page shows code examples in C# but VB works as well.

Step 1. Download and Install the Open Automation Software and Start the OAS Service

If you have not already done so, you will need to download and install the OAS platform.  Fully functional trial versions of the software are available for Windows, Windows IoT Core, Linux, Raspberry Pi and Docker on our downloads page.

On Windows run the downloaded Setup.exe file to install one or more of the Open Automation Software features. Select the default Typical installation if you are not sure what features to use or the Custom installation if you want to save disk space on the target system.  When prompted agree to the End User License Agreement to continue the installation.

For more detailed instructions and video tutorials, visit the installation guide for your system:
Windows Installation | Linux Installation | Raspberry Pi Installation | Dockers Installation

When the installation is finished the OAS Service Control application will appear.  Use this application to start the 4 Services. If this is the first time installing the software it will automatically enter Runtime with an example Tag Configuration.


Step 2. Configure Your Siemens Data Source

  1. First, you will need to open the Configure OAS application from the program group Open Automation Software.

  2. Select Configure >> License from the top menu and verify that Siemens is one of the available Drivers in the lower left of the form. The demo license will have this by default. If you do not see Siemens available, contact support@openautomationsoftware.com to update your license.

  3. Select Configure >> Drivers from the top menu.


  4. Select localhost or the remote service you wish to modify with the Select button to the right of the Network Node list.


  5. The Configure Drivers Screen will appear. Select Siemens from the Driver dropdown box.


  6. Enter a meaningful Driver Interface Name that you will refer to this physical connection when defining Tags with a Siemens Data Source.

  7. Define the properties for the desired physical connection.

  8. Click the Add Driver button above the Driver list in the left pane to add the Driver Interface as an available selection when defining Tags in the next step.

For more detailed instructions on configuring your Siemens data source, click here to see our Getting Started Siemens tutorial.


Step 3. Configure Your Tags

OAS provides multiple ways to add and define tags:

To add a Tag manually:

  1. In the OAS Configure Application, select Configure >> Tags from the top menu.


  2. Select localhost or the remote service you wish to modify with the Select button to the right of the Network Node list.


  3. Click on the Add Tag button located at the top of the Tag browser on the left portion of the screen.


  4. A dialog box will appear. Enter a name for your new tag and click ok.

  5. A configuration screen will appear for your new tag. Select your data source type in in the Data Source dropdown box.


  6. Specify the correct data type in the Data Type dropdown box.

  7. Click Apply Changes at the bottom right of the window.

For more detailed instructions on configuring your tags, click here to see our Getting Started Tags tutorial.


Step 4. Access Your Data from a C# or VB .NET Application

The .NET Standard 2.0 OASData assembly is used to provide read and write access to OAS tag variables and can target .NET 5, .NET 6, .NET 7, .NET Core 2.o or greater, .NET Framework 4.61 or greater, Xamarin.iOS 10.14, Xamarin.Android 8.0, and UWP 1.0.0.16299.


Start a new Visual Studio Project or Open Your Existing One

Microsoft Visual Studio 2015+ is recommended. For developing cross-platform .NET Standard or .NET Core solutions, Visual Studio 2019+ is recommended. For developing Android and/or iOS solutions, be sure to include Xamarin extensions to Visual Studio. After creating a new Visual Studio project, add a reference to the OASData assembly and all its dependencies, found in the OAS installation directory. This is typically:

C:\Program Files\Open Automation Software\OAS\Controls\NetStanard\OASData\OASData.dll



Create an instance of the OASData.Data class

static OASData.Data oasd = new OASData.Data();

Your application typically does not need more than one instance of the OASData.Data class. So, this can be created when your application starts.


Read Tags Asynchronously

oasd.ValuesChangedAll += OASDValuesChangedAll; 
private void OASDValuesChangedAll(string[] Tags, object[] Values, bool[] Qualities, DateTime[] TimeStamps);

oasd.AddTags(new string[] {
"Sine.Value",
"Random.Value",
"Ramp.Value"
});

Values are returned in the ValuesChangedAll event anytime values change in a tag variable. AddTags adds tags for subscription.


Read Tags Synchronously

Values = oasd.SyncReadTags(Tags, ref Errors, 10000);
  • This call returns an object array with the values for each tag variable.
  • Tags is a string array of tag names and variables to read.
  • Errors is an integer array returning: 0 if the tag variable quality is good 1 if the quality is bad 2 if the value could not be returned within the timeout specified.
  • Timeout is specified in milliseconds to wait for the call to return.

Write Tags Asynchronously

oasd.WriteTags(OASTags, OASValues);

With this call, if the tags data source is defined to a device, for example: Modbus; Siemens; Allen Bradley MQTT; OPC UA, or application writes to .Value will be written to the source defined.

Examples: Modbus, Siemens, AB, OPC UA, MQTT

  • Tags is a string array of tag names and variables.
  • Values is an object array containing the values to write to each tag.
  • TimeStamps array can optionally be provided to set the time of the value if the Data Source of the Tag is Value.

Write Tags Synchronously

Errors = oasd.SyncWriteTags(Tags, Values);
  • Tags is a string array of tag names and variables.
  • Values is an object array containing the values to write to each tag.
  • Errors is an Integer array that returns: 0 when successful; 1 when OAS Engine is not reachable; 2 when the Tags array size is not equal to the Values array.

Networking

Tag names can include an IP Address, network node name, or registered domain name if the application is deployed remote from the OAS Engine.

Basic Networking Example:

\\192.168.0.1\TagName.Value

Live Data Cloud Networking Example:

\\www.opcweb.com\RemoteSCADAHosting.MyLDCNode.TagName.Value

For more information, see Getting Started with OASData and IIoT Example Service Code or watch the video below:

How To Use the OAS .NET Real Time Data Access Library

The OASData.dll assembly is a .NET Standard 2.0 assembly which can be integrated into any .NET application with the following targets: .NET 5 or greater; NET Core 2.0 or greater ; .NET Framework 4.61 or greater; Xamarin.iOS 10.14 or greater; Xamarin.Android 8.0 or greater; and UWP 1.0.0.16299 or greater.  The articles below are useful guides to get you started.

How to Access Modbus Data from a C# or VB .NET Application

How to Access Modbus Data from a C# or VB .NET Application

Open Automation Software Tags can be defined to connect directly to Modbus slave devices or host data to Modbus masters with the built in Modbus Driver Interface which supports communications over ethernet and serial interfaces for Modbus TCP, Modbus RTU, and Modbus ASCII protocols. The OASData component is used for real time and historical data access against a local or remote OAS instance and can be used with .NET 5, .NET 6, .NET 7, .NET Core 2.0 or greater, .NET Framework 4.61 or greater, Xamarin.iOS 10.14, Xamarin.Android 8.0, and UWP 1.0.0.16299. This tutorial walks you though downloading and installing OAS, configuring a Modbus driver, configuring tags and reading and writing to them with the .Net Data Connector. This page shows code examples in C# but VB works as well.

Step 1. Download and Install the Open Automation Software and Start the OAS Service

If you have not already done so, you will need to download and install the OAS platform.  Fully functional trial versions of the software are available for Windows, Windows IoT Core, Linux, Raspberry Pi and Docker on our downloads page.

On Windows run the downloaded Setup.exe file to install one or more of the Open Automation Software features. Select the default Typical installation if you are not sure what features to use or the Custom installation if you want to save disk space on the target system.  When prompted agree to the End User License Agreement to continue the installation.

For more detailed instructions and video tutorials, visit the installation guide for your system:
Windows Installation | Linux Installation | Raspberry Pi Installation | Dockers Installation

When the installation is finished the OAS Service Control application will appear.  Use this application to start the 4 Services. If this is the first time installing the software it will automatically enter Runtime with an example Tag Configuration.


Step 2. Configure Your Modbus Data Source

  1. First, you will need to open the Configure OAS application from the program group Open Automation Software.
  2. Select Configure >> License from the top menu and verify that Modbus is one of the available Drivers in the lower left of the form. The demo license will have this by default. If you do not see Modbus available, contact support@openautomationsoftware.com to update your license.
  3. Select Configure >> Drivers from the top menu.

  4. Select localhost or the remote service you wish to modify with the Select button to the right of the Network Node list.

  5. The Configure Drivers Screen will appear. Select Modbus from the Driver dropdown box.

  6. Enter a meaningful Driver Interface Name that you will refer to this physical connection when defining Tags with a Modbus Data Source.
  7. Specify the Connection as Ethernet or Serial.
  8. Specify the Modbus Type as Master or Slave. Master will be used when communicating to a Modbus device. Slave will be used when other Modbus masters will be communicating to OAS.
  9. When setting up a Slave interface over Ethernet set the IP Address to the computer IPv4 IP address or network node name if the master is on a remote PC. You can also use 127.0.0.1 or localhost if the Modbus master will be on the same computer.

For more detailed instructions on configuring your Modbus data source, click here to see our Getting Started Modbus tutorial or watch the video tutorial below:


Step 3. Configure Your Tags

OAS provides multiple ways to add and define tags:

To add a Tag manually:

  1. In the OAS Configure Application, select Configure >> Tags from the top menu.

  2. Select localhost or the remote service you wish to modify with the Select button to the right of the Network Node list.

  3. Click on the Add Tag button located at the top of the Tag browser on the left portion of the screen.

  4. A dialog box will appear. Enter a name for your new tag and click ok.
  5. A configuration screen will appear for your new tag. Select your data source type in in the Data Source dropdown box.

  6. Specify the correct data type in the Data Type dropdown box.
  7. Click Apply Changes at the bottom right of the window.

For more detailed instructions on configuring your tags, click here to see our Getting Started Tags tutorial.


Step 4. Access Your Data from a C# or VB .NET Application

The .NET Standard 2.0 OASData assembly is used to provide read and write access to OAS tag variables and can target .NET 5, .NET 6, .NET 7, .NET Core 2.o or greater, .NET Framework 4.61 or greater, Xamarin.iOS 10.14, Xamarin.Android 8.0, and UWP 1.0.0.16299.

Start a new Visual Studio Project or Open Your Existing One

Microsoft Visual Studio 2015+ is recommended. For developing cross-platform .NET Standard or .NET Core solutions, Visual Studio 2019+ is recommended. For developing Android and/or iOS solutions, be sure to include Xamarin extensions to Visual Studio. After creating a new Visual Studio project, add a reference to the OASData assembly and all its dependencies, found in the OAS installation directory. This is typically:

C:\Program Files\Open Automation Software\OAS\Controls\NetStanard\OASData\OASData.dll

Create an instance of the OASData.Data class

static OASData.Data oasd = new OASData.Data();

Your application typically does not need more than one instance of the OASData.Data class. So, this can be created when your application starts.

Read Tags Asynchronously

oasd.ValuesChangedAll += OASDValuesChangedAll; 
private void OASDValuesChangedAll(string[] Tags, object[] Values, bool[] Qualities, DateTime[] TimeStamps);

oasd.AddTags(new string[] {
"Sine.Value",
"Random.Value",
"Ramp.Value"
});

Values are returned in the ValuesChangedAll event anytime values change in a tag variable. AddTags adds tags for subscription.

Read Tags Synchronously

Values = oasd.SyncReadTags(Tags, ref Errors, 10000);
  • This call returns an object array with the values for each tag variable.
  • Tags is a string array of tag names and variables to read.
  • Errors is an integer array returning: 0 if the tag variable quality is good 1 if the quality is bad 2 if the value could not be returned within the timeout specified.
  • Timeout is specified in milliseconds to wait for the call to return.

Write Tags Asynchronously

oasd.WriteTags(OASTags, OASValues);

With this call, if the tags data source is defined to a device, for example: Modbus; Siemens; Allen Bradley MQTT; OPC UA, or application writes to .Value will be written to the source defined.

Examples: Modbus, Siemens, AB, OPC UA, MQTT

  • Tags is a string array of tag names and variables.
  • Values is an object array containing the values to write to each tag.
  • TimeStamps array can optionally be provided to set the time of the value if the Data Source of the Tag is Value.

Write Tags Synchronously

Errors = oasd.SyncWriteTags(Tags, Values);
  • Tags is a string array of tag names and variables.
  • Values is an object array containing the values to write to each tag.
  • Errors is an Integer array that returns: 0 when successful; 1 when OAS Engine is not reachable; 2 when the Tags array size is not equal to the Values array.

Networking

Tag names can include an IP Address, network node name, or registered domain name if the application is deployed remote from the OAS Engine.

Basic Networking Example:

\\192.168.0.1\TagName.Value

Live Data Cloud Networking Example:

\\www.opcweb.com\RemoteSCADAHosting.MyLDCNode.TagName.Value

For more information, see Getting Started with OASData and IIoT Example Service Code or watch the video below:

IIoT Example Service Code

The OAS Example Service code example is a great way to learn how to programmatically add tags, update tag properties, and read and write live data to an OAS Engine.

You can view the .NET Programmatic Interface video for demonstration of the OAS Example Service Code and explanation of use of the most common methods.

  • 00:00 – Introduction
  • 00:33 – Source code example
  • 01:07 – Visual studio projects example
  • 01:33 – How to get a visual studio app
  • 01:40 – How to create projects and use visual studio
  • 03:05 – OAS service code example
  • 15:00 – Tag variables
  • 15:37 – How to browse tags
  • 17:10 – Networking
  • 20:55 – How to implement security
  • 21:31 – How to publish
  • 22:15 – How to register service
  • 23:41 – How to change project name
  • 26:20 – Questions

Download the OAS Example Service code.

Right click on the zip file that you have downloaded and choose Properties to select Unblock checkbox if Security warning is shown.

There are 4 Visual Studio projects all use the same methods to provide a working example.

  • C# .NET Core Console App
  • VB .NET Core Console App
  • C# Windows Service
  • VB Windows Service

These projects use the OASConfig.dll and OASData.dll assemblies that are included in the OAS platform installation directory, the default is C:\Program Files\Open Automation Software\OAS.

These projects are a good starting point if you need to setup unattended execution of processing and transferring data.  They can be easily modified to include your own routines.

Note: It is recommend to run Visual Studio in Administrative mode for full access to project files.  Either modify the properties of the shortcut under Advanced button or right click on the VS shortcut to Run as administrator.

Key Methods

Constructor

private OASData.Data oasd = new OASData.Data();
private OASConfig.Config oassys = new OASConfig.Config();

Create Tags

string DemoErrorString = "";
string DemoResultString = oassys.SetTagProperties(DemoPropertyValues, "localhost", ref DemoErrorString);
  • PropertyValues is an array of arrays
  • NetworkNode is optional and can be an IP Address network node, or registered domain name
  • ErrorString will set the string passed by reference with Success if successful, or an error string if the tags were not created or updated.

First element is an array containing the property names.  These property names will match the Tag CSV Export header.

object[] DemoDesiredColumns = new object[5];
DemoDesiredColumns[0] = "Tag"; // Required
DemoDesiredColumns[1] = "Value - Data Type";
DemoDesiredColumns[2] = "Value - Source";
DemoDesiredColumns[3] = "Value - Simulation Type";
DemoDesiredColumns[4] = "Value - Simulation Rate";
DemoPropertyValues[0] = DemoDesiredColumns; // First record is the header

Additional elements contain the values for each Tag.

object[] DemoTagValues1 = new object[5];
DemoTagValues1[0] = "OAS Demo Service.Source Tags.Ramp";
DemoTagValues1[1] = "Double";
DemoTagValues1[2] = "Simulation";
DemoTagValues1[3] = "Ramp";
DemoTagValues1[4] = 0.25;
DemoPropertyValues[1] = DemoTagValues1;

Write Tags Asynchronously

oasd.WriteTags(OASTags, OASValues);
  • Tags is a string array of tag names and variables.
  • Values is an object array containing the values to write to each tag.
  • TimeStamps array can optionally be provided to set the time of the value if the Data Source of the Tag is Value.

If the Data Source is defined to a device or application writes to .Value will be written to the source defined.

Examples: Modbus, Siemens, AB, OPC UA, MQTT

Read Tags Asynchronously

Call AddTags to add a list of tags for subscription.

oasd.AddTags(ReadTags);
  • Tags is a string array of tag names and variables.

Values will be returned in the ValuesChangedAll event anytime value changes in a tag variable.

oasd.ValuesChangedAll += OASDValuesChangedAll;
private void OASDValuesChangedAll(string[] Tags, object[] Values, bool[] Qualities, DateTime[] TimeStamps)

Call RemoveTags to remove any of the tag variables from subscription.

Write Tags Synchronously

SyncWriteTags is a blocking call that waits for the call to return from the service.

Errors = oasd.SyncWriteTags(Tags, Values);
  • Tags is a string array of tag names and variables.
  • Values is an object array containing the values to write to each tag.
  • Errors is an Integer array that returns
  • 0 when successful
  • 1 when OAS Engine is not reachable
  • 2 when the Tags array size is not equal to the Values array

Write Tags Synchronously with Confirmation

SyncWriteTagsWithConfirmation is a blocking call that waits for the values of the tag variables written call to return from the service.

Errors = oasd. SyncWriteTagsWithConfirmation(Tags, Values, 10000, 0.0001);
  • Tags is a string array of tag names and variables.
  • Values is an object array containing the values to write to each tag.
  • Include optional timeout with default of 10,000 milliseconds and deadband for floating point values with default of 0.0001.
  • Errors is an Integer array that returns
  • 0 when successful
  • 1 when OAS Engine is not reachable
  • 2 when the Values array is null
  • 3 when the Tags array size is not equal to the Values array

Read Tags Synchronously

SyncReadTags is a blocking call that obtains the current value of the list of tag variables.

Values = oasd.SyncReadTags(Tags, ref Errors, 10000);
  • The returned value is an object array with the values for each tag variable.
  • Tags is a string array of tag names and variables to read.
  • Errors is an integer array returning:
  • 0 if the tag variable quality is good
  • 1 if the quality is bad
  • 2 if the value could not be returned within the timeout specified
  • Timeout is specified in milliseconds to wait for the call to return.

Networking

Tag names can include an IP Address, network node name, or registered domain name if the application is deployed remote from the OAS Engine.

Basis Networking Example:

\\192.168.0.1\TagName.Value

If Live Data Cloud networking is implemented for self-hosting with a dynamic IP Address the LDC syntax is applicable.

Live Data Cloud Networking Example:

\\www.opcweb.com\RemoteSCADAHosting.MyLDCNode.TagName.Value

Register Service

Linux

Windows

  • Windows: Use InstallUtil to register service
  • Using Visual Studio Command Prompt as Administrator
  • InstallUtil OASDemoService.exe

To change the name of the service set the properties of ServiceInstaller1 in ProjectInstaller.

  • Description
  • DisplayName
  • ServiceName

See our OASConfig documentation and OASData documentation for more details.

Frequently Asked Questions – Programmatic Interface

Frequently Asked Questions – Programmatic Interface

After authentication, how long can I use the token and clientid?
The security token and client id granted to the caller after authentication represent a REST API session. As long as the server remains active (not rebooted), and the credentials are continually used, the session will not expire. If there is no activity on the session after 30 minutes, the session will expire and a 401 Unauthorized response will be returned for all operations. This idle timeout can be configured on the server using the OAS Configuration app. Go to Configure > Options, select your OAS server (usually localhost when configuring the current machine) and then go to the Networking tab. Here you can set the REST API Session Timeout in minutes.
How can I programmatically browse OPC Servers?
Yes, using the free to use OPCSystems component. Refer to the WinForm Example Code under the program group Open Automation Software-Example in the Form FormConfigureOPC. This example is also listed in this help file in Programmatic Interface - .NET Programmatic Configuration - Programmatic Access OPC Browsing.

Videos – Programmatic Interface

.NET Data Connector

Connect Visual Studio applications on-premise or over the Internet to live data using the .NET Data Connector, a 100% managed .NET component for asynchronous and synchronous communications.

Realtime Data Access .NET Applications

Demonstration of the OAS Example Service Code and explanation of use of the most common methods.

  • 00:00 – Introduction
  • 00:33 – Source code example
  • 01:07 – Visual studio projects example
  • 01:33 – How to get a visual studio app
  • 01:40 – How to create projects and use visual studio
  • 03:05 – OAS service code example
  • 15:00 – Tag variables
  • 15:37 – How to browse tags
  • 17:10 – Networking
  • 20:55 – How to implement security
  • 21:31 – How to publish
  • 22:15 – How to register service
  • 23:41 – How to change project name
  • 26:20 – Questions
Client Failover

.NET applications can implement an automated or controlled switch to data servers to add redundancy to your application.

View UDI – Videos to create your own OAS Drivers for cross platform deployment with the free Universal Driver Interface SDK.

.NET Realtime Data Access

The .NET Data Connector product provides read and write to realtime tags including data sources of direct drivers to Modbus, Allen Bradley, Siemens, OPC UA Servers, Classic OPC Servers, OPTO-22, MQTT, AWS IoT, Azure IoT Data Hub, Microsoft Excel, data from SQL Server, Oracle, Access, mySQL, MongoDB, REST API, and other .NET applications.

The OASData.dll assembly is a .NET Standard 2.0 assembly which can be integrated into any .NET application with the following targets.

  • .NET 5 or greater
  • .NET Core 2.0 or greater
  • .NET Framework 4.61 or greater
  • Xamarin.iOS 10.14 or greater
  • Xamarin.Android 8.0 or greater
  • UWP 1.0.0.16299 or greater

The same access can also be provided in the legacy assemblies for Framework 4.6 or less.
If you are using the OAS WPF HMI product use the OPCWPFDasbhoard.OPCWPFData component.
If you are using the OAS WinForm HMI product use the OPCControls.OPCControlData component.
For .NET Framework 4.6 or less projects without the need for user interface controls use the OPCSystemsDataConnector assembly.

The .NET Data Connector is also a very powerful method to turn any data from a .NET application into a realtime source for the Open Automation Software platform. This is easily done with the WriteTags method which you can optionally specify TimeStamps.

All components have the same easy to use programmatic methods.

The OAS Example Service Code is a working example of reading and writing tags synchronously and asynchronously.  This includes example projects for both C# and VB for .NET Core Console App to run on all operating systems including…

  • Linux
  • Windows
  • Mac
  • Android
  • iOS

There is also C# and VB projects to run as a Windows Service.  The code examples in all 4 projects are the same to show adding tags programmatically and the asynchronous and synchronous methods for reading and writing data.

Applications can be deployed locally or remotely and can optionally implement OAS Basic Networking or Live Data Cloud Networking.

All Tag Variables are accessible for reading and writing.

Examples:

  • TagName.Value
  • TagName.Desription
  • TagName.Units
  • TagName.HighHighAlarmLimit
  • TagName.HighHighAlarmActive (read only)

See a complete list of Tag Variables

Networking

Tag names can include an IP Address, network node name, or registered domain name if the application is deployed remote from the OAS Engine.

Basis Networking Example:

\\192.168.0.1\TagName.Value

If Live Data Cloud networking is implemented for self-hosting with a dynamic IP Address the LDC syntax is applicable.

Live Data Cloud Networking Example:

\\www.opcweb.com\RemoteSCADAHosting.MyLDCNode.TagName.Value

Common Methods

See our OASData documentation for more details.

And also Realtime Data Access – VB and C# – Windows Server and .NET Core Console App examples for programmatic tag creation and realtime data access.