main content

log analog input data to a file using ni devices -凯发k8网页登录

this example shows how to save data acquired in the background to a file.

create a dataacquisition with analog input channels

create a dataacquisition and add two analog input channels with voltage measurement type. for this example use a national instruments® x series data acquisition device, ni pcie-6363 card with id dev1.

d = daqlist("ni")
d =
  12×4 table
     deviceid                 description                    model             deviceinfo     
    ___________    __________________________________    _____________    ____________________
    "cdaq1mod1"    "national instruments ni 9205"        "ni 9205"        [1×1 daq.deviceinfo]
    "cdaq1mod2"    "national instruments ni 9263"        "ni 9263"        [1×1 daq.deviceinfo]
    "cdaq1mod3"    "national instruments ni 9234"        "ni 9234"        [1×1 daq.deviceinfo]
    "cdaq1mod4"    "national instruments ni 9201"        "ni 9201"        [1×1 daq.deviceinfo]
    "cdaq1mod5"    "national instruments ni 9402"        "ni 9402"        [1×1 daq.deviceinfo]
    "cdaq1mod6"    "national instruments ni 9213"        "ni 9213"        [1×1 daq.deviceinfo]
    "cdaq1mod7"    "national instruments ni 9219"        "ni 9219"        [1×1 daq.deviceinfo]
    "cdaq1mod8"    "national instruments ni 9265"        "ni 9265"        [1×1 daq.deviceinfo]
    "dev1"         "national instruments pcie-6363"      "pcie-6363"      [1×1 daq.deviceinfo]
    "dev2"         "national instruments ni elvis ii"    "ni elvis ii"    [1×1 daq.deviceinfo]
    "dev3"         "national instruments pcie-6363"      "pcie-6363"      [1×1 daq.deviceinfo]
    "dev4"         "national instruments pcie-6363"      "pcie-6363"      [1×1 daq.deviceinfo]
deviceinfo = d{9, "deviceinfo"}
deviceinfo = 
ni: national instruments pcie-6363 (device id: 'dev1')
   analog input supports:
      7 ranges supported
      rates from 0.0 to 2000000.0 scans/sec
      32 channels ('ai0' - 'ai31')
      'voltage' measurement type
   
   analog output supports:
      -5.0 to  5.0 volts,-10 to  10 volts ranges
      rates from 0.0 to 2000000.0 scans/sec
      4 channels ('ao0','ao1','ao2','ao3')
      'voltage' measurement type
   
   digital io supports:
      39 channels ('port0/line0' - 'port2/line6')
      'inputonly','outputonly','bidirectional' measurement types
   
   counter input supports:
      rates from 0.1 to 100000000.0 scans/sec
      4 channels ('ctr0','ctr1','ctr2','ctr3')
      'edgecount','pulsewidth','frequency','position' measurement types
   
   counter output supports:
      rates from 0.1 to 100000000.0 scans/sec
      4 channels ('ctr0','ctr1','ctr2','ctr3')
      'pulsegeneration' measurement type
   
dq = daq("ni");
addinput(dq, "dev1", 0:1, "voltage");
dq.channels
ans = 
    index    type    device    channel    measurement type          range              name   
    _____    ____    ______    _______    ________________    __________________    __________
      1      "ai"    "dev1"     "ai0"     "voltage (diff)"    "-10 to  10 volts"    "dev1_ai0"
      2      "ai"    "dev1"     "ai1"     "voltage (diff)"    "-10 to  10 volts"    "dev1_ai1"

create a log file

create the file log.bin and open it. the file identifier is used to write to the file.

fid1 = fopen("log.bin","w");

set the scansavailablefcn

during a background acquisition, the dataacquisition can be directed to handle acquired data in a specified way using the scansavailablefcn property.

dq.scansavailablefcn = @(src, evt) logdata(src, evt, fid1);

acquire data in the background

use start to acquire data for five seconds.

start(dq, "duration", seconds(5))

during normal operation, other matlab commands can execute during this acquisition. for this example, use pause in a loop to monitor the number of scans acquired for the duration of the acquisition.

while dq.running
    pause(0.5)
    fprintf("while loop: scans acquired = %d\n", dq.numscansacquired)
end
fprintf("acquisition has terminated with %d scans acquired\n", dq.numscansacquired);

close the log file

fclose(fid1);

load data from the log file

load file contents as a 3-column matrix into data.

fid2 = fopen('log.bin','r');
[data,count] = fread(fid2,[3,inf],'double');
fclose(fid2);

assign and plot the data

t = data(1,:);
ch = data(2:3,:);
plot(t, ch);

function logdata(src, ~, fid)
[data, timestamps, ~] = read(src, src.scansavailablefcncount, "outputformat", "matrix");
data = [timestamps, data]' ;
fwrite(fid,data,'double');
end
while loop: scans acquired = 500
while loop: scans acquired = 1000
while loop: scans acquired = 1500
while loop: scans acquired = 2000
while loop: scans acquired = 2500
while loop: scans acquired = 3000
while loop: scans acquired = 3500
while loop: scans acquired = 4000
while loop: scans acquired = 4500
while loop: scans acquired = 5000
acquisition has terminated with 5000 scans acquired
网站地图