01-11-2012 05:17 PM
Firstly I d like to say hello, im the very beginier to labview thus I appeal for some patience.
There were many threads dealing with that issue, I tried to get information from each one of them.
and I posted this mesaage in order to make sure if i `d got it well.
Well I ve got hardware and want to gather data from it, (its not array but 3 (variables?) and of course it has its own dll.
What am i supposed to do is?:
1.write other dll (VS) and there is asynchronous callback to device to get its position just like in exec aplication.
2. USe PostLVUserEvent() to make parameters familiar to labview (in dll)?
is that correct way of thinking ?
I dont have problems with using dll in labview, but I dont know much about how asyncrhnous callbacks works with dll
I hope its all clear.
I know the all answers are here but I d like to summarize it, especially that I could miss something.
01-11-2012 05:37 PM
You would use the Call Library Function Node to use the .dll. You can also import your .dll into Labview to create a sub-palette with all the Call Library function nodes embedded.
01-13-2012 09:00 AM
Hey JakubFr,
Here you can find an example how to call a dll in LabVIEW to get the proper information.
https://decibel.ni.com/content/docs/DOC-6171
Kind regards,
Ion R.
01-13-2012 10:03 AM
Hey,
Also I believe this information will be interesting for you
http://digital.ni.com/public.nsf/allkb/7253D2F0D91F68058625752F005AB672
Kind regards,
01-16-2012 07:30 AM
Hey, got some problems.
I made a dll which communicate with device and console applications using it, and it works fine (it reads position) . While using it in labview (call library function node only) to test, labview crashes, . Device seems to initialize before lv crashes (but it is only thing its done) There is a while loop in dll and I`ve notice LV is always crashing while calling library function like this(loop implemented in dll f.eg to coutinously adding sth). My question; would it work with postLVuserevent or its incorrect from the begining ?How to do that ? I need dll to pass asynchorously data to labview.
Will be thankful for further tips.
01-16-2012 07:41 AM
Try to define the function in DLL without the while loop, just as a simple function that sends the data.
You will use while loop in labview and you will call the function every time you need to read a value.
LabVIEW hangs because it can not stop the while that is running at the moment in that DLL.
01-16-2012 10:12 AM
Can you post the code?
If not, just be sure you're being careful with the memory you are passing between Labview and the DLL. For all parameters passed between the DLL and Labview, make sure you are fully allocating the memory in Labview. Don't ever pass memory allocated in the DLL back to Labview. And, from my experience, avoid all CIN functions that claim to manipulate Labview memory!
01-16-2012 12:12 PM
@JakubFrr wrote:
Device seems to initialize before lv crashes (but it is only thing its done) There is a while loop in dll and I`ve notice LV is always crashing while calling library function like this(loop implemented in dll f.eg to coutinously adding sth). My question; would it work with postLVuserevent or its incorrect from the begining ?How to do that ? I need dll to pass asynchorously data to labview.Will be thankful for further tips.
Am I correctly understanding that you now have two DLLs: the one that reads data from the hardware, and a second one that you wrote to wrap the first one? You're calling that second DLL from within LabVIEW? And the second DLL contains a while loop in one of the calls that LabVIEW makes?
It would help a lot to have your code - both for the wrapper DLL and in LabVIEW. Documentation and function prototypes for the DLL would be useful too. It is a bad idea to put a while loop that could run indefinitely inside a DLL call, because the DLL call will not return until the loop completes. If that's what's happening, it can cause LabVIEW to appear to hang, especially if your DLL is running in the user interface thread (the default unless you define the call as reentrant). Just adding PostLVUserEvent inside the while loop won't fix the problem; you need to rework your code to eliminate the loop.
If you need to pass data from the DLL to LabVIEW asynchronously, and the DLL expects that you will provide a callback function, you need to do create an additional DLL that contains at least two functions: one that is the actual callback function, and a second one that sets up the callback (makes the appropriate call to the original DLL, passing the address of the callback function). You will need to be able to call the second function from LabVIEW. The callback itself should contain either a call to PostLVUserEvent or some other mechanism to pass data to LabVIEW.
01-16-2012 01:29 PM
There is dll (api?) provided by drivers(generally speakin) with all functions needed and documentaion, and second dll written by me with imported this finctions , which actually isnt a wrapper, but only c dll.
Code was rewritten and divided (compared to previous one) to 3 functions in order to avoid loop... and there is no callback anymore , ...... but called from labview one by one these functions are running very slowly - otherwise from the exec application that I made to test dll. I hope its understable enough :>.
I think documentaion isnt necesary.
@JakubFrr wrote:HDCallbackCode HDCALLBACK GetDeviceStateCallback(void *pUserData) // - it was getting state before,now code`s changed beacuse of the while loop .. now it does nothing and I ve just noticed that its not necesarry
return HD_CALLBACK_CONTINUE;
}void init()
{HHD hHD = hdInitDevice(HD_DEFAULT_DEVICE);
gCallbackHandle = hdScheduleAsynchronous(
GetDeviceStateCallback, position, HD_MAX_SCHEDULER_PRIORITY);
hdEnable(HD_FORCE_OUTPUT);
/* Start the haptic rendering loop */
hdStartScheduler();
}
void work(float *a,float *b,float *c){
hdBeginFrame(hdGetCurrentDevice());
hdGetDoublev(HD_CURRENT_POSITION,position);
(*a)=position[0];
(*b)=position[1];
(*c)=position[2];
_sleep(50);
hdEndFrame(hdGetCurrentDevice());
}
void disable()
{hdStopScheduler();
hdUnschedule(gCallbackHandle);
hdDisableDevice( hdInitDevice(HD_DEFAULT_DEVICE));
}
I used pointers with simple functions dll like adding etc. before and it worked fine.
I admit I dont exactly get callbacks. Its seems I can manage without them now, but I`m aware that during more complicated 'operations' it would be much harder. Now it works in labview but very slowly...
@nathand wrote:
(makes the appropriate call to the original DLL, passing the address of the callback function).
Could you give tutorials/example/explanation?
With best ragards 🙂
01-16-2012 01:53 PM
The documentation would be helpful. Also, which data are you trying to get into LabVIEW? Can you attach your LabVIEW code? Which DLL, and which functions, are you calling from LabVIEW? How are you passing data from LabVIEW to the DLL and back? If you pass a pointer from LabVIEW to the DLL, and continue to use that pointer after the DLL call returns, you're setting yourself up for a crash. LabVIEW is free to deallocate or rearrange memory once a call to a DLL completes.
I don't have any example code I can share, but I believe you can find examples on this forum. In your LabVIEW code, you'll need to create a user event. Then, call an initialize function in the DLL you created, passing the user event reference as one parameter. The initialize function will call hdScheduleAsynchronous (I think, hard to know without documentation) to set up the callback. You'll also need to store the user event reference somewhere, either in a global variable (within the DLL) or, better, in the space pointed to by the pUserData parameter of the callback. The callback itself will need to call PostLVUserEvent with the data that you want sent to LabVIEW.