Writes the specified number of samples to the task buffer of an analog writer task.
Namespace:
Quanser.Hardware
Assembly:
Quanser.Hardware.Hil (in Quanser.Hardware.Hil.dll)
Syntax
Visual Basic (Declaration) |
---|
Function WriteAnalog ( _
numSamples As Integer, _
analogBuffer As Double() _
) As Integer |
C# |
---|
int WriteAnalog(
int numSamples,
double[] analogBuffer
) |
Visual C++ |
---|
int WriteAnalog(
int numSamples,
array<double>^ analogBuffer
) |
JavaScript |
---|
function writeAnalog(numSamples, analogBuffer); |
Parameters
- numSamples
- Type: System..::.Int32
The number of samples to write to the task buffer. Each "sample" consists of all the analog
output channels specified when the task was created using TaskCreateAnalogWriter(Int32, array<Int32>[]()[]).
For example, if numSamples is 5 and the task is configured to write three
channels, then the input buffer must contain at least 15 elements.
- analogBuffer
- Type: array<
System..::.Double
>[]()[]
An array containing the voltage values to write to the analog outputs. 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 analog input 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:
Return Value
The return value is the number of samples written to the task buffer. This value
may be less than the requested number of samples (including 0) if the task
buffer does not have sufficient space and 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 WriteAnalog
can write more samples in total then the total number of samples specified in
the call to Start(Hil..::.Clock, Double, Int32). However, only the number of samples specified in
Start(Hil..::.Clock, Double, Int32) will actually be processed and written to the hardware.
Remarks
Examples
This example illustrates how to write analog outputs using a task. The task writes analog channels 0-3
every millisecond using a hardware clock. The data to write is computed every 0.1 seconds, with 100 samples
being computed at a time. The task 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 samplesToWrite = 100;
double [] buffer = new double [samplesToWrite * channels.Length];
Hil.Task task;
/* ... fill buffer with samplesToWrite samples ... */
/* Create task */
task = Hil.TaskCreateAnalogWriter(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 = samples; index < samples; index += samples_to_write) {
/* ... fill buffer with next samplesToWrite samples ... */
/*
Block (if necessary) waiting to write next samplesToWrite samples.
However, it only waits for space in the task buffer (which can contain
samplesInBuffer samples), not for the data to actually be written to
the hardware. Hence, it will not block until the task buffer is full
(a little over one second in this example since samplesInBuffer is 1000).
*/
task.WriteAnalog(samplesToWrite, buffer);
}
/* Flush to make sure all data has been written to the hardware before stopping task */
task.Flush();
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 samplesToWrite As Integer = 100
Dim buffer(samplesToWrite * channels.Length - 1) As Double
Dim task As Hil.Task
Dim index As Integer
' ... fill buffer with samplesToWrite samples ...
' Create task
task = card.TaskCreateAnalogWriter(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 index = 0 To samples - 1 Step samplesToWrite
' ... fill buffer with next samplesToWrite sample ...
' Block (if necessary) waiting to write next samplesToWrite samples.
' However, it only waits for space in the task buffer (which can contain
' samplesInBuffer samples), not for the data to actually be written to
' the hardware. Hence, it will not block until the task buffer is full
' (a little over one second in this example since samplesInBuffer is 1000).
task.WriteAnalog(samplesToWrite, buffer)
Next
' Flush to make sure all data has been written to the hardware before stopping task
task.Flush()
task.Stop()
|
Visual C++ | Copy Code |
---|
array<int>^ channels = { 0, 1, 2, 3 };
double frequency = 1000;
int samples = 5000;
int samplesInBuffer = frequency;
int samplesToWrite = 100;
array<double>^ buffer = gcnew array<double>(samplesToWrite * channels->Length);
Hil::Task^ task;
/* ... fill buffer with samplesToWrite samples ... */
/* Create task */
task = card->TaskCreateAnalogWriter(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 += samplesToWrite) {
/* ... fill buffer with next samplesToWrite samples ... */
/*
Block (if necessary) waiting to write next samplesToWrite samples.
However, it only waits for space in the task buffer (which can contain
samplesInBuffer samples), not for the data to actually be written to
the hardware. Hence, it will not block until the task buffer is full
(a little over one second in this example since samplesInBuffer is 1000).
*/
task->WriteAnalog(samplesToWrite, buffer);
}
/* Flush to make sure all data has been written to the hardware before stopping task */
task->Flush();
task->Stop();
|
Exceptions
Exception | Condition |
---|
Quanser.Hardware..::.HilException |
If the write cannot be performed then an exception is thrown. This situtation
typically arises if the task buffer underflowed (ran out of data) after the last
call to this method.
|
See Also