Writes the specified number of samples to the task buffer of an other writer task.

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

Syntax

Visual Basic (Declaration)
Function WriteOther ( _
	numSamples As Integer, _
	otherBuffer As Double() _
) As Integer
C#
int WriteOther(
	int numSamples,
	double[] otherBuffer
)
Visual C++
int WriteOther(
	int numSamples, 
	array<double>^ otherBuffer
)
JavaScript
function writeOther(numSamples, otherBuffer);

Parameters

numSamples
Type: System..::.Int32
The number of samples to write to the task buffer. Each "sample" consists of all the other output channels specified when the task was created using TaskCreateOtherWriter(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.
otherBuffer
Type: array< System..::.Double >[]()[]

An array containing the values to write to the other 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 other 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:

0 1 3 0 1 3 ...

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 WriteOther 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

The WriteOther method writes the specified number of samples to the task buffer of a task created using the TaskCreateOtherWriter(Int32, array<Int32>[]()[]) method. If there's not enough space in the task buffer, then this method will block until space for the requested number of samples becomes available or the task stops. Since the task removes data from the task buffer and writes it to the hardware 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 only blocks until there is enough space available in the task buffer. Because the task buffer is depleted at a given sampling rate, calling this function only synchronizes the caller to that sampling rate if the task buffer is kept full. 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, WriteOther should be used to put data into the task buffer prior to starting the task!.

Writer tasks are typically used to stream data to HIL hardware. In this case the numSamples parameter is typically half the number of samples in the task buffer to implement double-buffering.

Examples

This example illustrates how to write other outputs using a task. The task writes other 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.TaskCreateOtherWriter(samplesInBuffer, channels);

/* Preload task buffer with first samplesToWrite samples prior to starting task */
task.WriteOther(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.WriteOther(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.TaskCreateOtherWriter(samplesInBuffer, channels)

' Preload task buffer with first samplesToWrite samples prior to starting task
task.WriteOther(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.WriteOther(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->TaskCreateOtherWriter(samplesInBuffer, channels);

/* Preload task buffer with first samplesToWrite samples prior to starting task */
task->WriteOther(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->WriteOther(samplesToWrite, buffer);
}

/* Flush to make sure all data has been written to the hardware before stopping task */
task->Flush();
task->Stop();

Exceptions

ExceptionCondition
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