Extern() Onunload


cycler

Recommended Posts

I am having a bit of trouble writing code for a Pico TC08 thermocouple datalogger, the problem seems to be with using a variable in the On Unload event in the user device configuration.

To open the unit I use the following code snippet:


global usbHandle
extern("usbtc08.dll", "word  usb_tc08_open_unit(word)", "u_tc08_open", "stdcall")

extern("usbtc08.dll", "word usb_tc08_close_unit(word)", "u_tc08_close", "stdcall")

usbHandle=u_tc08_open(0)

 

This opens the unit and returns the usb handle (usually 1).

Then I go through the rest of the set up and it works just fine. I can read the channels etc with no problems.

However when I come to close the unit when quitting DaqFactory the following code causes DaqFactory to hang if it's in the 'On Unload' event.

private temp
temp=u_tc08_close(usbHandle)

If I put the code in an onShutDown sequence it works ok. I have resorted to putting the following code in the 'On Unload' event which works, but would like to know why the above code does not work as I expected.

private i=1
private temp
while(i<10)
   temp=u_tc08_close(i)
   i++
endwhile

I have also tried various combinations of this in the new DaqFactory RC version 5_90_6 and it hangs on closing every time, even if the code is in the onShutDown sequence. The only way I could close the unit was in a 'normal' sequence started by a button.

 

Link to comment
Share on other sites

Most likely the DLL is not thread safe and doesn't like having close() called outside the primary thread of the application.

 

Truthfully, you probably don't need to call close() at all.  A decent DLL should be able to detect when its getting unloaded and clean up after itself.  Try not closing it and see if you run into any issues when you restart DAQFactory.

Link to comment
Share on other sites

Thank's for the quick reply!

The documentation with the DLL does actually state that it's thread safe. Not sure it's a thread problem because I can close it by specifying the device id directly (u_tc08_close(1)) or a private variable as above, but not if it's a global variable (u_tc08_close(usbHandle))

 

I tried quitting DaqFactory without closing and DaqFactory hangs in exactly the same way. The hang up leaves DF as a process in Windows Task Manager using 50% cpu. The data-logger access led keeps blinking and the USB lead needs to be unplugged before it can be accessed again.

Link to comment
Share on other sites

Ok, missed that part.  If it works with a constant or private but not a global than the issue is that on shutdown the globals have already been deleted so usbHandle probably doesn't exist.  It is then passing some value (probably 0) to the DLL and who knows what happens then.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.