„Függvénygenerátor program myDAQ-al” változatai közötti eltérés
1. sor: | 1. sor: | ||
− | 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 (hardware-timed data generation). | + | 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: | Creating a Task for handling the analog output: |
A lap 2019. szeptember 17., 12:20-kori változata
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);
- DevID: string identifying the device and the output terminal. For example: "myDAQ1/ao0".
- Vmin and Vmax is used to set the range of the analog output. In the case of the myDAQ device, -10, 10 V is the maximum output range.
For hardware-timed data generation, the timing of the Task also has to be specified:
OutTask.Timing.ConfigureSampleClock("", SampleRate, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, numPnts);
- SampleRate: double value, defines the number of samples generated in 1 second. The maximum sample rate available using the myDAQ device is 200 kHz.
- SampleQuantityMode.FiniteSamples: sets the data generation to finite mode. Using finite mode, data generation stops automatically after reaching the end of the output data.
- numPnts: number of points to be generated (length of output data)
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.