Reads the specified number of samples from the task buffer of an encoder reader task.
Namespace:
Quanser.Hardware
Assembly:
Quanser.Hardware.Hil (in Quanser.Hardware.Hil.dll)
Syntax
Visual Basic (Declaration) |
---|
Function ReadEncoder ( _
numSamples As Integer, _
encoderBuffer As Integer() _
) As Integer |
C# |
---|
int ReadEncoder(
int numSamples,
int[] encoderBuffer
) |
Visual C++ |
---|
int ReadEncoder(
int numSamples,
array<int>^ encoderBuffer
) |
JavaScript |
---|
function readEncoder(numSamples, encoderBuffer); |
Parameters
- numSamples
- Type: System..::.Int32
The number of samples to read from the task buffer. Each "sample" consists of all the encoder
input channels specified when the task was created using TaskCreateEncoderReader(Int32, array<Int32>[]()[]).
For example, if numSamples is 5 and the task is configured to read three
channels, then the output buffer will contain 15 elements.
- encoderBuffer
- Type: array<
System..::.Int32
>[]()[]
An array for receiving the count values read from the encoder inputs. The
array must contain numChannels * numSamples
elements, where numChannels
is the number of 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 encoder 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:
Return Value
The return value is the number of samples read from the task 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).
Remarks
Examples
This example illustrates how to read encoder inputs using a task. The task reads encoder 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;
int [] buffer = new int [samplesToRead * channels.Length];
Hil.Task task;
task = card.TaskCreateEncoderReader(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.ReadEncoder(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 Integer
Dim task As Hil.Task
Dim index As Integer
task = card.TaskCreateEncoderReader(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.ReadEncoder(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<int>^ buffer = gcnew array<int>(samplesToRead * channels->Length);
Hil::Task^ task;
task = card->TaskCreateEncoderReader(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->ReadEncoder(samplesToRead, buffer);
/* ... process sample ... */
}
task->Stop();
|
Exceptions
Exception | Condition |
---|
Quanser.Hardware..::.HilException |
If the read cannot be performed then an exception is thrown. This situtation
typically arises if the task buffer overflowed (ran out of space) after the last
call to this method.
|
See Also