Oszcilloszkóp program myDAQ-al

A Fizipedia wikiből

Oscilloscope program with myDAQ

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

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:


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 setting the chart axis in a way to zoom on the relevant parts of the measured data. The below code can be used to search for the first data point that falls below a threshold level and set the range of the X axis to display numPts data points after the crossing of the threshold level.

     // Display data on chart, search for the crossing of trigger level
     double threshold = 5; // threshold value
     int numPnts = 1000; // number of points to display
     int TriggerInd = -1;
     for (int i = 0; i < inData.Length; i++)
         double x = i;
         double y = inData[i];
         chart1.Series[0].Points.AddXY(x, y);  // Display data on chart
         // search for the first crossing of the trigger level
         if(TriggerInd < 0)
             if(y < Convert.ToDouble(threshold))
                   TriggerInd = i;
     // Apply trigger on chart1
     chart1.ChartAreas[0].AxisX.Minimum = Convert.ToDouble(TriggerInd);
     chart1.ChartAreas[0].AxisX.Maximum = Convert.ToDouble(TriggerInd) + Convert.ToDouble(numPnts);