LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Programatically create Cluster for C-DLL Call

Hi,

I am trying to get data from a function within a C-DLL. The output of that function is a struct, in fact multiple structs concatenated, where each struct corresponds to one device within my network.

I created a matching cluster in LabVIEW and getting the data of one struct works perfectly fine (I set the Datatype when calling this function to "adapt to type", see attachment).

Now the problem is when there are multiple units, so I need to get the data from multiple structs. I tried multiple methods to create an input type that works and found two:

1) Create one cluster with multiple of those "struct-like-cluster" in it

2) Create one long(er) cluster with as many elements as needed

In the attachment are Screenshots of the (simplified) clusters to get the data of 2 structs

 

Now the questions is how could I create those programatically? There can be any number of devices in my network so I can`t just use a static cluster. I googled for some time but it seems that the size of a Cluster is always fixed, but I somehow can`t believe that LabVIEW it is not able to create a cluster programatically.

 

Any suggestions are more than welcome, maybe there is also a solution that does not use "adapt to type" when calling the DLL-function, as long as I finally can get it to work I am open to anything

 

All the best,

Daniel

 

Download All
0 Kudos
Message 1 of 3
(2,457 Views)

Yes, modifying cluster type is change of the code, it is not done in run-time. Technically it is possible to create cluster typedef with scripting, use it in dll load vi, get data from dll, convert into array of instrument clusters. 

 

But if there is variable number of instruments, data size should be variable.

Can you

1) supply a string into dll with preset length (9*N),

2) split output into parts (2*I32+1 bool = 9 bytes),

3) typecast each part into cluster = corresponding instrument.

0 Kudos
Message 2 of 3
(2,398 Views)

Just as Alexander said. The clusters are pretty simple and simply passing a byte array of n * 9 bytes to the DLL and after the call parsing this byte array 9 bytes a time and converting it back into the individual elements isn't that much of work really.

Alternatively you can also Unflatten it into an array of these clusters.

 

But there is also another solution in this specific case. You can leave the parameter as Adapt to Type and configure the Data Format as Array Data Pointer. Then connect an array of these clusters to the terminal. Make sure to resize this array properly to the required number of elements before its wired to the left hand terminal on the CLN or you are creating a buffer overflow error when the DLL tries to write beyond the end of the array that you passed to it.

My preferred way is to use an explicit Initialize Array on the diagram as that makes it glaringly obvious to a code reviewer that there has been spent some time into thinking about this issue, and allows to easily check that the array is big enough.

 

But this will absolutely only work if your cluster is itself fully flat, that means, it does not include any array, string or other variable sized data element.

 

And there can be an alignment issue in these cases. Normally a C compiler would align structure elements to the smaller of either its element size or a specific default alignment, which usually is 8 bytes nowadays. But since it seemed to have worked properly with your two clusters inside the bigger cluster, it seems this DLL was compiled with byte packing. So it should work. But watch out for this anyways. If you end up reading strange values in all but the first cluster you might have to actually pad the cluster with 3 extra bytes at the end to make the next int32 in the following cluster land on its own 4 byte alignment.

Rolf Kalbermatter
My Blog
0 Kudos
Message 3 of 3
(2,391 Views)