OscilloscopeConnectionTest.cs

/* OscilloscopeConnectionTest.cs - for LibTiePie 0.5+
 *
 * This example performs a connection test.
 *
 * Find more information on http://www.tiepie.com/LibTiePie .
 */

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

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

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

        // Try to open an oscilloscope with connection test 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 connection test support:
                if (scp.HasConnectionTest)
                {
                    break;
                }
                else
                {
                    scp.Dispose();
                    scp = null;
                }
            }
        }

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

                // Enable all channels that support connection testing:
                for (UInt16 Ch = 0; Ch < channelCount; Ch++)
                {
                    scp.Channels[Ch].Enabled = scp.Channels[Ch].HasConnectionTest;
                }

                // Start connection on current active channels:
                scp.StartConnectionTest();

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

                // Get data:
                TriState[] Data = scp.GetConnectionTestData();

                // Print results:
                Console.WriteLine("Connection test result:");
                for (UInt16 ch = 0; ch < channelCount; ch++)
                {
                    Console.Write("Ch" + ch.ToString() + " = ");

                    switch (Data[ch])
                    {
                        case TriState.Undefined:
                            Console.WriteLine("undefined");
                            break;

                        case TriState.False:
                            Console.WriteLine("false");
                            break;

                        case TriState.True:
                            Console.WriteLine("true");
                            break;

                        default:
                            Console.WriteLine("unknown state");
                            break;
                    }
                }
            }
            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 connection test support!");
            Environment.Exit(1);
        }

        Environment.Exit(0);
    }
}