Programmatic Data Access

For a detailed presentation on programmatic data access view the following video.

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 OPC Controls data component can also be used in ASP.NET Web applications.  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 OPC Systems.NET.




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


Select Windows Application as the project type.


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

From the .NET Framework Components select the OPC Controls Data component and then select OK.


Add an OPCControlsData component to the Form.


Add a Button component onto the Form.

Change the Name of the button to buttonAddTags..

Change the Text property of the button to Add Tags.

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 OPC Systems 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 OPC Systems.NET Tags.  To see the full syntax of a DirectOPC Item use an OPC Control Label and browse an OPC Systems.NET Tag for the Text_OPCSystems_Tag property and select DirectOPC. 

The following is an example of a DirectOPC Item.

[DirectOPC:KEPware.KEPServerEx.V4\Channel_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.


For Visual Basic view the code of Form1.

Select OpcControlsData1 from the object list.

Select ValuesChangedAll from the event list.

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.

  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

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.



Run the application in Debug or Release mode.


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 bin\Release directory to the target systems or follow the Smart Client deployment section in this help file to deploy your application using Click Once Deployment.


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.

Continue: Programmatic Configuration