#!/usr/bin/env python3 """Pluto -> RTL-SDR loopback test: make a recording, transmit it in the background, capture with rtl_sdr at the same time, then decode. Every frequency comes from --freq and the two IF offsets, so the transmitter, the capture and the decoder can't disagree: Pluto LO = freq - tx_if (signal sits tx_if above it) RTL LO = freq - rx_if (signal sits rx_if above it, clear of DC) ./ota_test.py # SF9, 62.5 kHz, 223.580 MHz ./ota_test.py --sf 12 --seconds 20 --rx-gain 25 ./ota_test.py --sf 7 --msg "hello" --repeat 10 --cw-id The Pluto is always stopped on exit, including Ctrl-C and errors. Files: _tx.sigmf-meta/-data (transmitted) and .cu8 (capture). """ import argparse, os, shutil, signal, subprocess, sys, time HERE = os.path.dirname(os.path.abspath(__file__)) def show(cmd): print("$ " + " ".join(cmd), flush=True) def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--src", default="KD2HSG", help="your callsign") ap.add_argument("--msg", default=None, help="message (default 'test de ')") ap.add_argument("--sf", type=int, default=9) ap.add_argument("--bw", type=float, default=62.5e3) ap.add_argument("--freq", type=float, default=223.58e6, help="signal center, Hz") ap.add_argument("--repeat", type=int, default=5, help="frames per recording") ap.add_argument("--gap", type=float, default=0.5, help="seconds between frames") ap.add_argument("--cw-id", action="store_true", help="CW ID after the frames") ap.add_argument("--coherent", action="store_true", help="coherent payload (index + QPSK); default only at SF 7-9 above 62.5 kHz") ap.add_argument("--noncoherent", action="store_true", help="index-only payload; default at SF >= 10 and at <= 62.5 kHz") ap.add_argument("--tx-fs", type=float, default=1e6) ap.add_argument("--tx-if", type=float, default=100e3, help="signal offset above Pluto LO") ap.add_argument("--tx-gain", type=float, default=-30.0, help="Pluto TX gain, dB") ap.add_argument("--uri", default="ip:192.168.2.1", help="Pluto URI") ap.add_argument("--rx-fs", type=float, default=2.4e6) ap.add_argument("--rx-if", type=float, default=250e3, help="signal offset above RTL LO") ap.add_argument("--rx-gain", type=float, default=30.0, help="RTL-SDR tuner gain, dB") ap.add_argument("--rtl-device", default="0", help="rtl_sdr -d index or serial") ap.add_argument("--seconds", type=float, default=12.0, help="capture length") ap.add_argument("--name", default="ota", help="file name prefix") a = ap.parse_args() if not shutil.which("rtl_sdr"): sys.exit("rtl_sdr not found on PATH") msg = a.msg or f"test de {a.src}" tx_rec, cap = f"{a.name}_tx", f"{a.name}.cu8" rx_lo = a.freq - a.rx_if py = sys.executable # 1. transmit recording cmd = [py, os.path.join(HERE, "tx_file.py"), tx_rec, "--src", a.src, "--msg", msg, "--sf", str(a.sf), "--bw", repr(a.bw), "--fs", repr(a.tx_fs), "--freq", repr(a.freq), "--if-offset", repr(a.tx_if), "--repeat", str(a.repeat), "--gap", repr(a.gap)] + (["--cw-id"] if a.cw_id else []) \ + (["--noncoherent"] if a.noncoherent else []) \ + (["--coherent"] if a.coherent else []) show(cmd) subprocess.run(cmd, check=True) sys.path.insert(0, HERE) import iq_io n = iq_io.recording_samples(tx_rec) if n > iq_io.PLUTO_MAX_SAMPLES and a.repeat > 1: # Pluto TX buffer limit rep = max(1, int(a.repeat * iq_io.PLUTO_MAX_SAMPLES / n)) print(f"recording too large for the Pluto's buffer ({n / 1e6:.1f} M samples); " f"remaking with {rep} frame(s)", flush=True) cmd[cmd.index("--repeat") + 1] = str(rep) show(cmd) subprocess.run(cmd, check=True) if iq_io.recording_samples(tx_rec) > iq_io.PLUTO_MAX_SAMPLES: sys.exit("even one frame exceeds the Pluto's buffer; lower --tx-fs") # 2. Pluto in the background; wait until it reports it is transmitting cmd = [py, os.path.join(HERE, "tools", "pluto_tx.py"), tx_rec, "--uri", a.uri, "--gain", repr(a.tx_gain), "--seconds", repr(a.seconds + 60)] show(cmd) pluto = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, env=dict(os.environ, PYTHONUNBUFFERED="1")) try: t0 = time.time() while True: line = pluto.stdout.readline() if not line: sys.exit(f"pluto_tx exited (code {pluto.wait()}) before transmitting") print(" pluto: " + line.rstrip(), flush=True) if "looping" in line: break if time.time() - t0 > 30: sys.exit("pluto_tx didn't start within 30 s") time.sleep(1.0) # let the buffer start playing # 3. capture while it transmits cmd = ["rtl_sdr", "-d", a.rtl_device, "-f", str(int(round(rx_lo))), "-s", str(int(a.rx_fs)), "-g", repr(a.rx_gain), "-n", str(int(a.seconds * a.rx_fs)), cap] show(cmd) r = subprocess.run(cmd) if r.returncode != 0: sys.exit(f"rtl_sdr failed (code {r.returncode})") finally: # 4. always stop the transmitter if pluto.poll() is None: pluto.send_signal(signal.SIGINT) try: out, _ = pluto.communicate(timeout=10) except subprocess.TimeoutExpired: pluto.kill() out, _ = pluto.communicate() for line in (out or "").splitlines(): print(" pluto: " + line) # 5. decode cmd = [py, os.path.join(HERE, "rx_file.py"), cap, "--format", "cu8", "--fs", repr(a.rx_fs), "--lo", repr(rx_lo), "--freq", repr(a.freq), "--sf", str(a.sf), "--bw", repr(a.bw), "-v"] show(cmd) sys.exit(subprocess.run(cmd).returncode) if __name__ == "__main__": main()