Skip to content

CtLab Library Code Examples

JosiCoder edited this page Nov 13, 2016 · 3 revisions

Here are some code examples to demonstrate how the CtLab Library is typically used.

Samples on the Device Level

These samples show how device-specific interfaces are used. They are related to the FPGA Lab. However, similar interfaces can be easily developed for other devices.

AM Generator Sample

The code below generates an amplitude-modulated signal using two DDS generator channels.

// Configure DDS channel 0 (carrier, modulated by DDS channel 1).
signalGenerator.DdsGenerators[0].Waveform = Waveforms.Sine;
signalGenerator.DdsGenerators[0].Frequency = 1000;
signalGenerator.DdsGenerators[0].Amplitude = (short)(fpgaLab.DdsGenerators[0].MaximumAmplitude * 1 / 2);
signalGenerator.DdsGenerators[0].AmplitudeModulationSource = ModulationAndSynchronizationSources.DdsGenerator1;

// Configure DDS channel 1 (modulator).
signalGenerator.DdsGenerators[1].Waveform = Waveforms.Sine;
signalGenerator.DdsGenerators[1].Frequency = 100;
signalGenerator.DdsGenerators[1].Amplitude = (short)(fpgaLab.DdsGenerators[1].MaximumAmplitude * 1 / 4);

// Flush all modifications, i.e. send all set commands that have modified values.
appliance.SendSetCommandsForModifiedValues();

Frequency Counter Sample

The following example shows how to configure the universal counter for frequency measurement with a gate time of 100ms. The universal counter is automatically polled for value changes which will raise the ValueChanged event.

// Configure the universal counter.
signalGenerator.UniversalCounter.PrescalerMode = PrescalerModes.GatePeriod_100ms;

// Flush all modifications, i.e. send all set commands that have modified values.
appliance.SendSetCommandsForModifiedValues();

// Listen to counter changes and display them.
signalGenerator.UniversalCounter.ValueChanged +=
    (sender, e) => Console.WriteLine("Counter reported a new frequency: {0}", e.Value);

// Send the cached query commands periodically.
appliance.StartSendingQueryCommands(_queryCommandSendPeriod);

Samples on the Basic Level

These samples show how the low-level interfaces are used. They use commands and messages and are the foundation for the device-specific interfaces.

Command Sample

The code below sends the value of 128 to subchannel 1.

// Send a command.
var setCommandClass = new SetCommandClass(_channel, 1);
setCommandDictionary.Add(setCommandClass);
setCommandClass.SetValue(128);
setCommandDictionary.SendCommandsForModifiedValues();

Message Sample

This example shows how to handle messages, in this case those received from subchannel 255.

// Prepare to receive messages.
var messageContainer = messageCache.Register(_channel, 255);
messageContainer.MessageUpdated +=
    (sender, e) => Console.WriteLine("Message received, channel {0}/{1}, raw value {2}",
                                     messageContainer.Message.Channel,
                                     messageContainer.Message.Subchannel,
                                     messageContainer.Message.RawValue);

Clone this wiki locally