From 2c120065f5c14144486973f84ef75449a53f1e38 Mon Sep 17 00:00:00 2001 From: DJ2LS <75909252+DJ2LS@users.noreply.github.com> Date: Tue, 9 Mar 2021 20:56:17 +0100 Subject: [PATCH] first async / non block transmission really ugly at this point. we need a lot of code cleanup. --- data_handler.py | 4 ++-- modem.py | 47 +++++++++++++++++++++++++++++++++++++++-------- sock.py | 39 ++++++++++++++++++++++++++++----------- 3 files changed, 69 insertions(+), 21 deletions(-) diff --git a/data_handler.py b/data_handler.py index 512d320d..ab3b0a2a 100644 --- a/data_handler.py +++ b/data_handler.py @@ -661,7 +661,7 @@ def arq_disconnect_received(data_in): # PING HANDLER ############################################################################################################# -def transmit_ping(callsign): +async def transmit_ping(callsign): static.DXCALLSIGN = bytes(callsign, 'utf-8') static.DXCALLSIGN_CRC8 = helpers.get_crc_8(static.DXCALLSIGN) logging.info("PING ["+ str(static.MYCALLSIGN, 'utf-8') + "] >>> [" + str(static.DXCALLSIGN, 'utf-8') + "] [BER."+str(static.BER)+"]") @@ -721,7 +721,7 @@ async def transmit_cq(): for i in range(0,3): modem.transmit_signalling(cq_frame) - + while static.ARQ_STATE == 'SENDING_SIGNALLING': time.sleep(0.1) diff --git a/modem.py b/modem.py index 03a114f4..8059de94 100644 --- a/modem.py +++ b/modem.py @@ -53,7 +53,10 @@ class RF(): frames_per_buffer=static.AUDIO_FRAMES_PER_BUFFER, #n_nom_modem_samples output=True, output_device_index=static.AUDIO_OUTPUT_DEVICE, #static.AUDIO_OUTPUT_DEVICE - ) + ) + + + self.streambuffer = bytes(2) #--------------------------------------------START DECODER THREAD FREEDV_DECODER_THREAD_10 = threading.Thread(target=self.receive, args=[10], name="FREEDV_DECODER_THREAD_10") FREEDV_DECODER_THREAD_10.start() @@ -67,7 +70,10 @@ class RF(): FREEDV_DECODER_THREAD_14 = threading.Thread(target=self.receive, args=[static.FREEDV_SIGNALLING_MODE], name="FREEDV_DECODER_THREAD_14") FREEDV_DECODER_THREAD_14.start() + FREEDV_PLAYBACK_THREAD = threading.Thread(target=self.play_audio, name="FREEDV_DECODER_THREAD_14") + FREEDV_PLAYBACK_THREAD.start() + #--------------------------------------------CONFIGURE HAMLIB Hamlib.rig_set_debug(Hamlib.RIG_DEBUG_NONE) @@ -78,6 +84,7 @@ class RF(): self.my_rig.open () + if static.HAMLIB_PTT_TYPE == 'RIG_PTT_RIG': self.hamlib_ptt_type = Hamlib.RIG_PTT_RIG elif static.HAMLIB_PTT_TYPE == 'RIG_PTT_SERIAL_DTR': @@ -94,9 +101,29 @@ class RF(): self.hamlib_ptt_type = Hamlib.RIG_PTT_NONE self.my_rig.set_ptt(self.hamlib_ptt_type,0) + + def play_audio(self): + + while True: + time.sleep(0.11) + state_before_transmit = static.CHANNEL_STATE + #print(len(self.streambuffer)) + #print(bytes(self.streambuffer)) + #print(static.CHANNEL_STATE) + + #print(len(self.streambuffer)) + while len(self.streambuffer) > 0: + static.CHANNEL_STATE = 'SENDING_SIGNALLING' + if len(self.streambuffer) > 0: + print("test") + print(len(self.streambuffer)) + self.stream_tx.write(self.streambuffer) + time.sleep(0.1) + self.streambuffer = bytes(0) + static.CHANNEL_STATE = state_before_transmit #-------------------------------------------------------------------------------------------------------- - def transmit_signalling(self,ack_buffer): + def transmit_signalling(self,data_out): state_before_transmit = static.CHANNEL_STATE static.CHANNEL_STATE = 'SENDING_SIGNALLING' @@ -117,7 +144,7 @@ class RF(): mod_out_preamble = mod_out_preamble() buffer = bytearray(payload_per_frame) # use this if CRC16 checksum is required ( DATA1-3) - buffer[:len(ack_buffer)] = ack_buffer # set buffersize to length of data which will be send + buffer[:len(data_out)] = data_out # set buffersize to length of data which will be send crc = ctypes.c_ushort(self.c_lib.freedv_gen_crc16(bytes(buffer), payload_per_frame)) # generate CRC16 crc = crc.value.to_bytes(2, byteorder='big') # convert crc to 2 byte hex string @@ -126,20 +153,25 @@ class RF(): self.c_lib.freedv_rawdatapreambletx(freedv, mod_out_preamble) self.c_lib.freedv_rawdatatx(freedv,mod_out,data) # modulate DATA and safe it into mod_out pointer + txbuffer = bytearray() txbuffer += bytes(mod_out_preamble) txbuffer += bytes(mod_out) # -------------- transmit audio - logging.debug("SENDING SIGNALLING FRAME " + str(ack_buffer)) - self.stream_tx.write(bytes(txbuffer)) - + logging.debug("SENDING SIGNALLING FRAME " + str(data_out)) + #asyncio.run(self.stream_tx.write(bytes(txbuffer))) + #self.stream_tx.write(bytes(txbuffer)) + self.streambuffer = bytes() + self.streambuffer = bytes(txbuffer) + #print(self.streambuffer) + #print(len(self.streambuffer)) self.my_rig.set_ptt(self.hamlib_ptt_type,0) static.PTT_STATE = False static.CHANNEL_STATE = state_before_transmit self.c_lib.freedv_close(freedv) - time.sleep(0.5) + #time.sleep(0.5) #-------------------------------------------------------------------------------------------------------- # GET ARQ BURST FRAME VOM BUFFER AND MODULATE IT async def transmit_arq_burst(self): @@ -316,7 +348,6 @@ class RF(): stuck_in_sync_10_counter += 1 #self.c_lib.freedv_set_sync(freedv, 0) logging.warning("MODEM | SYNC 10 TRIGGER | M:" + str(mode) + " | " + str(static.CHANNEL_STATE)) - if stuck_in_sync_counter == 33 and self.c_lib.freedv_get_rx_status(freedv) == 10: logging.critical("MODEM | stuck in sync #1") diff --git a/sock.py b/sock.py index a93ad92f..be095537 100644 --- a/sock.py +++ b/sock.py @@ -20,6 +20,10 @@ import helpers +import asyncbg + + + class CMDTCPRequestHandler(socketserver.BaseRequestHandler): def handle(self): @@ -45,6 +49,7 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler): # CQ CQ CQ ----------------------------------------------------- if data == 'CQCQCQ': asyncio.run(data_handler.transmit_cq()) + #asyncio.run(asyncbg.call(data_handler.transmit_cq)) #######self.request.sendall(b'CALLING CQ') @@ -53,8 +58,18 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler): #send ping frame and wait for ACK pingcommand = data.split('PING:') dxcallsign = pingcommand[1] - data_handler.transmit_ping(dxcallsign) - + #data_handler.transmit_ping(dxcallsign) + ##loop = asyncio.get_event_loop() + ##loop.create_task(data_handler.transmit_ping(dxcallsign)) + ##loop.run() + + #asyncio.new_event_loop() + #asyncio.ensure_future(data_handler.transmit_ping(dxcallsign)) + + asyncio.run(data_handler.transmit_ping(dxcallsign)) + + #asyncio.create_task(data_handler.transmit_ping(dxcallsign)) + #asyncio.run(data_handler.transmit_ping(dxcallsign)) # ARQ CONNECT TO CALLSIGN ---------------------------------------- if data.startswith('ARQ:CONNECT:'): @@ -105,9 +120,11 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler): static.TNC_STATE = 'BUSY' arqdata = data.split('ARQ:') data_out = bytes(arqdata[1], 'utf-8') + asyncio.run(data_handler.arq_transmit(data_out)) - + ###asyncio.run(asyncbg.call(data_handler.arq_transmit, data_out)) + print("die funktion läuft weiter...") #data_handler.arq_transmit(data_out) #TRANSMIT_ARQ = threading.Thread(target=data_handler.transmit, args=[data_out], name="TRANSMIT_ARQ") #TRANSMIT_ARQ.start() @@ -146,14 +163,14 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler): if data == 'GET:DATA_STATE': output = { "RX_BUFFER_LENGTH": str(len(static.RX_BUFFER)), - "TX_N_MAX_RETRIES": static.TX_N_MAX_RETRIES, - "ARQ_TX_N_FRAMES_PER_BURST": static.ARQ_TX_N_FRAMES_PER_BURST, - "ARQ_TX_N_BURSTS": static.ARQ_TX_N_BURSTS, - "ARQ_TX_N_CURRENT_ARQ_FRAME": static.ARQ_TX_N_CURRENT_ARQ_FRAME, - "ARQ_TX_N_TOTAL_ARQ_FRAMES": static.ARQ_TX_N_TOTAL_ARQ_FRAMES, - "ARQ_RX_FRAME_N_BURSTS": static.ARQ_RX_FRAME_N_BURSTS, - "ARQ_RX_N_CURRENT_ARQ_FRAME": static.ARQ_RX_N_CURRENT_ARQ_FRAME, - "ARQ_N_ARQ_FRAMES_PER_DATA_FRAME": static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME + "TX_N_MAX_RETRIES": str(static.TX_N_MAX_RETRIES), + "ARQ_TX_N_FRAMES_PER_BURST": str(static.ARQ_TX_N_FRAMES_PER_BURST), + "ARQ_TX_N_BURSTS": str(static.ARQ_TX_N_BURSTS), + "ARQ_TX_N_CURRENT_ARQ_FRAME": str(static.ARQ_TX_N_CURRENT_ARQ_FRAME), + "ARQ_TX_N_TOTAL_ARQ_FRAMES": str(static.ARQ_TX_N_TOTAL_ARQ_FRAMES), + "ARQ_RX_FRAME_N_BURSTS": str(static.ARQ_RX_FRAME_N_BURSTS), + "ARQ_RX_N_CURRENT_ARQ_FRAME": str(static.ARQ_RX_N_CURRENT_ARQ_FRAME), + "ARQ_N_ARQ_FRAMES_PER_DATA_FRAME": str(static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME ) } jsondata = json.dumps(output) self.request.sendall(bytes(jsondata, encoding))