Oscilloscoop voorbeeld

View on GitHub

/* 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.LibTiePie;

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 (UInt32 i = 0; i < DeviceList.Count; i++)
        {
            DeviceListItem item = DeviceList.GetItemByIndex(i);
            if (item.CanOpen(DeviceType.Oscilloscope))
            {
                scp = item.OpenOscilloscope();

                // Check for block measurement support:
                if ((scp.MeasureModes & Constants.MM_BLOCK) != 0)
                {
                    break;
                }
                else
                {
                    scp.Dispose();
                    scp = null;
                }
            }
        }

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

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

                // Set sample frequency:
                scp.SampleFrequency = 1e6; // 1 MHz

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

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

                // For all channels:
                for (UInt16 ch = 0; ch < channelCount; ch++)
                {
                    OscilloscopeChannel channel = scp.Channels[ch];

                    if (!channel.IsAvailable)
                    {
                        continue;
                    }

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

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

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

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

                // Disable all channel trigger sources:
                for (UInt16 ch = 0; ch < channelCount; ch++)
                {
                    scp.Channels[ch].Trigger.Enabled = false;
                }

                // Setup channel trigger:
                OscilloscopeChannelTrigger 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:
                float[][] data = scp.GetData();

                // Open file with write/update permissions:
                string filename = "OscilloscopeBlock.csv";
                StreamWriter file = new StreamWriter(filename, false);

                // Write the data to csv:
                if (File.Exists(filename))
                {
                    // Write csv header:
                    file.Write("Sample");
                    for (UInt16 i = 0; i < channelCount; i++)
                    {
                        file.Write(string.Format(";Ch{0}", i + 1));
                    }
                    file.Write(Environment.NewLine);

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

                    Console.WriteLine("Data written to: " + filename);

                    // Close file:
                    file.Close();
                }
                else
                {
                    Console.WriteLine("Couldn't open file: " + filename);
                    Environment.Exit(1);
                }
            }
            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);
    }
}