From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, 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: 

collecting data from hardware using dll

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.

 

 

0 Kudos
Message 1 of 14
(3,523 Views)

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.

-----------------------------------------------------------------------------------------
Reese, (former CLAD, future CLD)

Some people call me the Space Cowboy!
Some call me the gangster of love.
Some people call me MoReese!
...I'm right here baby, right here, right here, right here at home
0 Kudos
Message 2 of 14
(3,522 Views)

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.

0 Kudos
Message 3 of 14
(3,483 Views)

Hey,

Also I believe this information will be interesting for you

 

http://digital.ni.com/public.nsf/allkb/7253D2F0D91F68058625752F005AB672

 

Kind regards,

0 Kudos
Message 4 of 14
(3,477 Views)

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.

0 Kudos
Message 5 of 14
(3,452 Views)

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.

0 Kudos
Message 6 of 14
(3,447 Views)

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!

0 Kudos
Message 7 of 14
(3,430 Views)

@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.

0 Kudos
Message 8 of 14
(3,422 Views)
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 🙂 

 

 

0 Kudos
Message 9 of 14
(3,416 Views)

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.

0 Kudos
Message 10 of 14
(3,410 Views)