OscilloscopeBlock.cs

/* OscilloscopeBlock.cs
 *
 * This example performs a block mode measurement and writes the data to OscilloscopeBlock.csv.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

using System;
using System.IO;
using System.Threading;
using TiePie.Hardware;

class OscilloscopeBlockExample
{
    public static void Main()
    {
        // Print library information:
        PrintInfo.PrintLibraryInfo();

        // Enable network search:
        Network.AutoDetectEnabled = true;

        // Update device list:
        DeviceList.Update();

        // Try to open an oscilloscope with block measurement support:
        Oscilloscope scp = null;

        for (var i = 0u; i < DeviceList.Count; i++)
        {
            using (var item = DeviceList.GetItemByIndex(i))
            {
                if (item.CanOpen(DeviceType.Oscilloscope))
                {
                    scp = item.OpenOscilloscope();

                    // Check for block measurement support:
                    if (scp.MeasureModes.HasFlag(MeasureMode.Block))
                        break;
                    else
                    {
                        scp.Dispose();
                        scp = null;
                    }
                }
            }
        }

        if (scp != null)
        {
            try
            {
                // Get the number of channels:
                var channelCount = scp.Channels.Count;

                // Set measure mode:
                scp.MeasureMode = MeasureMode.Block;

                // Set sample rate:
                scp.SampleRate = 1e6; // 1 MHz

                // Set record length:
                scp.RecordLength = 10000; // 10 kS
                var recordLength = scp.RecordLength; // Read actual record length.

                // Set pre sample ratio:
                scp.PreSampleRatio = 0; // 0 %

                // For all channels:
                foreach (var ch in scp.Channels)
                {
                    if (!ch.IsAvailable)
                        continue;

                    // Enable channel to measure it:
                    ch.Enabled = true;

                    // Set range:
                    ch.Range = 8; // 8 V

                    // Set coupling:
                    ch.Coupling = Coupling.DCV; // DC Volt
                }

                // Set trigger timeout:
                scp.Trigger.TimeOut = 100e-3; // 100 ms

                // Disable all channel trigger sources:
                foreach (var ch in scp.Channels)
                    ch.Trigger.Enabled = false;

                // Setup channel trigger:
                var channelTrigger = scp.Channels[0].Trigger; // Ch 1

                // Enable trigger source:
                channelTrigger.Enabled = true;

                // Kind:
                channelTrigger.Kind = TriggerKind.RisingEdge;

                // Level:
                channelTrigger.Level[0] = 0.5; // 50 %

                // Hysteresis:
                channelTrigger.Hysteresis[0] = 0.05; // 5 %

                // Print oscilloscope info:
                PrintInfo.PrintDeviceInfo(scp);

                // Start measurement:
                scp.Start();

                // Wait for measurement to complete:
                while (!scp.IsDataReady)
                    Thread.Sleep(10); // 10 ms delay, to save CPU time.

                // Get data:
                var data = scp.GetData();

                // Write the data to csv:
                var filename = "OscilloscopeBlock.csv";
                using (var file = new StreamWriter(filename))
                {
                    // Write csv header:
                    file.Write("Sample");
                    for (var i = 0; i < channelCount; i++)
                        file.Write($",Ch{i + 1}");
                    file.Write(Environment.NewLine);

                    // Write the data to csv:
                    for (var i = 0ul; i < recordLength; i++)
                    {
                        file.Write($"{i}");
                        for (var ch = 0; ch < channelCount; ch++)
                        {
                            if (data[ch] != null)
                                file.Write($",{data[ch][i]}");
                        }
                        file.Write(Environment.NewLine);
                    }
                }

                Console.WriteLine($"Data written to: {filename}");
            }
            catch (System.Exception e)
            {
                Console.WriteLine($"Exception: {e.Message}");
                Environment.Exit(1);
            }

            // Close oscilloscope:
            scp.Dispose();
            scp = null;
        }
        else
        {
            Console.WriteLine("No oscilloscope available with block measurement support!");
            Environment.Exit(1);
        }

        Environment.Exit(0);
    }
}