public void Sweep(Object source, ElapsedEventArgs e) { Console.WriteLine("SWEEP"); AgSalLib.salGetSensorTime(sensorHandle, out timestamp); AgSalLib.salGetSensorLocation(sensorHandle, out location); if (!firstRow) { tempRow.Add(timestamp.ToString()); tempRow.Add(location.latitude.ToString()); tempRow.Add(location.longitude.ToString()); tempRow.Add(extTime); tempRow.Add(extLat); tempRow.Add(extLon); } spin.Turn(rowCounter, timestamp.ToString(), location.latitude.ToString(), location.longitude.ToString()); AgSalLib.SalError sweepError = AgSalLib.salStartSweep2( out measurementHandle, sensorHandle, ref sweepParams, ref pSegmentTable, ref flowControl, segmentCallback ); getStatus(); }
private bool DetectSegment(SweepParams measParams, FFTParams fftParams, List <double> powerList, List <double> frequencies, double cf, uint numFftsToCopy, ref bool overloadFlag) { AgSalLib.SalError err; AgSalLib.SweepParms sweepParams = new AgSalLib.SweepParms(); AgSalLib.FrequencySegment[] fs = new AgSalLib.FrequencySegment[1]; switch (measParams.Window.ToLower()) { case "hanning": sweepParams.window = sweepParams.window = AgSalLib.WindowType.Window_hann; break; case "gauss-top": sweepParams.window = sweepParams.window = AgSalLib.WindowType.Window_gaussTop; break; case "flattop": sweepParams.window = sweepParams.window = AgSalLib.WindowType.Window_flatTop; break; case "rectangular": sweepParams.window = sweepParams.window = AgSalLib.WindowType.Window_uniform; break; default: Utilites.LogMessage("Invalid window type in " + "calibration json file"); return(true); } switch (measParams.TimeOverlap) { case 50: fs[0].overlapType = AgSalLib.OverlapType.OverlapType_on; break; default: fs[0].overlapType = AgSalLib.OverlapType.OverlapType_off; break; } switch (measParams.Detector.ToLower()) { case "rms": fs[0].averageType = fs[0].averageType = AgSalLib.AverageType.Average_rms; break; case "sample": fs[0].averageType = fs[0].averageType = AgSalLib.AverageType.Average_off; break; case "positive": fs[0].averageType = fs[0].averageType = AgSalLib.AverageType.Average_peak; break; default: Utilites.LogMessage("Invalid Detector type in " + "calibration json file"); return(true); } switch (measParams.Antenna) { case 0: fs[0].antenna = AgSalLib.AntennaType.Antenna_Terminated2; break; case 1: fs[0].antenna = AgSalLib.AntennaType.Antenna_1; break; case 2: fs[0].antenna = AgSalLib.AntennaType.Antenna_2; break; default: Utilites.LogMessage("Invalid antenna value " + "in cal json file"); return(true); } sweepParams.numSweeps = 1; sweepParams.numSegments = 1; sweepParams.sweepInterval = 0.0; sweepParams.monitorMode = AgSalLib.MonitorMode.MonitorMode_off; sweepParams.monitorInterval = 0.0; // data return type for sweepParams is always real float32 dbm fs[0].numFftPoints = fftParams.NumFftBins; fs[0].numAverages = fftParams.NumFftsToAvg; fs[0].firstPoint = 0; fs[0].numPoints = fs[0].numFftPoints; fs[0].centerFrequency = cf; fs[0].sampleRate = fftParams.SampleRate; fs[0].preamp = measParams.PreAmp; fs[0].attenuation = measParams.Attenuation; fs[0].dataType = AgSalLib.FftDataType.FftData_db; // Setup pacing AgSalLib.salFlowControl flowControl = new AgSalLib.salFlowControl(); flowControl.pacingPolicy = 1; // wait when full flowControl.maxBacklogMessages = 50; err = AgSalLib.salStartSweep2(out measHandle, sensorHandle, ref sweepParams, ref fs, ref flowControl, null); if (SensorError(err, "salStartSweep")) { if (err == AgSalLib.SalError.SAL_ERR_SENSOR_NOT_CONNECTED) { // lost connection to sensor, kill the process // service will start it again Environment.Exit(1); } return(true); } AgSalLib.SweepStatus status; double elapsed; AgSalLib.salGetSweepStatus2(measHandle, 0, out status, out elapsed); // wait until sweep is finished while (status == AgSalLib.SweepStatus.SweepStatus_running) { AgSalLib.salGetSweepStatus2(measHandle, 0, out status, out elapsed); } // get data from sweep AgSalLib.SegmentData dataHeader = new AgSalLib.SegmentData(); // Agilent DLL requires float array even though it uses double // everywhere else ..... annoying float[] frequencyData = new float[fftParams.NumFftBins]; // how long to read before exiting int maxDataReadMilliSeconds = 1000; DateTime t0 = DateTime.Now; bool dataRetrieved = false; int startIndex = 0; while (!dataRetrieved) { TimeSpan elapsedTime = DateTime.Now.Subtract(t0); if (elapsedTime.Milliseconds > maxDataReadMilliSeconds) { Utilites.LogMessage("Getting segment data timed out, " + "restarting cal"); } err = AgSalLib.salGetSegmentData(measHandle, out dataHeader, frequencyData, (uint)frequencyData.Length * 4); switch (err) { case AgSalLib.SalError.SAL_ERR_NONE: if (dataHeader.errorNum != AgSalLib.SalError.SAL_ERR_NONE) { string message = "Segment data header returned an error: \n\n"; message += "errorNumber: " + dataHeader.errorNum.ToString() + "\n"; message += "errorInfo: " + dataHeader.errorInfo; Utilites.LogMessage(message); return(true); } // get the data // check if need to remove anti aliasing // need startIndex after the while loop so declared outside if (measParams.RemoveAntiAliasing) { startIndex = GetStartIndex(fftParams.SampleRate, (int)fftParams.NumFftBins); } FloatArrayToListOfDoubles(frequencyData, powerList, numFftsToCopy, startIndex); // check if overload occured if (dataHeader.overload != 0) { overloadFlag = true; } else { overloadFlag = false; } dataRetrieved = true; break; case AgSalLib.SalError.SAL_ERR_NO_DATA_AVAILABLE: // data is not available yet ... break; case AgSalLib.SalError.SAL_ERR_SENSOR_NOT_CONNECTED: // lost connection to sensor, kill the process // service will start it again Environment.Exit(1); break; default: SensorError(err, "salGetSegmentData"); break; } } if (frequencies != null) { // calculate frquencies for (int i = 0; i < numFftsToCopy; i++) { frequencies.Add(dataHeader.startFrequency + (startIndex - 1) * dataHeader.frequencyStep + i * dataHeader.frequencyStep); } } return(false); }