Example #1
0
        public void Run()
        {
            //Set this variable to have all text socket commands printed to the console.
            SaleaeClient.PrintCommandsToConsole = true;

            //Make sure to enable the socket server in the Logic software preferences, and make sure that it is running!

            //This demo is designed to show some common socket commands, and interacts best with either the simulation or real Logic 8, Logic Pro 8, or Logic Pro 16.

            //lets run a quick demo!
            Console.WriteLine("Logic Socket API demonstation application.\n");

            //Get IP address of software.
            Console.WriteLine("enter host IP address, or press enter for localhost");
            String host = Console.ReadLine();

            if (host.Length == 0)
            {
                host = "127.0.0.1";
            }

            //Get port of software
            Console.WriteLine("enter host port, or press enter for default ( 10429 )");
            String port_str = Console.ReadLine();

            if (port_str.Length == 0)
            {
                port_str = "10429";
            }
            int port = int.Parse(port_str);

            //attempt to connect with socket.
            Console.WriteLine("Connecting...");
            try
            {
                Client = new SaleaeClient(host, port);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while connecting: " + ex.Message);
                Console.ReadLine();
                return;
            }
            StringHelper.WriteLine("Connected");
            Console.WriteLine("");

            //Get a list of connected devices. (when no devices are connected, this should return 4 simulation devices)
            var devices       = Client.GetConnectedDevices();
            var active_device = devices.Single(x => x.IsActive == true);

            Console.WriteLine("currently availible devices:");
            devices.ToList().ForEach(x => Console.WriteLine(x.Name));

            Console.WriteLine("currently active device: " + active_device.Name);
            Console.WriteLine("");
            Console.WriteLine("Press Enter to Continue");
            Console.ReadLine();

            //list all analyzers currently added to the session.
            var analyzers = Client.GetAnalyzers();

            if (analyzers.Any())
            {
                Console.WriteLine("Current analyzers:");
                analyzers.ToList().ForEach(x => Console.WriteLine(x.AnalyzerType));
                Console.WriteLine("");
                Console.WriteLine("Press Enter to Continue");
                Console.ReadLine();
            }

            //change the active channels, but only if the active device supports that.
            if (active_device.DeviceType == DeviceType.Logic8 || active_device.DeviceType == DeviceType.LogicPro8 || active_device.DeviceType == DeviceType.LogicPro16)
            {
                Console.WriteLine("changing active channels");
                Client.SetActiveChannels(new int[] { 2, 5, 6, 7 }, new int[] { 0, 1 });
                Console.WriteLine("");
                Console.WriteLine("Press Enter to Continue");
                Console.ReadLine();
                //showing active channels
                List <int> active_digital_channels = new List <int>();
                List <int> active_analog_channels  = new List <int>();
                Client.GetActiveChannels(active_digital_channels, active_analog_channels);
                Console.WriteLine("Active Digital Channels: " + String.Join(", ", active_digital_channels.ToArray()));
                Console.WriteLine("Active Analog Channels: " + String.Join(", ", active_analog_channels.ToArray()));
                //display the currently selected sample rate and performance option.
                var current_sample_rate = Client.GetSampleRate();
                Console.WriteLine("The previously selected sample rate was: " + current_sample_rate.DigitalSampleRate.ToString() + " SPS (digital), " + current_sample_rate.AnalogSampleRate.ToString() + " SPS (analog)");
                if (current_sample_rate.AnalogSampleRate > 0 && current_sample_rate.DigitalSampleRate > 0)
                {
                    PerformanceOption current_performance_option = Client.GetPerformanceOption();
                    Console.WriteLine("Currently selected performance option: " + current_performance_option.ToString());
                }

                //change the sample rate!
                var possible_sample_rates = Client.GetAvailableSampleRates();

                if (possible_sample_rates.Any(x => x.AnalogSampleRate == 125000))
                {
                    Console.WriteLine("Changing sample rate");
                    Client.SetSampleRate(possible_sample_rates.First(x => x.AnalogSampleRate == 125000));
                    Console.WriteLine("");
                    Console.WriteLine("Press Enter to Continue");
                    Console.ReadLine();
                }

                //set trigger. There are 4 digital channels. all need to be specified.
                Console.WriteLine("setting trigger");
                Client.SetTrigger(new Trigger[] { Trigger.None, Trigger.PositivePulse, Trigger.Low, Trigger.High }, 1E-6, 5E-3);
                Console.WriteLine("");
                Console.WriteLine("Press Enter to Continue");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("to see more cool features demoed by this example, please switch to a Logic 8, Logic Pro 8, or Logic Pro 16. Physical or simulation");
            }

            if (active_device.DeviceType == DeviceType.LogicPro8 || active_device.DeviceType == DeviceType.LogicPro16 || active_device.DeviceType == DeviceType.Logic16)
            {
                DigitalVoltageOption voltage_option = Client.GetDigitalVoltageOptions().SingleOrDefault(x => x.IsSelected == true);
                if (voltage_option != null)
                {
                    Console.WriteLine("Currently selected voltage option: " + voltage_option.Description);
                }
            }

            //change the recording length to 250 ms.
            Console.WriteLine("setting capture time");
            Client.SetCaptureSeconds(0.25);
            Console.WriteLine("");
            Console.WriteLine("Press Enter to Continue");
            Console.ReadLine();

            //start a capture.
            Console.WriteLine("starting capture");
            Client.Capture();
            Console.WriteLine("capture complete");
            //the following 3 functions require software 1.2.18 or newer.
            var range      = Client.GetCaptureRange(); //1.2.18+
            var view_state = Client.GetViewState();    //1.2.18+

            Console.WriteLine("Press Enter to Continue");
            Console.ReadLine();
            Console.WriteLine("Setting ViewState");
            Client.SetViewState(500.0, range.TriggerSample); //1.2.18+

            Console.WriteLine("Press Enter to Exit");
            Console.ReadLine();
        }
Example #2
0
        public void TriggerOffsetTest()
        {
            //connect the signal generator to channel 0 of Logic8/Pro8/Pro16. (do not use Logic4, the original Logic, or Logic16)
            //set signal generator to 1 hz square wave and start.



            if (System.IO.Directory.Exists(folder_path) == false)
            {
                System.IO.Directory.CreateDirectory(folder_path);
            }

            //digital only CSV all time
            ExportDataStruct export_settings = new ExportDataStruct
            {
                FileName = folder_path + "trigger_digital_csv_alltime",
                //DataExportMixedExportMode = DataExportMixedModeExportType.DigitalOnly,
                ExportChannelSelection = DataExportChannelSelection.AllChannels,
                //DigitalChannelsToExport = new int[] {},
                //AnalogChannelsToExport = new int[] {},

                SamplesRangeType = DataExportSampleRangeType.RangeAll,
                StartingTime     = -0.005,          //-5mS
                EndingTime       = 0.005,           //+5mS

                DataExportType    = DataExportType.ExportCsv,
                CsvIncludeHeaders = CsvHeadersType.CsvIncludesHeaders,
                CsvDelimiterType  = CsvDelimiterType.CsvComma,
                CsvOutputMode     = CsvOutputMode.CsvOneColumnPerBit,
                CsvTimestampType  = CsvTimestampType.CsvTime,
                //CsvDisplayBase = CsvBase.CsvHexadecimal,
                CsvDensity = CsvDensity.CsvTransition,

                //BinaryOutputMode = BinaryOutputMode.BinaryEverySample,
                //BinaryBitShifting = BinaryBitShifting.BinaryOriginalBitPositions,
                //BinaryOutputWordSize = BinaryOutputWordSize.Binary16Bit,

                //AnalogFormat = AnalogOutputFormat.ADC
            };


            client.SetActiveChannels(new int[] { 0 }, new int[] { });
            client.SetCapturePretriggerBufferSize(1000000); //1M pre-trigger buffer. that's 10ms at 100 MSPS.
            client.SetCaptureSeconds(0.01);                 //10ms, prob go to 30 ms
            client.SetSampleRate(new SampleRate {
                DigitalSampleRate = 100000000, AnalogSampleRate = 0
            });


            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);


            //digital only CSV time range
            export_settings.FileName         = folder_path + "trigger_digital_csv_timerange";
            export_settings.SamplesRangeType = DataExportSampleRangeType.RangeTimes;
            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);

            //digital only CSV time range with sample numbers

            export_settings.FileName         = folder_path + "trigger_digital_csv_timerange_samples";
            export_settings.SamplesRangeType = DataExportSampleRangeType.RangeTimes;
            export_settings.CsvTimestampType = CsvTimestampType.CsvSample;
            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);

            //digital only binary all time
            export_settings = new ExportDataStruct
            {
                FileName = folder_path + "trigger_digital_binary_alltime",
                //DataExportMixedExportMode = DataExportMixedModeExportType.DigitalOnly,
                ExportChannelSelection = DataExportChannelSelection.AllChannels,
                //DigitalChannelsToExport = new int[] {},
                //AnalogChannelsToExport = new int[] {},

                SamplesRangeType = DataExportSampleRangeType.RangeAll,
                StartingTime     = -0.005,          //-5mS
                EndingTime       = 0.005,           //+5mS

                //DataExportType = DataExportType.ExportCsv,
                //CsvIncludeHeaders = CsvHeadersType.CsvIncludesHeaders,
                //CsvDelimiterType = CsvDelimiterType.CsvTab,
                //CsvOutputMode = CsvOutputMode.CsvOneColumnPerBit,
                //CsvTimestampType = CsvTimestampType.CsvTime,
                //CsvDisplayBase = CsvBase.CsvHexadecimal,
                //CsvDensity = CsvDensity.CsvTransition,

                BinaryOutputMode     = BinaryOutputMode.BinaryEverySample,
                BinaryBitShifting    = BinaryBitShifting.BinaryOriginalBitPositions,
                BinaryOutputWordSize = BinaryOutputWordSize.Binary16Bit,

                //AnalogFormat = AnalogOutputFormat.ADC
            };

            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);

            //digital only binary time_rage
            export_settings.FileName         = folder_path + "trigger_digital_binary_timerange";
            export_settings.SamplesRangeType = DataExportSampleRangeType.RangeTimes;

            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);

            //digital only binary all time with sample numbers
            export_settings.FileName         = folder_path + "trigger_digital_binary_alltime_with_sample_number";
            export_settings.SamplesRangeType = DataExportSampleRangeType.RangeAll;
            export_settings.BinaryOutputMode = BinaryOutputMode.BinaryEveryChange;

            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);

            //digital only binary time range with sample numbers
            export_settings.FileName         = folder_path + "trigger_digital_binary_range_with_sample_number";
            export_settings.SamplesRangeType = DataExportSampleRangeType.RangeTimes;

            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);

            //digital only VCD all time
            export_settings.FileName         = folder_path + "trigger_digital_vcd_alltime";
            export_settings.DataExportType   = DataExportType.ExportVcd;
            export_settings.SamplesRangeType = DataExportSampleRangeType.RangeAll;

            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);

            //digital only VCD time range
            export_settings.FileName         = folder_path + "trigger_digital_vcd_range";
            export_settings.SamplesRangeType = DataExportSampleRangeType.RangeTimes;

            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);

            //digital only Matlab all time
            export_settings.FileName         = folder_path + "trigger_digital_matlab_alltime";
            export_settings.DataExportType   = DataExportType.ExportMatlab;
            export_settings.SamplesRangeType = DataExportSampleRangeType.RangeAll;

            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);

            //digital only Matlab time range
            export_settings.FileName         = folder_path + "trigger_digital_matlab_range";
            export_settings.DataExportType   = DataExportType.ExportMatlab;
            export_settings.SamplesRangeType = DataExportSampleRangeType.RangeTimes;

            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 5)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, false);



            //Mix mode full mix CSV all time
            export_settings = new ExportDataStruct
            {
                FileName = folder_path + "trigger_mix_both_csv_alltime",
                //DataExportMixedExportMode = DataExportMixedModeExportType.DigitalOnly,
                ExportChannelSelection = DataExportChannelSelection.AllChannels,
                //DigitalChannelsToExport = new int[] {},
                //AnalogChannelsToExport = new int[] {},

                SamplesRangeType = DataExportSampleRangeType.RangeAll,
                StartingTime     = -0.005,          //-5mS
                EndingTime       = 0.005,           //+5mS

                DataExportType    = DataExportType.ExportCsv,
                CsvIncludeHeaders = CsvHeadersType.CsvIncludesHeaders,
                CsvDelimiterType  = CsvDelimiterType.CsvComma,
                CsvOutputMode     = CsvOutputMode.CsvOneColumnPerBit,
                CsvTimestampType  = CsvTimestampType.CsvTime,
                CsvDisplayBase    = CsvBase.CsvHexadecimal,
                CsvDensity        = CsvDensity.CsvTransition,

                //BinaryOutputMode = BinaryOutputMode.BinaryEverySample,
                //BinaryBitShifting = BinaryBitShifting.BinaryOriginalBitPositions,
                //BinaryOutputWordSize = BinaryOutputWordSize.Binary16Bit,

                AnalogFormat = AnalogOutputFormat.Voltage
            };

            client.SetActiveChannels(new int[] { 0 }, new int[] { 0 });
            client.SetCapturePretriggerBufferSize(5000000); //5M pre-trigger buffer. that's 10ms at 500 MSPS.
            client.SetCaptureSeconds(0.01);                 //10ms, prob go to 30 ms
            client.SetSampleRate(new SampleRate {
                DigitalSampleRate = 500000000, AnalogSampleRate = 125000
            });

            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 35)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, true);



            //mix mode both matlab time range
            export_settings.FileName         = folder_path + "trigger_mixed_both_matlab_range";
            export_settings.DataExportType   = DataExportType.ExportMatlab;
            export_settings.SamplesRangeType = DataExportSampleRangeType.RangeTimes;
            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 35)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, true);

            //mix mode digital only csv range
            export_settings.FileName                  = folder_path + "trigger_mixed_digital_only_csv_range";
            export_settings.SamplesRangeType          = DataExportSampleRangeType.RangeTimes;
            export_settings.DataExportType            = DataExportType.ExportCsv;
            export_settings.ExportChannelSelection    = DataExportChannelSelection.SpecificChannels;
            export_settings.DataExportMixedExportMode = DataExportMixedModeExportType.DigitalOnly;
            export_settings.DigitalChannelsToExport   = new int[] { 0 };

            client.Capture();             //blocks until capture is complete, but processing is not complete.
            if (client.BlockUntillProcessingCompleteOrTimeout(new TimeSpan(0, 0, 35)) == false)
            {
                throw new Exception("processing took too long");
            }

            client.ExportData2(export_settings, true, true);
        }