hr50ctl/hr50ctl.py

167 lines
5.0 KiB
Python
Executable File

#!/usr/bin/python3
import sys
import getopt
import time
import serial
from prettytable import PrettyTable
serial_port = '/dev/ttyUSB0'
baud = 19200
bands = {
'6': '0',
'10': '1',
'12': '2',
'15': '3',
'17': '4',
'20': '5',
'30': '6',
'40': '7',
'60': '8',
'80': '9',
'160': '10'
}
vlcd = {
'STA': '-',
'PTT': '-',
'BND': '-',
'VLT': '-',
'PEP': '-',
'AVG': '-',
'SWR': '-',
'TMP': '-'
}
keying_methods = {
"off" : "0",
"ptt" : "1",
"cor" : "2",
"qrp" : "3"
}
def get_serial(port, baud):
try:
ser = serial.Serial(port, baud, timeout=2)
return ser
except serial.serialutil.SerialException as e:
print("The following error has occured while opening the serial connection to {} with {} baud:".format(port, baud))
print(e)
sys.exit(2)
# sends a command and an optional parameter to the
# Hardrock-50 via the serial interface
def send_command(cmd, param):
ser = get_serial(serial_port, baud)
res = None
try:
command = cmd + param + ';'
ser.write(str.encode(command))
res = ser.readline().decode("utf-8").rstrip()
except Exception as e:
print("The following error has occured while sending the command {} to the HR50:".format(port, baud))
print(e)
ser.close()
return res
# Executed two commands vie the serial interface,
# parsed the result ad polulates a dict with the
# collected information
def get_info():
ser = get_serial(serial_port, baud)
try:
ser.write(b'HRRX;')
time.sleep(0.5)
res = ser.readline().decode("utf-8").rstrip().replace(';', '').split(',')
except Exception as e:
print("The following error has occured while sending the command {} to the HR50:".format(port, baud))
print(e)
ser.close()
return None
vlcd['STA'] = res[0]
vlcd['PTT'] = res[1]
vlcd['BND'] = res[2]
vlcd['TMP'] = res[3]
vlcd['VLT'] = res[4]
ser.write(b'HRMX;')
time.sleep(0.5)
res = ser.readline().decode("utf-8").rstrip().split()
ser.close()
vlcd['PEP'] = res[1][1:]
vlcd['AVG'] = res[2][1:]
if res[3][1:] != "0":
vlcd['SWR'] = res[3][1:2] + "." + res[3][2:3]
def main(argv):
show_info = True
try:
opts, args = getopt.getopt(argv,"hb:sntk:")
except getopt.GetoptError:
print("Error :(")
print("Please consult help via ./hr50ctl.py -h")
sys.exit(2)
for opt, arg in opts:
if opt == '-n':
show_info = False
if opt == '-h':
print("\nUSAGE: # ./hr50ctl.py -b <band> -t -s -k <keying mode>")
print("\nPARAMETERS:")
print(" -k: the following keying methods are available:")
print(" off, cor, ptt, qrp")
print(" This parameter changes the method how the HR50 will be put into TX mode.")
print(" -b: the following bands are available:")
print(" 6, 10, 12, 15, 20, 17, 20, 30, 40, 60, 80, 160")
print(" This parameter changes the band of the HR50")
print(" -t: no parameter required.")
print(" This parameters initiates the tuning during the next PTT")
print(" -s: no parameter required.")
print(" Query the status of the last tuning cycle")
print(" -n: no parameter required.")
print(" only send command, don't query status afterwards")
print("\nEXAMPLES:")
print(" Change the band to 12m: # ./hr50ctl.py -b 12")
print(" Change the keying method to PTT: # ./hr50ctl.py -k ptt")
print(" Change the band to 6m and tune: # ./hr50ctl.py -b 6 -t\n")
sys.exit()
elif opt == "-b":
if arg in bands:
# change the band
send_command("HRBN",bands[arg])
elif opt == "-t":
# let the HR50 know that it should return
send_command("HRTU","1")
elif opt == "-s":
# query result of last tuning cycle
print(send_command("HRTS",""))
elif opt == "-k":
if arg in keying_methods:
# let the HR50 know that it should retunr
send_command("HRMD",keying_methods[arg])
if show_info:
# query all desired info from the HR50
get_info()
# print info to stdout
table = PrettyTable()
table.add_row([
"STA: " + vlcd["STA"],
"PTT: " + vlcd["PTT"],
"BND: " + vlcd["BND"],
"VLT: " + vlcd["VLT"]])
table.add_row([
"PEP: " + vlcd["PEP"],
"AVG: " + vlcd["AVG"],
"SWR: " + vlcd["SWR"],
"TMP: " + vlcd["TMP"]])
table.align = "l"
table.header = False
print("")
print(table)
print("")
if __name__ == "__main__":
main(sys.argv[1:])