From 9061eef65a1a50bfa33b1e712ad20b816887c0b9 Mon Sep 17 00:00:00 2001 From: DJ2LS <75909252+DJ2LS@users.noreply.github.com> Date: Thu, 2 Sep 2021 19:41:01 +0200 Subject: [PATCH] moved to hamlib binaries --- tnc/daemon.py | 2 +- tnc/main.py | 23 ++++++++++++-- tnc/modem.py | 29 +++++++++++------ tnc/rigctld.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 12 deletions(-) create mode 100644 tnc/rigctld.py diff --git a/tnc/daemon.py b/tnc/daemon.py index b7a58449..4876fd70 100644 --- a/tnc/daemon.py +++ b/tnc/daemon.py @@ -123,7 +123,7 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler): print("STARTING TNC !!!!!") print(received_json["parameter"][0]) #os.system("python3 main.py --rx 3 --tx 3 --deviceport /dev/ttyUSB0 --deviceid 2028") - print(DEBUG) + if DEBUG: p = subprocess.Popen("exec python3 main.py --rx "+ str(rx_audio) +" --tx "+ str(tx_audio) +" --deviceport "+ str(deviceport) +" --deviceid "+ str(deviceid) + " --serialspeed "+ str(serialspeed) + " --ptt "+ str(ptt), shell=True) else: diff --git a/tnc/main.py b/tnc/main.py index aff5fc65..a8563519 100644 --- a/tnc/main.py +++ b/tnc/main.py @@ -11,7 +11,7 @@ Created on Tue Dec 22 16:58:45 2020 import argparse import threading import static - +import subprocess if __name__ == '__main__': @@ -23,7 +23,7 @@ if __name__ == '__main__': PARSER.add_argument('--tx', dest="audio_output_device", default=0, help="transmitting sound card", type=int) PARSER.add_argument('--port', dest="socket_port", default=3000, help="Socket port", type=int) PARSER.add_argument('--deviceport', dest="hamlib_device_port", default="/dev/ttyUSB0", help="Socket port", type=str) - PARSER.add_argument('--deviceid', dest="hamlib_device_id", default=3011, help="Socket port", type=int) + PARSER.add_argument('--deviceid', dest="hamlib_device_id", default=3011, help="Socket port", type=str) PARSER.add_argument('--serialspeed', dest="hamlib_serialspeed", default=9600, help="Serialspeed", type=str) PARSER.add_argument('--ptt', dest="hamlib_ptt_type", default='RTS', help="PTT Type", type=str) @@ -54,3 +54,22 @@ if __name__ == '__main__': WATCHDOG_SERVER_THREAD = threading.Thread(target=helpers.watchdog, name="watchdog") WATCHDOG_SERVER_THREAD.start() + + + # Start RIGCTLD + + if static.HAMLIB_PTT_TYPE == "RTS": + dtr_state = "OFF" + else: + dtr_state = "NONE" + + command = "exec ./hamlib/linux/rigctld -r " + str(static.HAMLIB_DEVICE_PORT) + \ + " -s "+ str(static.HAMLIB_SERIAL_SPEED) + \ + " -P "+ str(static.HAMLIB_PTT_TYPE) + \ + " -m "+ str(static.HAMLIB_DEVICE_ID) + \ + " --set-conf=dtr_state=" + dtr_state + p = subprocess.Popen(command, shell=True) + + + + diff --git a/tnc/modem.py b/tnc/modem.py index b2ea0dee..34f26192 100644 --- a/tnc/modem.py +++ b/tnc/modem.py @@ -21,9 +21,11 @@ import helpers import static import data_handler -import Hamlib +#import Hamlib +import rigctld +rigctld = rigctld.Rigctld() # test @@ -103,7 +105,7 @@ class RF(): # --------------------------------------------CONFIGURE HAMLIB - + ''' # try to init hamlib try: Hamlib.rig_set_debug(Hamlib.RIG_DEBUG_NONE) @@ -156,13 +158,14 @@ class RF(): except: print("can't open rig") - + ''' # -------------------------------------------------------------------------------------------------------- def ptt_and_wait(self, state): if state: static.PTT_STATE = True - self.my_rig.set_ptt(self.hamlib_ptt_type, 1) + #self.my_rig.set_ptt(self.hamlib_ptt_type, 1) + rigctld.ptt_enable() ptt_togle_timeout = time.time() + 0.1 while time.time() < ptt_togle_timeout: pass @@ -174,8 +177,8 @@ class RF(): pass static.PTT_STATE = False - self.my_rig.set_ptt(self.hamlib_ptt_type, 0) - + #self.my_rig.set_ptt(self.hamlib_ptt_type, 0) + rigctld.ptt_disable() @@ -643,9 +646,17 @@ class RF(): static.SNR = 0 def get_radio_stats(self): - static.HAMLIB_FREQUENCY = float(self.my_rig.get_freq()/1000) - (hamlib_mode, static.HAMLIB_BANDWITH) = self.my_rig.get_mode() - static.HAMLIB_MODE = Hamlib.rig_strrmode(hamlib_mode) + #static.HAMLIB_FREQUENCY = float(self.my_rig.get_freq()/1000) + #(hamlib_mode, static.HAMLIB_BANDWITH) = self.my_rig.get_mode() + #static.HAMLIB_MODE = Hamlib.rig_strrmode(hamlib_mode) + + static.HAMLIB_FREQUENCY = rigctld.get_frequency() + static.HAMLIB_MODE = rigctld.get_mode()[0] + static.HAMLIB_BANDWITH = rigctld.get_mode()[1] + print(static.HAMLIB_MODE) + print(static.HAMLIB_FREQUENCY) + print(static.HAMLIB_BANDWITH) + def calculate_fft(self, data_in): diff --git a/tnc/rigctld.py b/tnc/rigctld.py new file mode 100644 index 00000000..17eeeaba --- /dev/null +++ b/tnc/rigctld.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +import socket +import logging + +# rigctl - https://github.com/darksidelemm/rotctld-web-gui/blob/master/rotatorgui.py#L35 +# https://github.com/xssfox/freedv-tnc/blob/master/freedvtnc/rigctl.py + + +class Rigctld(): + """ rotctld (hamlib) communication class """ + # Note: This is a massive hack. + + def __init__(self, hostname="localhost", port=4532, poll_rate=5, timeout=5): + """ Open a connection to rotctld, and test it for validity """ + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.settimeout(timeout) + + self.hostname = hostname + self.port = port + self.connect() + logging.debug(f"Rigctl intialized") + + def get_model(self): + """ Get the rotator model from rotctld """ + model = self.send_command(b'_') + return model + + def connect(self): + """ Connect to rotctld instance """ + self.sock.connect((self.hostname,self.port)) + model = self.get_model() + if model == None: + # Timeout! + self.close() + raise Exception("Timeout!") + else: + return model + + + def close(self): + self.sock.close() + + + def send_command(self, command): + """ Send a command to the connected rotctld instance, + and return the return value. + """ + self.sock.sendall(command+b'\n') + try: + return self.sock.recv(1024) + except: + return None + + def ptt_enable(self): + logging.debug(f"PTT enabled") + self.send_command(b"T 1") + + def ptt_disable(self): + logging.debug(f"PTT disabled") + self.send_command(b"T 0") + + def get_frequency(self): + data = self.send_command(b"f") + if data is not None: + data = data.split(b'\n') + try: + freq = int(data[0]) + except: + freq = 0 + print(data) + return freq + + def get_mode(self): + data = self.send_command(b"m") + if data is not None: + data = data.split(b'\n') + try: + mode = str(data[0], "utf-8") + bandwith = int(data[1]) + except: + print(data) + mode = '0' + bandwith = 0 + return [mode, bandwith] +