Függvénygenerátor program myDAQ-al

A Fizipedia wikiből

Function generator program with myDAQ

The following program generates a sinusoidal signal on an analog output using a NationalInstruments DAQ card (for example myDAQ). It uses the built-in sample clock of the device for timing (hardware-timed data generation).

Creating a Task for handling the analog output:

    OutTask = new NationalInstruments.DAQmx.Task();
    OutTask.AOChannels.CreateVoltageChannel(DevID, "",
       Vmin, Vmax, AOVoltageUnits.Volts);

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

    OutTask.Timing.ConfigureSampleClock("", SampleRate,
    SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, numPnts);

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

Finally, an AnalogSingleChannelWriter object is created and associated with the Task handling the output:

    analogWriter = new AnalogSingleChannelWriter(OutTask.Stream);

To start generating the signal, first a double array has to be created. Then the array is passed to the AnalogSingleChannelWriter object and the DAQ card starts generating the data. In the below example, a sinusoidal signal is generated.

    // Generate data for output
    double[] outData = new double[numPnts];
    for (int i = 0; i < numPnts; i++)
    {
         outData[i] = Math.Sin((double)i / numPnts * 2 * Math.PI);
    }
 
    // Start Output
     analogWriter.WriteMultiSample(true, outData);

After the generation stopped, the resources used by the task has to be released:

  OutTask.Dispose();


Note: when using SampleQuantityMode.FiniteSamples, the data generation stops automatically after the last value is generated in the outdata array. In order to repeatedly generate data, these code lines have to execute in a loop or in a timer event.