Ejemplo n.º 1
0
    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 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);
    }
Ejemplo n.º 2
0
    public static void PrintOscilloscopeInfo(Oscilloscope scp)
    {
        try
        {
            Console.WriteLine("Oscilloscope:");
            Console.WriteLine("  Channel count             : " + scp.Channels.Count.ToString());
            Console.WriteLine("  Connection test           : " + scp.HasConnectionTest.ToString());
            Console.WriteLine("  Measure modes             : " + MeasureModeToStr(scp.MeasureModes));
            Console.WriteLine("  Measure mode              : " + MeasureModeToStr((UInt32)scp.MeasureMode));
            Console.WriteLine("  Auto resolution modes     : " + AutoResolutionModeToStr(scp.AutoResolutionModes));
            Console.WriteLine("  Auto resolution mode      : " + AutoResolutionModeToStr((UInt64)scp.AutoResolutionMode));
            Console.WriteLine("  Resolutions               : " + String.Join(", ", Array.ConvertAll(scp.Resolutions, x => x.ToString())));
            Console.WriteLine("  Resolution                : " + scp.Resolution.ToString());
            Console.WriteLine("  Resolution enhanced       : " + scp.IsResolutionEnhanced.ToString());
            Console.WriteLine("  Clock outputs             : " + ClockOutputToStr(scp.ClockOutputs));
            Console.WriteLine("  Clock output              : " + ClockOutputToStr((UInt32)scp.ClockOutput));
            try
            {
                Console.WriteLine("  Clock output frequencies  : " + String.Join(", ", Array.ConvertAll(scp.ClockOutputFrequencies, x => x.ToString())));
                Console.WriteLine("  Clock output frequency    : " + scp.ClockOutputFrequency.ToString());
            }
            catch
            {
            }
            Console.WriteLine("  Clock sources             : " + ClockSourceToStr(scp.ClockSources));
            Console.WriteLine("  Clock source              : " + ClockSourceToStr((UInt32)scp.ClockSource));
            try
            {
                Console.WriteLine("  Clock source frequencies  : " + String.Join(", ", Array.ConvertAll(scp.ClockSourceFrequencies, x => x.ToString())));
                Console.WriteLine("  Clock source frequency    : " + scp.ClockSourceFrequency.ToString());
            }
            catch
            {
            }

            Console.WriteLine("  Record length max         : " + scp.RecordLengthMax.ToString());
            Console.WriteLine("  Record length             : " + scp.RecordLength.ToString());
            Console.WriteLine("  Sample frequency max      : " + scp.SampleFrequencyMax.ToString());
            Console.WriteLine("  Sample frequency          : " + scp.SampleFrequency.ToString());

            if (scp.MeasureMode == MeasureMode.Block)
            {
                Console.WriteLine("  Segment count max         : " + scp.SegmentCountMax.ToString());
                if (scp.SegmentCountMax > 1)
                {
                    Console.WriteLine("  Segment count             : " + scp.SegmentCount.ToString());
                }
            }

            if (scp.HasTrigger)
            {
                Console.WriteLine("  Pre sample ratio          : " + scp.PreSampleRatio.ToString());

                Double timeout = scp.TriggerTimeOut;
                Console.Write("  Trigger time out          : ");
                if (timeout == Constants.TO_INFINITY)
                {
                    Console.WriteLine("Infinite");
                }
                else
                {
                    Console.WriteLine(timeout.ToString());
                }

                if (scp.HasTriggerDelay)
                {
                    Console.WriteLine("  Trigger delay max         : " + scp.TriggerDelayMax.ToString());
                    Console.WriteLine("  Trigger delay             : " + scp.TriggerDelay.ToString());
                }

                if (scp.HasTriggerHoldOff)
                {
                    Console.WriteLine("  Trigger hold off count max: " + scp.TriggerHoldOffCountMax.ToString());
                    Console.WriteLine("  Trigger hold off count    : " + scp.TriggerHoldOffCount.ToString());
                }
            }

            for (UInt16 ch = 0; ch < scp.Channels.Count; ch++)
            {
                OscilloscopeChannel channel = scp.Channels[ch];
                Console.WriteLine("  Channel" + (ch + 1).ToString() + ":");
                Console.WriteLine("    Connector type          : " + ConnectorTypeToStr((UInt32)channel.ConnectorType));
                Console.WriteLine("    Differential            : " + channel.IsDifferential.ToString());
                Console.WriteLine("    Impedance               : " + channel.Impedance.ToString());
                Console.WriteLine("    Connection test         : " + channel.HasConnectionTest.ToString());
                Console.WriteLine("    Available               : " + channel.IsAvailable.ToString());
                Console.WriteLine("    Enabled                 : " + channel.Enabled.ToString());
                Console.WriteLine("    Bandwidths              : " + String.Join(", ", Array.ConvertAll(channel.Bandwidths, x => x.ToString())));
                Console.WriteLine("    Bandwidth               : " + channel.Bandwidth.ToString());
                Console.WriteLine("    Couplings               : " + CouplingToStr(channel.Couplings));
                Console.WriteLine("    Coupling                : " + CouplingToStr((UInt64)channel.Coupling));
                Console.WriteLine("    Auto ranging            : " + channel.AutoRanging.ToString());
                Console.WriteLine("    Ranges                  : " + String.Join(", ", Array.ConvertAll(channel.Ranges, x => x.ToString())));
                Console.WriteLine("    Range                   : " + channel.Range.ToString());
                Console.WriteLine("    Probe gain              : " + channel.ProbeGain.ToString());
                Console.WriteLine("    Probe offset            : " + channel.ProbeOffset.ToString());
                if (channel.HasSafeGround)
                {
                    Console.WriteLine("    SafeGround enabled      : " + channel.SafeGroundEnabled.ToString());
                    Console.WriteLine("    SafeGround threshold max: " + channel.SafeGroundThresholdMax.ToString());
                    Console.WriteLine("    SafeGround threshold min: " + channel.SafeGroundThresholdMin.ToString());
                    Console.WriteLine("    SafeGround threshold    : " + channel.SafeGroundThreshold.ToString());
                }

                if (channel.HasTrigger)
                {
                    OscilloscopeChannelTrigger trigger = channel.Trigger;
                    Console.WriteLine("    Trigger:");
                    Console.WriteLine("      Available             : " + trigger.IsAvailable.ToString());
                    Console.WriteLine("      Enabled               : " + trigger.Enabled.ToString());
                    Console.WriteLine("      Kinds                 : " + TriggerKindToStr(trigger.Kinds));
                    Console.WriteLine("      Kind                  : " + TriggerKindToStr((UInt64)trigger.Kind));
                    Console.WriteLine("      Level modes           : " + TriggerLevelModeToStr(trigger.LevelModes));
                    Console.WriteLine("      Level mode            : " + TriggerLevelModeToStr((UInt32)trigger.LevelMode));

                    if (trigger.Level.Count != 0)
                    {
                        Double[] levels = new Double[trigger.Level.Count];
                        for (UInt32 i = 0; i < trigger.Level.Count; i++)
                        {
                            levels[i] = trigger.Level[i];
                        }
                        Console.WriteLine("      Levels                : " + String.Join(", ", Array.ConvertAll(levels, x => x.ToString())));
                    }

                    if (trigger.Hysteresis.Count != 0)
                    {
                        Double[] hysteresis = new Double[trigger.Hysteresis.Count];
                        for (UInt32 i = 0; i < trigger.Hysteresis.Count; i++)
                        {
                            hysteresis[i] = trigger.Hysteresis[i];
                        }
                        Console.WriteLine("      Histeresis            : " + String.Join(", ", Array.ConvertAll(hysteresis, x => x.ToString())));
                    }

                    Console.WriteLine("      Conditions            : " + TriggerConditionToStr(trigger.Conditions));
                    if (trigger.Conditions != Constants.TCM_NONE)
                    {
                        Console.WriteLine("      Condition             : " + TriggerConditionToStr((UInt32)trigger.Condition));
                    }

                    if (trigger.Time.Count != 0)
                    {
                        Double[] times = new Double[trigger.Time.Count];
                        for (UInt32 i = 0; i < trigger.Time.Count; i++)
                        {
                            times[i] = trigger.Time[i];
                        }
                        Console.WriteLine("      Times                 : " + String.Join(", ", Array.ConvertAll(times, x => x.ToString())));
                    }
                }
            }

            PrintTriggerInputsInfo(scp.TriggerInputs);
            PrintTriggerOutputsInfo(scp.TriggerOutputs);
        }
        catch (System.Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
    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);
    }
    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 and a generator in the same device:
        Oscilloscope scp = null;
        Generator    gen = null;

        for (UInt32 i = 0; i < DeviceList.Count; i++)
        {
            DeviceListItem item = DeviceList.GetItemByIndex(i);
            if (item.CanOpen(DeviceType.Oscilloscope) && item.CanOpen(DeviceType.Generator))
            {
                scp = item.OpenOscilloscope();

                if ((scp.MeasureModes & Constants.MM_BLOCK) != 0)
                {
                    gen = item.OpenGenerator();
                    break;
                }
                else
                {
                    scp.Dispose();
                    scp = null;
                }
            }
        }

        if (scp != null && gen != null)
        {
            try
            {
                // Oscilloscope settings:

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

                    // 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 = 1; // 1 s

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

                // Locate trigger input:
                TriggerInput triggerInput = scp.TriggerInputs.GetById(Constants.TIID_GENERATOR_NEW_PERIOD); // or Constants.TIID_GENERATOR_START or Constants.TIID_GENERATOR_STOP

                if (triggerInput == null)
                {
                    throw new System.Exception("Unknown trigger input!");
                }

                // Enable trigger input:
                triggerInput.Enabled = true;

                // Generator settings:

                // Set signal type:
                gen.SignalType = SignalType.Triangle;

                // Set frequency:
                gen.Frequency = 1e3; // 1 kHz

                // Set amplitude:
                gen.Amplitude = 2; // 2 V

                // Set offset:
                gen.Offset = 0; // 0 V

                // Enable output:
                gen.OutputOn = true;

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

                // Print generator info:
                PrintInfo.PrintDeviceInfo(gen);

                // Start measurement:
                scp.Start();

                // Start signal generation:
                gen.Start();

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

                // Stop generator:
                gen.Stop();

                // Disable output:
                gen.OutputOn = false;

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

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

                // Output CSV data:
                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(";" + 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;

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

        Environment.Exit(0);
    }