Creates a task for reading from analog inputs and writing to analog outputs at the same time.

Namespace:  Quanser.Hardware
Assembly:  Quanser.Hardware.Hil (in Quanser.Hardware.Hil.dll)

Syntax

Visual Basic (Declaration)
Public Function TaskCreateAnalogReaderAnalogWriter ( _
	samplesInBuffer As Integer, _
	inputChannels As Integer(), _
	outputChannels As Integer() _
) As Hil..::.Task
C#
public Hil..::.Task TaskCreateAnalogReaderAnalogWriter(
	int samplesInBuffer,
	int[] inputChannels,
	int[] outputChannels
)
Visual C++
public:
Hil..::.Task^ TaskCreateAnalogReaderAnalogWriter(
	int samplesInBuffer, 
	array<int>^ inputChannels, 
	array<int>^ outputChannels
)
JavaScript
function taskCreateAnalogReaderAnalogWriter(samplesInBuffer, inputChannels, outputChannels);

Parameters

samplesInBuffer
Type: System..::.Int32

The number of samples in the task buffers. The ReadAnalogWriteAnalog(Int32, array<Double>[]()[], array<Double>[]()[]) method cannot read or write more samples than this in a single call. If the task input buffer overflows because ReadAnalogWriteAnalog(Int32, array<Double>[]()[], array<Double>[]()[]) has not been called in time to remove the data from the task buffer then the next call to ReadAnalogWriteAnalog(Int32, array<Double>[]()[], array<Double>[]()[]) will throw an HilException exception. Likewise, if the task output buffer underflows because ReadAnalogWriteAnalog(Int32, array<Double>[]()[], array<Double>[]()[]) has not been called in time to add data to the task output buffer then the next call to ReadAnalogWriteAnalog(Int32, array<Double>[]()[], array<Double>[]()[]) will also throw an HilException exception. See Hil..::.Task for more information on task buffers.

inputChannels
Type: array< System..::.Int32 >[]()[]

An array containing the numbers of the analog input channels to be read by the task. Channel numbers are zero-based. Thus, channel 0 is the first channel, channel 1 the second channel, etc.

Select a board type from the list for board-specific details: .

outputChannels
Type: array< System..::.Int32 >[]()[]

An array containing the numbers of the analog output channels to be written by the task. Channel numbers are zero-based. Thus, channel 0 is the first channel, channel 1 the second channel, etc.

Select a board type from the list for board-specific details: .

Return Value

Returns a Hil..::.Task interface for manipulating the task, including starting and stopping the task, and for reading and writing the samples for the task.

Remarks

The TaskCreateAnalogReaderAnalogWriter method creates a task for reading from the specified analog input channels and writing to the specified analog output channels at the same time. The task allows other operations to be performed while the analog inputs are being read and the analog outputs are being written "in the background". The data read from the analog inputs is stored in an internal circular "task input buffer". The data written to the analog outputs is read from an internal circular "task output buffer". The application may read the data from the task input buffer and write data to the task output buffer at any time using the ReadAnalogWriteAnalog(Int32, array<Double>[]()[], array<Double>[]()[]) method. Data may also be read separately from the task input buffer using the ReadAnalog(Int32, array<Double>[]()[]) method, and data may also be written into the task output buffer using the WriteAnalog(Int32, array<Double>[]()[]) method. The size of this task buffer is determined by the samplesInBuffer parameter.

The WriteAnalog(Int32, array<Double>[]()[]) method is typically called prior to starting the task in order to put the initial samples in the the task output buffer. After the task is started, ReadAnalogWriteAnalog(Int32, array<Double>[]()[], array<Double>[]()[]) is typically called to read the analog input values from the task input buffer and to put more data into the task output buffer for the analog outputs.

The task does not actually start reading from the analog inputs and storing the data in the task input buffer or reading the data from the task output buffer and writing it to the analog outputs until the Start(Hil..::.Clock, Double, Int32) method is called. In order for data to be available in the task buffer as soon as the task starts, store data in the buffer using WriteAnalog(Int32, array<Double>[]()[]) prior to starting the task.

Each sampling instant, the task first reads from the analog input channels selected and stores the data in the task input buffer. Then, in the same sampling instant, the task extracts one sample from the task output buffer and writes to the selected analog output channels. This synchronization of input and output is particularly useful for system identification because the time at which a sample is read and a sample is written is known, so no postprocessing is required to determine the response delay in the system been identified.

Since the task writes to the analog outputs at the sampling rate specified when the task is started, it will be reading data from the task output buffer at that rate. Thus, ReadAnalogWriteAnalog(Int32, array<Double>[]()[], array<Double>[]()[]) or WriteAnalog(Int32, array<Double>[]()[]) must be called to add more data to the task output buffer before all the data in the buffer is depleted. Otherwise the task will have no data to write to the analog outputs and will return with an HilException exception the next time ReadAnalogWriteAnalog(Int32, array<Double>[]()[], array<Double>[]()[]) or WriteAnalog(Int32, array<Double>[]()[]) is called. See Hil..::.Task for more information on tasks.

Examples

This example illustrates how to read analog inputs and write analog outputs at the same time using a task. The task reads analog input channels 0-1 while writing to analog output channels 2-3 every millisecond using a hardware clock. The data may be processed each sampling instant. It runs for 5 seconds before stopping. Exceptions are ignored for simplicity.
C# Copy Code
int [] inputChannels      = { 0, 1 };
int [] outputChannels     = { 2, 3 };
double frequency          = 1000;
int    samples            = 5000;
int    samplesInBuffer    = frequency;
int    samplesToReadWrite = 1;

double [] inputBuffer  = new double [samplesToReadWrite * inputChannels.length];
double [] outputBuffer = new double [samplesToReadWrite * outputChannels.length];
Hil.Task task;

/* ... fill output buffer with samplesToReadWrite samples to write ... */

task = card.TaskCreateAnalogReaderAnalogWriter(samplesInBuffer, inputChannels, outputChannels);

/* Preload task output buffer with first samplesToReadWrite samples prior to starting task */
task.WriteAnalog(samplesToReadWrite, outputBuffer);

/* Start task */
task.Start(Hil.Clock.Hardware0, frequency, samples);
for (int index = 0; index < samples; index += samplesToReadWrite) {
    /* ... fill output buffer with next samplesToReadWrite samples to write ... */

    /* 
        Block (if necessary) waiting to read next samplesToReadWrite samples from
        the hardware and for space in the task output buffer.
    */
    task.ReadAnalogWriteAnalog(samplesToReadWrite, inputBuffer, outputBuffer);

    /* ... process samplesToReadWrite samples read ... */
}
task.Stop();
Visual Basic Copy Code
Dim inputChannels() As Integer = {0, 1}
Dim outputChannels() As Integer = {2, 3}
Dim frequency as Double = 1000
Dim samples As Integer = 5000
Dim samplesInBuffer As Integer = frequency
Dim samplesToReadWrite As Integer = 1

Dim inputBuffer(samplesToReadWrite * inputChannels.Length - 1) As Double
Dim outputBuffer(samplesToReadWrite * outputChannels.Length - 1) As Double
Dim task As Hil.Task
Dim index As Integer

' ... fill output buffer with samplesToReadWrite samples to write ...

' Create task
task = card.TaskCreateAnalogReaderAnalogWriter(samplesInBuffer, inputChannels, outputChannels)

' Preload task buffer with first samplesToReadWrite samples prior to starting task
task.WriteAnalog(samplesToWrite, outputBuffer)

' Start task
task.Start(Hil.Clock.Hardware0, frequency, samples)
For index = 0 To samples - 1 Step samplesToReadWrite
    ' ... fill buffer with next samplesToReadWrite samples to write ...

    ' Block (if necessary) waiting to read next samplesToReadWrite samples
    ' from the hardware and for space in the task output buffer.
    task.ReadAnalogWriteAnalog(samplesToReadWrite, inputBuffer, outputBuffer)

    ' ... process samplesToReadWrite samples read ...
Next
task.Stop()
Visual C++ Copy Code
array<int>^ inputChannels      = { 0, 1 };
array<int>^ outputChannels     = { 2, 3 };
double      frequency          = 1000;
int         samples            = 5000;
int         samplesInBuffer    = frequency;
int         samplesToReadWrite = 1;

array<double>^ inputBuffer  = gcnew array<double>(samplesToReadWrite * inputChannels->Length);
array<double>^ outputBuffer = gcnew array<double>(samplesToReadWrite * outputChannels->Length);
Hil::Task^ task;

/* ... fill output buffer with samplesToReadWrite samples to write ... */

/* Create task */
task = card->TaskCreateAnalogReaderAnalogWriter(samplesInBuffer, channels);

/* Preload task buffer with first samplesToWrite samples prior to starting task */
task->WriteAnalog(samplesToWrite, buffer);

/* Start task */
task->Start(Hil::Clock::Hardware0, frequency, samples);
for (int index = 0; index < samples; index += samplesToReadWrite) {
    /* ... fill output buffer with next samplesToReadWrite samples to write ... */

    /* 
        Block (if necessary) waiting to read next samplesToReadWrite samples from
        the hardware and for space in the task output buffer.
    */
    task->ReadAnalogWriteAnalog(samplesToReadWrite, inputBuffer, outputBuffer);

    /* ... process samplesToReadWrite samples read ... */
}
task->Stop();

Exceptions

ExceptionCondition
Quanser.Hardware..::.HilException If the task cannot be created then an exception is thrown. This situtation typically arises if the board does not support analog I/O or tasks.

See Also