Professional OPC
Development Tools

logos

Online Forums

Technical support is provided through Support Forums below. Anybody can view them; you need to Register/Login to our site (see links in upper right corner) in order to Post questions. You do not have to be a licensed user of our product.

Please read Rules for forum posts before reporting your issue or asking a question. OPC Labs team is actively monitoring the forums, and replies as soon as possible. Various technical information can also be found in our Knowledge Base. For your convenience, we have also assembled a Frequently Asked Questions page.

Do not use the Contact page for technical issues.

OPC UA .NET C# Subscribing to Complex Data Casting Error

More
06 Oct 2022 11:14 #11135 by support
Hello. Thanks for letting me know.

If you subscribe to a change, but then read the same node inside the handler, there are following possible bad consequences:

1. It is inefficient, and especially so with relatively large data arrays/structures you are dealing with, because more OPC UA calls need to me made, and more data processed and transferred over the network.

2. What's worse, you can lose data. The subscription gives you all updates, with a guarantee. By using the data from the Read instead, some changes may be skipped.

Regards
The following user(s) said Thank You: amlynar

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

More
05 Oct 2022 17:07 #11134 by amlynar
Hello,

I was able to discover the cause of the cast error. In another class I was subscribing to the tag and trying to cast it as a byte[,]. Once I removed it, the error disappeared. It runs perfectly fine now.

I do ignore the value only because I use the subscribe function to notify my program when a change was made in the data of the array tag. Then I call "OnCamArrayChange()" to update my list. I guess I could use the generic object as well to update my list.

Thank you!

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

More
05 Oct 2022 07:58 #11133 by support
Thank you.

1. I have problems following the logic of your code. If I read it well, you subscribe to changes of some variable, but then, in the callback/event handler, you actually ignore the notification object which contains the variable data: I am talking about this statement - "value => { OnCamArrayChange();" - the "value" here is not used for anything. And, inside the callback, you then do some Read-s: Are those the Read for the variable you subscribed to? Can you please explain the approach, and why isn't the data you subscribed to used for anything?

2. Please also expand e.AttributeData.Value[0], and continue expanding some more levels. And post here.

3. And now the main thing: Please configure Visual Studio to break into debugger when the exception occurs. To do so: Debug -> Windows -> Exception Settings. Click on "Common Language Runtime Exceptions". Into the Search box, type InvalidCastException. Check the box next to "System.InvalidCastException". The re-run you program in debugger. It should break into the debugger when the exception occurs. Post here the information showing where it has stopped, and the code around it.

Best regards
The following user(s) said Thank You: amlynar

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

More
04 Oct 2022 18:44 #11132 by amlynar
If it is blurry I screenshot it closer




Attachments:

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

More
04 Oct 2022 18:36 #11131 by amlynar
This is the expanded version as requested. It just repeats the data.




This is what is inside OnCamArrayChange method. It just reads elements from tags and updates a list.
Attachments:

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

More
04 Oct 2022 18:05 - 04 Oct 2022 18:16 #11130 by support
Hello.
(I have edited this post because I have realized parts of it were incorrect)

1. I have asked you to use the debugger and post "... expanded so that one can see the data". What you have posted is not expanded. You need to click expand the row with "e", and continue expanding.

2. What is inside the OnCamArrayChange method?

Regards
Last edit: 04 Oct 2022 18:16 by support.

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

More
04 Oct 2022 17:37 #11129 by amlynar
Hello,

I am not trying to cast anything which is why I am confused. I just want the subscription to call a function using the callback when it notices a change in the Array of array tag.

What is inside the delegate is the code I pass in this:
camArrayHandle = OpcClient.Get().SubscribeToTag(Tags.GenV_Cam_Array, value => {
                       OnCamArrayChange();
            });

By "console output" I meant what shows in the debug output. It is the cast error I wrote in my first message.
Error:
Exception thrown: 'System.InvalidCastException' in PacScreensRmg.exe
OpcLabs.EventTracing.LogEntries Error: 1 : LogEntry("OPCLabs-EasyUAClient",Error,0,101). CALLBACK ERROR. An exception of type "System.InvalidCastException" from source "PacScreensRmg" has occurred in callback 'System.EventHandler`1[OpcLabs.EasyOpc.UA.OperationModel.EasyUADataChangeNotificationEventArgs]'. The exception descend follows. (0) System.InvalidCastException PacScreensRmg (<.ctor>b__79_5) -> Unable to cast object of type 'OpcLabs.EasyOpc.UA.ComplexData.UAGenericObject[]' to type 'System.Byte[,]'.

Contents of e.AttributeData.Value:
Attachments:

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

More
04 Oct 2022 16:14 #11128 by support
Hello.

Your post is missing the truly relevant pieces of code - that is, the actual inside of the delegate , and/or the "console output" you referred to,.

But in general, you cannot cast UAGenericObject[] to Byte[,], that is to be expected.

I would be able to help you most quickly if you break inside the SubscribeDataChange handler somewhere, and display in the debugger the contents of e.AttributeData.Value, expanded so that one can see the data. It will also give you understanding of the data you are receiving.

On the first level, there will be an array of UAGenericObject-s. You cannot reasonably cast it to anything, you need to address individual elements of it. Then, each such element has a GenericData property. In this property, there will be a value of type that is some concrete derivation of UAGenericObject - see opclabs.doc-that.com/files/onlinedocs/QuickOpc/Latest/User%2...g%20with%20generic%20data.html . It is likely that it will be SequenceData, but I cannot be sure - that's why I suggest to inspect it in the debugger. If it is SequenceData, you will use the methods/properties it has to access the element(s) of the sequence (= array).

See also:
- opclabs.doc-that.com/files/onlinedocs/QuickOpc/Latest/User%2...ocessing%20generic%20data.html
- opclabs.doc-that.com/files/onlinedocs/QuickOpc/Latest/User%2...ml#Generic%20data%20kinds.html
- opclabs.doc-that.com/files/onlinedocs/QuickOpc/Latest/User%2...plex%20Data%20subscribing.html

Best regards

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

More
04 Oct 2022 14:51 - 04 Oct 2022 15:56 #11127 by amlynar
Hello,

I am trying to create a subscription to an array of array tag, The array has 101 elements and inside each of those elements is 4 other elements. The goal is that anytime a change is made in one of those elements inside the array, I want it to call a function named OnCamArrayChange(). I am using the SubscribeDataChange function. The subscription seems to work however on the console output I am receiving an invalid cast error. I have tried to cast UAGenericObject in the delegate and byte[ , ] but it keeps giving that same error. Below is the error I receive and I have included the code it calls.

Cast Error:
Exception thrown: 'System.InvalidCastException' in PacScreensRmg.exe
OpcLabs.EventTracing.LogEntries Error: 1 : LogEntry("OPCLabs-EasyUAClient",Error,0,101). CALLBACK ERROR. An exception of type "System.InvalidCastException" from source "PacScreensRmg" has occurred in callback 'System.EventHandler`1[OpcLabs.EasyOpc.UA.OperationModel.EasyUADataChangeNotificationEventArgs]'. The exception descend follows. (0) System.InvalidCastException PacScreensRmg (<.ctor>b__79_5) -> Unable to cast object of type 'OpcLabs.EasyOpc.UA.ComplexData.UAGenericObject[]' to type 'System.Byte[,]'.

Code:
- Gets called in a constructor within a class
 
camArrayHandle = OpcClient.Get().SubscribeToTag(Tags.GenV_Cam_Array, value => {
OnCamArrayChange();
});
 
 


- Within a created OpcClient class

 
public delegate void OnTagChanged(object Value);
 
public int SubscribeToTag(string nodeId, OnTagChanged onTagChanged)
{
        if (IsInitialized)
        {
        return easyUAClient1.SubscribeDataChange(Tags.ENDPOINT_DESCRIPTOR, nodeId, 1, (_, e) =>
        {
        if (e.AttributeData != null && e.AttributeData.HasValue)
        {
onTagChanged(e.AttributeData.Value;);
        }
        });
        }
        else return NO_BINDING;
}
 


Thank you
Last edit: 04 Oct 2022 15:56 by support. Reason: code formatting

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

Moderators: support
Time to create page: 0.113 seconds