Reads the specified number of samples from the task input buffer and writes the same number of samples to the task output buffer of an analog reader-writer task.

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

Syntax

Visual Basic (Declaration)
Function ReadAnalogWriteAnalog ( _
	numSamples As Integer, _
	analogInputBuffer As Double(), _
	analogOutputBuffer As Double() _
) As Integer
C#
int ReadAnalogWriteAnalog(
	int numSamples,
	double[] analogInputBuffer,
	double[] analogOutputBuffer
)
Visual C++
int ReadAnalogWriteAnalog(
	int numSamples, 
	array<double>^ analogInputBuffer, 
	array<double>^ analogOutputBuffer
)
JavaScript
function readAnalogWriteAnalog(numSamples, analogInputBuffer, analogOutputBuffer);

Parameters

numSamples
Type: System..::.Int32
The number of samples to read from the task input buffer and to write to the task output buffer. Each "sample" consists of all the analog input channels and analog output channels specified when the task was created using TaskCreateAnalogReaderAnalogWriter(Int32, array<Int32>[]()[], array<Int32>[]()[]). For example, if numSamples is 5 and the task is configured to read two channels and write three channels, then the input buffer must contain at least 10 elements and the output buffer must contain at least 15 elements.
analogInputBuffer
Type: array< System..::.Double >[]()[]

An array for receiving the voltage values read from the analog inputs. The array must contain numInputChannels * numSamples elements, where numInputChannels is the number of input channels specified when the task was created. The array is organized as a linear array of samples, with each sample consisting of a group of channels. For example, if analog input channels 0, 1 and 3 are being read, than the data appears in the array as follows, where the numbers correspond to channel numbers:

0 1 3 0 1 3 ...
analogOutputBuffer
Type: array< System..::.Double >[]()[]

An array containing the voltage values to write to the analog outputs. The array must contain numOutputChannels * numSamples elements, where numOutputChannels is the number of output channels specified when the task was created. The array must be organized as a linear array of samples, with each sample consisting of a group of channels. For example, if analog output channels 0, 1 and 3 are being written, than the data must appear in the array as follows, where the numbers correspond to channel numbers:

0 1 3 0 1 3 ...

Return Value

The return value is the number of samples read from the task input buffer and written to the task output buffer. This value may be less than the requested number of samples (including 0) if the task is stopped or has finished processing the total number of samples indicated in the call to Start(Hil..::.Clock, Double, Int32).

Note that successive calls to ReadAnalogWriteAnalog can never read more samples in total than the total number of samples specified in the call to Start(Hil..::.Clock, Double, Int32). It is possible to write more samples than this to the task output buffer using WriteAnalog(Int32, array<Double>[]()[]) but only the number of samples specified in Start(Hil..::.Clock, Double, Int32) will actually be processed and written to the hardware.

Remarks

The ReadAnalogWriteAnalog method reads the specified number of samples from the task input buffer and writes the same number of samples to the task output buffer of a task created using TaskCreateAnalogReaderAnalogWriter(Int32, array<Int32>[]()[], array<Int32>[]()[]). If the requested number of samples is not yet available in the task input buffer then this method will block until the specified number of samples is available. Also, if there's not enough space in the task output buffer, then this method will block until there is space in the task output buffer or the task stops. Since the task both reads the analog inputs and stores the data in the task input buffer, and removes data from the task output buffer and writes it to the analog outputs at the sampling rate specified in the call to Start(Hil..::.Clock, Double, Int32), this method will never block for longer than the given number of samples times the sampling period.

Note that this method first reads as many samples as it can from the task input buffer and writes as many samples as it can to the task output buffer without blocking. Only then does it block if necessary to complete the operation. Thus, it will put as much data as it can in the task output buffer before waiting for the task input buffer to be filled with the requisite number of samples. These semantics allow the analog outputs to be updated without interruption, even though the method waits for the analog inputs to be read.

Because this method blocks until enough input data is available and the task input buffer is filled at a given sampling rate, calling this function synchronizes the caller to that sampling rate (provided the task input buffer is not being filled faster than we can read the data). Thus, the ReadAnalogWriteAnalog method may be used to implement system identification, synchronous data streaming and other operations requiring a fixed sampling rate. For data streaming, the numSamples parameter is typically half the number of samples in the task buffers to implement double-buffering.

The task output buffer is depleted at the same sampling rate. Hence, data must be written to the task buffer before the task buffer is completely depleted or else the next attempt to write to the task buffer will throw a HilExceptionBufferOverflow exception. As a result, WriteAnalog(Int32, array<Double>[]()[]) should be used to put data into the task output buffer prior to starting the task!.

Internally, samples are always read from the hardware prior to writing to the hardware. Hence, in a loopback test, the values read always reflect the samples written in the previous sampling instant. These semantics help to ensure that the inputs are not sampled while the outputs are transitioning to their new states.

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 read or write cannot be performed then an exception is thrown. This situtation typically arises if the task output buffer underflowed (ran out of data) or the task input buffer overflowed (ran out of space) after the last call to this method.

See Also