|
| 1 | +#! /usr/bin/python3 |
| 2 | +##--------------------------------------------------------------------\ |
| 3 | +# hackrfpy 'examples/tx_test_tone.py' |
| 4 | +# The library's TX side, demonstrated the SAFE way: the deliberate |
| 5 | +# RX->TX mode switch (which prints the safety banner), a constant-wave |
| 6 | +# test tone via transmit_cw, and the dead-man duration bound so the |
| 7 | +# transmitter can never outlive the script. |
| 8 | +# |
| 9 | +# This is the only example that transmits. It is deliberately timid: |
| 10 | +# - duration is REQUIRED and capped at 10 s |
| 11 | +# - txvga defaults low; the RF amp is never enabled here |
| 12 | +# - --print-cmd previews the exact hackrf_transfer command w/o running |
| 13 | +# |
| 14 | +# TRANSMITTING IS REGULATED. Use a dummy load or a shielded setup, and |
| 15 | +# operate only within your license privileges and local law. A CW |
| 16 | +# carrier is useful for antenna checks and for giving a nearby receiver |
| 17 | +# (or your own second SDR) a known signal to find. |
| 18 | +# |
| 19 | +# Usage: |
| 20 | +# uv run python examples/tx_test_tone.py --freq 433.92M --seconds 2 |
| 21 | +# uv run python examples/tx_test_tone.py --freq 915M --seconds 2 --print-cmd |
| 22 | +# |
| 23 | +# |
| 24 | +# Author(s): Lauren Linkous |
| 25 | +##--------------------------------------------------------------------\ |
| 26 | +import argparse |
| 27 | +import sys |
| 28 | + |
| 29 | +from hackrfpy import HackRF, parse_freq |
| 30 | + |
| 31 | +MAX_SECONDS = 10.0 |
| 32 | + |
| 33 | + |
| 34 | +def main(): |
| 35 | + p = argparse.ArgumentParser( |
| 36 | + description="Bounded CW test-tone transmit (TX-gated).") |
| 37 | + p.add_argument("--freq", required=True, |
| 38 | + help="carrier frequency (parse_freq notation, e.g. 433.92M)") |
| 39 | + p.add_argument("--rate", default="2M", help="sample rate (default 2M)") |
| 40 | + p.add_argument("--seconds", type=float, required=True, |
| 41 | + help=f"transmit duration; required, max {MAX_SECONDS:g}s") |
| 42 | + p.add_argument("--txvga", type=int, default=4, |
| 43 | + help="TX VGA gain dB (default 4, deliberately low)") |
| 44 | + p.add_argument("--amplitude", type=int, default=64, |
| 45 | + help="CW DAC amplitude 0-127 (default 64)") |
| 46 | + p.add_argument("--print-cmd", action="store_true", |
| 47 | + help="preview the command without transmitting") |
| 48 | + p.add_argument("--tools-dir", default=None) |
| 49 | + args = p.parse_args() |
| 50 | + |
| 51 | + if not args.print_cmd and not 0 < args.seconds <= MAX_SECONDS: |
| 52 | + print(f"--seconds must be in (0, {MAX_SECONDS:g}]", file=sys.stderr) |
| 53 | + return 2 |
| 54 | + |
| 55 | + h = HackRF(tools_dir=args.tools_dir, verbose=True) |
| 56 | + freq, rate = parse_freq(args.freq), parse_freq(args.rate) |
| 57 | + |
| 58 | + # The deliberate confirmation: transmit() refuses in the default RX |
| 59 | + # mode, and this switch prints the one-time TX safety banner. |
| 60 | + h.set_mode("tx") |
| 61 | + h.transmit_cw(freq, rate, amplitude=args.amplitude, txvga=args.txvga, |
| 62 | + duration=args.seconds, print_cmd=args.print_cmd) |
| 63 | + h.set_mode("rx") # leave the object safe for whatever follows |
| 64 | + if not args.print_cmd: |
| 65 | + print(f"[*] transmitted {args.seconds:g}s CW at {freq/1e6:g} MHz") |
| 66 | + return 0 |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + sys.exit(main()) |
0 commit comments