public static RTLDevice[] GetActiveDevices() { uint _devicecount = NativeMethods.rtlsdr_get_device_count(); var _result = new RTLDevice[_devicecount]; for (var i = 0u; i < _devicecount; i++) { // get device name string _name = NativeMethods.rtlsdr_get_device_name(i); // get device pointer System.IntPtr _dev; if (NativeMethods.rtlsdr_open(out _dev, i) > 0) { throw new InvalidOperationException("Cannot open RTL device. Is the device locked somewhere?"); } try { // get tuner type RtlSdrTunerType _tunertype; _tunertype = NativeMethods.rtlsdr_get_tuner_type(_dev); // get descriptor strings StringBuilder _manufact = new StringBuilder(256); StringBuilder _product = new StringBuilder(256); StringBuilder _serial = new StringBuilder(256); NativeMethods.rtlsdr_get_usb_strings(_dev, _manufact, _product, _serial); RTLGainStages _gainstages = new RTLGainStages(); RTLTunerGains _tunergains = new RTLTunerGains(); RtlSdrGainMode _gainmode = RtlSdrGainMode.GAIN_MODE_MANUAL; double _maxpower = 0; // try to get all possible gain settings for all stages // requires special version of rtlsdr.dll try { // iterate through 256 possible stages for (byte stage = 0; stage < 255; stage++) { // get number of gains per stage int _count = NativeMethods.rtlsdr_get_tuner_stage_gains(_dev, stage, null, null); // stopop iteration at first stage with no entries or error if (_count <= 0) { break; } var _gains = new int[_count]; StringBuilder _desc = new StringBuilder(256); // get gains NativeMethods.rtlsdr_get_tuner_stage_gains(_dev, stage, _gains, _desc); // add new entry to array _gainstages.AddGains(_desc.ToString(), _gains); } } catch { // no gain settings found or wrong version of rtlsdr.dll } if (_gainstages.Count > 0) { // gain settings per stage were found --> assuming that special version of rtlsdr is loaded // we can set special settings per stage by ourselves // save default gain settings to file to ensure that we have at least default settings on a file SaveDefaultGainSettings(_tunertype, _gainstages); // try to read gain settings per stage from file // set gain mode to gain per stage _gainmode = RtlSdrGainMode.GAIN_MODE_PERSTAGE; // calculate file name according to tuner type string filename = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + _tunertype.ToString() + ".tun"; try { using (StreamReader sr = new StreamReader(filename)) { while (!sr.EndOfStream) { // read a line string s = sr.ReadLine().Trim(); if (!String.IsNullOrEmpty(s) && !s.StartsWith("//")) { // not comment, try to split into single values try { string[] a = s.Split(';'); // check array for length --> must be equal to stages count if (a.Length != _gainstages.Count) { throw (new InvalidOperationException("Tuner type: " + _tunertype + "\n" + "Filename: " + filename + "\n" + "Number of entries does not match number of stages" + " [" + _gainstages.Count.ToString() + "] in line: " + s)); } // initialize gain arry int[] gains = new int[_gainstages.Count]; // set gain values for (int index = 0; index < a.Length; index++) { // convert string to int try { gains[index] = System.Convert.ToInt32(a[index].Trim()); } catch { throw (new InvalidOperationException("Tuner type: " + _tunertype + "\n" + "Filename: " + filename + "\n" + "Invalid gain entry in line: " + s)); } // validate entry with gain stages if (!_gainstages.ValidateGain(_gainstages[index], gains[index])) { throw (new InvalidOperationException("Tuner type: " + _tunertype + "\n" + "Filename: " + filename + "\n" + "Gain entry is not valid for this stage[ " + _gainstages[index] + "] in line: " + s)); } } // sum up and correct tuner gains int _gainsum = 0; switch (_tunertype) { case RtlSdrTunerType.E4000: _gainsum = _gainstages.GainSumE4000(gains); break; case RtlSdrTunerType.R820T: _gainsum = _gainstages.GainSumR820T(gains); break; default: _gainsum = _gainstages.GainSumDefault(gains); break; } // add them to TunerGains object _tunergains.AddGains(_gainsum, gains); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error Reading Tuner Gains from File" + filename); } } } } } catch { // do nothing if file not found } } if (_tunergains.Count <= 0) { // file read was not successful or old version of rtlsdr.dll is loaded // try to get per stage settings from DLL // try to set gain mode to manual if (NativeMethods.rtlsdr_set_tuner_gain_mode(_dev, RtlSdrGainMode.GAIN_MODE_MANUAL) != 0) { throw new InvalidOperationException("Cannot set tuner gain mode: " + RtlSdrGainMode.GAIN_MODE_MANUAL.ToString()); } if (_tunertype == RtlSdrTunerType.E4000) { // OK, we have a E4000 tuner and we will emulate a sensitivity mode // set emulated gains according to SM5BSZ int[] _gains = { -250, -200, -150, -100, -50, 0, 50, 100, 150, 200, 250 }; // add each tuner gain to tuner gains, gain settings per stage set to 0 entries (not used) foreach (int gain in _gains) { int[] _dummy = new int[0]; _tunergains.AddGains(gain, _dummy); } // store gain mode, we will use it later _gainmode = RtlSdrGainMode.GAIN_MODE_SENSITIVITY; } else { // very old or very official version of rtlsdr.dll // try to get standard stages from DLL // get size of tuner gain table first int _count = NativeMethods.rtlsdr_get_tuner_gains(_dev, null); if (_count < 0) { // set count to 0 if fails _count = 0; } // get tuner gain table if count > 0 int[] _gains = new int[_count]; if (_count >= 0) { // get tuner gains from dll if (NativeMethods.rtlsdr_get_tuner_gains(_dev, _gains) < 0) { throw new InvalidOperationException("Cannot get tuner gains."); } // add each tuner gain to tuner gains, gain settings per stage set to 0 entries (not used) foreach (int gain in _gains) { int[] _dummy = new int[0]; _tunergains.AddGains(gain, _dummy); } } // store gain mode, we will use it later _gainmode = RtlSdrGainMode.GAIN_MODE_MANUAL; } } // set MaxGain according to tuner type switch (_tunertype) { case RtlSdrTunerType.E4000: _maxpower = SupportFunctions.ToLinear(50); break; case RtlSdrTunerType.R820T: _maxpower = SupportFunctions.ToLinear(45); break; default: _maxpower = SupportFunctions.ToLinear(50); break; } // initialize new RTLDevice entry _result[i] = new RTLDevice { Index = i, TunerType = _tunertype, Name = _name, Manufacturer = _manufact.ToString(), Product = _product.ToString(), Serial = _serial.ToString(), GainMode = _gainmode, GainStages = _gainstages, TunerGains = _tunergains, MaxPower = _maxpower }; } finally { // try to close the device anyway NativeMethods.rtlsdr_close(_dev); } } return(_result); }
public static extern int rtlsdr_set_tuner_gain_mode(IntPtr dev, RtlSdrGainMode mode);