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.

Using State object for local variables

More
25 Aug 2012 12:26 #1000 by support
R.,

I think that the main issue here is that the State argument itself cannot be changed, once it is passed to the SubscribeXXXX method. This is by design.
What happens with the TextBox etc. is that you can change the contents of it, i.e. a property of that text object, but you still cannot (in the event handler) exchange the text box for a different object.
So, when you pass in a ‘bool’, this is not a reference type but rather an immutable value type, the behavior is the same: You cannot change it (and it has no properties either).

This can be overcome, though: The customer can create a simple class that will just hold the variable, something like:

class BoolVariable
{
public bool Value { get; set; }
}

Then create an instance of it before the SubscribeXXXX call:

var bool1 = new BoolVariable();

and then pass Bool1 as State to SubscribeXXXX.

Accessing the value will require to use constructs like bool1.Value instead of just bool1.
In the event handler, the value could be changed by

((BoolVariable)e.State).Value = (bool)e.vtq.Value;

Separate classes will be needed for other data types.
In order to handle more data types than just bool, the event handler will have to recognize which object is in the State (e.g. by using the ‘is’ operator test), or, you can Subscribe separately for each data types, and use a callback method that’ll handle just State-s of that particular data type, different with each Subscribe.

I haven’t tried any of this actually, but it should work; of you run into problems, let me know. The class can even be made into a generic:

class Variable
{
public T Value { get; set; }
}

And then the code would use Variable, Variable etc.
It might also be possible to cheat a little and use Nullable class from .NET framework that originally has a different purpose but could serve as well.

I should say, however, that without further coding, this approach has an inherent synchronization problem, and to make it right, there should probably also be some locking code added.

Version 5.20 will support “mapping” of OPC items to properties and fields on .NET objects. The customer will then be able to declare an object holding his variables, set up the mapping to OPC items, start the subscription, and the properties will be changed automatically, without writing an event handler at all.

Best regards,

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

More
25 Aug 2012 12:25 #999 by support
From: R.
Sent: Friday, August 24, 2012 9:34 PM
To: Zbynek Zahradnik
Subject: Using State object for local variables

Hello Zbynek,

I have a customer who is using [QuickOPC.NET] in C# application. We are wondering if it is possible to use the state object for local variables instead of form control objects.

For example, this customer may have 10 values that are coming back as Booleans and another 10 that are barcodes as sting objects. So in his code his has variables such as Bool1, bool2, bool3, barcode1, barcode2, etc.

He wants to be able to pass in those variables (bool1, bool2, etc) into the state property of a subscription such as:

opcGroup[0] = new OpcLabs.EasyOpc.DataAccess.DAItemGroupArguments("127.0.0.1", "SWToolbox.TOPServer.V5", "Channel1.Device1.Tag1", 100, Bool1);


then in the callback assign the updated item’s value to the state object, such as:

e.State = e.vtq.Value;

I was thinking that this would allow him to have much cleaner code rather than having to create an if..then or case statement to match up the item ID with a variable on every callback.

I haven’t been successful in making this work. If I were to replace Bool1 with a textbox control on the form, it works fine. What’s the difference between passing in a form control as the state object and a variable? Is there any work around to make this work?

Please let me know if my description of the problem doesn’t make sense.

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

Moderators: support
Time to create page: 0.054 seconds