Rob French
4948c79d84
(a) rig to control GQRX (b) GQRX to control rig There's a 0.5 second hang time whenever a given input is made, to prevent dueling inputs. The key thing appears to be, whenever GQRX sets the frequency, I need to tell the rig, but then also, tell GQRX again, so that it centers up the frequency. Other thing to note is that the frequencies are all negative. I wonder if that is actually necessary.
212 lines
6.4 KiB
Python
Executable File
212 lines
6.4 KiB
Python
Executable File
#!/usr/bin/env python
|
|
#
|
|
# Copyright 2017 Alexander Fasching
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
|
|
from __future__ import print_function, division
|
|
|
|
import sys
|
|
import argparse
|
|
import socket
|
|
import time
|
|
|
|
DEBUG=True
|
|
|
|
# States
|
|
WAITING = 0
|
|
RIG_CMD = 1
|
|
RIG_WAIT = 2
|
|
GQRX_CMD = 3
|
|
GQRX_WAIT = 4
|
|
|
|
delay_secs = 0.5
|
|
timeout = 0.0
|
|
|
|
usb_if_offset = -2250
|
|
lsb_if_offset = 0
|
|
|
|
rs = 0
|
|
gs = 0
|
|
|
|
def calc_lnb_lo(freq, ifreq, isusb=True):
|
|
if DEBUG:
|
|
print('Freq: ' + str(freq))
|
|
print('IF Freq: ' + str(ifreq))
|
|
if isusb:
|
|
if DEBUG:
|
|
print('USB IF: ' + str(ifreq + usb_if_offset))
|
|
return - freq - (ifreq - usb_if_offset)
|
|
else:
|
|
if DEBUG:
|
|
print('LSB IF: ' + str(ifreq + lsb_if_offset))
|
|
return - freq - (ifreq + lsb_if_offset)
|
|
|
|
def get_rig_freq(rs):
|
|
rs.send(b'm\n')
|
|
is_usb = True if rs.recv(1024).splitlines()[0] == "USB" else False
|
|
rs.send(b'f\n')
|
|
return (int(rs.recv(1024)), is_usb)
|
|
|
|
def set_rig_freq(rs, gs, freq, ifreq, isusb):
|
|
if DEBUG:
|
|
print('Set Rig freq: {}'.format(freq).encode())
|
|
|
|
rs.send(b'F ' + str(freq) + '\n')
|
|
rs.recv(1024)
|
|
|
|
lnb = calc_lnb_lo(freq, ifreq, isusb)
|
|
if DEBUG:
|
|
print('Set LNB Freq: {}'.format(lnb).encode())
|
|
|
|
gs.send('LNB_LO {}'.format(lnb).encode())
|
|
gs.recv(1024)
|
|
|
|
if DEBUG:
|
|
print('Set GQRX Freq: {}'.format(freq).encode())
|
|
|
|
gs.send('F {}'.format(-1 * freq).encode())
|
|
gs.recv(1024)
|
|
|
|
|
|
def get_gqrx_freq(gs):
|
|
gs.send(b'f\n')
|
|
return int(gs.recv(1024)) * -1
|
|
|
|
def set_gqrx_freq(gs, freq, ifreq, isusb):
|
|
lnb = calc_lnb_lo(freq, ifreq, isusb)
|
|
|
|
if DEBUG:
|
|
print('Set LNB Freq: {}'.format(lnb).encode())
|
|
|
|
gs.send('LNB_LO {}'.format(lnb).encode())
|
|
gs.recv(1024)
|
|
|
|
if DEBUG:
|
|
print('Set GQRX Freq: {}'.format(freq).encode())
|
|
|
|
gs.send('F {}'.format(-1 * freq).encode())
|
|
gs.recv(1024)
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('-ga', '--gqrx-address', type=str, default='localhost',
|
|
metavar='A', help='address that Gqrx is listening on')
|
|
parser.add_argument('-gp', '--gqrx-port', type=int, default=7356,
|
|
metavar='P', help='remote control port configured in Gqrx')
|
|
parser.add_argument('-ra', '--rigctld-address', type=str, default='localhost',
|
|
metavar='A', help='address that rigctld is listening on')
|
|
parser.add_argument('-rp', '--rigctld-port', type=int, default=4532,
|
|
metavar='P', help='listening port of rigctld')
|
|
parser.add_argument('-i', '--interval', type=int, default=1000,
|
|
metavar='T', help='update interval in milliseconds')
|
|
parser.add_argument('-f', '--ifreq', type=float, default=68.33,
|
|
metavar='F', help='intermediate frequency in MHz')
|
|
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
rs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
rs.connect((args.rigctld_address, args.rigctld_port))
|
|
except Exception as e:
|
|
print('Connection to rigctld failed:', e, file=sys.stderr)
|
|
return 1
|
|
|
|
try:
|
|
gs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
gs.connect((args.gqrx_address, args.gqrx_port))
|
|
except Exception as e:
|
|
print('Connection to Gqrx failed:', e, file=sys.stderr)
|
|
return 1
|
|
|
|
try:
|
|
if_freq = int(args.ifreq * 1e6)
|
|
old_is_usb = True
|
|
tmp_is_usb = True
|
|
old_rig_freq = 0
|
|
old_gqrx_freq = 0
|
|
|
|
(rig_freq, is_usb) = get_rig_freq(rs)
|
|
gqrx_freq = get_gqrx_freq(gs)
|
|
|
|
state = WAITING
|
|
|
|
while True:
|
|
old_is_usb = is_usb
|
|
old_rig_freq = rig_freq
|
|
old_gqrx_freq = gqrx_freq
|
|
|
|
(rig_freq, is_usb) = get_rig_freq(rs)
|
|
gqrx_freq = get_gqrx_freq(gs)
|
|
|
|
if state == WAITING:
|
|
if rig_freq != old_rig_freq or is_usb != old_is_usb:
|
|
set_gqrx_freq(gs, rig_freq, if_freq, is_usb)
|
|
timeout = time.time() + delay_secs
|
|
state = RIG_CMD
|
|
if DEBUG:
|
|
print("\nNew state: RIG_CMD\n");
|
|
|
|
elif gqrx_freq != old_gqrx_freq:
|
|
tmp_is_usb = is_usb
|
|
set_rig_freq(rs, gs, gqrx_freq, if_freq, tmp_is_usb)
|
|
timeout = time.time() + delay_secs
|
|
state = GQRX_CMD
|
|
if DEBUG:
|
|
print("\nNew state: GQRX_CMD\n");
|
|
|
|
elif rig_freq != gqrx_freq:
|
|
set_gqrx_freq(gs, rig_freq, if_freq, is_usb)
|
|
timeout = time.time() + delay_secs
|
|
state = RIG_CMD
|
|
if DEBUG:
|
|
print("\nNew state: RIG_CMD\n");
|
|
|
|
if state == RIG_CMD:
|
|
if rig_freq != old_rig_freq or is_usb != old_is_usb:
|
|
set_gqrx_freq(gs, rig_freq, if_freq, is_usb)
|
|
timeout = time.time() + delay_secs
|
|
elif time.time() > timeout or rig_freq == gqrx_freq:
|
|
state = WAITING
|
|
if DEBUG:
|
|
print("\nNew state: WAITING\n");
|
|
|
|
if state == GQRX_CMD:
|
|
if gqrx_freq != old_gqrx_freq:
|
|
set_rig_freq(rs, gs, gqrx_freq, if_freq, tmp_is_usb)
|
|
timeout = time.time() + delay_secs
|
|
elif time.time() > timeout or rig_freq == gqrx_freq:
|
|
state = WAITING
|
|
if DEBUG:
|
|
print("\nNew state: WAITING\n");
|
|
is_usb = tmp_is_usb
|
|
|
|
else:
|
|
pass
|
|
|
|
time.sleep(args.interval / 1000.0)
|
|
except KeyboardInterrupt:
|
|
pass
|
|
except Exception as e:
|
|
print('Unexpected error:', e, file=sys.stderr)
|
|
return 1
|
|
|
|
rs.close()
|
|
gs.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main() or 0)
|