Getting Started – .NET Data Connector

The following steps can be used to add real-time read and write data functions to a C#, C++, or Visual Basic.NET WinForm or Windows Service application.

The .NET Data Connector data component can also be used in ASP.NET or .NET MVC Web applications within server-side .NET code. Refer to the Read Values and Write Values Forms in the VB.NET example for programmatic interface of using the OPC Controls Data component.

All properties are programmatically accessible. The component can also be used with Visual Studio 2003 with the older 1.1 Framework version of Open Automation Software.

Step 1

Start Visual Studio and select File->New->Project to create a new C#, C++, or VB.

.NET WinForm HMI 292

Step 2

Select Windows Application as the project type.

.Net Trend 359

Step 3

From the Toolbox if OPCSystemsData component is not available right click in the Toolbox and select Choose Items. If it is available to Step 4.

.NET WinForm HMI 294

From the .NET Framework Components select the OPCSystemsData component from the OPCSystemsDataConnector assembly and then select OK.

Step 4

Add an OPCSystemsData component to the Form.

Step 5

Add a Button component onto the Form.

.NET Data Connector 410

Change the Name of the button to buttonAddTags.

.NET Data Connector 411

Change the Text property of the button to Add Tags.

.NET Data Connector 412

Double click on the button and enter the following code:

For VB:

Private Sub ButtonAddTags_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAddTags.Click
   Dim tagsToAdd(1) As String
   tagsToAdd(0) = "Ramp.Value"
   tagsToAdd(1) = "Pump.Value"
End Sub

For C#:

private void buttonAddTags_Click(object sender, EventArgs e)
   string[] tagsToAdd = new string[2];
   tagsToAdd[0] = "Ramp.Value";
   tagsToAdd[1] = "Pump.Value";

Note: If you wish to run this application on remote PCs make sure to include the Network Node or IP Address of the OAS Service source.  To connect to remote tags simply include a network node name or IP address as follows.




You can also use the OPCControlsNetworkNodes component to alias one network node for another. Refer to the VB.NET Example for a working demonstration.

Note: All Tag names are case sensitive.  Ramp.Value is valid, ramp.value is not.

Note: You can additionally use DirectOPC items with the OPC Controls Data component to connect directly to OPC Server Items without having to create Open Automation Software Tags. To see the full syntax of a DirectOPC Item use an OPC Control Label and browse an Open Automation Software Tag for the Text_OPCSystems_Tag property and select DirectOPC.

The following is an example of a DirectOPC Item:

[DirectOPC:KEPware.KEPServerEx.V4Channel_1.Device_1.Tag_1:DirectOPCUpdateRate:1:DirectOPCDataType:Double Float:DirectOPCAccessPath:]

OPC Server: KEPware.KEPServerEx.V4

OPC Item: Channel_1.Device_1.Tag_1

OPC Update Rate: 1

Data Type: Double Float

All client components can use this alternative syntax for connecting directly to OPC Servers.  This provides a direct pass through for OPC Items on local and remote systems.

Step 6

View the code of Form1.

Select OpcSystemsData1 from the object list.

Select ValuesChangedAll from the event list.

.NET Data Connector 414

Enter the following code or your own code to do what every you desire with the data. Make sure to first check the Quality as True before using the Value as a valid value.
For VB:

Private Sub OpcControlsData1_ValuesChangedAll(ByVal Tags() As String, ByVal Values() As Object, ByVal Qualities() As Boolean, ByVal TimeStamps() As Date) Handles OpcControlsData1.ValuesChangedAll
        Dim ValueString As String
        Dim Index As Int32
        For Index = 0 To Tags.GetLength(0) - 1
            If Qualities(Index) Then
                ValueString = "Tag: " + Tags(Index) + " is " _
                + Values(Index).ToString + " at " _
                + TimeStamps(Index).ToString("HH:mm:ss.fff")
                ValueString = "Tag: " + Tags(Index) + " is bad"
            End If
    End Sub

For C#:

// In Form1.cs
private void DataConnector_ValuesChangedAll(string[] Tags, object[] Values, bool[] Qualities, DateTime[] TimeStamps)
    String ValueString = "";
    for (int i = 0; i < Tags.Length; i++)
        if (Qualities[i])
            ValueString = "Tag: "+Tags[i]+" is "+Values[i].ToString()+" at "+TimeStamps[i].ToString();
            ValueString = $"Tag: {Tags[i]} is bad";
//In Form1.Designer.cs
private void InitializeComponent()
    .   (Generated setup code)

    // opcSystemsData1
    this.opcSystemsData1.ValuesChangedAll += new OPCSystemsDataConnector.OPCSystemsData.ValuesChangedAllEventHandler(this.DataConnector_ValuesChangedAll);

All values are queued in the order they are received from the Service and you may receive multiple values for the same tag in the same event.

Step 7

Run the application in Debug or Release mode.

.NET WinForm HMI 305

Step 8

To deploy the application to remote nodes first make sure the Tags as described in Step 5 are set to a Network Node or IP Address. You can also use the OPCControlsNetworkNodes component to alias the “localhost” node for another IP Address or node name. Registered domain names are also allowed.

Then simply copy the files in the binRelease directory to the target systems or follow the Smart Client deployment section in this help file to deploy your application using Click Once Deployment.

Step 9

Refer to the VB.NET example for demonstration example of all properties of all components.

The Read Values Form is a good resource for programmatic interface for reading values and working with both a Queue and a Hashtable.

The Write Values Form is a good resource on how to programmatically write values.