Test and Measurement Forum

USB ADC11 and MATLAB

Post general discussions on using our drivers to write your own software here

USB ADC11 and MATLAB

Postby ACLOTM_1 » Thu Oct 22, 2009 5:08 am

Hi,

I have manage to create a starting point for interfacing the USB ADC11 with MATLAB. So far all i can do is:

-open single and multiple units
-get unit information
-set the sample interval
-set the unit(s) to run
-Close single and multiple units

The the problem i am stuck with atm is that the unit is never ready (i.e the return of the UsbAdc11Ready function is always 0) and i cant return any values.

I do not expect any help from PICO technicians as i have already sent them an email. I just though i would put this up for those looking to interface with Matlab as you might be able to use this and maybe work out the problem and get further than i could.

The Headfile:

This is the custom header file that i wrote to use with the matlab code. It basically is the same as the usbadc11.xls function definition, however written in C.

--------------------------------------------------------------------------------------
tester.h
--------------------------------------------------------------------------------------
void UsbAdc11ApplyFix(short fix_no, short value);

int UsbAdc11OpenUnit();

int UsbAdc11CloseUnit(int handle);

int UsbAdc11GetUnitInfo(int handle, char *string, int lth, int line_no);

int UsbAdc11Run(int handle, long no_of_values, int method);

int UsbAdc11Ready(int handle);

int UsbAdc11Stop(int handle);

long UsbAdc11SetInterval(int handle, long us_for_block, long ideal_no_of_samples,int * channels, int no_of_channels);

long UsbAdc11GetValues(int handle, int * values, long no_of_values);

long UsbAdc11GetTimesAndValues(int handle, long * times, int *values, long no_of_values);

int UsbAdc11GetValue(int handle, int channel, int * reading);
-------------------------------------------------------------------------------------

The Code:

The Malab code was written in the same format as that of the usbadc11.xls example that is provided by PICO tech so that easy comparisons between codes can be made.

--------------------------------------------------------------------------------------
test.m
--------------------------------------------------------------------------------------
%Load the usbadc11.ddl
loadlibrary('usbadc11','tester.h');
%List the usbadc.dll functions
libfunctions('usbadc11')
%view function definitions
libfunctionsview('usbadc11')

%Open ADC-11 on USB
handle1 = calllib('usbadc11','UsbAdc11OpenUnit')

if handle1 > 0

%Get the unit information
S = '';
p1 = libpointer('cstring',S);
[SLength, S]= calllib('usbadc11','UsbAdc11GetUnitInfo',handle1,p1,255,4);

ADC11_UBS1 = S


%Set the sample interval
%Say that we want to take 100 readings in 20000us
%from channels 1 and 2

channels = 1:2;
p2 = libpointer('int32Ptr',channels);
[sampleinterval, p2] = calllib('usbadc11','UsbAdc11SetInterval',handle1, 20000,100, p2,2)

% Set the unit to run
ok = calllib('usbadc11','UsbAdc11Run',handle1,100, 0)

%Wait for unit to finish taking samples
ready = 0
while ready == 0
ready = calllib('usbadc11','UsbAdc11Ready',handle1);
end

%Collect Samples
times = [];
values = [];
p4 = libpointer('int32Ptr',times)
p5 = libpointer('int32Ptr',values)

[out, times, vales] = calllib('usbadc11','UsbAdc11GetTimesAndValues',handle1,p4,p5,100)

%Close unit
calllib('usbadc11','UsbAdc11CloseUnit',handle1)
end
--------------------------------------------------------------------------------------
ACLOTM_1
 

Re: USB ADC11 and MATLAB

Postby ACLOTM » Thu Oct 22, 2009 5:15 am

oh i forgot to log in, thats why i couldn't upload a file. Oh well.

Just as a side note to the above, i also tried importing the USBACD11Api.h as the header file used by matlab, but it also gave the same results.
ACLOTM
Newbie
 
Posts: 0
Joined: Thu Oct 22, 2009 4:39 am

Re: USB ADC11 and MATLAB

Postby ACLOTM » Thu Oct 22, 2009 5:57 am

Ok,

As an alternative to talking directly to .dll files in MATLAB, i have also managed to get MATLAB to work with DDE in the exact same way as the excel example dde_piclog_multi_chn_927.xls provided by PICO tech.

The requirements to collect data by DDE in MATLAB is that piclog is open and all of your sensors are setup and ready to go.

The Code
---------------------------------------------------------------------------
%Initiate DDE converstaion between MATALB and PLW
chan = ddeinit('PLW','Current')

%Request data from DDE server application
returndata1 = ddereq(chan,'Name',[1 1]) % [1 1] means string
returndata2 = ddereq(chan,'Value') %default is [1 0] number
returndata3 = ddereq(chan,'Units',[1 1])
-------------------------------------------------------------------

This is a much more simpler way to interface to matlab!
ACLOTM
Newbie
 
Posts: 0
Joined: Thu Oct 22, 2009 4:39 am

Re: USB ADC11 and MATLAB

Postby harrissimo » Mon Oct 18, 2010 10:29 am

I've managed to get the ADC20/24 datalogger running in MATLAB.

Here is the post I've made with my code

topic7027.html

I think the problem you (may) have had with the code not returning 1 for 'HRDLReady' command was that the unit needs to have data in it before it returns a 1. It took me about an hour to realise that was what was going on!

Hope this helps!
harrissimo
Newbie
 
Posts: 0
Joined: Mon Oct 18, 2010 10:15 am


Return to General Software Discussions

Who is online

Users browsing this forum: No registered users and 0 guests