Skip to content

Instantly share code, notes, and snippets.

@keflavich
Last active July 25, 2020 00:24
Show Gist options
  • Select an option

  • Save keflavich/727eb593c367cacc73f9e9fa50b8d8d3 to your computer and use it in GitHub Desktop.

Select an option

Save keflavich/727eb593c367cacc73f9e9fa50b8d8d3 to your computer and use it in GitHub Desktop.
Installation script for the rtlsdr on windows
import pkgutil
from pathlib import Path
import sys
import requests
import zipfile
from io import BytesIO
from ctypes import CDLL
from ctypes.util import find_library
import subprocess
import os
subprocess.check_call([sys.executable, "-m", "pip", "install", 'https://github.com/roger-/pyrtlsdr/archive/master.zip'])
loader = pkgutil.get_loader('rtlsdr')
rtlsdr_path = Path(loader.path)
dllpath = rtlsdr_path.parents[2] / 'DLLs'
binpath = rtlsdr_path.parents[3] / 'Library' / 'bin'
print(f"dllpath={dllpath}, binpath={binpath}")
response = requests.get('https://github.com/rtlsdrblog/rtl-sdr/releases/download/v1.1/bt_driver.zip')
response.raise_for_status()
zf = zipfile.ZipFile(BytesIO(response.content))
zf.extractall(binpath, members=[x for x in zf.namelist() if 'bias' in x or 'dll' in x or 'rtl' in x])
tee = binpath / "rtl_biast.exe"
assert os.path.isfile(tee)
nbits = 64 if (sys.maxsize > 2**32) else 32
url = f'https://github.com/librtlsdr/librtlsdr/releases/download/v0.7.0/librtlsdr-0.7.0-13-gc79d-x{nbits}.zip'
response = requests.get(url)
response.raise_for_status()
zf = zipfile.ZipFile(BytesIO(response.content))
zf.extractall(binpath, members=[x for x in zf.namelist() if 'dll' in x])
zf.extractall(dllpath, members=[x for x in zf.namelist() if 'dll' in x])
dlls = [x for x in zf.namelist() if 'dll' in x]
print(f"Extracted {dlls} to {binpath} and {dllpath}")
assert find_library('librtlsdr'),"Installation failed."
assert find_library('librtlsdr.dll'),"Installation failed."
device_id = 0
device_check = subprocess.run([str(binpath / "rtl_tcp.exe")], check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(device_check)
for row in device_check.stderr.split(b"\n"):
if b'Realtek' in row:
device_id = int(row.strip().split(b":")[0])
assert subprocess.call(f"{tee} -d {device_id} -b 1".split()) == 0
print(tee)
assert CDLL(find_library('librtlsdr')), "Installation failed - likely wrong bitnumber library."
"""
If the installation fails, check that both librtlsdr.dll and libusb-1.0.dll are on the Windows system executable path (not the python path).
Also, check that the dll is the appropriate 32 / 64 bit version
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment