OscilloscopeStream.cs

/* OscilloscopeStream.cs - for LibTiePie 0.5+
 *
 * This example performs a stream mode measurement and writes the data to OscilloscopeStream.csv.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

using System;
using System.IO;
using System.Threading;
using TiePie.LibTiePie;

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

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

        // Try to open an oscilloscope with with stream measurement support:
        TiePie.LibTiePie.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 stream measurement support:
                if ((scp.MeasureModes & Constants.MM_STREAM) != 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.Stream;

                // Set sample frequency:
                scp.SampleFrequency = 1e3; // 1 kHz

                // Set record length:
                scp.RecordLength = 1000; // 1 kS
                UInt64 recordLength = scp.RecordLength; // Read actual record length.

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

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

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

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

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

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

                if (File.Exists(filename))
                {
                    // Start measurement:
                    scp.Start();

                    // 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);

                    UInt64 currentSample = 0;

                    for (UInt16 chunk = 0; chunk <= 9; chunk++) // Measure 10 chunks.
                    {
                        // Print a message, to inform the user that we still do something:
                        Console.WriteLine("Data chunk " + chunk.ToString());

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

                        // Throw error on data overflow:
                        if (scp.IsDataOverflow)
                        {
                            throw new System.Exception("Data overflow!");
                        }

                        // Get data:
                        float[][] data = scp.GetData();

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

                        currentSample += recordLength;
                    }

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

                    // Close file:
                    file.Close();

                    // Stop measurement:
                    scp.Stop();
                }
                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 stream measurement support!");
            Environment.Exit(1);
        }

        Environment.Exit(0);
    }
}