Professional OPC
Development Tools

logos

How to subscribe and process changes to a boolean tag

More
03 May 2018 15:04 - 03 May 2018 15:05 #6297 by support
When Exception is null, the e.AttributeData contains what has been sent by the server. But that does not always mean that e.AttributeData.Value is valid.

You might be getting a "bad quality" as a first update. Before accessing e.AttributeData.Value, I would check e.AttributeData.HasValue. Your current code might work as well, but I still recommend this.

Best regards
Last edit: 03 May 2018 15:05 by support.

Please Log in or Create an account to join the conversation.

More
03 May 2018 14:39 - 03 May 2018 15:05 #6296 by wmitc2375
I think it is working. Not sure if this is overkill on needed logic but it seems to filter out all the non (seam detected0 events I am need to process.
        private void easyUAClient1_DataChangeNotification(object sender, EasyUADataChangeNotificationEventArgs e)
        {
 
            // if exception, do nothing.
            if (e.Exception != null)
            {
                return;
            }
 
            // if no attribute data, do nothing.
            if (e.AttributeData.Value == null)
            {
                return;
            }
 
            // try casting attribute data as a booleana and if false, seam detect reset event, do nothing.
            try
            {
                if (Convert.ToBoolean(e.AttributeData.Value) == false)
                {
                    return;
                }
            }
            // if unable to cast to boolean, not what we need to be processing, do nothing.
            catch
            {
                return;
            }
Last edit: 03 May 2018 15:05 by support.

Please Log in or Create an account to join the conversation.

More
03 May 2018 14:16 - 03 May 2018 14:36 #6295 by wmitc2375
Tried this but for some reason, when I set my Boolean tag to True, it seems to be firing multiple events. In my event handler after it passes the tests shown below, it is reading the value of the tag and if it is True, it does needed processing then writes the tag back to False. The event handler seems to get called twice when I set the tag to True using an OPC-UA client. Here is what I added from your last post

when subscribing....
easyUAClient1.SubscribeDataChange(epd, node, 10000);
 easyUAClient1.DataChangeNotification += easyUAClient1_DataChangeNotification;
 
 string seamDetectTagChanged = "SeamDetected";
 easyUAClient1.SubscribeMultipleMonitoredItems(new[] {
 new EasyUAMonitoredItemArguments(seamDetectTagChanged, "opc.tcp://10.0.16.82:4096",
 "ns=2;s=[default]Test Data/Test Data/Sam_Coater_Seam_Detect", 1000)
 });

in the event handler...

 private void easyUAClient1_DataChangeNotification(object sender, EasyUADataChangeNotificationEventArgs e)
        {
 
            // if exception, do nothing.
            if (e.Exception != null)
            {
                return;
            }
 
            object state = e.Arguments.State;
            try
            {
                if (Convert.ToString(state) != "SeamDetected")
                {
                    return;
                }
            }
            catch
            {
                //if not seam detected state, return.
                return;
            }
 
 
     //continue to do processing of detected seam...
Last edit: 03 May 2018 14:36 by support.

Please Log in or Create an account to join the conversation.

More
03 May 2018 12:42 #6293 by support
The event handled should first check whether the Exception property of the eventArgs is null. If it is not-null, the event indicates a problem.

If Exception is null, the event carries data.

If you are subscribed to multiple items, you can distinguish them

1. By information contained in eventArgs.Arguments - for example, eventArgs.Arguments.NodeDescriptor contains information about the node involved. An example for that:

opclabs.doc-that.com/files/onlinedocs/QuickOpc/Latest/User%2...es%20for%20data%20changes.html

2. Faster and more generic approach with many items is to pass some information in when you are subscribing - for it, there is a State argument or property, and then get eventArgs.Arguments.State and use whatever information you find there. So with each notification you get the State that was used when subscribing. The State can be any 'object', and it can therefore point right to something that identifies the item for you.

Regards

Please Log in or Create an account to join the conversation.

More
03 May 2018 12:21 #6292 by wmitc2375
I thought I had it working but it is not exactly. I used a line like this -

easyUAClient1.DataChangeNotification += easyUAClient1_DataChangeNotification;

to create an event handler. However, that event handler is getting called multiple times, when the program first starts up, and then when I change a Boolean tag, it is getting called several times. I need to know how to interrogate and only go through the logic of the event handler when it is a change in Boolean value of the tag I need to monitor. Can you provide a generic example of how to test in the event handler for a change to a single tag like wrapping if logic around it or doing a return right at the top? Or even a line I can use to know what is causing the event? Thanks.

Please Log in or Create an account to join the conversation.

More
03 May 2018 06:33 #6290 by support
I understand that you made this part working later. Will respond to your other post as well.

A note to the code: The UserName and Password properties are rarely, if ever, used with OPC UA. Certainly never with "opc.tcp:" scheme. In theory they might be used to authenticate access to the resource when HTTP is used, but I have never seen it in practice. The user authentication is done on OPC UA level instead, using the UserIdentity property.

Best regards

Please Log in or Create an account to join the conversation.

More
02 May 2018 20:42 #6288 by wmitc2375
I figured it out, I was missing this line -

easyUAClient1.DataChangeNotification += easyUAClient1_DataChangeNotification;

Please Log in or Create an account to join the conversation.

More
02 May 2018 20:33 #6287 by wmitc2375
Code at initialization -

EasyUAClient easyUAClient1;
UAEndpointDescriptor epd = new UAEndpointDescriptor();
UANodeDescriptor node = new UANodeDescriptor();


easyUAClient1 = new EasyUAClient();
epd.UserName = "opcuauser";
epd.Password = "password";
epd.Port = 4096;
epd.Host = "10.0.16.82";
node = new UANodeDescriptor("ns=2;s=[default]Test Data/Test Data/Sam_Coater_Seam_Detect");

//try to subscribe to change events.
easyUAClient1.SubscribeDataChange(epd, node, 500);



//does not seem to be firing, set a breakpoint in debug and change tag value but seeing event.
static void easyUAClient1_DataChangeNotification(object sender, EasyUADataChangeNotificationEventArgs e)
{
// Display value
// Remark: Production code would check e.Exception before accessing e.AttributeData.
Console.WriteLine(e.AttributeData.Value);
}

Please Log in or Create an account to join the conversation.

Moderators: support
Time to create page: 0.213 seconds

      

 Recommend this on Google