added error handling

This commit is contained in:
Michael Clemens 2021-10-07 10:14:02 +01:00
parent 4d86d188a4
commit d35a4de034

View File

@ -1,9 +1,9 @@
#!/usr/bin/python3
import serial
import sys
import time
import getopt
import time
import serial
from prettytable import PrettyTable
serial_port = '/dev/ttyUSB0'
@ -34,23 +34,48 @@ vlcd = {
'TMP': '-'
}
keying_methods = {
"off" : "0",
"ptt" : "1",
"cor" : "2",
"qrp" : "3"
}
def get_serial(port, baud):
try:
ser = serial.Serial(port, baud, timeout=1)
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 = serial.Serial(serial_port, baud, timeout=1)
command = cmd + param + ';'
#print(command)
ser.write(str.encode(command))
ser = get_serial(serial_port, baud)
try:
command = cmd + param + ';'
ser.write(str.encode(command))
except Exception as e:
print("The following error has occured while sending the command {} to the HR50:".format(port, baud))
print(e)
ser.close()
# Executed two commands vie the serial interface,
# parsed the result ad polulates a dict with the
# collected information
def get_info():
ser = serial.Serial(serial_port, baud, timeout=1)
ser.write(b'HRRX;')
time.sleep(0.5)
res = ser.readline().decode("utf-8").rstrip().replace(';', '').split(',')
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]
@ -59,18 +84,12 @@ def get_info():
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]
ser.close()
keying_methods = {
"off" : "0",
"ptt" : "1",
"cor" : "2",
"qrp" : "3"
}
def main(argv):
try:
@ -112,7 +131,7 @@ def main(argv):
get_info()
# print info to stdout
table = PrettyTable(['1', '2', '3', '4'])
table = PrettyTable()
table.add_row([
"STA: " + vlcd["STA"],
"PTT: " + vlcd["PTT"],
@ -125,7 +144,9 @@ def main(argv):
"TMP: " + vlcd["TMP"]])
table.align = "l"
table.header = False
print("")
print(table)
print("")
if __name__ == "__main__":
main(sys.argv[1:])