by E. Carr Everbach, Swarthmore College Engineering Dept.
last modified 17 December 2002
Outline of headings
A. Data Acquisition toolbox and DAQ
A. Data Acquisition toolbox and DAQ
Install the data acquisition or communications boards as per installation directions. For National Instruments products, the Measurement and Automation Explorer MAX application allows setup and testing of the functionality of each board. Although it is important to run MAX to test the installation, Matlab will NOT remember any configuration or settings changes made in MAX. Any non-default settings must be made using the software methods within Matlab, as follows.
1. It is worth walking through the following help files, if you have time:
help daq help daq demos demo toolbox daq daqschool
2. To find out which hardware drivers Matlab has access to (these are called "adaptors" in Matlab parlance), type:
x = daqhwinfo
The variable "x" is a data structure with sub-elements that you can access by using "dot notation" as follows:
x.InstalledAdaptors
will print out the list of driver names, such as 'nidaq' and 'winsound'. If the driver you wish to use is not there, then Matlab cannot talk to the board.
3. For whichever driver you intend to use, you can get more detailed information about it using daqhwinfo for that driver, such as:
y = daqhwinfo('nidaq') % for the National Instruments NiDAQ board
Then, using dot notation, you can query subfields, such as:
y.ObjectConstructorName(:)
which gives the syntax to associate a particular function of a board, such as analog or digital input or output, with a variable name you choose. For example,
ai = analoginput('nidaq',1)
creates a variable "ai" that is associated with the analog input on NiDAQ board 1. The variable "ai" is called an "analog input object."
4. Once you've created an association between a board function and a variable like "ai", you can get more detailed information about configuration possibilities using dot notation:
info = daqhwinfo(ai) info.DifferentialIDs
5. To modify the various possible configuration options, there are four methods:
6. Once you've configured the board characteristics (SampleRate, SamplesPerTrigger, etc.), you can either save the data acquisition object using:
7. For analog input, it is necessary to add channels, one for each input line you intend to use. There are three ways, the second being preferred because it allows use of the dot notation:
1. It is worth walking through the following, if you have time: instrhelp.
2. To find out which hardware drivers Matlab has access to (these are called "adaptors" in Matlab parlance), type:
x = instrhwinfo
The variable "x" is a data structure with sub-elements that you can access by using "dot notation" as follows:
x.SupportedInterfaces
will print out the list of interface names, such as 'gpib' and 'serial'. If the interface you wish to use is not there, then Matlab cannot talk to the instruments you have connected to your computer.
3. For whichever driver you intend to use, you can get more detailed information about it using instrhwinfo for that driver, such as:
y = instrhwinfo('gpib')
for a GPIB board or
y = instrhwinfo('serial')
for access to the serial port.
To associate a particular interface, such as gpib, with a variable name you choose, use the synatx
g = gpib('ni',0,1)
which creates a variable "g" that is associated with the gpib device of address 1 using National Instruments gpib board 0 (also called GPIB0). The variable "g" is called a "gpib object."
4. Once you've created an object, you can get more detailed information about configuration possibilities using dot notation:
info = propinfo(g) info.InputBufferSize
Note that this only tells you what the possibilities are, including maximum or minimum values allowed for certain fields.
5. To modify the various possible configuration options, there are two methods:
i. Use "set" and "get" as follows:set(g,'EOSMode', 'off'); k = get(g,'EOSMode');ii. Use dot notation right with the variable "g" as follows:
g.EOIMode = 'off';Type "instrhelp" or "instrhelp gpib" or "instrhelp propinfo" for more details on setting the various communications parameters.
6. Once you've configured the board characteristics (SampleRate, SamplesPerTrigger, etc.), you can either save the data acquisition object using:
1. It is worth walking through the following, if you have time: instrhelp.
2. To find out which hardware drivers Matlab has access to (these are called "adaptors" in Matlab parlance), type:
x = instrhwinfo
The variable "x" is a data structure with sub-elements that you can access by using "dot notation" as follows:
x.InstalledAdaptors
will print out the list of driver names, such as 'serial' and 'visa'. If the driver you wish to use is not there, then Matlab cannot use that port.
3. For whichever driver you intend to use, you can get more detailed information about it using instrhwinfo for that driver, such as:
y = instrhwinfo('serial')
for a serial port (e.g., 'COM1') or
y = instrhwinfo('visa')
for access to VISA-accessible boards.
Then, using dot notation, you can query subfields, such as:
y.ObjectConstructorName(:)
which gives the syntax to associate a particular function of a board, such as , with a variable name you choose. For example, s = serial('COM1') creates a variable "s" that is associated with the serial port at COM1. The variable "s" is called a "serial object." Also, for example, v = visa('agilent', 'GPIB0::7::INSTR') creates a "visa object" that addresses the instrument at visa address 7.
4. Once you've created an object, you can get more detailed information about configuration possibilities using dot notation:
info = propinfo(s) info.AvailableSerialPorts
5. To modify the various possible configuration options, there are four methods:
i. z = propinfo(s)then use dot notation such asz.OutputBufferSizeor
z.OutputBufferSize = 45ii. Use "set" and "get" as follows:
set(s,'BaudRate', 9600); k = get(s,'StopBits');iii. Use dot notation right with the variable "s" as follows:
s.Terminator = 'CRLF';iv. Type
instrhelp
and use the graphical user interface to configure a serial object.
6. Once you've configured the serial port characteristics (BaudRate, StopBits, Terminator, FlowControl, Parity, etc.), you can either save the data acquisition object using:
1. Once configured, analog input is begun using the syntax: start(ai) for analog input, which streams the data either to memory (default) or disk (depending on the state of the setting of LoggingMode). To access the data after the logging is complete, use the command:
[d,t] = getdata(ai);
which moves the data from memory into the arrays "d" (data) and "t" (elapsed time). If you want to examine the input voltages prior to transferring them to Matlab variables, use peekdata(ai).
For example:
clear ai = analoginput('nidaq',1); % get parameters for LabPC-1200 board addchannel(ai,0); ai.SampleRate=100000; % sample that channel 100 kHz ai.SamplesPerTrigger=1000; % one hundredth of a second of data ai.TriggerType='Immediate'; % do it now start(ai) [datastuff, timestuff]= getdata(ai); % grab it from memory delete(ai) % free up memory plot(timestuff,datastuff) % show it
2. For analog output, first load the desired output voltage into memory using the putdata command:
putdata(ao, 2.45);
sets 2.45 volts to go the output channel, or for multiple channels,
putdata(ao, [-3.2, 1.7])
sets -3.2 V on the channel 0 and 1.7 V on output channel 1.
After queuing up the output voltages, write them with start(ao)
For example:
ao = analogoutput('nidaq', 1); % open analog output addchannel(ao,0); % create analog output channel 0 set(ao,'SampleRate',100); % output infrequent updates since DC set(ao,'TriggerType','Immediate'); % do it immediately upon start command putdata(ao, 10); % put out 10V on analog output channel start(ao); % start output waittilstop(ao,2); % wait for completion (2 seconds max)
3. Digital I/O is written a bit at a time, and each port can be either input or output:
dio = digitalio('nidaq',1); addline(dio, 0:7, 'out'); pval = [11110101]; putvalue(dio,pval); gval = getvalue(dio);
For example:
dio = digitalio('nidaq', 1); %open digital i/o channel chan = addline(dio, 0:7, 0, 'out'); % set the dio channel 0 to output alloff = [0 0 0 0 0 0 0 0]; % set all bits to zero oneon = [1 0 0 0 0 0 0 0]; % toggle LSB on putvalue(chan, alloff); % start from low state gval = getvalue(dio); if(gval ~= alloff) fprintf('Error writing alloff\n'); output = 1; % set error flag end putvalue(chan, oneon); % flip to high state (stepper motor advances on rising edge) gval = getvalue(dio); if(gval ~= oneon) fprintf('Error writing oneon\n'); output = 1; end delete(dio) % remove i/o channel from memory
4. Always delete the data acquisition object you have created when you are done with it, using the delete command.
1. Once configured, a GPIB object must be opened with the fopen command:
g = gpib('ni',0,1); fopen(g);
Writing to a GPIB device is accomplished using the fprintf command, with the GPIB object as the destination for the (ascii text) strings written. Reading from a GPIB device is the same, only using the fscanf command. For example:
fprintf(g,'tdiv?');
or
fprintf(g,'%s','tdiv?');
to write the string "tdiv?" to object g
data = fscanf(g);
or
data = fscanf(g,'%s');
to read from object g
2. Always close the object (which can be reopened with fopen) to disconnect it when finished, then delete the variable to remove it from memory:
fclose(g); delete(g); clear g;
See GPIB syntax above, since it is the same procedure for serial and VISA ports. For example, here is code to read and write to a syringe pump via serial port:
pump = serial('COM2'); % use COM2 serial port, since mouse is COM1 fopen(pump); h = get(pump, 'Status'); % check to see if it worked OK if (h == 'open') set(pump,'BaudRate',9600,'StopBits',1); set(pump,'Terminator','CR/LF','Parity','none'); set(pump,'FlowControl','none'); ratei = int2str(rate); % convert integer to string if (ratei ~= '0') whole = ['ratei ',ratei,' ',which]; fprintf(pump,'%s\n',whole); data = fscanf(pump); % get and discard the first garbage response fprintf(pump, 'ratei?'); % ask pump for infusion rate data = fscanf(pump); % get and discard the first garbage response output = fscanf(pump); % get the pump reply string fprintf(pump,'run'); data = fscanf(pump); % get and discard the first garbage response else fprintf(pump,'stop') data = fscanf(pump); % get and discard the first garbage response output = 'stopped'; % return indication of stop end fclose(pump); delete(pump); clear pump; else fprintf('Error opening serial port: Status = %s\n', h); output = ['Error ',h]; end
and here is code to ask an Agilent device its identity using VISA:
g = visa('agilent','GPIB0::7::INSTR'); fopen(g) h = get(g,'Status'); fprintf(g,'*IDN'); k = get(g,'ValuesSent'); data = fscanf(g,'%s'); fclose(g); delete(g); clear g;