mirror of
https://codeberg.org/mclemens/hr50-api.git
synced 2025-01-03 01:16:42 -05:00
first commit
This commit is contained in:
parent
436a03507e
commit
9b004c8043
0
__init__.py
Normal file
0
__init__.py
Normal file
2
bootstrap.sh
Executable file
2
bootstrap.sh
Executable file
@ -0,0 +1,2 @@
|
||||
export FLASK_APP=hr50api.py
|
||||
flask run --host=0.0.0.0
|
109
hr50api.py
Normal file
109
hr50api.py
Normal file
@ -0,0 +1,109 @@
|
||||
from flask import Flask, jsonify, request
|
||||
import sys
|
||||
import getopt
|
||||
import time
|
||||
import serial
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
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 connecti on 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 t he 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
|
||||
if res and len(res) > 3:
|
||||
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]
|
||||
else:
|
||||
vlcd['PTT'] = "ERR"
|
||||
|
||||
|
||||
@app.route('/status')
|
||||
def get_status():
|
||||
get_info()
|
||||
return jsonify(vlcd)
|
||||
|
||||
|
||||
@app.route('/tune', methods=['POST'])
|
||||
def set_tune():
|
||||
return '', 204
|
||||
|
Loading…
Reference in New Issue
Block a user