LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there another way to index elements out of an array?

I am reading some 200 values from the modbus input registers but I want only some of them to be displayed on the front panel. 

girish_jadhav_0-1612265456650.png

If I do it this way, I will have to index each individual element separately. Is there way to mention the size of the array you want to read and the starting index, so that you get individual register values that can be displayed on FP?

 

0 Kudos
Message 1 of 6
(1,384 Views)

@girish_jadhav wrote:

If I do it this way, I will have to index each individual element separately. Is there way to mention the size of the array you want to read and the starting index, so that you get individual register values that can be displayed on FP?


There are several ways to do this. The right choice depends a great deal on how you want to do it.

 

What you describe (mention the size of the array you want to read and the starting index, so that you get individual register values that can be displayed on FP) is pretty much how you do it now. Of course the 2 array subsets are redundant, as you can simply increase the indices. Note that indices by default increment. So if the first element is 1, the next, if left unwired) is 2. The first one, if unwired, defaults to 0.

 

You could build an array of control references, and an array of indices. Then, for each index get the value and put it in the control by it's reference, using it's Value property.

 

You can also use the control\indicator label (or caption) to get the index. It's a string, so convert it to a number... If you 'encode' the text, you can get All Controls from the front panel, and check the label to be selective.

 

You can make a cluster, and cast the data to the cluster. But you'd need to make dummy elements for indices that are not used.  Then show the entire cluster... Or unbundle to each individual element. Or use variants and control references to set the values...

 

No method will be perfect. Using text introduces the risk of typo's. Clusters are clear, but still need work. Manual selection is a lot of work... You have options, but you'll need to give and take.

Message 2 of 6
(1,370 Views)

First question would be why read 86 registers when you only ever use 8 of them? And you have even bugs in there. Your relative humidity (SR) will always show 0 since you reference an invalid value in the array. You only copy 4 elements out but index element number 4 which is the 5th element in that array.

 

I would for this particular case start with one register later and only read 9 elements and simply read index 0, 1, 2, 3, 5, 6, 7, 8 out of the resulting array. There is absolutely no need to copy the two 4 byte subarrays out of the full array to index them further.

 

Another possibility would be to create a cluster that contains the different elements in the correct order and then Unflatten the array into this cluster.

 

As long as the index is incrementing by one, you do not need to wire the index value to the Index from Array function. Those indices will default to value 0 for the first element and then increase by one automatically for every element of the Index from Array function.

 

This is fully equivalent to your code (aside from the invalid index 4 you use):

Modbus Read.png

 

Rolf Kalbermatter
My Blog
Message 3 of 6
(1,367 Views)

You are already doing this using Array Subset, there is no better way.

However you don't need to wire every index: the first default index is 0, the default for the others are the previous index incremented by one; for example, if you wire the only the third index out of four with 5, the actual indexes will be 0, 1, 5, 6.

Also, in the upper part of your vi there is a mistake, because you are extracting a subarray with 4 elements only, so, 4 as an index is out of range and will always return the default U16 value (null).

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
Message 4 of 6
(1,354 Views)

Thank you the explanation. I am reading 86 because all the values are also written to a text file and out of 86, I want to display only some of the values. 

Yeah 4 (should have been 3) was a mistake. I didn't know that the indices are automatically incremented by 1! That saves a lot of time and your solution is perfect. But what if I values are spread wide across those 86 register values? Like after reading first 10, the next values that are needed to be displayed starts from address 40. 

 

@rolfk wrote:

Another possibility would be to create a cluster that contains the different elements in the correct order and then Unflatten the array into this cluster.


How can I do this? Can you give me an example?

0 Kudos
Message 5 of 6
(1,343 Views)

My own preference is for something like this, which Wiebe also suggested earlier:

 

Example_VI.png

 

In this case, "First register" lets the code know in which index of "registers" to find the relevant value for each control in the cluster.

 

The biggest advantage this has is that as you add values that you read, you don't need to modify the code to support them, just encode their values in a way that the code knows how to read (in this example I simply used the label as the register number, which might or might not work for you). You can also move the code to a subVI, handle different types, etc.

 

This is a highly simplified version, but it shows the basic idea.

 


___________________
Try to take over the world!
0 Kudos
Message 6 of 6
(1,307 Views)