From e4d36e5f214b82a5918c9f967db3027ff5984f03 Mon Sep 17 00:00:00 2001 From: dj2ls Date: Tue, 28 Dec 2021 17:05:48 +0100 Subject: [PATCH] first rigctl fallback thanks Franco --- tnc/daemon.py | 21 ++++++++-- tnc/main.py | 7 ++-- tnc/modem.py | 9 ++++- tnc/rig.py | 49 +++++++++++++++++------ tnc/rigctl.py | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ tnc/sock.py | 3 +- tnc/static.py | 3 +- 7 files changed, 177 insertions(+), 21 deletions(-) create mode 100644 tnc/rigctl.py diff --git a/tnc/daemon.py b/tnc/daemon.py index 76d28f7f..51cad3c4 100755 --- a/tnc/daemon.py +++ b/tnc/daemon.py @@ -20,9 +20,9 @@ import serial.tools.list_ports import static import crcengine import re -import rig import logging, structlog, log_handler + log_handler.setup_logging("daemon") # get python version, which is needed later for determining installation path python_version = str(sys.version_info[0]) + "." + str(sys.version_info[1]) @@ -197,7 +197,14 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler): options.append(stop_bits) options.append('--handshake') options.append(handshake) + + if HAMLIB_USE_RIGCTL: + options.append('--rigctl') + + + + # try running tnc from binary, else run from source # this helps running the tnc in a developer environment @@ -351,11 +358,19 @@ if __name__ == '__main__': # --------------------------------------------GET PARAMETER INPUTS PARSER = argparse.ArgumentParser(description='Simons TEST TNC') PARSER.add_argument('--port', dest="socket_port",default=3001, help="Socket port", type=int) - + PARSER.add_argument('--rigctl', dest="hamlib_use_rigctl",action="store_true", default=False, help="force using of rigctl") + ARGS = PARSER.parse_args() PORT = ARGS.socket_port - + HAMLIB_USE_RIGCTL = ARGS.hamlib_use_rigctl + if HAMLIB_USE_RIGCTL: + structlog.get_logger("structlog").warning("using rigctl....") + import rigctl as rig + else: + structlog.get_logger("structlog").warning("using rig.......") + import rig # --------------------------------------------START CMD SERVER DAEMON_THREAD = threading.Thread(target=start_daemon, name="daemon") DAEMON_THREAD.start() + diff --git a/tnc/main.py b/tnc/main.py index a8d6fe4b..97691137 100644 --- a/tnc/main.py +++ b/tnc/main.py @@ -36,9 +36,8 @@ if __name__ == '__main__': PARSER.add_argument('--data_bits', dest="hamlib_data_bits", default="8", help="Hamlib data bits", type=str) PARSER.add_argument('--stop_bits', dest="hamlib_stop_bits", default="1", help="Hamlib stop bits", type=str) PARSER.add_argument('--handshake', dest="hamlib_handshake", default="None", help="Hamlib handshake", type=str) - + PARSER.add_argument('--rigctl', dest="hamlib_use_rigctl", action="store_true", default=False, help="force using of rigctl") - ARGS = PARSER.parse_args() @@ -52,7 +51,9 @@ if __name__ == '__main__': static.HAMLIB_SERIAL_SPEED = ARGS.hamlib_serialspeed static.HAMLIB_DATA_BITS = ARGS.hamlib_data_bits static.HAMLIB_STOP_BITS = ARGS.hamlib_stop_bits - static.HAMLIB_HANDSHAKE = ARGS.hamlib_handshake + static.HAMLIB_HANDSHAKE = ARGS.hamlib_handshake + static.HAMLIB_USE_RIGCTL = ARGS.hamlib_use_rigctl + print(ARGS.hamlib_use_rigctl) # we need to wait until we got all parameters from argparse first before we can load the other modules import sock diff --git a/tnc/modem.py b/tnc/modem.py index 20a0ac73..5650e4ec 100644 --- a/tnc/modem.py +++ b/tnc/modem.py @@ -21,7 +21,14 @@ import data_handler import re import queue import codec2 -import rig + +print(static.HAMLIB_USE_RIGCTL) +if static.HAMLIB_USE_RIGCTL: + structlog.get_logger("structlog").warning("using rigctl....") + import rigctl as rig +else: + structlog.get_logger("structlog").warning("using rig.......") + import rig # option for testing miniaudio instead of audioop for sample rate conversion #import miniaudio diff --git a/tnc/rig.py b/tnc/rig.py index a75fae1d..e1bdb20f 100644 --- a/tnc/rig.py +++ b/tnc/rig.py @@ -4,6 +4,7 @@ import sys import re import logging, structlog, log_handler import atexit +import subprocess # try importing hamlib @@ -29,9 +30,23 @@ try: else: structlog.get_logger("structlog").warning("[TNC] Hamlib outdated", found=hamlib_version, recommend=min_hamlib_version) except Exception as e: - structlog.get_logger("structlog").critical("[TNC] Hamlib not found", error=e) - - + structlog.get_logger("structlog").warning("[TNC] Python Hamlib binding not found", error=e) + try: + structlog.get_logger("structlog").warning("[TNC] Trying to open rigctl", error=e) + rigctl = subprocess.Popen("rigctl -V",shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True) + hamlib_version = rigctl.stdout.readline() + hamlib_version = hamlib_version.split(' ') + print(hamlib_version[0]) + print(hamlib_version[1]) + print(hamlib_version[2]) + if hamlib_version[1] == 'Hamlib': + rigctl = True + print(hamlib_version) + else: + rigctl = False + raise Exception + except Exception as e: + structlog.get_logger("structlog").critical("[TNC] HAMLIB NOT INSTALLED", error=e) class radio: def __init__(self): @@ -50,13 +65,13 @@ class radio: def open_rig(self, devicename, deviceport, hamlib_ptt_type, serialspeed, pttport, data_bits, stop_bits, handshake): self.devicename = devicename - self.deviceport = deviceport + self.deviceport = str(deviceport) self.serialspeed = str(serialspeed) # we need to ensure this is a str, otherwise set_conf functions are crashing - self.hamlib_ptt_type = hamlib_ptt_type - self.pttport = pttport - self.data_bits = data_bits - self.stop_bits = stop_bits - self.handshake = handshake + self.hamlib_ptt_type = str(hamlib_ptt_type) + self.pttport = str(pttport) + self.data_bits = str(data_bits) + self.stop_bits = str(stop_bits) + self.handshake = str(handshake) # try to init hamlib @@ -66,6 +81,7 @@ class radio: # get devicenumber by looking for deviceobject in Hamlib module try: self.devicenumber = int(getattr(Hamlib, self.devicename)) + print(self.devicenumber) except: structlog.get_logger("structlog").error("[DMN] Hamlib: rig not supported...") self.devicenumber = 0 @@ -80,6 +96,16 @@ class radio: self.my_rig.set_conf("data_bits", self.data_bits) self.my_rig.set_conf("ptt_pathname", self.pttport) + + print(self.my_rig.get_conf("rig_pathname")) + print(self.my_rig.get_conf("retry")) + print(self.my_rig.get_conf("serial_speed")) + print(self.my_rig.get_conf("serial_handshake")) + print(self.my_rig.get_conf("stop_bits")) + print(self.my_rig.get_conf("data_bits")) + print(self.my_rig.get_conf("ptt_pathname")) + + if self.hamlib_ptt_type == 'RIG': self.hamlib_ptt_type = Hamlib.RIG_PTT_RIG @@ -159,8 +185,9 @@ class radio: (hamlib_mode, bandwith) = self.my_rig.get_mode() return bandwith - def set_mode(self, mode): - return 0 + # not needed yet beacuse of some possible problems + #def set_mode(self, mode): + # return 0 def get_ptt(self): return self.my_rig.get_ptt() diff --git a/tnc/rigctl.py b/tnc/rigctl.py new file mode 100644 index 00000000..91c05158 --- /dev/null +++ b/tnc/rigctl.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 +# +# versione mia di rig.py per gestire Ft897D tramite rigctl e senza +# fare alcun riferimento alla configurazione +# +# e' una pezza clamorosa ma serve per poter provare on-air il modem +# +import subprocess +# +import sys +import re +import logging, structlog, log_handler +import atexit +# for rig_model -> rig_number only + + +class radio: + def __init__(self): + + self.devicename = '' + self.devicenumber = '' + self.deviceport = '' + self.serialspeed = '' + self.hamlib_ptt_type = '' + self.my_rig = '' + self.pttport = '' + self.data_bits = '' + self.stop_bits = '' + self.handshake = '' + + def open_rig(self, devicename, deviceport, hamlib_ptt_type, serialspeed, pttport, data_bits, stop_bits, handshake): + + self.devicename = devicename + self.deviceport = deviceport + self.serialspeed = str(serialspeed) # we need to ensure this is a str, otherwise set_conf functions are crashing + self.hamlib_ptt_type = hamlib_ptt_type + self.pttport = pttport + self.data_bits = data_bits + self.stop_bits = stop_bits + self.handshake = handshake + + # get devicenumber by looking for deviceobject in Hamlib module + try: + import Hamlib + self.devicenumber = int(getattr(Hamlib, self.devicename)) + except: + if int(self.devicename): + self.devicenumber = int(self.devicename) + else: + self.devicenumber = 6 + structlog.get_logger("structlog").warning("[TNC] RADIO NOT FOUND USING DUMMY!", error=e) + + + print(self.devicenumber, self.deviceport, self.serialspeed) + self.cmd = 'rigctl -m %d -r %s -s %d ' % (int(self.devicenumber), self.deviceport, int(self.serialspeed)) + + # eseguo semplicemente rigctl con il solo comando T 1 o T 0 per + # il set e t per il get + + # set ptt to false if ptt is stuck for some reason + self.set_ptt(False) + return True + + def get_frequency(self): + cmd = self.cmd + ' f' + sw_proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True) + command_channel = sw_proc.stdout + freq = command_channel.readline() + return int(freq) + + def get_mode(self): + #(hamlib_mode, bandwith) = self.my_rig.get_mode() + #return Hamlib.rig_strrmode(hamlib_mode) + return 'PKTUSB' + + def get_bandwith(self): + #(hamlib_mode, bandwith) = self.my_rig.get_mode() + bandwith = 2700 + return bandwith + + def set_mode(self, mode): + # non usata + return 0 + + def get_ptt(self): + cmd = self.cmd + ' t' + sw_proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True) + command_channel = sw_proc.stdout + status = command_channel.readline() + return status + + def set_ptt(self, state): + cmd = self.cmd + ' T ' + print('set_ptt', state) + if state: + cmd = cmd + '1' + else: + cmd = cmd + '0' + print('set_ptt', cmd) + + sw_proc = subprocess.Popen(cmd, shell=True, text=True) + return state + + def close_rig(self): + #self.my_rig.close() + return diff --git a/tnc/sock.py b/tnc/sock.py index 37394fbd..c9bafa9d 100644 --- a/tnc/sock.py +++ b/tnc/sock.py @@ -337,8 +337,7 @@ def start_cmd_socket(): structlog.get_logger("structlog").info("[TNC] Starting TCP/IP socket", port=static.PORT) # https://stackoverflow.com/a/16641793 socketserver.TCPServer.allow_reuse_address = True - cmdserver = ThreadedTCPServer( - (static.HOST, static.PORT), ThreadedTCPRequestHandler) + cmdserver = ThreadedTCPServer((static.HOST, static.PORT), ThreadedTCPRequestHandler) server_thread = threading.Thread(target=cmdserver.serve_forever) server_thread.daemon = True server_thread.start() diff --git a/tnc/static.py b/tnc/static.py index 10edeb76..7d2745b9 100644 --- a/tnc/static.py +++ b/tnc/static.py @@ -43,7 +43,8 @@ HAMLIB_SERIAL_SPEED = '9600' HAMLIB_PTT_PORT = '/dev/ttyUSB0' HAMLIB_STOP_BITS = '1' HAMLIB_DATA_BITS = '8' -HANLIB_HANDSHAKE = 'None' +HAMLIB_HANDSHAKE = 'None' +HAMLIB_USE_RIGCTL = False HAMLIB_FREQUENCY = 0 HAMLIB_MODE = ''