public string GetMeasruementMessageLog()
        {
            string log = Marshal.PtrToStringUni(FRA4PicoscopeAPI.GetMessageLog());

            FRA4PicoscopeAPI.ClearMessageLog();
            return(log);
        }
 private void HandlePicoException(string message)
 {
     logService.Error(message);
     FRA4PicoscopeAPI.Cleanup();
     scopeConnected = false;
     throw new ApplicationException(message);
 }
        public void StartMeasurement()
        {
            if (!scopeConnected)
            {
                ConnectPicoscope();
            }
            string checkSettingsResult = CheckSettings();

            if (!checkSettingsResult.Equals(""))
            {
                throw new ApplicationException(checkSettingsResult);
            }
            FRA4PicoscopeAPI.ClearMessageLog();
            SetLogVerbosity();
            SetUpChannels();
            SetFRASettings();
            SetFRATuning();
            logService.Debug("Starting Picoscope measurement...");
            byte result = FRA4PicoscopeAPI.StartFRA(StartFrequencyHz, StopFrequencyHz, StepsPerDecade);

            if (result != 1)
            {
                HandlePicoException("Failed to record plot: " + result.ToString());
            }
            else
            {
                logService.Info("Picoscope measurement succesfully started");
            }
        }
        public string CheckSettings()
        {
            StringWriter result = new StringWriter();

            if (TestJigReferenceResistor < 0)
            {
                result.WriteLine("Test Jig Reference resistor cannot be negative!");
            }

            if (!scopeConnected)
            {
                ConnectPicoscope();                  // must be connected to check the min frequency
            }
            double minFrequency = FRA4PicoscopeAPI.GetMinFrequency(RESOLUTION_T.RESOLUTION_MAX);

            if (StartFrequencyHz < minFrequency)
            {
                result.WriteLine("Start frequency must be higher than " + minFrequency + " Hz");
            }
            if (StopFrequencyHz < minFrequency)
            {
                result.WriteLine("Stop frequency must be higher than " + minFrequency + " Hz");
            }
            if (StartFrequencyHz > StopFrequencyHz)
            {
                result.WriteLine("Start frequency must be higher than Stop frequency !");
            }
            if (InputDUTChannel == OutputDUTChannel)
            {
                result.WriteLine("Input channel and output channel cannot be identical");
            }
            if (InitialStimulus < 0)
            {
                result.WriteLine("Initial Stimulus cannot be negative");
            }
            //if (InitialStimulus > MaxStimulus) result.WriteLine("Initial Stimulus must be smaller than max stimulus");
            if (StepsPerDecade < 1)
            {
                result.WriteLine("Steps per decade must be greater than zero!");
            }
            if (PhaseWrappingThreshold <= 0)
            {
                result.WriteLine("Phase wrapping threshold must be greater than zero!");
            }
            if (ExtraSettlingTimeMs < 0)
            {
                result.WriteLine("Extra settling time stimulus cannot be negative!");
            }

            if (!result.ToString().Equals(""))
            {
                logService.Warn("Settings incorrect:" + result.ToString());
            }
            return(result.ToString());
        }
 protected virtual void Dispose(bool disposing)
 {
     if (!is_disposed)  // only dispose once!
     {
         if (disposing) // Not in destructor, OK to reference other objects
         {
             FRA4PicoscopeAPI.Cleanup();
             scopeConnected = false;
         }
         // perform cleanup for this object itself
     }
     this.is_disposed = true;
 }
 private void SetLogVerbosity()
 {
     logService.Debug("Setting Log Verbosity...");
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.SCOPE_ACCESS_DIAGNOSTICS, LogScopeAccessDiagnostics);
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.FRA_PROGRESS, LogFRAProgress);
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.STEP_TRIAL_PROGRESS, LogStepTrailProgress);
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.SIGNAL_GENERATOR_DIAGNOSTICS, LogSignalGeneratorDiagnostics);
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.AUTORANGE_DIAGNOSTICS, LogAutoRangeDiagnostics);
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.ADAPTIVE_STIMULUS_DIAGNOSTICS, LogAdapticeStimulusDiagnostics);
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.SAMPLE_PROCESSING_DIAGNOSTICS, LogSampleProcessingDiagnostics);
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.DFT_DIAGNOSTICS, LogDFTDiagnostics);
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.SCOPE_POWER_EVENTS, LogScopePowerEvents);
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.SAVE_EXPORT_STATUS, LogSaveExportStatus);
     FRA4PicoscopeAPI.SetLogVerbosityFlag(LOG_MESSAGE_FLAGS_T.FRA_WARNING, LogFRAWarnings);
 }
        private void SetUpChannels()
        {
            logService.Info("Setting up Picoscope succesfully channels...");
            logService.Info("Input DUT channel:" + InputDUTChannel.ToString() + "   Coupling:" + InputDUTCoupling.ToString() + "   Atten:" + InputDUTAttenuation.ToString() + "   DC Offset:" + InputDUTDCOffset.ToString());
            logService.Info("Output DUT channel:" + OutputDUTChannel.ToString() + "   Coupling:" + OutputDUTCoupling.ToString() + "   Atten:" + OutputDUTAttenuation.ToString() + "   DC Offset:" + OutputDUTDCOffset.ToString());
            logService.Info("Initial stimulus:" + InitialStimulus.ToString() + "   Max Stimulis:" + MaxStimulus + "   Stimmulus Offset:" + StimulusOffset.ToString());
            byte result = FRA4PicoscopeAPI.SetupChannels(InputDUTChannel, InputDUTCoupling, InputDUTAttenuation, InputDUTDCOffset, OutputDUTChannel, OutputDUTCoupling, OutputDUTAttenuation, OutputDUTDCOffset, InitialStimulus, MaxStimulus, StimulusOffset);

            if (result != 1)
            {
                HandlePicoException("Failed to setup channels and stimulus: " + result.ToString());
            }
            else
            {
                logService.Info("Picoscope channels and stimulus succesfully setup");
            }
        }
        private void SetFRASettings()
        {
            logService.Debug("Setting FRA Settings...");
            byte stimulusMode = 0;

            if (AdaptiveStimulusMode)
            {
                stimulusMode = 1;
            }
            byte sweepDec = 0;

            if (SweepDirection == SweepDirection.Decending)
            {
                sweepDec = 1;
            }
            FRA4PicoscopeAPI.SetFraSettings(SamplingMode, stimulusMode, TargetResponseAmplitude, sweepDec, PhaseWrappingThreshold);
        }
        public void ConnectPicoscope()
        {
            byte result = 1;

            if (!scopeConnected)
            {
                logService.Info("Connecting Picoscope...");
                try
                {
                    result = FRA4PicoscopeAPI.Initialize();
                    if (result != 1)
                    {
                        HandlePicoException("Failed to initialize Picoscope API: " + result.ToString());
                    }
                    else
                    {
                        logService.Info("Picoscope API succesfully initialized");
                    }
                }
                catch (Exception e)
                {
                    logService.Error(e.Message.ToString());
                    logService.Error(e.StackTrace);
                    logService.Error(e.HResult.ToString());
                    throw;
                }
                FRA4PicoscopeAPI.EnableMessageLog(true);
                FRA4PicoscopeAPI.AutoClearMessageLog(false);
                result = FRA4PicoscopeAPI.SetScope(""); // TODO: add SN to directly connect to previous scope (currently API does not support reading SN)
            }
            if (result != 1)
            {
                HandlePicoException("Failed to select Picoscope: " + result.ToString());
                scopeConnected = false;
            }
            else
            {
                logService.Info("Picoscope succesfully connected");
                scopeConnected = true;
            }
        }
        public void GetResults(out double[] frequenciesLogHz, out double[] gainsDB, out double[] phasesDegrees)
        {
            frequenciesLogHz = null; gainsDB = null; phasesDegrees = null;

            FRA_STATUS_T status = GetStatus();

            while (((status = GetStatus()) == FRA_STATUS_T.FRA_STATUS_IN_PROGRESS) || (status == FRA_STATUS_T.FRA_STATUS_DATA))
            {
                System.Threading.Thread.Sleep(1000);
            }

            if (status == FRA_STATUS_T.FRA_STATUS_COMPLETE)
            {
                logService.Info("Picoscope measurement succesfully completed");

                int numOfSteps = FRA4PicoscopeAPI.GetNumSteps();
                logService.Info("Number of recorded steps:" + numOfSteps.ToString());
                if (numOfSteps <= 0)
                {
                    HandlePicoException("Number of recorded steps was equal or below zero!");
                }


                frequenciesLogHz = new double[numOfSteps];
                gainsDB          = new double[numOfSteps];
                double[] wrappedPhaseDegrees = new double[numOfSteps];
                phasesDegrees = new double[numOfSteps];

                FRA4PicoscopeAPI.GetResults(frequenciesLogHz, gainsDB, wrappedPhaseDegrees, phasesDegrees);
                logService.Info("Picoscope Results succesfully collected:" + numOfSteps.ToString());
            }
            else
            {
                HandlePicoException("Plot record was not completed succesfully: " + status.ToString());
            }
        }
 public FRA_STATUS_T GetStatus()
 {
     return(FRA4PicoscopeAPI.GetFraStatus());
 }
 public void AbortMeasurement()
 {
     logService.Info("Aborting Picoscope measurement...");
     FRA4PicoscopeAPI.CancelFRA();
 }
 private void SetFRATuning()
 {
     logService.Debug("Setting FRA Tuning...");
     FRA4PicoscopeAPI.SetFraTuning(PurityLowerLimit, ExtraSettlingTimeMs, AutoRangeTriesPerStep, AutoRangeTolerance, SmallSignalResolutionTolerance, MaxAutorangeAmplitude,
                                   InputStartRange, OutputStartRange, AdaptiveStimulusTriesPerStep, TargetResponseAmplitudeTolerance, MinCyclesCaptured, MaxDftBandWidth, LowNoiseOversampling, RESOLUTION_T.RESOLUTION_AUTO);
 }