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

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

Syntax

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

Parameters

samplesInBuffer
Type: System..::.Int32

The number of samples in the task buffers. The ReadDigitalWriteDigital(Int32, array<SByte>[]()[], array<SByte>[]()[]) method cannot read or write more samples than this in a single call. If the task input buffer overflows because ReadDigitalWriteDigital(Int32, array<SByte>[]()[], array<SByte>[]()[]) has not been called in time to remove the data from the task buffer then the next call to ReadDigitalWriteDigital(Int32, array<SByte>[]()[], array<SByte>[]()[]) will throw an HilException exception. Likewise, if the task output buffer underflows because ReadDigitalWriteDigital(Int32, array<SByte>[]()[], array<SByte>[]()[]) has not been called in time to add data to the task output buffer then the next call to ReadDigitalWriteDigital(Int32, array<SByte>[]()[], array<SByte>[]()[]) 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 digital 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 digital 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 TaskCreateDigitalReaderDigitalWriter method creates a task for reading from the specified digital input channels and writing to the specified digital output channels at the same time. The task allows other operations to be performed while the digital inputs are being read and the digital outputs are being written "in the background". The data read from the digital inputs is stored in an internal circular "task input buffer". The data written to the digital 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 ReadDigitalWriteDigital(Int32, array<SByte>[]()[], array<SByte>[]()[]) method. Data may also be read separately from the task input buffer using the ReadDigital(Int32, array<SByte>[]()[]) method, and data may also be written into the task output buffer using the WriteDigital(Int32, array<SByte>[]()[]) method. The size of this task buffer is determined by the samplesInBuffer parameter.

The WriteDigital(Int32, array<SByte>[]()[]) 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, ReadDigitalWriteDigital(Int32, array<SByte>[]()[], array<SByte>[]()[]) is typically called to read the digital input values from the task input buffer and to put more data into the task output buffer for the digital outputs.

The task does not actually start reading from the digital inputs and storing the data in the task input buffer or reading the data from the task output buffer and writing it to the digital outputs until the Start(Hil..::.Clock, Double, Int32) method is called. Before starting the task, the directions of the digital I/O lines should be set using the SetDigitalDirections(array<Int32>[]()[], array<Int32>[]()[]) method. In order for data to be available in the task buffer as soon as the task starts, also store data in the buffer using WriteDigital(Int32, array<SByte>[]()[]) prior to starting the task.

Each sampling instant, the task first reads from the digital 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 digital 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 digital 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, ReadDigitalWriteDigital(Int32, array<SByte>[]()[], array<SByte>[]()[]) or WriteDigital(Int32, array<SByte>[]()[]) 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 digital outputs and will return with an HilException exception the next time ReadDigitalWriteDigital(Int32, array<SByte>[]()[], array<SByte>[]()[]) or WriteDigital(Int32, array<SByte>[]()[]) is called. See Hil..::.Task for more information on tasks.

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.

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 task cannot be created then an exception is thrown. This situtation typically arises if the board does not support digital I/O or tasks.

See Also