/* OscilloscopeBlockSegmented.cs
*
* This example performs a block mode measurement of 5 segments and writes the data to OscilloscopeBlockSegmented.csv.
*
* Find more information on http://www.tiepie.com/LibTiePie .
*/
using System;
using System.IO;
using System.Threading;
using TiePie.LibTiePie;
class OscilloscopeBlockSegmentedExample
{
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) & (scp.SegmentCountMax > 1))
{
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 = 1000; // 1 kS
UInt64 recordLength = scp.RecordLength; // Read actual record length.
// Set pre sample ratio:
scp.PreSampleRatio = 0; // 0 %
// Set number of segments:
scp.SegmentCount = 5; // 5 segments
OscilloscopeChannel channel;
// For all channels:
for (UInt16 ch = 0; ch < channelCount; ch++)
{
channel = scp.Channels[ch];
// Disable channel:
channel.Enabled = false;
// Set range:
channel.Range = 8; // 8 V
// Set coupling:
channel.Coupling = Coupling.DCV; // DC Volt
}
// Enable channel to measure it:
channel = scp.Channels[0];
channel.Enabled = true;
// 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 all data from the scope:
float[][][] data = new float[scp.SegmentCount][][];
UInt16 seg = 0;
while (scp.IsDataReady)
{
data[seg] = scp.GetData();
seg++;
}
// Open file with write/update permissions:
string filename = "OscilloscopeBlockSegmented.csv";
StreamWriter file = new StreamWriter(filename, false);
// Write the data to csv:
if (File.Exists(filename))
{
// Write csv header:
file.Write("Sample");
for (seg = 0; seg < scp.SegmentCount; seg++)
{
file.Write(string.Format(";Segment{0}", seg + 1));
}
file.Write(Environment.NewLine);
// Write the Ch1 data to csv:
for (UInt64 i = 0; i < recordLength; i++)
{
file.Write(i.ToString());
for (seg = 0; seg < scp.SegmentCount; seg++)
{
file.Write(";" + data[seg][0][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 and segmented trigger support!");
Environment.Exit(1);
}
Environment.Exit(0);
}
}