|
Return to Documents list
How to do subscription reads
Introduction:
This paper outlines how to perform subscription reads within your .NET Application.
Subscription reads are a way of receiving notifications of when a Tag changes value. This essentially allows your application to simply request for
notifications of when the Tag changes values, thus preventing your application from continuously polling the tag and you detecting if the value has changed or not.
This paper shows all code in VB.NET.
Getting Started
Setting-up the Subscriptions
The "RequestItem" method is used to request a subscription to a Tag.
OPC Data Control ActiveX Users: "RequestItem" is the equivalent of "ConnectName" and "ConnectObject".
The RequestItem method is defined in the OPC Server object, namely:
The RequestItem takes the following arguments:
- Machine Name -- the name of the computer where the OPC Server resides
- Server Class -- the name (PROGID) of the OPC Server to connect to
- Item ID -- the name of the Tag to subscribe to
- Access Path -- optional, and will be ignored in this paper
- Data Type -- optional, and will be ignored in this paper
- Mode -- an object containing the timeout settings for this connection
- Tag -- optional, and will be ignored in this paper.
Lets take a look at some ways in which you can subscribe to a Tag, as there are a couple of ways in which you can setup a subscription...
Method #1 - simple subscription accepting default timeout values
First, we define an instance of a Tag:
Next, we define an instance of an OPC Server connection object:
Now we setup the subscription:
oItem = oSvr.RequestItem("", "SWToolbox.TOPServer", _ "Channel_0_User_Defined.Ramp.Ramp1")
The instruction above is too long to fit on one line, so it has been split into 2 lines. The above code shows a subscription being setup to tag "Channel_0_User_Defined.Ramp.Ramp1" in the
"TOPServer" OPC Server on the local "" computer.
Method #2 - subscription, with custom timeout values
Sometimes, the default timeout settings need to be custom configured according to your needs. There are many timeout settings in the OPC Web Client, all
of which are described in the products Help file.
Here is an example of how you can subscribe to a Tag and specify some custom timeout settings:
Dim oTimeouts As OPCLabs.EasyOPCDANet.IEasyDAModeNet = oSvr.DefaultMode With oTimeouts
.ClientAttachTimeout = 10000 '10 seconds until the call at the opcserver completes .ServerStatusFirstRefreshTimeout = 5000 '5 second timeout waiting for first update End With
There are a number of other timeout settings available to you, refer to the products Help file for more information.
Now to subscribe to the Tag with these custom timeout settings:
oItem = oSvr.RequestItem("", "SWToolbox.TOPServer", "Channel_0_User_Defined.Ramp.Ramp1", Nothing, Nothing, oTimeouts,
Nothing))
Note that "oTimeouts" is passed-in as a parameter.
Specifying the Update Rate / Polling Frequency
After you have defined the subscription (as shown above) you can programmatically change the scan-rate of the subscription by changing the "RequestedUpdateRate" property on the tag
object which is shown as "oItem" in our examples above.
So to specify a Tag to be polled every second, I would enter the following code:
You can programmatically change the update rate at any time.
Last Step in the Subscription configuration - Event Handler
After setting up the subscription, you must define an event handler to receive the notification that the Tag has changed value. The IItemNet
object defines a simple event that you will need to raise:
AddHandler oItem.MeasurementChange, AddressOf itemchanged
The above line of code is binding the "MeasurementChange" event on the "oItem" object (previously defined), to the "itemChanged" procedure which we now need to
define:
Private Sub itemchanged(ByVal sender As Object, ByVal e As EventArgs) End Sub
Subscribing to Multiple Tags
By defining an array of Items, you can setup a subscription to many items. Here's a simple example showing:
Private oItem As IItemNet Private oSvr As New OPCLabs.EasyOPCDANet.EasyDACOM
oItem = oSvr.RequestItem("", "SWToolbox.TOPServer", "Ch1.Dev1.Tag1") AddHandler oItem.MeasurementChange, AddressOf itemchanged
oItem = oSvr.RequestItem("", "SWToolbox.TOPServer", "Ch1.Dev1.Tag2") AddHandler oItem.MeasurementChange, AddressOf itemchanged
oItem = oSvr.RequestItem("", "SWToolbox.TOPServer", "Ch1.Dev1.Tag3") AddHandler oItem.MeasurementChange, AddressOf itemchanged
Some tips:
- Define a procedure to do the actual subscription and handle any exceptions
- Call the procedure and pass into that procedure the name of the Tag... for example:
Private Sub subscribeTo(ByVal tagname As String) Try Dim oTimeouts As OPCLabs.EasyOPCDANet.IEasyDAModeNet = oSvr.DefaultMode With oTimeouts
.ClientAttachTimeout = 10000 '10 seconds until the call at the opcserver completes .ServerStatusFirstRefreshTimeout = 5000 '5 second timeout waiting for first update End With
oItem = oSvr.RequestItem("", "SWToolbox.TOPServer", tagname, Nothing, Nothing, oTimeouts, tagname) AddHandler oItem.MeasurementChange, AddressOf itemchanged
Catch ex As Exception MessageBox.Show("Unable to subscribe to tag. " & ex.Message) End Try End Sub
Responding to the Tag Value Changes
Now that you have defined your subscriptions, and configured your event, it is time to figure out what tag has changed and what its new settings are.
Lets look at the event structure:
Private Sub itemchanged(ByVal sender As Object, ByVal e As EventArgs) End Sub
The "sender" object will actually contain the tag object. The tag object contains the value, qality and timestamp. Here's how to create a variable object
of IItemNet and get the values into it:
Dim theTag As OPCLabs.EasyOPCDANet.IItemNet theTag = DirectCast(sender, OPCLabs.EasyOPCDANet.IItemNet)
Now, we can simply obtain our value, time and quality from the "theTag" object:
Dim strMessage As String strMessage = "Value = " & theTag.VTQ.Value.ToString & vbCrLF
strMessage += "Quality = " & theTag.VTQ.Quality.ToString & vbCrLf strMessage += "Timestamp = " & theTag.VTQ.Timestamp.ToString MessageBox.Show(strMessage)
Conclusion
This paper has explained how subscriptions are setup and used within the OPC Web Client. Examples are available to download at:
- VB.NET Example
- C# Example
|