11-27-2012 03:18 AM
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
Solved! Go to Solution.
11-27-2012 03:48 AM
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.
11-27-2012 03:57 AM
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
11-27-2012 04:01 AM
I would first find out if the vendor has a LabVIEW compatible driver. Otherwise, it will have to be tailor made.
11-27-2012 05:37 AM
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?
11-27-2012 08:30 AM
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
11-27-2012 09:09 AM - edited 11-27-2012 09:09 AM
I look to this before, didn't help, but thanks.
11-27-2012 10:56 PM
All I can suggest at this point is perhaps you can find a work mate who can help with the "C" side of things.
11-28-2012 12:43 AM
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.
11-28-2012 01:40 AM
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.