Skip to content

Commit

Permalink
Written README. Reordered code. Added exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Mattii committed May 6, 2019
1 parent f9ef8f1 commit ffbd577
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 29 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# gqrx-fscanner
Script that drives gqrx via telnet to implement frequency scanning
> Script that drives gqrx via telnet to implement frequency scanning.
# Setup
1) **Configure** *Allowed hosts* and *Port* in gqrx from Tools>Remote control settings
2) **Enable** remote control by checking Tools>Remote control
3) **Change** `HOST` and `PORT` in the code to match your configuration (default values should work)

# Usage
## Scan frequencies from file
`./gqrx-fscanner file.txt`

`file.txt` must contain a list of integer frequencies in Hz, one per line.

## Scan frequencies from gqrx bookmarks
`./gqrx-fscanner 145000000 148000000`

Scan bookmarked frequencies between 145MHz and 148MHz.
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
- more checks on operations: read from file, connect to telnet, etc..
- add connectionreset exception
- use wait time parameter integrated in telnet instead of manual sleep
68 changes: 41 additions & 27 deletions gqrx-fscanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,39 @@
HOST = "127.0.0.1"
PORT = 7356

# Send a command via telnet and return the answer
def parse_bookmarks(lf, hf):
'''Parse gqrx bookmark file and extract frequencies between lf and hf'''

tmp_freq = []

with open(os.path.expanduser("~")+'/.config/gqrx/bookmarks.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
for row in csv_reader:
if len(row) == 0:
continue
if row[0][0] == '#':
row[0] = row[0][1:]
labels = [i.strip() for i in row] # Strip out all the spaces
elif labels != None and len(labels) == len(row) == 5:
row = [i.strip() for i in row] # Strip out all the spaces
row = dict(zip(labels,row))
if int(row['Frequency']) < hi_freq and int(row['Frequency']) > lo_freq:
tmp_freq.append(row['Frequency'])

return tmp_freq

def send_cmd(s):
'''Send a command via telnet and return the answer'''

cmd = s + '\n'
cmd = cmd.encode('UTF-8')
tn.write(cmd)
sleep(0.1)
sleep(0.1) # Wait answer
try:
a = tn.read_eager()
except EOFError:
print("Connection closed")
exit(1)
sys.exit(1)

return a.decode('UTF-8')

Expand All @@ -31,45 +53,37 @@ def send_cmd(s):
index = 0
TIME_SLOT = 0.3 # seconds between skips

if len(sys.argv) == 1:
with open("freq.txt", "rt") as f:
if len(sys.argv) == 2:
# Read from txt
with open(sys.argv[1], "rt") as f:
FREQ = f.read().splitlines()
elif len(sys.argv) == 3:
try:
lo_freq = int(sys.argv[1])
hi_freq = int(sys.argv[2])
except:
print("Your argument is invalid!")
sys.exit(-1)
sys.exit(1)

# Open bookmark
with open(os.path.expanduser("~")+'/.config/gqrx/bookmarks.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
for row in csv_reader:
if len(row) == 0:
continue
if row[0][0] == '#':
row[0] = row[0][1:]
labels = [i.strip() for i in row]
elif labels != None and len(labels) == len(row):
if len(row) == 6:
row = [i.strip() for i in row]
row = dict(zip(labels,row))
if int(row['Frequency']) < hi_freq and int(row['Frequency']) > lo_freq:
FREQ.append(row['Frequency'])
#elif len(labels) == 2:
# Parse gqrx bookmarks
FREQ = parse_bookmarks(lo_freq, hi_freq)

else:
print("Usage:")
print("Read from gqrx bookmarks: ./script low_freq high_freq")
print("Read from freq.txt : ./script\n")
sys.exit(-1)
print("Read from gqrx bookmarks: ./script low_freq high_freq")
print("Read frequencies from file: ./script path/to/file\n")
sys.exit(1)

if len(FREQ) == 0:
print("No frequencies given")
sys.exit(1)

tn = telnetlib.Telnet(HOST, PORT)

# Open telnet connection with gqrx
try:
tn = telnetlib.Telnet(HOST, PORT)
except ConnectionRefusedError:
print("Connection refused\nCheck gqrx network settings")
sys.exit(1)

while(1):
sql = send_cmd("l SQL") # read SQUELCH level
Expand Down

0 comments on commit ffbd577

Please sign in to comment.