hr50ctl/hr50ctl.py

167 lines
5.0 KiB
Python
Raw Permalink Normal View History

2021-10-05 22:10:50 +00:00
#!/usr/bin/python3
import sys
import getopt
2021-10-07 09:14:02 +00:00
import time
import serial
2021-10-05 22:10:50 +00:00
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'
2021-10-06 12:20:28 +00:00
}
2021-10-05 22:10:50 +00:00
vlcd = {
'STA': '-',
'PTT': '-',
'BND': '-',
'VLT': '-',
'PEP': '-',
'AVG': '-',
'SWR': '-',
'TMP': '-'
2021-10-06 12:20:28 +00:00
}
2021-10-05 22:10:50 +00:00
2021-10-07 09:14:02 +00:00
keying_methods = {
"off" : "0",
"ptt" : "1",
"cor" : "2",
"qrp" : "3"
}
def get_serial(port, baud):
try:
2021-11-07 16:48:54 +00:00
ser = serial.Serial(port, baud, timeout=2)
2021-10-07 09:14:02 +00:00
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)
2021-10-05 22:10:50 +00:00
# sends a command and an optional parameter to the
# Hardrock-50 via the serial interface
def send_command(cmd, param):
2021-10-07 09:14:02 +00:00
ser = get_serial(serial_port, baud)
2021-11-07 16:48:54 +00:00
res = None
2021-10-07 09:14:02 +00:00
try:
command = cmd + param + ';'
ser.write(str.encode(command))
2021-11-07 16:48:54 +00:00
res = ser.readline().decode("utf-8").rstrip()
2021-10-07 09:14:02 +00:00
except Exception as e:
print("The following error has occured while sending the command {} to the HR50:".format(port, baud))
print(e)
2021-10-05 22:10:50 +00:00
ser.close()
2021-11-07 16:48:54 +00:00
return res
2021-10-05 22:10:50 +00:00
# Executed two commands vie the serial interface,
# parsed the result ad polulates a dict with the
# collected information
def get_info():
2021-10-07 09:14:02 +00:00
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
2021-10-05 22:10:50 +00:00
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()
2021-10-07 09:14:02 +00:00
ser.close()
2021-10-05 22:10:50 +00:00
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):
2021-11-07 16:48:54 +00:00
show_info = True
2021-10-05 22:10:50 +00:00
try:
2021-11-07 16:48:54 +00:00
opts, args = getopt.getopt(argv,"hb:sntk:")
2021-10-05 22:10:50 +00:00
except getopt.GetoptError:
print("Error :(")
print("Please consult help via ./hr50ctl.py -h")
2021-10-05 22:10:50 +00:00
sys.exit(2)
for opt, arg in opts:
2021-11-07 16:48:54 +00:00
if opt == '-n':
show_info = False
2021-10-05 22:10:50 +00:00
if opt == '-h':
2021-11-07 16:48:54 +00:00
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")
2021-11-07 16:48:54 +00:00
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")
2021-10-06 12:20:28 +00:00
print(" Change the band to 6m and tune: # ./hr50ctl.py -b 6 -t\n")
2021-10-05 22:10:50 +00:00
sys.exit()
elif opt == "-b":
2021-10-05 22:10:50 +00:00
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")
2021-11-07 16:48:54 +00:00
elif opt == "-s":
# query result of last tuning cycle
print(send_command("HRTS",""))
elif opt == "-k":
if arg in keying_methods:
2021-10-05 22:10:50 +00:00
# let the HR50 know that it should retunr
send_command("HRMD",keying_methods[arg])
2021-10-05 22:10:50 +00:00
2021-11-07 16:48:54 +00:00
if show_info:
# query all desired info from the HR50
get_info()
2021-10-05 22:10:50 +00:00
2021-11-07 16:48:54 +00:00
# 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("")
2021-10-05 22:10:50 +00:00
if __name__ == "__main__":
main(sys.argv[1:])