Writes the specified number of samples to the PWM channels at the indicated sampling rate.

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

Syntax

Visual Basic (Declaration)
Public Sub WritePwmBuffer ( _
	clock As Hil..::.Clock, _
	frequency As Double, _
	numSamples As Integer, _
	channels As Integer(), _
	buffer As Double() _
)
C#
public void WritePwmBuffer(
	Hil..::.Clock clock,
	double frequency,
	int numSamples,
	int[] channels,
	double[] buffer
)
Visual C++
public:
void WritePwmBuffer(
	Hil..::.Clock clock, 
	double frequency, 
	int numSamples, 
	array<int>^ channels, 
	array<double>^ buffer
)
JavaScript
function writePwmBuffer(clock, frequency, numSamples, channels, buffer);

Parameters

clock
Type: Quanser.Hardware..::.Hil..::.Clock

The clock used to time the operation. Note that some clocks allow faster sampling rates than others. See the Hil..::.Clock enumeration for more information on clocks.

Select a board type to the list for board-specific details: .

frequency
Type: System..::.Double

The frequency in Hertz at which to write to the PWM output channels. For example, if frequency is set to 1000, then the WritePwmBuffer method will write all the selected channels every millisecond.

numSamples
Type: System..::.Int32

The number of samples to generate. Each "sample" consists of all the PWM output channels specified. For example, if frequency is set to 1000 and numSamples is set to 5000, then the WritePwmBuffer method will return after 5 seconds, having written 5000 samples. If 3 channels have been selected, then the buffer must therefore contain 15,000 elements.

channels
Type: array< System..::.Int32 >[]()[]

An array containing the numbers of the PWM output channels to which to write. Channel numbers are zero-based. Thus, channel 0 is the first channel, channel 1 the second channel, etc.

Select a board type to the list for board-specific details: .

buffer
Type: array< System..::.Double >[]()[]

An array containing the values to write to the PWM outputs. How these values are interpreted depends on the PWM mode. The PWM mode is configured using the SetPwmMode(array<Int32>[]()[], array<Hil..::.PwmMode>[]()[]) method. The array must contain channels.Length * numSamples elements. The array must be organized as a linear array of samples, with each sample consisting of a group of channels. For example, if PWM output 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 ...

Remarks

The WritePwmBuffer method writes the specified number of samples to the PWM channels at the given sampling frequency. This method does not return until all the data has been written.

The interpretation of the PWM samples to be written depends upon the PWM mode. Typically the data is interpreted as a duty cycle, in which a magnitude of 0.0 denotes a 0% duty cycle and magnitude of 1.0 indicates a 100% duty cycle. The sign determines the polarity of the output for those boards supporting bidirectional PWM outputs. However, other PWM modes are possible with some boards. Refer to the SetPwmMode(array<Int32>[]()[], array<Hil..::.PwmMode>[]()[]) method for details.

Examples

This example illustrates how to write PWM outputs at a specified rate. It writes 5000 samples to PWM channels 0-1 at 1 kHz. The call to WritePwmBuffer does not return until all the samples have been written, 5 seconds later. One Hertz sine waves are written to the outputs, by varying the duty cycle from 0% to 100% in a sine wave pattern. Unipolar PWM outputs are assumed. Exceptions are ignored for simplicity.
C# Copy Code
int []    channels  = { 0, 1 };
double    frequency = 1000;
int       samples   = 5000;
double [] buffer    = new double [samples * channels.Length];
double    time;
int       s, c;

for (s = 0; s < samples; s++) {
   time = s / frequency;
   for (c = 0; c < channels.Length; c++) {
       buffer[s * channels.Length + c] = 0.5 + 0.5 * Math.sin(2 * Math.PI * time);
   }
}
   
card.WritePwmBuffer(Hil.Clock.Hardware0, frequency, samples, channels, buffer);
Visual Basic Copy Code
Dim channels() As Integer = {0, 1}
Dim frequency As Double = 1000
Dim samples As Integer = 5000
Dim buffer(samples * channels.Length - 1) As Double
Dim time As Double
Dim s As Integer
Dim c As Integer

For s = 0 To samples
   time = s / frequency
   For c = 0 To channels.Length
       buffer(s * channels.Length + c) = 0.5 + 0.5 * Math.sin(2 * Math.PI * time)
   Next
Next

card.WritePwmBuffer(Hil.Clock.Hardware0, frequency, samples, channels, buffer)
Visual C++ Copy Code
array<int>^    channels  = { 0, 1 };
double         frequency = 1000;
int            samples   = 5000;
array<double>^ buffer    = gcnew array<double>(samples * channels->Length);
double         time;
int            s, c;

for (s = 0; s < samples; s++) {
   time = s / frequency;
   for (c = 0; c < channels->Length; c++) {
       buffer[s * channels->Length + c] = 0.5 + 0.5 * Math::sin(2 * Math::PI * time);
   }
}
   
card->WritePwmBuffer(Hil::Clock::Hardware0, frequency, samples, channels, buffer);

Exceptions

ExceptionCondition
Quanser.Hardware..::.HilException If the write cannot be performed then an exception is thrown. This situtation typically arises if the board does not support PWM outputs or the hardware resources required are in use by a task.

See Also