forked from g0fcu/gqrx-hamlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gqrx-hamlib-fldigi.py
132 lines (116 loc) · 3.87 KB
/
gqrx-hamlib-fldigi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# gqrx-hamlib - a gqrx to Hamlib interface to keep frequency
# between gqrx and a radio in sync when using gqrx as a panadaptor
# using Hamlib to control the radio
#
# The Hamlib daemon (rigctld) must be running, gqrx started with
# the 'Remote Control via TCP' button clicked and
# comms to the radio working otherwise an error will occur when
# starting this program. Ports used are the defaults for gqrx and Hamlib.
#
# Return codes from gqrx and Hamlib are printed to stderr
#
# This program is written in Python 3.5
# Python libraries required are:
# - socket
# - sys
# - getopt
# - time
# - xmlrpc.client
#
# To run it type the following on the command line in the directory where
# you have placed this file:
# python3.5 ./gqrx-hamlib-fldigi.py [-f]
#
# The -f option will cause the program to tune fldigi to the gqrx frequency.
#
# Copyright 2017 Simon Kennedy, G0FCU, g0fcu at g0fcu.com
# 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/>.
import socket
import sys, getopt
import time
fldigi_option_set = 0
if len(sys.argv) > 0:
try:
opts, args = getopt.getopt(sys.argv, 'f',)
except getopt.GetoptError:
print('gqrx-hamlib.py [-f]')
sys.exit(2)
for index in range(len(args)):
if args[index] == '-f':
fldigi_option_set = 1
if fldigi_option_set == 1:
import xmlrpc.client
TCP_IP = "localhost"
RIG_PORT = 4532
GQRX_PORT = 7356
FLDIGI_PORT = 7362
MESSAGE = ""
forever = 1
rig_freq = 0
gqrx_freq = 0
old_rig_freq = 0
old_gqrx_freq = 0
def getfreq(PORT):
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
# Bind the socket to the port
server_address = (TCP_IP, PORT)
sock.connect(server_address)
sock.sendall(b'f\n')
# Look for the response
amount_received = 0
amount_expected = 8 #len(message)
while amount_received < amount_expected:
data = sock.recv(16)
amount_received += len(data)
sock.close()
return data
def setfreq(PORT, freq):
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
# Bind the socket to the port
server_address = (TCP_IP, PORT)
sock.connect(server_address)
build_msg = 'F ' + str(freq) + '\n'
MESSAGE = bytes(build_msg, 'utf-8')
sock.sendall(MESSAGE)
# Look for the response
amount_received = 0
amount_expected = 7 #len(message)
while amount_received < amount_expected:
data = sock.recv(16)
amount_received += len(data)
sock.close()
return data
if fldigi_option_set == 1:
server = xmlrpc.client.ServerProxy('http://{}:{}/'.format(TCP_IP, FLDIGI_PORT))
while forever:
time.sleep(0.2)
rig_freq = getfreq(RIG_PORT)
if rig_freq != old_rig_freq:
# set gqrx to Hamlib frequency
rc = setfreq(GQRX_PORT, float(rig_freq))
print('Return Code from GQRX: {0}'.format(rc))
old_rig_freq = rig_freq
old_gqrx_freq = rig_freq
gqrx_freq = getfreq(GQRX_PORT)
if gqrx_freq != old_gqrx_freq:
# set Hamlib to gqrx frequency
rc = setfreq(RIG_PORT, float(gqrx_freq))
print('Return Code from Hamlib: {0}'.format(rc))
# Set fldigi to gqrx frequency
if fldigi_option_set == 1:
server.main.set_frequency(float(gqrx_freq))
old_gqrx_freq = gqrx_freq
old_rig_freq = gqrx_freq