コード例 #1
0
ファイル: Interface.cs プロジェクト: DavidAlloza/SNASharp
        public float[] RunSweepMode(Int64 nBaseFrequency,
                                    Int64 nFrequencyStep,
                                    int nCount,
                                    bool bUseInear          = false,
                                    CBackNotifier Notifier  = null,
                                    BackgroundWorker Worker = null,
                                    Int16[] NativeDatas     = null,
                                    bool bUseRawMode        = false,
                                    UInt16 RawModeBase      = 500)
        {
            int nSubBlockSize = 9999;
            int nPass         = nCount / nSubBlockSize;
            int nRest         = nCount % nSubBlockSize;

            float[] fullBuffer = new float[nCount];

            port.DiscardInBuffer();
            port.DiscardOutBuffer();

            port.ReadTimeout = DeviceDef.AcquisitionTimeout;


            for (int nFullBlock = 0; nFullBlock < nPass; nFullBlock++)
            {
                int      nOffset       = nFullBlock * nSubBlockSize;
                Int16[]  SubNativeData = new Int16[nSubBlockSize];
                float [] fSubBuffer    = RunSweepModeBlock(nBaseFrequency + nOffset * nFrequencyStep, nFrequencyStep, nSubBlockSize, nCount, nOffset, bUseInear, Notifier, Worker, SubNativeData, bUseRawMode, RawModeBase);
                fSubBuffer.CopyTo(fullBuffer, nOffset);

                if (NativeDatas != null)
                {
                    SubNativeData.CopyTo(NativeDatas, nOffset);
                }
            }

            if (nRest > 0)
            {
                int     nOffset       = nPass * nSubBlockSize;
                Int16[] SubNativeData = new Int16[nRest];
                float[] fSubBuffer    = RunSweepModeBlock(nBaseFrequency + nOffset * nFrequencyStep, nFrequencyStep, nRest, nCount, nOffset, bUseInear, Notifier, Worker, SubNativeData, bUseRawMode, RawModeBase);
                fSubBuffer.CopyTo(fullBuffer, nOffset);

                if (NativeDatas != null)
                {
                    SubNativeData.CopyTo(NativeDatas, nOffset);
                }
            }

            if (Notifier != null)
            {
                Notifier.SendProgress(0, 0.0f);
            }

            return(fullBuffer);
        }
コード例 #2
0
ファイル: Interface.cs プロジェクト: DavidAlloza/SNASharp
        public void RunCalibration(CBackNotifier Notifier, int nCount, bool bLinear = false)
        {
            CalibrationValues.nFirstCalibrationFrequency = DeviceDef.MinFrequencyInHz;
            CalibrationValues.nLastCalibrationFrequency  = DeviceDef.MaxFrequencyInHz;

            Int16[] Out = new Int16[nCount];

            float[] Result = RunSweepMode(DeviceDef.MinFrequencyInHz,
                                          ((DeviceDef.MaxFrequencyInHz - DeviceDef.MinFrequencyInHz)) / nCount,
                                          nCount,
                                          bLinear,
                                          Notifier,
                                          null,
                                          Out);
            SNASharp.Utility.FilterArray(Out, 2);

            CalibrationValues.SetReferenceArray(CurrentLevel, Out, bLinear);
            Notifier.SendProgress(0, 0.0f);
        }
コード例 #3
0
ファイル: Interface.cs プロジェクト: DavidAlloza/SNASharp
        float[] RunSweepModeBlock(Int64 nBaseFrequency,
                                  Int64 nFrequencyStep,
                                  int nCount,
                                  int nFullCaptureSize,
                                  int nFullCaptureOffset,
                                  bool bUseLinear         = false,
                                  CBackNotifier Notifier  = null,
                                  BackgroundWorker Worker = null,
                                  Int16 [] NativeDatas    = null,
                                  bool bUseRawMode        = false,
                                  UInt16 RawModeBase      = 500)
        {
            float[] DataOut       = new float[nCount];
            byte[]  StreamFromSNA = new byte[4];

            nBaseFrequency += (Int64)DeviceDef.TrackingModeFrequencyShift;
            Int64 PPMCorrectionHz = (Int64)((nBaseFrequency * (double)DeviceDef.DefaultPPMCorrection) / 1000000.0);

            nBaseFrequency += PPMCorrectionHz;

            int nMessageSize;

            if (DeviceDef.CaptureDelay_µs == 0)
            {
                nMessageSize = 2 + 9 + 8 + 4;
            }
            else
            {
                nMessageSize = 2 + 9 + 8 + 4 + 3;
            }

            byte[] OutMessage = new byte[nMessageSize];

            OutMessage[0] = 0x8f;
            if (!bUseLinear)
            {
                if (DeviceDef.CaptureDelay_µs == 0)
                {
                    OutMessage[1] = (byte)'x';
                }
                else
                {
                    OutMessage[1] = (byte)'a';
                }
            }
            else
            {
                if (DeviceDef.CaptureDelay_µs == 0)
                {
                    OutMessage[1] = (byte)'w';
                }
                else
                {
                    OutMessage[1] = (byte)'b';
                }
            }


            byte[] cBaseFrequency = BuildZeroLeftString(nBaseFrequency / DeviceDef.FrequencyDivisor, 9);
            byte[] cFrequencyStep = BuildZeroLeftString(nFrequencyStep / DeviceDef.FrequencyDivisor, 8);
            byte[] cCount         = BuildZeroLeftString(nCount, 4);
            byte[] cDelay         = BuildZeroLeftString(DeviceDef.CaptureDelay_µs, 3);

            cBaseFrequency.CopyTo(OutMessage, 2);
            cFrequencyStep.CopyTo(OutMessage, 2 + 9);
            cCount.CopyTo(OutMessage, 2 + 9 + 8);

            if (DeviceDef.CaptureDelay_µs != 0)
            {
                cDelay.CopyTo(OutMessage, 2 + 9 + 8 + 4);
            }

            port.Write(OutMessage, 0, OutMessage.Length);

            // new we can read all mesurements
            int nLowByte  = 0;
            int nHighByte = 0;

            bool aTimeoutAsOccured = false;
            int  nPreviousProgress = -1;

            for (int i = 0; i < nCount; i++)
            {
                try
                {
                    if (!aTimeoutAsOccured)
                    {
                        StreamFromSNA[0] = (byte)port.ReadByte();
                        StreamFromSNA[1] = (byte)port.ReadByte();

                        if (DeviceDef.SweepDataFormat == AnalyzerInterface.DeviceDef.SamplingDataFormat._16Bits_2Channel)
                        {
                            StreamFromSNA[2] = (byte)port.ReadByte();
                            StreamFromSNA[3] = (byte)port.ReadByte();
                        }
                    }
                }
                catch (TimeoutException)
                {
                    aTimeoutAsOccured = true;
                    StreamFromSNA[0]  = (byte)nLowByte;
                    StreamFromSNA[1]  = (byte)nHighByte;
                }

                nLowByte  = StreamFromSNA[0];
                nHighByte = StreamFromSNA[1];

                int nMesure = (nHighByte << 8) + nLowByte;

                if (nMesure < 1)
                {
                    nMesure = 1;
                }

                if (NativeDatas != null)
                {
                    NativeDatas[i] = (Int16)nMesure;
                }

                if (!bUseLinear)
                {
                    if (bUseRawMode)
                    {
                        DataOut[i] = (((float)nMesure) - RawModeBase) * DeviceDef.VerticalResolutiondB;
                    }
                    else
                    {
                        // sampling from logarithmic detector
                        DataOut[i] = (((float)nMesure) - CalibrationValues.GetZeroDbValue(nBaseFrequency + nFrequencyStep * i, CurrentCalibrationLevel, false)) * DeviceDef.VerticalResolutiondB;
                    }
                }
                else
                {
                    // sampling from linear detector
                    //if (nMesure == 0)
                    //   nMesure = 1;

                    if (bUseRawMode)
                    {
                        float fConvertedTodB = 20.0f * (float)Math.Log10((double)nMesure / ((double)RawModeBase));
                        DataOut[i] = fConvertedTodB;
                    }
                    else
                    {
                        float fConvertedTodB = 20.0f * (float)Math.Log10((double)nMesure / ((double)CalibrationValues.GetZeroDbValue(nBaseFrequency + nFrequencyStep * i, CurrentCalibrationLevel, true)));
                        DataOut[i] = fConvertedTodB;
                    }
                }

                int nProgress = ((i + nFullCaptureOffset) * 100) / nFullCaptureSize;

                if (nProgress - nPreviousProgress > 1)
                {
                    if (Notifier != null)
                    {
                        Notifier.SendProgress(nProgress, DataOut[i]);
                    }

                    if (Worker != null)
                    {
                        Worker.ReportProgress(nProgress);
                    }

                    nPreviousProgress = nProgress;
                }
            }

            return(DataOut);
        }