Oszcilloszkóp program myDAQ-al

A Fizipedia wikiből
A lap korábbi változatát látod, amilyen Magyarkuti (vitalap | szerkesztései) 2019. szeptember 19., 12:51-kor történt szerkesztése után volt.

The following program measures a voltage signal on the analog input of a NationalInstruments DAQ card (for example myDAQ). It uses the built-in sample clock of the device for timing (hardware-timed data acquisition).

Creating a Task for handling the analog input:

    InTask = new NationalInstruments.DAQmx.Task();
    InTask.AIChannels.CreateVoltageChannel(DevID, "",
        AITerminalConfiguration.Differential, Vmin, Vmax, AIVoltageUnits.Volts);
  • DevID: string identifying the device and the input terminal. For example: "myDAQ1/ai0".
  • AITerminalConfiguration.Differential: sets differential voltage measurement, the analog input has two terminals (- and +), voltage is measured between the two terminals. Other option would be for example: referenced single ended measurement (AITerminalConfiguration.RSE). In this case, the analog input has one terminal, the voltage is measured with respect to ground. The myDAQ device only supports differential voltage measurement.
  • Vmin and Vmax is used to set the range of the analog input. In the case of the myDAQ device, -10, 10 V is the maximum input range.

For hardware-timed data acquisition, the timing of the Task also has to be specified:

    InTask.Timing.ConfigureSampleClock("", SampleRate,
        SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, numPnts);
  • SampleRate: double value, defines the number of samples measured in 1 second. The maximum sample rate available using the myDAQ device is 200 kHz.
  • SampleQuantityMode.FiniteSamples: sets the data acquisition to finite mode. Using finite mode, data acquisition stops automatically after all requested data points are collected.
  • numPnts: number of points to measure (length of input data)

Note: the length of the measurement: L (sec) = numPts / SampleRate

Finally, an AnalogSingleChannelReader object is created and associated with the Task handling the input:

    analogReader = new AnalogSingleChannelReader(InTask.Stream);

When using hardware-timed data acquisition, the measurement is controlled by the DAQ card and it runs in parallel with the C# program running on the PC. The measurement can be started by starting the Task handling the input:

   // Start the measurement of the analog input
   InTask.Start();

The data is collected by the DAQ card and stored in the memory. When using finite mode, the measurement stops automatically after the number of measured data points reaches the value defined previously by numPnts. Then the data can be read using the analogReader object which returns the data points in an array:

   // Read Data from input
   double[] inData = new double[numPnts];
   inData = analogReader.ReadMultiSample(numPnts);

Note: if the measurement is still running, the program is waiting at the function: analogReader.ReadMultiSample(numPnts) until all of the requested data points are collected.

After the measurement is stopped and the measured data is read from the memory, the resources used by the task has to be released:

  InTask.Dispose();


Note: when using SampleQuantityMode.FiniteSamples, the data acquisition stops automatically after the collection of the requested number of data points. In order to repeatedly measure data, these code lines have to execute in a loop or in a timer event.

Implementing a software trigger

Although the myDAQ card does not support hardware triggering (for example to start the data acquisition when the input voltage crosses a given threshold), the measurement program can realize a trigger function by cutting out the relevant parts from the measured data. The below code can be used to search for the first datapoint that falls below a threshold and cut out a threshold