Skip to content

Rtl sdr

pysdrlib.rtl_sdr

RTL-SDR dongles through librtlsdr (receive only).

Specs

  • Frequency (24 MHz - 1.766 GHz): the R820T/R820T2/R828D tuner on nearly every current dongle. The older E4000 and FC00xx tuners cover other ranges
  • Sample rate (225,001 - 300,000 Hz and 900,001 - 3.2 MHz): rates between the two ranges snap to the nearest edge. Over about 2.56 MHz a dongle on USB 2.0 usually drops samples
  • Samples (complex uint8, offset binary, converted to complex64)
  • Gain (dB): one tuner gain, set_rx_rf_gain (same as set_rx_gain), 0 - 49.6 in uneven steps; the driver snaps to the nearest. set_agc(True) (or set_rx_gain_mode("auto")) lets the tuner choose instead, until a gain is set
  • Bias-T (RTL-SDR Blog V3 and V4 only), frequency correction in ppm (set_freq_correction)
  • open(serial_number=None, index=0) picks a dongle when several are attached

Setup

The first time pysdrlib.rtl_sdr is imported it looks for librtlsdr on the system, then in the cache, and otherwise builds from vendor/rtl-sdr (needs cmake, a C compiler and the libusb-1.0 development files).

The bundled build detaches the kernel's DVB driver (dvb_usb_rtl28xxu) while the dongle is in use, and reattaches it afterwards. A system librtlsdr built without that fails to open the dongle until you blacklist the module. On Linux, add a udev rule (before 70-uaccess.rules) if you don't have permission to use the dongle, and replug it:

/etc/udev/rules.d/60-rtlsdr.rules
SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="2838|2832", MODE="0660", TAG+="uaccess"

Run sudo udevadm control --reload-rules to reload udev rules

Modules:

Name Description
rtlsdr

Functions:

Name Description
find

[DeviceInfo] of the RTL-SDR dongles attached

find

find()

[DeviceInfo] of the RTL-SDR dongles attached

Source code in src/pysdrlib/rtl_sdr/__init__.py
def find():
    """[DeviceInfo] of the RTL-SDR dongles attached"""
    from ..devices import DeviceInfo
    from . import lib
    found = []
    units = [lib.rtl_sdr.device_info(i) for i in range(lib.rtl_sdr.device_count())]
    serials = [u[3] for u in units]
    for i, (name, manufacturer, product, serial) in enumerate(units):
        # Many dongles share a serial (00000001): those are opened by their position instead
        args = {"serial": serial} if serial and serials.count(serial) == 1 else {"index": i}
        found.append(DeviceInfo("rtl_sdr", name or f"{manufacturer} {product}".strip(), serial or None, i, args,
                                {"manufacturer": manufacturer, "product": product}))
    return found

rtlsdr

Classes:

Name Description
RTLSDR

RTLSDR

RTLSDR()

Bases: NativeDevice

Methods:

Name Description
set_rx_rf_gain

Tuner gain, 0-49.6 dB. Turns automatic gain off

set_agc

Let the tuner choose its gain, or (False) return to the gain last set: set_rx_gain_mode("auto" / "manual")

set_freq_correction

Correct for the crystal's frequency error, in parts per million: whole ppm in the hardware (tuner and

Source code in src/pysdrlib/rtl_sdr/rtlsdr.py
def __init__(self):
    super().__init__()
    self._ppm_hw = 0   # whole ppm the hardware corrects (see set_freq_correction)
set_rx_rf_gain
set_rx_rf_gain(gain=None, channel=0)

Tuner gain, 0-49.6 dB. Turns automatic gain off

Source code in src/pysdrlib/rtl_sdr/rtlsdr.py
def set_rx_rf_gain(self, gain=None, channel=0):
    """Tuner gain, 0-49.6 dB. Turns automatic gain off"""
    self._check_channel("rx", channel)
    return self._set_gain_stage("RX", "RF", gain, self._set_rx_rf_gain, channel)
set_agc
set_agc(enable)

Let the tuner choose its gain, or (False) return to the gain last set: set_rx_gain_mode("auto" / "manual")

Source code in src/pysdrlib/rtl_sdr/rtlsdr.py
def set_agc(self, enable):
    """Let the tuner choose its gain, or (False) return to the gain last set: `set_rx_gain_mode("auto" / "manual")`"""
    self.set_rx_gain_mode("auto" if enable else "manual")
set_freq_correction
set_freq_correction(ppm, channel=0, accuracy=None)

Correct for the crystal's frequency error, in parts per million: whole ppm in the hardware (tuner and resampler), the fraction left over in what it is asked for. accuracy: as Device.set_freq_correction

Source code in src/pysdrlib/rtl_sdr/rtlsdr.py
def set_freq_correction(self, ppm, channel=0, accuracy=None):
    """Correct for the crystal's frequency error, in parts per million: whole ppm in the hardware (tuner and
    resampler), the fraction left over in what it is asked for. accuracy: as `Device.set_freq_correction`"""
    self._ensure_open("set_freq_correction")
    whole = int(round(ppm))
    self.device.set_freq_correction(whole)
    self._ppm_hw = whole
    super().set_freq_correction(ppm - whole, channel, accuracy)
    return ppm