LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

insert into cluster by String name

Solved!
Go to solution

Hello,

 

I am writing a Labview program to query .NET system Management Base and get connected device ( like USB CDC ) listed with corresponding Com Port, VID, PID, SN manufacturer name ect..

I got to the point where I am getting the strings I need and want to nicely organize them in a cluster array.

Here is my code, I wanted a better way of replacing the circled section of the code.

basicaly, I can programmatically get the cluster names , but when bundeling back the cluster, I can't figure out a way of doing it programmatically inside the while loop: looping on the number of elements of the cluster and placing the values in the right place.

Untitled.png

Is there any way of doing it ?

 

Thanks a lot

Amine

 

0 Kudos
Message 1 of 10
(6,435 Views)

Hi mam,

 

use a simple approach like using a case structure!

 

- In your loop you keep the cluster in a shift register.

- using the Label.Text string as selector of a case in a case structure you can call a case for each label. In the case use BundleByName to set the corresponding cluster element.

 

I always shiver when I see ArrayToCluster followed by Unbundle.

I shiver even more when ArrayToCluster is set to default size of 9 elements even when the programmer knows there are less elements in the array…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 10
(6,397 Views)

@GerdW wrote:

Hi mam,

 

use a simple approach like using a case structure!

 

- In your loop you keep the cluster in a shift register.

- using the Label.Text string as selector of a case in a case structure you can call a case for each label. In the case use BundleByName to set the corresponding cluster element.

 

I always shiver when I see ArrayToCluster followed by Unbundle.

I shiver even more when ArrayToCluster is set to default size of 9 elements even when the programmer knows there are less elements in the array…


It scares me, too, but I've had to grit my teeth and use it before.  Maybe you can come up with a solution for a case where you have a pure array that nees to go into a cluster?  Indexing the array seems just as dicey as ArrayToCluster as far as scalability goes.  (What happens if the cluster order changes, for instance?)  I would like to get rid of those nasty guys if at all possible.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 3 of 10
(6,378 Views)

I agree with Gerd that there are quite a few code constructs that make me shiver too. Another one is the use of "insert into array" with N wired to the index instead of "build array".

 

It is difficult to tell without seeing the actual code, because many things are not visible from a plain code picture. What is the structure of the cluster (e.g. if there are only three elements, you should not wire a 3 to N of the autoindexing FOR loop, leave it unwired!). Where is the cluster array coming from and where is it going?

 

If the cluster contains exactly three string elements (nothing else!) and they are ordered as in your "bundle by name" node, all you need to do is set the "array to cluster" output size to 3 and wire it into a build array node (top input wired to your cluster array and botton imput wired to the new cluster, output wired out the right sinde of the picture). No need fo any local variables. In fact if this entire code is just inside an outer FOR loop that is not shown, you should be autoindexing on the right loop boundary to create the array. No need for "building" or "inserting" or whatever. (All we see is [i] wired to "insert into array, so there has to be an outer loop! ;))

 

Can you attach the actual VI so we can give more targeted advice? Currently, we are flying mostly blind. 😄

 

0 Kudos
Message 4 of 10
(6,370 Views)
Solution
Accepted by topic author mam1nej

@altenbach wrote:

If the cluster contains exactly three string elements (nothing else!) and they are ordered as in your "bundle by name" node, all you need to do is set the "array to cluster" output size to 3 and wire it into a build array node (top input wired to your cluster array and botton imput wired to the new cluster, output wired out the right side of the picture).


Here is a simplified illustration that just sets to value to the label. (top image). Since labels are set at compile time, maybe all you need is get the names once at the start of the program and then use a plain string array instead.

 

Another possibility would be to use the reference to the element to set the value inside the loop, then get the final cluster value afterwards (bottom of image).

 

(There are simplified examples. All you need is substitute the code that gets your specific new string instead.)

 

0 Kudos
Message 5 of 10
(6,354 Views)

I didn't reply earlier because I didn't understand the problem.  Now, having seen some of the responses by Altenbach and others, I think I maybe "get it".  Unfortunately, I can't easily "show" my response since I don't have your base code to use as a prototype (please, please, please, do not attach pictures, but attach code, either as entire VIs or as Snippets, segments of code saved as PNGs that can be placed into a LabVIEW Block Diagram and reconstituted as code -- you'll find "Create VI Snippet from Selection" on the Edit Menu of the Block Diagram).

 

The code fragment you have, I think, is almost right (except for the part after the FOR loop).  Once you have the String Value of the ManagementBaseObject whose name matches the Cluster Label, simply wire it to the Control Value, as Altenbach shows you, inside the For loop.  Also, assuming that your Cluster has three elements, don't wire a 3 to the N indicator of the For loop -- let Array Autoindexing do the work for (no pun intended) you.

 

Bob Schor

0 Kudos
Message 6 of 10
(6,310 Views)

Dear All,

 

First of all, thank you very much for helping out. Although I don't have access to Labview at home, I am pretty sure , , and suggestions covers what I needed to acheive.

Sorry for not posting the code in my early post, I though the image was enough.

I will post a snippet I am working on when I finish implementing your suggestions. I think it will be useful for many people that are trying to do the same thing : having their VI automatically/VISA resource select the COM: port by VID/PID/Manufacturer ect.

 

Thanks again

Best regards,

Amine

 

0 Kudos
Message 7 of 10
(6,288 Views)

altenbach wrote::
Since labels are set at compile time, maybe all you need is get the names once at the start of the program and then use a plain string array instead.

 I would probably just define the strings using a diagram constant. It seems like a complicated detour to use the cluster element labels to have critical information.

 

What if somebody translates the UI and changes the labels randomly? --> the VI will stop to work correctly!

What if you want to encapsulate the lower code part into a subVI (What you should do anyway!)? -> you need to deal with explicit references or duplicate the cluster/typedef to the subVI.

What if one if the labels is misspelled -> you need to correct the cluster and should also correct the cluster of the string array input and output, etc. Herding cats!

 

Using a plain string array is more direct and leads to simpler, more self-documenting, and more easily maintainable code. Put it all into a subVI and there is exactly one place where the strings need to be changed if needed.

 

0 Kudos
Message 8 of 10
(6,277 Views)

Hello there, I know it's an old post but I'd really like to learn how you did this: 

"writing a Labview program to query .NET system Management Base and get connected device ( like USB CDC ) listed with corresponding Com Port, VID, PID, SN manufacturer name ect..". That's exactly what I've been trying to figure out...Do you still have the code and can you share how you did that? Thanks very much.

0 Kudos
Message 9 of 10
(3,564 Views)

Hello,

 

I remember this issue, I actually ended up building my own DLL to interface with the Setupapi library functions.  these methods allow you to query certain register keys and retrieve the Hardware IDs ( VID/PID ) and the friendlyName that contains the Com port number in case of CDC Class  devices. 
You need to make several calls to different methods involving complex data structures in each call, It was much easier for me to do it in C++ than in Labview. 
let me know if you need help with that in c++ and I might be able to send you an example code.

 

good luck

Amine

 

Message 10 of 10
(3,538 Views)