From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calling a C Struct from a LV CallLibrary Function.

I have a DLL called Canopen.dll.

 

I have an example C-Code on how to use the functions in the DLL.

 

Have been able to call a few of the functions in LV using the Call Library Function.

 

But one function is a problem as its arguments are from a Struct. It goes like this:

 

Function Call
short CMA_ConfigCANChannel(
unsigned char usChannelHandle,
CMA_CAN_CONFIG stCANConfig);


Parameter List


typedef struct
{
unsigned char ucBaudrate;
unsigned char ucPresc;
unsigned char ucSjw;
unsigned char ucTSeg1;
unsigned char ucTSeg2;
unsigned char ucAccCode;
unsigned char ucAccMask;
} CMA_CAN_CONFIG;

 

Now how to convert this to be suitable fro CallLibrary Function ?? (  Actually there is one example in the NI site but that is in LV5.0 and my LV2009 cannot open it !!)

 

Thanks

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 1 of 36
(3,767 Views)

Create a cluster consisting of U8 integers. You will need to use 8 of them since the struct has 7 and you will need to make sure you account for alignment to memory locations. You can then just use "Adapt to Type" for the parameter type. LabVIEW comes with a very detailed example on calling DLLs. Open the Example Finder and search for "DLL".

0 Kudos
Message 2 of 36
(3,757 Views)

Hi,

 

I'm not sure but I suggest you use a cluster (a LabVIEW Cluster is equivalent to a C Struct).

 

So in your case, you may pass a 7 item cluster to your "call library function node" (cluster containing ucBaudrate, ucPresc, etc...).

 

Hope this helps.

 

Best,

 

J.

0 Kudos
Message 3 of 36
(3,756 Views)

Oups, smercurio answered before I post my answer. I think it's explanation is most complete than mine. 🙂

0 Kudos
Message 4 of 36
(3,754 Views)

Unfortunately I think smercurio's answer is incorrect in this case.  Rolfk answered this question here.  To pass a struct by value, create one parameter for each element of the struct.

0 Kudos
Message 5 of 36
(3,741 Views)

You know, I didn't even see it was being passed by value. Guess it's habit for me to always see it passed by reference.

0 Kudos
Message 6 of 36
(3,737 Views)

 

How to handle int array  and string array inside a C structure to call it from LV call library function?

 

Function:

short CMA_GetVersion( unsigned char ucChannelHandle, CMA_VersionStruct *stpVersion);

 

C Structure:
typedef struct
{
unsigned long ulHWSerialNumber;
unsigned char ucHWMainRevision;
unsigned char ucHWSubRevision;
unsigned char ucHWIdentString[18];
unsigned char ucSWMajorVersion;
unsigned char ucSWMinorVersion;
unsigned char ucSWBuild;
} CMA_VersionStruct;

 

 

Function:

short CMA_InstallPDO_E( unsigned char ucChannelHandle, CMA_PDOConfig, unsigned short *uspPDOHandle);

 

C Structure:
typedef struct
{
unsigned char ucPDOType;
unsigned long ulCOBID;
unsigned char ucTransType;
unsigned char ucAccType;
unsigned char ucEventNotif;
unsigned char ucaDefVal[8];
unsigned short usEventTimer;

unsigned char ucNrOfByte;

unsigned char ucNoInitTrans;
} CMA_PDOConfig;

 

Hope someone helps.

 

Thanks

 

Kousy

0 Kudos
Message 7 of 36
(3,712 Views)

This has been asked over and over again on this forum.  You need to replace the array in the C struct with a LabVIEW cluster containing the same number of elements.  For example, you would replace ucHWIdentString[18] with a cluster of 18 U8 values.  The easiest way to generate such a cluster is by wiring an empty array of U8 to "Array to Cluster" and setting the number of cluster elements appropriately.  Create a control or constant as necessary from the output of Array to Cluster.  To get a string into that structure, you can use String to Array of U8, followed by Array to Cluster.  The reverse operation will get the you the string out of that structure.

0 Kudos
Message 8 of 36
(3,698 Views)

nathand,

 

I have tried Adapt to Type and Handle by value for the above mentioned functions. Still Im getting LV Error 1097 when trying out GetVersion function call and Functional Error of -110 when trying out InstallPDO_E. Im attaching the VI. Can you please look into it?

 

 

Thanks

 

Kousy

0 Kudos
Message 9 of 36
(3,684 Views)

Based on the random clusters floating around your block diagram, it looks like you tried everything except the one thing I suggested.  Create a cluster that matches your C structure, and replace the fixed-size arrays with equivalent clusters.  For the CMA_VersionStruct, that means that ucHWIdentString should be replaced with a cluster containing 18 U8 values (U8 is the same size as char).  Then pass the entire CMA_VersionStruct cluster as a single parameter set to Adapt to Type (the Data Format doesn't matter), because you want to pass it by reference, not by value.

0 Kudos
Message 10 of 36
(3,669 Views)