LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

vc++ dll with multiple data types

Solved!
Go to solution

Ok so now the cluster is able to identify multiple data types. What if there we're two structures inside of a main structure, would the main cluster have two sub clusters in labview with different data types?

 

i.e.

 

typedef struct
 { 
  double dD1;
  double dD2;
  int I1;
 } Cluster_1;

 

typedef struct
 {
  int D;
  int E;
  int F;
 } Cluster_2;

 

struct ParentStructure
{
 Cluster_1 *pCluster_1;
 Cluster_2 *pCluster_2;
};

 

 

0 Kudos
Message 11 of 27
(1,711 Views)

I was able to get the two structures to display the variables using two clusters. Now the question is if i have an array in one of the structures, how do i get that to work in Labview?

 

typedef struct
 { 
  double dD1[10];
  double dD2;
  int I1;
 } Cluster_1;

 

typedef struct
 {
  int D;
  int E;
  int F;
 } Cluster_2;

 

struct ParentStructure
{
 Cluster_1 pCluster_1;
 Cluster_2 pCluster_2;
};

 

 

 _declspec(dllexport) int _cdecl AOS_GetData_George(void *dataCluster)
{
 parentStructure = (ParentStructure *) dataCluster;
 parentStructure->pCluster_1.dD1[0] = 1.0;
 //parentStructure->pCluster_1.dD1[1] = 1.1;
 //parentStructure->pCluster_1.dD1[3] = 1.3;
 //parentStructure->pCluster_1.dD1[5] = 1.5;
 //parentStructure->pCluster_1.dD1[7] = 1.7;
 parentStructure->pCluster_1.dD2 = 2.2;
 parentStructure->pCluster_1.I1 = 3;
 parentStructure->pCluster_2.D = 4;
 parentStructure->pCluster_2.E = 5;
 parentStructure->pCluster_2.F = 6;
  
 return 1;
}

 

 

0 Kudos
Message 12 of 27
(1,700 Views)
Now you're getting complicated. When you start dealing with arrays inside of clusters you need to start playing a few games. The safest way to do this is to use a wrapper DLL. However, you can usually get this to work right from LabVIEW, as long as the structures are not too complicated. You should open the Example Finder (Help -> Find Examples) and search for "DLL". There's an example called "Call DLL". Run it, and scroll down the list to "Simple Cluster String & Array". This deals with having a cluster that has a DBL, a string, and an array of integers. As you can see, you have to convert data structures since LabVIEW stores strings and arrays differently than C. In your case you would not have the string, and the array would be an array of floats. Note that the constant of 50 would be 10 in your case.
0 Kudos
Message 13 of 27
(1,694 Views)
I was able to get one array of any data type to work inside the cluster. Do you know of any ways to get multiple arrays with different data types to work inside a cluster?  
0 Kudos
Message 14 of 27
(1,679 Views)
You would need to follow the same basic principle, but you'd need to be careful about byte packing. This may or may or may not have been done with the DLL you're using. Do a search on this forum on clusters and DLLs and byte packing/boundary.
0 Kudos
Message 15 of 27
(1,675 Views)

ghatz85 wrote:
I was able to get one array of any data type to work inside the cluster. Do you know of any ways to get multiple arrays with different data types to work inside a cluster?  

You need to distinguish between fixed size arrays inside a structure and variable sized arrays. Fixed size arrays have a fixed dimensional size specified such as

 

struct {

.....

type name[10];

.....

} StructName;

 

and are inlined into the structure. In LabVIEW you can represent such arrays by creating a cluster which contains the specified number of elements (obviously only practical for arrays and strings which are not to long as you do not want to create clusters containing 100ds of elements).

 

However variable sized arrays and strings are put into a structure by the C compiler only as a pointer and look like this:

 

struct {

.....

type *name;

.....

} StructName;

 

These arrays (pointers) can not be easily represented by LabVIEW since LabVIEW does not really know anything equivalent to C pointers on diagram level. And LabVIEW arrays and strings are completely different than C arrays (or strings which are really just special arrays). So there is no way to easily embed variable sized arrays or strings into a structure on the LabVIEW diagram level to pass to a Call Library Function.

Message Edited by rolfk on 01-28-2010 10:45 PM
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 16 of 27
(1,668 Views)

For right now, i'm working with fixed sized arrays.

 

typedef struct
 {
  double T1;
  float T3[3];
  int T2[3];
 } Test_array;
 

struct MainTestArray
{
 Test_array pTest_array;
};

 

 

_declspec(dllexport) int _cdecl AOS_GetData_Array_GH(void *testArray)
{
 maintestArray = (MainTestArray *) testArray;
 maintestArray->pTest_array.T1 = 1.11;
 maintestArray->pTest_array.T3[0] = 2.5;
 maintestArray->pTest_array.T3[1] = 2.6;
 maintestArray->pTest_array.T2[0]=36;
 maintestArray->pTest_array.T2[1]=2;

 return 1;
}

 

 

I'm not too sure I completely understand your explanation of how to get multiple arrays to work. Right now I have a cluster with a numeric indicator (double), an array with three numeric indicators (sgl) and array with five numeric indicators (I32). The double and the floats show up but then the array of ints show up as zeros. Am I missing something?

 

Sorry like I said, I'm still new with Labview.

 

The main file that I'm working with is under: GH Test DLL Folder\Read-Write DLL\GH Test DLL modified.vi

 

 

0 Kudos
Message 17 of 27
(1,656 Views)

ghatz85 wrote:

I'm not too sure I completely understand your explanation of how to get multiple arrays to work. Right now I have a cluster with a numeric indicator (double), an array with three numeric indicators (sgl) and array with five numeric indicators (I32). The double and the floats show up but then the array of ints show up as zeros. Am I missing something?


Rolf's comments were more generic. In your case you have fixed-size arrays, so you can use clusters as shown in the "Call DLL" example. What's basically being done is that the data stored in the arrays is being "repackaged" so that it would be located in memory in the same way that C expects it to be. Because an Array to Cluster function is being used, this means there's a limit to this method. The Array to Cluster function is limited to 256 elements. Thus, you cannot deal with fixed-sized arrays larger than that.  If you're dealing with variable-sized arrays you can't use that method in the first place because LabVIEW does not store arrays in memory the same way as C. 

 

As to your code: I believe you are referring to the  Array Test VI, since that's the one that calls the AOS_GetData_Array_GH function. You are not following what the "Call DLL" example does. I have no idea why you're interleaving the floats and the integers. The Reshape Array function is first used to make sure you have an array that's the same size as the fixed-size array in the target structure. Thus, in your case both arrays would be "reshaped" to 3 since that's the size as defined in the structure. This is then converted to a cluster. This is the "repackaging" part. Each element of the cluster is then bundled into a new cluster. This makes sure that the bytes are in the expected order. This new cluster is what the DLL will receive. Please look at the "Call DLL" example in more detail. 

0 Kudos
Message 18 of 27
(1,632 Views)

smercurio_fc wrote:

The Reshape Array function is first used to make sure you have an array that's the same size as the fixed-size array in the target structure. Thus, in your case both arrays would be "reshaped" to 3 since that's the size as defined in the structure. This is then converted to a cluster. This is the "repackaging" part.


Haven't looked at the code but I don't think reshape Array is necessary. The Array To Cluster node has a right click popup dialog that defines the element size of the clusterindependant of the actual size of the incoming array.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 19 of 27
(1,628 Views)

Guys,

 

Thanks for the help again. It finally works. I attached a jpeg with the solution that works. The key was to specify how many elements were in each array which is done by right clicking on the array to cluster function (thanks Rolf).

 

 

0 Kudos
Message 20 of 27
(1,623 Views)