Created
October 4, 2019 08:11
-
-
Save davidhoness/220d8316c7d4369a5cccbefe2e309bd8 to your computer and use it in GitHub Desktop.
Python script for tuning rtl_fm using input from WxToImg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import serial | |
import socket | |
import time | |
import subprocess | |
""" | |
DC0;SQ0000;FA00137100000;MD4;FA00137102450;FA00137102440;FA00137102436;... | |
Sent over serial by WxToImg for doppler tuning if below is set; | |
Options > Recording Options > | |
receiver type = Kenwood | |
receiver port = /dev/ttyAMA0 | |
receiver baud rate = 9600 | |
Short Raspberry Pi UART TXD/RXD pins together | |
""" | |
IN_DEV = "/dev/ttyAMA0" | |
BAUD = 9600 | |
END_MSG = ";" | |
RTL_RATE = "60k" | |
class rtl_fm_remote(object): | |
""" | |
For remote control of rtl_fm command line program | |
""" | |
def __init__(self, | |
host="localhost", | |
port=6020): | |
self._host = host | |
self._port = port | |
self._s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
self._s.connect((self._host, self._port)) | |
def set_freq(self, freq): | |
self.send_cmd(0, freq) | |
def set_mode(self, mode): | |
self.send_cmd(1, mode) | |
def set_squelch(self, squelch): | |
self.send_cmd(2, squelch) | |
def set_gain(self, gain): | |
self.send_cmd(3, gain) | |
def send_cmd(self, cmd, param): | |
cmd_bytes = (cmd).to_bytes(1, "little") | |
param_bytes = (param).to_bytes(32, "little") | |
self._s.send(cmd_bytes + param_bytes) | |
def __del__(self): | |
self._s.close() | |
rtl_proc = None | |
play_proc = None | |
rtl_remote = None | |
last_rx_time = 0 | |
ser = serial.Serial(port=IN_DEV, baudrate=BAUD) | |
buffer = "" | |
rx_freq = 0 | |
while True: | |
time.sleep(.1) | |
buffer += ser.read(ser.inWaiting()).decode("UTF8") | |
if END_MSG in buffer: | |
lines = buffer.split(END_MSG) | |
buffer = "" | |
for line in lines: | |
if len(line) > 0 and line.startswith("FA"): | |
str_freq = line[2:] | |
if str_freq.isnumeric(): | |
last_rx_time = time.time() | |
rx_freq = int(str_freq) | |
if rtl_proc is None: | |
rtl_cmd = ["rtl_fm", "-M", "fm", "-f", str(rx_freq), "-s", RTL_RATE, "-E", "deemp", "-F", "9"] | |
play_cmd = ["play", "-r", RTL_RATE, "-t", "raw", "-e", "s", "-b", "16", "-c", "1", "-V1", "-"] | |
rtl_proc = subprocess.Popen(rtl_cmd, stdout=subprocess.PIPE) | |
time.sleep(1) | |
play_proc = subprocess.Popen(play_cmd, stdin=rtl_proc.stdout) | |
time.sleep(1) | |
rtl_remote = rtl_fm_remote() | |
else: | |
rtl_remote.set_freq(rx_freq) | |
if rtl_proc is not None: | |
if time.time() - last_rx_time > 60: | |
del(rtl_remote) | |
rtl_remote = None | |
play_proc.kill() | |
play_proc.wait() | |
play_proc = None | |
rtl_proc.kill() | |
rtl_proc.wait() | |
rtl_proc = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment