Jump to content

Using polymorphic VI as data selector for a Var_Tag array data type


Recommended Posts

Posted

Hi all,

In the efforts to Have a clean data wire in my app I am suing a data wire such as this:

image.png.722b961161636fa9c71ea0eaf61505a6.png

I Have a VI that reads and gets and reset the data on the wire and am using the RefNum for manipulating or storing the data on the wire meaning I only have a reference wire passed through my multiple loops.

To get variant and translate it into my data I decided to use a polymorphic VI and use the type as a data type selector for my convertor:

image.png.60eb04bb10c364648889a28ef4b1546b.png  image.png.46259aa7e4cc8a273e35ae20a4bda800.png

 

Is this proper or there is better or more efficient way?

Cheers,
M

Posted

How do you look up the data in your array? Via --Tag?   If you are going to do that you might as well just use a map.

But anyway, I am not a huge fan of this approach. I think better composition into clusters or objects will also make your wiring neater, more testable, scalable etc

I do recognise the need for nice neat diagrams without wires going all over the place, but proper decomposition/architecture normally fixes this.

So in general I would have to say you are not on the right track with this approach, but this is of course just my opinion.

Posted

I must admit that I may have misunderstood what the OP wants to do, hence my answer may have been completely off. Is he rather trying to construct an array of heterogeneous elements, by flattening all his possible data types into a variant?

Posted

Thank you, Neil and Enserge,

I agree with you; however, while preparing my app, I realised that every time I needed to add a new function, I had to change data types or add a new cluster, which led to VIs breaking. This was the only solution I could think of to be compatible with every wire time in the terminals.

 

Cheers

Posted
On 7/10/2025 at 8:44 AM, Mahbod Morshedi said:

 

image.png.60eb04bb10c364648889a28ef4b1546b.png  image.png.46259aa7e4cc8a273e35ae20a4bda800.png

Looking at the example of the OP, it looks he's already aware of typedefs. To him to say what exactly he meant.

Posted

Maybe the OP is trying to achieve a generic storage container (functional global of sort) using a variant. 

Something akin to this:

image.png.bd76eecfca5b6532c29ecd151e623c46.png

Here I am using a typedef enum (Variable) to ask for a specific item, which is stored as an attribute of a single variant.

There is no  need to use a DVR to avoid copies of the variant (if that is the concern), as the only thing which is needed is a copy of the accessor VI (shown above) wherever a variable is needed.

There is no need to bother with a polymorphic VI as the output variant is of the requested type and will easily convert to the right type of wire with the drop of Variant to Data primitive, as long as the resulting wire is connected to a terminal. If not (for instance it is connected to a numeric primitive), then yes a polymorphic wrapper VI might be neat (but still a lot of work).

When adding a new attribute, simply modify the Variable typedef and add a case. No VI should break.

The only drawback of this approach is that that VI has to be non-reentrant and therefore, wherever it is used, this will interfere with parallelism.

This can be to some extent avoided with DVR read action, which might be the reason the OP chose that approach.

My 2 cts.

Posted

Maybe the question is if you have to be committed to this array of clusters of string+variant for some specific reason, or if it is an attempt to implement some architecture which might have a different implementation. I.e., I don't understand if yours is a general question about design, or a specific question about the mechanics of converting variants to data.

Posted
12 hours ago, Mahbod Morshedi said:

this is the VI I am using. Maybe it will be more clear this way.

Data Tag.vi 30.98 kB · 4 downloads

You are reinventing variant attributes and the corresponding primitives (see my post above).

This being said, there is a lot of things to be commented on your code from a G-style point of view :-) but I'll leave that to others.

As far as wrapping the Variant to Data primitive into a polymorphic VI, see my comment above as well. A nicety, but is this really worth the hassle?

I have done that myself for a few common types (numerics, string, path, and array of such) and I find that very handy, but I wouldn't bother for types I use occasionally, and in particular, application-specific types. Just my opinion.

Posted (edited)
3 hours ago, X___ said:

You are reinventing variant attributes and the corresponding primitives (see my post above).

This being said, there is a lot of things to be commented on your code from a G-style point of view :-) but I'll leave that to others.

As far as wrapping the Variant to Data primitive into a polymorphic VI, see my comment above as well. A nicety, but is this really worth the hassle?

I have done that myself for a few common types (numerics, string, path, and array of such) and I find that very handy, but I wouldn't bother for types I use occasionally, and in particular, application-specific types. Just my opinion.

yup, completely agree with this ^^^^

I have done something similar in the past where I used a global variant as a data store, and then used attributes to store/get data (and some convenience VIs to convert to the known types). It was a bit of an experiment in a system that had many data generators (different interfaces to independent pieces of hardware) and several hundreds of data variables (tags) all communicated over OPC-UA, so had a fairly sophisticated method to set/get by name as I didn't want to deal with such a mega-cluster anywhere. It worked fine, but in the end I would not really recommend it for simple systems where you just dont want to make the proper data types.

Edited by Neil Pate

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.