LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

dll struct to LabVIEW cluster

Solved!
Go to solution

Hello. Currently I am trying to read the data writen in dll struct and see that data in LabVIEW cluster, as a result, I would like to use that data in my futher project. But I am facing a problem. The main problem is, that I can read data, but that data is incorrect. 

 

Here is the dll structure:

typedef struct PHAROS_MOTOR_CONTROLLER_state_tag
{
SYSTEMTIME TimeStamp;
unsigned char MotorControllerState;
unsigned long MotorPosition;
unsigned short MotorCurrent;

unsigned int AmbientHumidity;

float Humidity;
float Temperature;
float DewPoint;

float Detector_2Ph;

} PHAROS_MOTOR_CONTROLLER_state;

 

 

This is the function, that I am trying to call by using Call Library Function Node:

bool PHAROS_API_Motor_Get_State(PHAROS_MOTOR_CONTROLLER_state* state)

Returns true and fills state structure with the last received state information of Motor controller or
returns false if no state messages were received or timeout interval passed since last reception.
This function returns immediately.

 

So, maybe You have any suggestions, how to recive correct data to cluster?

Thank you in advance!

 

Simas

 

 

 

 

0 Kudos
Message 1 of 17
(10,089 Views)

The main problem here is that this DLL function is returning a pointer to the structure you show in the typedef but the memory for the data was allocated in the DLL and not in LabVIEW. I found this to be very unreliable and do not recommend it. You are better off writing a "C" wrapper DLL (like a middleman) that will take in preallocated memory from LabVIEW, and fill in that memory with the structure (via pointer) returned from the vendor DLL.

 

Either that or call the vendor and ask if they have a LabVIEW driver, i.e., VXI PlugNPlay.

0 Kudos
Message 2 of 17
(10,079 Views)

Thanks for you answer.

 

Where I could get a wrapper in C language? Because I don't have any experience in writing DLL in C language

0 Kudos
Message 3 of 17
(10,076 Views)

I would first find out if the vendor has a LabVIEW compatible driver. Otherwise, it will have to be tailor made.

0 Kudos
Message 4 of 17
(10,060 Views)

I have recived an email from vendor and he said that they don't have LabVIEW compatible driver 😕

 

So I would like to do it in tailor made. Any suggestions or help?

0 Kudos
Message 5 of 17
(10,050 Views)

You can have a look at the dll calling example that ships with labview. The 2011 example is located here:

C:\Program Files\National Instruments\LabVIEW 2011\examples\dll\data passing\Call Native Code.llb\Call DLL.vi


CLA CTAChampionI'm attending the GLA Summit!
Subscribe to the Test Automation user group: UK Test Automation Group
0 Kudos
Message 6 of 17
(10,039 Views)

I look to this before, didn't help, but thanks.

 

 

0 Kudos
Message 7 of 17
(10,035 Views)

All I can suggest at this point is perhaps you can find a work mate who can help with the "C" side of things.

0 Kudos
Message 8 of 17
(10,012 Views)
Solution
Accepted by topic author SimasLTU

This is fairly easy to do, and has been asked repeatedly on this forum.  Despite BillMe's comments, there is no need to write any sort of wrapper in C.  All you need to do is create a cluster in LabVIEW that duplicates the layout of the C struct.  When you configure the call library function node, configure it as "Adapt to type" and wire the cluster to the input for that parameter.  It will automatically be passed by reference (as a pointer).  The output side of that parameter will have your data.  You may need to add some padding elements to the cluster - struct elements in C are normally aligned, whereas LabVIEW clusters are packed - but once you get this right, it will work.  You'll need to replace the SYSTEMTIME item with an equivalent cluster inside the larger cluster (a search for SYSTEMTIME will show you that it's simply 8 2-byte values).

 

You should check if the header file includes a #pragma pack() line, which will indicate if the struct is aligned in some non-standard way.  If it follows standard packing, you'll need to add 3 bytes of padding after MotorControllerState and 2 bytes after MotorCurrent.  I'm assuming here that for your DLL "int" and "long" are equivalent, even though MotorPosition is defined as long and AmbientHumidity is defined as int.

 

If you're able to retrieve some data, but it's incorrect data, most likely it's because you're missing the padding.  If you can't figure out where the padding is necessary, or cannot get it to work, please attach your code showing what you're trying to do.

Message 9 of 17
(10,006 Views)

nathand is correct and I apologize for reading the function call incorrectly and jumping to the wrong conclusion. Thank goodness for oversight on the part of the discussion forum members.

0 Kudos
Message 10 of 17
(10,002 Views)