LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Getting the Sizeof Clusters in LabVIEW

Thanks again for all your help NathanD.  

 

As far as the variable data length issue you raise, my plan is to set the cluster control strings and arrays to values whose lengths correspond to the C structure definition. Then I'll pass a reference to that cluster to my elusive generic cluster filler from byte array VI, aka GCFFBA.vi. So LabVIEW will know the length of all the data types - they will be set in the cluster whose reference I will pass to the GCFFBA.vi

 

I will be using the cluster typedef to display strings to the user, so isn't acceptable to show it as a string subcluster.  It needs to be in easily readable form.

 

My dream is that GCFFBA.vi will be able to fill any current or future cluster without modifiying or creating any additional VIs, by just modifiying or creating the cluster typedef.   

 

I am thinking more about what else would have to be done to handle programmatically filling cluster values of various data types from a byte array.  It will be complex and may require recursion.  If and when I get a VI together that does this part of the job, I'll post a PNG of the block diagram.

 

In the meantime, does anyone know how to get the size of a cluster in LabVIEW assuming all its strings and arrays are initialized to fixed sizes?

 

Thanks,

Hans 

 

 

 

.

 

 

0 Kudos
Message 11 of 21
(2,917 Views)

Hi Hans,

in the attached vi, you can see what i meant in  my first post. I think with this you can get enough information to do what you want.

 

Hope it helps.

Mike

0 Kudos
Message 12 of 21
(2,898 Views)

Thank you MikeS.  Your VI gave me a couple insights that will indeed help.

 

Insight 1 - Variant to Flattened String puts out a Type Descriptor array as well as the flattened string.  Somehow I had missed that.  I had seen the Type Descriptor explanation in the LabVIEW help, but thought that info was embedded in the flattened string.

 

Insight 2 - The least significant byte of the second element of the Type Descriptor array is an enumerated value that gives the data type of a control.  That will be useful in making an easily readable case structure to switch on the data type and use the appropriate method to get the size of that data type.  It looks like the size info will be available in different bytes of the flattened string for different data types.

 

I'm confident now that I'll be able to acheive my dream VI, but it may be a while before I work on it because the project that I will use it in has been moved to a lower priority.  I'll post when I make more progress.

 

Thanks again,

Hans

Message 13 of 21
(2,847 Views)

I use this to get length of any structure. Works so far.

 

Length.jpg 

Message 14 of 21
(2,712 Views)

I like the idea of least effort, but when I tried that diagram, it returned a length of 21 when the actual cluster size is 5 bytes for the string + 4 for the I32 + 8 for the double = 17.

 

Hans 

0 Kudos
Message 15 of 21
(2,672 Views)
The string is 5 bytes plus 4 bytes to tell the length of the string.  (5+4) + 4 + 8 =21
Message 16 of 21
(2,672 Views)
That does seem to work.  If I add more strings to the cluster, I get the right number of bytes when I subtract 4 bytes for each string.  Thanks guys!
0 Kudos
Message 17 of 21
(2,666 Views)

Idea wrote:

I use this to get length of any structure. Works so far.

 

Length.jpg 


 

I believe you can simplify this by using flatten to string directly on the cluster. Also, you should note that some languages (e.g. Japanese) use more than one byte per character. I believe string length returns the number of bytes anyway, but you would want to make sure.

___________________
Try to take over the world!
0 Kudos
Message 18 of 21
(2,658 Views)

nathand wrote:

I think you are making this more complicated than necessary.  If you are willing to accept fixed-size data in your LabVIEW data types (as you are in your C program by declaring the size in the header file), you don't need any cluster-filling VI at all - you can use LabVIEW's typecast to directly convert your byte array into LabVIEW data.  You can build up your initial fixed-sized cluster using "array to cluster" so that if your C header changes you can just right-click once and update the cluster size.  If you have not already done so, take a look in the LabVIEW help for "How LabVIEW stores data in memory."

 

As far as I know, unlike LabVIEW, C provides no way to determine the length of an array even when the length is specifically declared.  The only way to get a string length in C is to iterate through each character until a null is found, regardless of the length that may have been declared for the string.  LabVIEW is able to provide string lengths and array dimensions because it stores that information as part of the data type; C treats an array as a pointer plus an offset without setting any limit on that offset.


Actually all modern C compilers should return the maximum size of all fixed size data resulting in the value 100 for following sizeof():

 

char test[100;

int length = sizeof(test);

 

The result will be always 100 independant of the actual string being present or not in the fixed size string variable test.

 

Rolf Kalbermatter

Rolf Kalbermatter
My Blog
0 Kudos
Message 19 of 21
(2,616 Views)

There is a much simpler way to accomplish this providing your input is a defined cluster (as opposed to dynamically built). Pass a reference to the cluster, rather than wiring the cluster to a variant. Now you can use property nodes to access the cluster. See attached example. Note this will not work for a LabVIEW class.

 

0 Kudos
Message 20 of 21
(1,168 Views)