moved back to python hamlib binding

This commit is contained in:
DJ2LS 2021-09-06 18:50:12 +02:00 committed by GitHub
parent 7fc6872ff5
commit 4a3f10126a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 39 deletions

View file

@ -131,20 +131,23 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
if sys.platform == "linux": if sys.platform == "linux":
# we need to make sure we have execution privileges # we need to make sure we have execution privileges
p = subprocess.Popen("chmod +x ./hamlib/linux/rigctld", shell=True) #try:
#run hamlib rigctld network service # p = subprocess.Popen("chmod +x ./hamlib/linux/rigctld", shell=True)
command = "exec ./hamlib/linux/rigctld -r " + str(deviceport) + \ #except:
" -s "+ str(serialspeed) + \ # print("rigctld not started...")
" -P "+ str(ptt) + \ ##run hamlib rigctld network service
" -m "+ str(deviceid) + \ #command = "exec ./hamlib/linux/rigctld -r " + str(deviceport) + \
" --set-conf=dtr_state=" + dtr_state #" -s "+ str(serialspeed) + \
try: #" -P "+ str(ptt) + \
p = subprocess.Popen(command, shell=True) #" -m "+ str(deviceid) + \
time.sleep(0.3) #" --set-conf=dtr_state=" + dtr_state
except: #try:
print("hamlib not started") # p = subprocess.Popen(command, shell=True)
sys.exit() # time.sleep(0.3)
#except:
# print("hamlib not started")
# sys.exit()
pass
elif sys.platform == "darwin": elif sys.platform == "darwin":
print("platform not yet supported") print("platform not yet supported")
sys.exit() sys.exit()

View file

@ -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('--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('--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('--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=str) PARSER.add_argument('--deviceid', dest="hamlib_device_id", default=3011, help="Socket port", type=int)
PARSER.add_argument('--serialspeed', dest="hamlib_serialspeed", default=9600, help="Serialspeed", 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) PARSER.add_argument('--ptt', dest="hamlib_ptt_type", default='RTS', help="PTT Type", type=str)

View file

@ -21,10 +21,13 @@ import helpers
import static import static
import data_handler import data_handler
#import Hamlib import sys
sys.path.append("hamlib/linux")
import Hamlib
import numpy as np import numpy as np
import rigctld #import rigctld
rigctld = rigctld.Rigctld() #rigctld = rigctld.Rigctld()
MODEM_STATS_NR_MAX = 320 MODEM_STATS_NR_MAX = 320
@ -55,8 +58,16 @@ class RF():
def __init__(self): def __init__(self):
# -------------------------------------------- LOAD FREEDV # -------------------------------------------- LOAD FREEDV
libname = pathlib.Path().absolute() / "codec2/build_linux/src/libcodec2.so" try:
self.c_lib = ctypes.CDLL(libname) # we check at first for libcodec2 in root - necessary if we want to run it inside a pyinstaller binary
libname = pathlib.Path("libcodec2.so.1.0")
self.c_lib = ctypes.CDLL(libname)
except:
# if we cant load libcodec from root, we check for subdirectory
# this is, if we want to run it without beeing build in a dev environment
print("running libcodec from source bild")
libname = pathlib.Path().absolute() / "codec2/build_linux/src/libcodec2.so.1.0"
self.c_lib = ctypes.CDLL(libname)
# --------------------------------------------CREATE PYAUDIO INSTANCE # --------------------------------------------CREATE PYAUDIO INSTANCE
self.p = pyaudio.PyAudio() self.p = pyaudio.PyAudio()
# --------------------------------------------OPEN AUDIO CHANNEL RX # --------------------------------------------OPEN AUDIO CHANNEL RX
@ -96,9 +107,15 @@ class RF():
# --------------------------------------------CONFIGURE HAMLIB # --------------------------------------------CONFIGURE HAMLIB
'''
# try to init hamlib # try to init hamlib
try: try:
print(static.HAMLIB_DEVICE_ID)
print(static.HAMLIB_DEVICE_PORT)
print(static.HAMLIB_SERIAL_SPEED)
print(static.HAMLIB_PTT_TYPE)
Hamlib.rig_set_debug(Hamlib.RIG_DEBUG_NONE) Hamlib.rig_set_debug(Hamlib.RIG_DEBUG_NONE)
self.my_rig = Hamlib.Rig(static.HAMLIB_DEVICE_ID) self.my_rig = Hamlib.Rig(static.HAMLIB_DEVICE_ID)
@ -147,16 +164,17 @@ class RF():
self.my_rig.open() self.my_rig.open()
except: except:
print("Unexpected error:", sys.exc_info()[0])
print("can't open rig") print("can't open rig")
'''
# -------------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------------------
def ptt_and_wait(self, state): def ptt_and_wait(self, state):
if state: if state:
static.PTT_STATE = True 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() #rigctld.ptt_enable()
ptt_togle_timeout = time.time() + 0.1 ptt_togle_timeout = time.time() + 0.1
while time.time() < ptt_togle_timeout: while time.time() < ptt_togle_timeout:
pass pass
@ -168,8 +186,8 @@ class RF():
pass pass
static.PTT_STATE = False 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() #rigctld.ptt_disable()
@ -274,9 +292,7 @@ class RF():
# lets see if this is a good idea.. # lets see if this is a good idea..
static.ARQ_DATA_CHANNEL_LAST_RECEIVED = int(time.time()) # we need to update our timeout timestamp static.ARQ_DATA_CHANNEL_LAST_RECEIVED = int(time.time()) # we need to update our timeout timestamp
static.ARQ_START_OF_BURST = int(time.time()) # we need to update our timeout timestamp static.ARQ_START_OF_BURST = int(time.time()) # we need to update our timeout timestamp
self.my_rig.set_ptt(self.hamlib_ptt_type, 1)
state_before_transmit = static.CHANNEL_STATE state_before_transmit = static.CHANNEL_STATE
static.CHANNEL_STATE = 'SENDING_DATA' static.CHANNEL_STATE = 'SENDING_DATA'
@ -637,11 +653,13 @@ class RF():
static.SNR = 0 static.SNR = 0
def get_radio_stats(self): def get_radio_stats(self):
static.HAMLIB_FREQUENCY = int(self.my_rig.get_freq())
static.HAMLIB_FREQUENCY = rigctld.get_frequency() (hamlib_mode, static.HAMLIB_BANDWITH) = self.my_rig.get_mode()
static.HAMLIB_MODE = rigctld.get_mode()[0] static.HAMLIB_MODE = Hamlib.rig_strrmode(hamlib_mode)
static.HAMLIB_BANDWITH = rigctld.get_mode()[1] #static.HAMLIB_FREQUENCY = rigctld.get_frequency()
#static.HAMLIB_MODE = rigctld.get_mode()[0]
#static.HAMLIB_BANDWITH = rigctld.get_mode()[1]
def calculate_fft(self, data_in): def calculate_fft(self, data_in):
@ -649,11 +667,13 @@ class RF():
audio_data = np.fromstring(data_in, np.int16) audio_data = np.fromstring(data_in, np.int16)
# Fast Fourier Transform, 10*log10(abs) is to scale it to dB # Fast Fourier Transform, 10*log10(abs) is to scale it to dB
# and make sure it's not imaginary # and make sure it's not imaginary
dfft = 10.*np.log10(abs(np.fft.rfft(audio_data))) # we need to try this in case of division by zero
try:
print(dfft.tolist()) dfft = 10.*np.log10(abs(np.fft.rfft(audio_data)))
except:
dfft = [0]
dfftlist = dfft.tolist() dfftlist = dfft.tolist()
static.FFT = dfftlist[:400]
static.FFT = dfftlist[:512]
return dfft return dfft