Creates a task for reading from digital inputs.

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

Syntax

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

Parameters

samplesInBuffer
Type: System..::.Int32

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

channels
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: .

Return Value

Returns a Hil..::.Task interface for manipulating the task, including starting and stopping the task, and for reading the samples collected by the task "in the background".

Remarks

The TaskCreateDigitalReader method creates a task for reading from the specified digital input channels. The task allows other operations to be performed while the digital inputs are being read "in the background". The data is read into an internal circular "task buffer" from which it can be read at any time using the ReadDigital(Int32, array<SByte>[]()[]) method. The size of this task buffer is determined by the samplesInBuffer parameter.

The task does not actually start reading from the digital inputs 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. 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 ReadDigital(Int32, array<SByte>[]()[]) method failing to read the digital I/O as expected.

Examples

This example illustrates how to read digital inputs using a task. The task reads digital channels 0-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 [] channels        = { 0, 1, 2, 3 };
double frequency       = 1000;
int    samples         = 5000;
int    samplesInBuffer = frequency;
int    samplesToRead   = 1;

sbyte [] buffer = new sbyte [samplesToRead * channels.Length];
Hil.Task task;

task = card.TaskCreateDigitalReader(samplesInBuffer, channels);
task.Start(Hil.Clock.Hardware0, frequency, samples);
for (int index = 0; index < samples; index += samplesToRead) {
    /* Block (if necessary) waiting for next samplesToRead samples */
    task.ReadDigital(samplesToRead, buffer);

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

Dim buffer(samplesToRead * channels.Length - 1) As SByte
Dim task As Hil.Task
Dim index As Integer

task = card.TaskCreateDigitalReader(samplesInBuffer, channels)
task.Start(Hil.Clock.Hardware0, frequency, samples)
For index = 0 To samples - 1 Step samplesToRead
    ' Block (if necessary) waiting for next samplesToRead samples
    task.ReadDigital(samplesToRead, buffer)

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

array<char>^ buffer = gcnew array<char>(samplesToRead * channels->Length);
Hil::Task^ task;

task = card->TaskCreateDigitalReader(samplesInBuffer, channels);
task->Start(Hil::Clock::Hardware0, frequency, samples);
for (int index = 0; index < samples; index += samplesToRead) {
    /* Block (if necessary) waiting for next samplesToRead samples */
    task->ReadDigital(samplesToRead, buffer);

    /* ... process sample ... */
}
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 inputs or tasks.

See Also