Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How to contineously generate non periodic Analogue Output

Hi,
 
All the DAQmx .NET library examples with contineous analogue output are generating a periodic waveform: The wave form is written to the task's "writer" stream, after which the output task is repeating the waveform again and again.
 
But how do you modify the wave-form "on the fly" ?
 
I tried to modify the "SynchronizeAIAOUsingDigTrigger" example by writing at new waveform to the output-task's writer  in the "InputRead" callback (just after the code where the retrieved input data is plotted on the "InputWaveforGraph") as follows:
------------------------------------------------

// This callback is invoked everytime a Read completes

private void InputRead(IAsyncResult ar)

{

try

{

if (runningTask == ar.AsyncState)

{

// Read the data

#if

NETFX2_0

//For .NET Framework 2.0, use the AnalogWaveform<double> type.

AnalogWaveform<double> data = reader.EndReadWaveform(ar);

#else

//For .NET Framework 1.1, use the AnalogWaveform type.

AnalogWaveform data = reader.EndReadWaveformDouble(ar);

#endif

//inputWaveformGraph.PlotWaveformAppend(data); Changed to Plot:

inputWaveformGraph.ClearData();

inputWaveformGraph.PlotWaveform(data);

// "Write and plot" code added:

double[] data1 = new double[data.Samples.Count];

for (int i = 0; i <= data.Samples.Count-1; i++)

{

data1[i] = (

double)data.Samples[i].Value;

}

writer.WriteMultiSample(

false, data1);

outputWaveformGraph.PlotY(data1);

 

// Set up next callback

#if

NETFX2_0

reader.BeginReadWaveform(

Convert.ToInt32(numberOfSamplesNumericEdit.Value), inputCallback, inputTask);

#else

reader.BeginReadWaveformDouble(Convert.ToInt32(numberOfSamplesNumericEdit.Value), inputCallback, inputTask);

#endif

}

}

catch (Exception ex)

{

StopTask();

MessageBox.Show(ex.Message);

}

}

-----------------------------------------------

That modification had two effects when running the code:

1)  The OutputWaveformGraph is contineously updata "as expected", but the InputWaveformGraph (which, with the code above, should look exactly like the OutputWaveformGraph) is only occasionally updated. Most of the time it is empty but occasionally  it is updated as intended. After pressing "stop" it shows the same contents as the OutputWaveformGraph.

2) When the program is executed on a "real device" (not a simulated device), NI USB-6211, then even with a modest input and output sample frequency (100 Hz with a 100 sample buffer) an "Attempted to read samples that are no longer available" error occurs after ~10 input buffers.

 

I have a feeling  that if I had any idea of how the DAQmx .NET classes actually worked "under the hood" the modification I made to the example was pretty stupid and naive. However: where on earth (i.e. in the documentation of the DAQmx .NET classes) am I supposed to look for information on how to make a more "intelligent" attemp ? Apart from "two line" reference information about the class members, the documentation of how to use each of the members is very sparse.

 

Regards

Morten Hvidberg-Knudsen

0 Kudos
Message 1 of 2
(3,925 Views)
Hi Morten,

From the behavior you have described, my initial guess is that you are experiencing the regeneration effect of DAQmx.  In DAQmx, regeneration refers to generating the same data more than once.  By default, the WriteRegenerationMode property is set to AllowRegeneration. The WriteRegenerationMode property specifies whethere to allow DAQmx to generate the same data multiple times.  As I mentioned, the default state is AllowRegeneration which allows DAQmx to regenerate samples that the device previously generated.  When you choose this value, the write marker returns to the beginning of the buffer after the device generates all the samples currently in the buffer.  What you need to do is set this property to DoNotAllowRegeneration which doesn't allow DAQmx to regenerate the samples the device previously generated. When you choose this value, DAQmx waits for you to write more samples to the buffer or until the timeout expires.  This property is set through the Task.Stream object. In the example you posted, there is a output task called outputTask which is a Task object.  Therefore you would say

outputTask.Stream.WriteRegenerationMode = WriteRegenerationMode.DoNotAllowRegeneration;

Also, I noticed that you are writing to the outputTask in the callback for reading the input (i.e. InputRead). Its better if you create a new callback for outputting following the same pattern as the InputRead callback.

One suggestion that I will make is make you should consider using the DAQ Assistant in Visual Studio. The DAQ Assistant integrates directly into the Visual Studio IDE and is intended to help you get started with including DAQmx functionality in your programs. The DAQ Assistant generates code to configure tasks, read data from a task, display data on a UI, etc.  I would suggest taking a look in the NI Measurement Studio Help at the topics entitled Walkthrough: Creating a Measurement Studio NI-DAQmx Application and Creating a Measurement Studio DAQ .NET Application.  What's great about the DAQ Assistant to is that you can set the regeneration mode property through a nice GUI interface as shown in the attached snapshot.

Since you are also beginning your journey of learning DAQmx, I would suggest taking a look at the Learn 10 Functions in NI-DAQmx and Handle 80 Percent of Your Data Acquisition Application and Transition from Traditional NI-DAQ (Legacy) to NI-DAQmx using Microsoft Visual Basic .NET tutorials. The first tutorial shows some .NET code snippets which should help out and the second tutoriial makes references to Traditioanl DAQ API associated with the CWDAQ controls (which is what you might have been using since you mentioned you were using ComponentWorks). 

You also mentioned that you were strugglign finding the documentation for the DAQmx API. We are always trying to improve our help so if you could perhaps explain what you found was so confusing.

Hope this helps!  Let me know if you are still having issues or if what I am suggesting doesn't fix your problem.

Best Regards,

Jonathan N.
National Instruments
0 Kudos
Message 2 of 2
(3,841 Views)