Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

Direct DLL calls in MATLAB 6.5+


  • Please log in to reply
18 replies to this topic

#1 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 10 July 2003 - 06:49 AM

Apparently MATLAB 6.5 and higher supports direct DLL calls, and thus the drivers from the labjack.com downloads page are no longer needed. The following is an email we received from a customer: I have tried accessing the LabJack DLL directly from MATLAB using the Mathworks new DLL support (http://www.mathworks...ata/33513.shtml) and, rather to my surprise, it seems to work! I have copied below all that is needed to access the interface. It is odd that the call-by-reference parameters need an input value (otherwise MATLAB complains about an unmatched signature) but this appears to be harmless. The new DLL support is specified only for MATLAB 6.5, so it may be worth upgrading and trying this route ... -Contact Info Removed- ***** LabJack from MATLAB via direct DLL access ***** >> header='C:\Program Files\LabJack\drivers\ljackuw.h'; >> loadlibrary('ljackuw',header) >> libmethods('ljackuw','-full') Methods for class lib.ljackuw: [int32, int32Ref, int32Ref, singleRef] EAnalogIn(int32Ref, int32, int32, int32, int32Ref, singleRef) [int32, int32Ref] EAnalogOut(int32Ref, int32, single, single) [int32, int32Ref, doubleRef, doubleRef] ECount(int32Ref, int32, int32, doubleRef, doubleRef) [int32, int32Ref, int32Ref] EDigitalIn(int32Ref, int32, int32, int32, int32Ref) [int32, int32Ref] EDigitalOut(int32Ref, int32, int32, int32, int32) [int32, int32Ref, int32Ref, int32Ref, int32Ref, int32Ref, singleRef] AISample(int32Ref, int32, int32Ref, int32, int32, int32, int32Ref, int32Ref, int32, int32Ref, singleRef) [int32, int32Ref, int32Ref, int32Ref, singleRef] AIStreamStart(int32Ref, int32, int32, int32, int32, int32, int32Ref, int32Ref, singleRef, int32, int32, int32) int32 AIStreamClear(int32) [int32, int32Ref, int32Ref, int32Ref, uint32Ref] AOUpdate(int32Ref, int32, int32, int32, int32Ref, int32Ref, int32, int32, uint32Ref, single, single) [int32, singleRef] BitsToVolts(int32, int32, int32, singleRef) [int32, int32Ref] VoltsToBits(int32, int32, single, int32Ref) [int32, int32Ref, int32Ref, int32Ref, uint32Ref] Counter(int32Ref, int32, int32Ref, int32Ref, int32, int32, uint32Ref) [int32, int32Ref, int32Ref, int32Ref, int32Ref, int32Ref] DigitalIO(int32Ref, int32, int32Ref, int32, int32Ref, int32Ref, int32, int32Ref) single GetDriverVersion string GetErrorString(int32, string) [single, int32Ref] GetFirmwareVersion(int32Ref) [int32, uint32Ref, uint32Ref, uint32Ref, uint32Ref, uint32Ref, uint32Ref] GetWinVersion(uint32Ref, uint32Ref, uint32Ref, uint32Ref, uint32Ref, uint32Ref) [int32, int32Ref] LocalID(int32Ref, int32) [int32, int32Ref] NoThread(int32Ref, int32) [int32, int32Ref] ReEnum(int32Ref) [int32, int32Ref] Reset(int32Ref) [int32, int32Ref] ResetLJ(int32Ref) [int32, int32Ref] Watchdog(int32Ref, int32, int32, int32, int32, int32, int32, int32, int32, int32, int32) [int32, int32Ref, int32Ref, int32Ref, int32Ref, int32Ref] ReadMem(int32Ref, int32, int32Ref, int32Ref, int32Ref, int32Ref) [int32, int32Ref] WriteMem(int32Ref, int32, int32, int32, int32, int32, int32) >> [a1,a2,a3,a4,a5,a6,a7]=calllib('ljackuw','GetWinVersion',0,0,0,0,0,0) a1 = 0 a2 = 5 a3 = 0 a4 = 2195 a5 = 2 a6 = 3 a7 = 0

#2 A3aan

A3aan
  • Members
  • 1 posts

Posted 26 November 2003 - 01:57 AM

I'm working with Matlab 6.5 and i'm using the labjack library, But i'm missing the method AIStreamRead. Also missing are the functions AIBurst and Listall. I was reading the list above and can't find them either.
What am i doing wrong.

hdr = 'C:\program files\labjack\drivers\ljackuw.h';
lib = 'ljackuw';
loadlibrary('ljackuw',hdr,'alias','labjack');
libfunctionsview('labjack');

Is there an other way to use this function

Thanks for helping

#3 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 03 December 2003 - 02:49 PM

We have asked someone to take a look at these, but am not sure if there is an immediate solution. Based on the output above and your observation, it appears that Matlab simply does not recognize an of our functions that have 2-dimensional arrays. In the meantime, perhaps you should try using our Matlab drivers (from downloads page) rather than direct DLL calls.

#4 TJDesign

TJDesign
  • Members
  • 2 posts

Posted 10 December 2003 - 08:24 AM

I'm trying to extract the single precision returning 4 voltage vars from lib function AISample() using Matlab6.5 R13, and maybe need a trick to do it? I'm using 4 single-ended channels (0-3). The double() conversion call returns zeros, as you can see at the end of the example code below. Maybe matlab doesn't allow for conversion of a single array, only a single instance of a single at a time? Here is the content of my .m file that sets up for the call: ==================================================== header = 'C:\Program Files\LabJack\drivers\ljackuw.h'; loadlibrary('ljackuw',header); libfunctionsview('ljackuw'); idnum = int32(-1) p1 = libpointer('int32Ptr', idnum) stateIO = [int32(0),int32(0),int32(0),int32(0)] p2 = libpointer('int32Ptr', stateIO) channel = [int32(0),int32(1),int32(2),int32(3)] p3=libpointer('int32Ptr',channel) gain = [int32(0),int32(0),int32(0),int32(0)] p4 = libpointer('int32Ptr', gain) ov = int32(0) pov = libpointer('int32Ptr',ov) v(1,4) = single(0) % this is to create an array of 4 single precision locations pv = libpointer('singlePtr',v) calllib('ljackuw','AISample',p1,0,p2,0,0,4,p3,p4,0,pov,pv) % here is an attempt from the command window % at conversion to double after the call... TJD>> x=get(pv) x = Value: [2.4902 2.5000 0.0049 -0.0098] DataType: 'singlePtr' TJD>> x = double(v) x = 0 0 0 0

#5 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 10 December 2003 - 09:42 AM

In response to the 2D array problem mentioned above (AIBurst, AIStreamRead, ListAll), try using the attached header file instead of our normal header file (ljackuw.h). It seems that a slight difference in the pointer definitions helps Matlab.

Attached Files



#6 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 10 December 2003 - 09:47 AM

Time to start a new topic. In the post above with pointer problems in AISample, note that StateIO is a pointer to a single value not an array.

#7 TJDesign

TJDesign
  • Members
  • 2 posts

Posted 10 December 2003 - 10:23 AM

I fixed the StateIO definition problem. When I loaded the ljackuw_Mat.h header, it did not resolve the single to double issue, however. I still get zeros. Is there an ljackuw_Mat.lib library file as well, or do I just need to change the header file? Thanks.

#8 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 10 December 2003 - 11:56 AM

The header file is related to a different sub-topic within this topic, not your pointer problems. Someone just sent me this info that looks like the answer: The problem this person is experiencing is because the pointers MATLAB uses are not really pointers in the true sense. MATLAB's pointers do not actually contain the address of the variable they point to. They work more like structures. When an external function executes it does not modify variables that are pointed to by input arguments. Instead, the 'value' property of the pointer is modified. That's why when they try to convert the voltages from single to double they get an array of zeros. The voltage array v was never changed from its initialized values. What they need to do is extract the array of voltages from the 'value' property of the pointer (pv), and then do the conversion. >> x = get(pv,'value'); >> x = double(x); >> x x = 1.4307 1.4307 1.4258 1.4258

#9 tanthiamhuat

tanthiamhuat
  • Members
  • 2 posts

Posted 16 December 2003 - 10:23 PM

Dear LabJack support and other experts: I am using Matlab 6.5.1 (Release 13, with Service Pack 1) and GenericDll_1p1. it is very useful for calling the Generic DLLs. I am able to load all the functions using loadlibrary function, feel very glad, . However, I still encounter some problems which I hope the experts out there are able to provide me with some solutions. say when I type this at Matlab command prompt >> libfunctions ABC -full % ABC is just any of my DLL Functions in library ABC: ..... ..... int32 ABCTUR(int32, int32, int32) [int32 voidPtr] ABCUser(int32, int32, int32, voidPtr, int32, int32, int32, int32) ..... ..... I am able to use the first function in MATLAB without any problem. >>[a]=calllib('ABC','ABCTUR',2,1,0) % this gives me the correct output. however, I am not sure how to use the second function, especially how to pass the input and output parameters which is of type voidPtr, in MATLAB. Can anyone gives me some examples? Greatly appreciate any relevant help or advice. Sincere regards, Thiam Huat

#10 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 17 December 2003 - 09:20 AM

What is "GenericDll_1p1"? I am not sure what the output value of voidPtr means. As you can see in the first post of this topic, none of our functions have this output type. Look a couple of posts above, as there is a snippet showing how to "get" the "value" pointed to by a pointer.

#11 Guest_Guest_*

Guest_Guest_*
  • Guests

Posted 21 December 2003 - 08:48 PM

"GenericDll_1p1" is available for download at
ftp://ftp.mathworks.com/pub/tech-support/...ericDll_1p1.exe
it is the lastest DLL interface to MATLAB.

it contains the dll_interface_guide.pdf, which on page 2-5, contains a function mxSetData which has both input and output of type voidPtr. But I am not sure how to use that. but I am sure we are able to manipulate functions which contain input and output parameters of type voidPtr.

thanks.

#12 jsachs

jsachs
  • Members
  • 7 posts

Posted 13 October 2005 - 10:33 AM

could someone post an example of using AIBurst and/or the AIStream functions using the MATLAB calllib()? I'm familiar w/ the MATLAB DLL functions but not with how to use them w/ Labjack.

#13 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 13 October 2005 - 11:52 AM

Looking at the posts above, I can see that you need to have the special header file, and you need to use the info about getting the values from the pointer. Check out the following topic that discusses matlab streaming with direct dll calls and with our U12 matlab drivers.

https://forums.labja...p?showtopic=608


Here is an m file to do a burst using our U12 matlab drivers:

https://forums.labja...p?showtopic=762

#14 jsachs

jsachs
  • Members
  • 7 posts

Posted 13 October 2005 - 12:10 PM

Thanks, the problem I was having was with the 2-D arrays. The data was looking like garbage -- I finally realized the new header file declares the voltages as a 1-D array which means you have to reshape it into a 2-D array: x0 = get(pv,'value'); x0 = reshape(x0,4,4096)'; % unflatten array and extract relevant values x0 = x0(1:k,1:length(chanarray)); Works fine now. Got the stream functions working also. Key here was to make sure I re-initialized the "voltages" and "stateIOout" arrays between calls, otherwise it seemed to be putting the data in the wrong place.

#15 jsachs

jsachs
  • Members
  • 7 posts

Posted 17 October 2005 - 11:44 AM

a completely different application (SPI communication): what happened to the Synch() function in ljackuw_Mat.h ? I copied it from the original ljackuw.h -- seems to handle a function call to the DLL OK. (Though for some reason SCK isn't getting set correctly)

#16 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 17 October 2005 - 12:07 PM

The header file ljackuw_Mat.h attached above was actually user-submitted. Looking at it I can see it is from driver version 1.074, before the Synch function was added. Try the attached which I just made from the V1.18 header file.

Attached Files



#17 jsachs

jsachs
  • Members
  • 7 posts

Posted 17 October 2005 - 12:16 PM

Thanks for the quick response & for the updated file. (re: the SCLK "problem" -- I think my manually-updated ljackuw_Mat.h was OK, but I had my oscilloscope zoomed out too much to see. :unsure: Seems to work now.)

#18 andrew

andrew
  • Members
  • 1 posts

Posted 19 June 2006 - 01:05 PM

Hi, I am trying to download the updated "ljackuw_Mat.h" file, but I believe that it was deleted from the server. Is there any chance that you could post it again. Thanks, Andrew

#19 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 20 June 2006 - 01:08 PM

Fixed. The recent process of updating the forum seemed to cause .h files to be disallowed. The above link should be working now.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users