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 digital reader-writer task.

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

Syntax

Visual Basic (Declaration)
Function ReadDigitalWriteDigital ( _
	numSamples As Integer, _
	digitalInputBuffer As SByte(), _
	digitalOutputBuffer As SByte() _
) As Integer
C#
int ReadDigitalWriteDigital(
	int numSamples,
	sbyte[] digitalInputBuffer,
	sbyte[] digitalOutputBuffer
)
Visual C++
int ReadDigitalWriteDigital(
	int numSamples, 
	array<signed char>^ digitalInputBuffer, 
	array<signed char>^ digitalOutputBuffer
)
JavaScript
function readDigitalWriteDigital(numSamples, digitalInputBuffer, digitalOutputBuffer);

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 digital input channels and digital output channels specified when the task was created using TaskCreateDigitalReaderDigitalWriter(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.
digitalInputBuffer
Type: array< System..::.SByte >[]()[]

An array for receiving the state values read from the digital 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 digital 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 ...
digitalOutputBuffer
Type: array< System..::.SByte >[]()[]

An array containing the state values to write to the digital 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 digital 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 ReadDigitalWriteDigital 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 WriteDigital(Int32, array<SByte>[]()[]) but only the number of samples specified in Start(Hil..::.Clock, Double, Int32) will actually be processed and written to the hardware.

Remarks

The ReadDigitalWriteDigital 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 TaskCreateDigitalReaderDigitalWriter(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 digital inputs and stores the data in the task input buffer, and removes data from the task output buffer and writes it to the digital 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 digital outputs to be updated without interruption, even though the method waits for the digital 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 ReadDigitalWriteDigital 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. Before starting the task, the directions of the digital I/O lines should be set using the SetDigitalDirections(array<Int32>[]()[], array<Int32>[]()[]) method.

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, WriteDigital(Int32, array<SByte>[]()[]) should be used to put data into the task output buffer prior to starting the task!.

Warning

Many cards allow the digital I/O lines to be programmed as inputs or outputs. The digital I/O lines are configured as inputs or outputs using the SetDigitalDirections(array<Int32>[]()[], array<Int32>[]()[]) method. All the channels which will be used as digital inputs or outputs must be configured accordingly using this function. Failure to configure the digital I/O may result in the ReadDigitalWriteDigital(Int32, array<SByte>[]()[], array<SByte>[]()[]) method failing to read or write the digital I/O as expected.

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 digital inputs and write digital outputs at the same time using a task. The task reads digital input channels 0-1 while writing to digital 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;

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

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

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

/* Preload task output buffer with first samplesToReadWrite samples prior to starting task */
task.WriteDigital(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.ReadDigitalWriteDigital(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 SByte
Dim outputBuffer(samplesToReadWrite * outputChannels.Length - 1) As SByte
Dim task As Hil.Task
Dim index As Integer

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

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

' Preload task buffer with first samplesToReadWrite samples prior to starting task
task.WriteDigital(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.ReadDigitalWriteDigital(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<char>^ inputBuffer  = gcnew array<char>(samplesToReadWrite * inputChannels->Length);
array<char>^ outputBuffer = gcnew array<char>(samplesToReadWrite * outputChannels->Length);
Hil::Task^ task;

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

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

/* Preload task buffer with first samplesToWrite samples prior to starting task */
task->WriteDigital(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->ReadDigitalWriteDigital(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