diff --git a/data_handler.py b/data_handler.py index d53403c6..8d69aea3 100644 --- a/data_handler.py +++ b/data_handler.py @@ -194,10 +194,7 @@ def arq_data_received(data_in): # IF THE FRAME PAYLOAD CRC IS EQUAL TO THE FRAME CRC WHICH IS KNOWN FROM THE HEADER --> SUCCESS if frame_payload_crc == static.FRAME_CRC: logging.log(25, "ARQ | RX | DATA FRAME SUCESSFULLY RECEIVED! :-) ") - - transfer_rates = helpers.calculate_transfer_rate() - print(str(transfer_rates[0]) + " bit/s") - print(str(transfer_rates[1]) + " B/min") + # append received frame to RX_BUFFER static.RX_BUFFER.append(complete_data_frame) @@ -209,11 +206,11 @@ def arq_data_received(data_in): ack_frame[2:3] = static.MYCALLSIGN_CRC8 # TRANSMIT ACK FRAME FOR BURST----------------------------------------------- - time.sleep(0.5) # 0.5 + #time.sleep(0.5) # 0.5 logging.info("ARQ | TX | ARQ DATA FRAME ACK [" + str(static.FRAME_CRC.hex()) + "] [BER." + str(static.BER) + "]") modem.transmit_signalling(ack_frame) - + modem.transmit_signalling(ack_frame) while static.CHANNEL_STATE == 'SENDING_SIGNALLING': time.sleep(0.01) @@ -226,6 +223,21 @@ def arq_data_received(data_in): print("ARQ_FRAME_EOF_RECEIVED " + str(static.ARQ_FRAME_EOF_RECEIVED)) print(static.ARQ_RX_FRAME_BUFFER) logging.error("ARQ | RX | DATA FRAME NOT SUCESSFULLY RECEIVED!") + + # BUILDING ACK FRAME FOR DATA FRAME ----------------------------------------------- + nak_frame = bytearray(14) + nak_frame[:1] = bytes([63]) + nak_frame[1:2] = static.DXCALLSIGN_CRC8 + nak_frame[2:3] = static.MYCALLSIGN_CRC8 + + # TRANSMIT ACK FRAME FOR BURST----------------------------------------------- + #time.sleep(0.5) # 0.5 + logging.info("ARQ | TX | NAK") + + modem.transmit_signalling(nak_frame) + while static.CHANNEL_STATE == 'SENDING_SIGNALLING': + time.sleep(0.01) + helpers.arq_reset_frame_machine() @@ -353,6 +365,8 @@ def arq_transmit(data_out): helpers.arq_reset_ack(False) static.ARQ_RPT_FRAMES = [] + # the order of ACK check is important! speciall the FRAME ACK after RPT needs to be checked really early! + # --------------- BREAK LOOP IF FRAME ACK HAS BEEN RECEIVED EARLIER AS EXPECTED elif static.ARQ_FRAME_ACK_RECEIVED: @@ -367,11 +381,6 @@ def arq_transmit(data_out): pass # no break here so we can continue with the next try of repeating the burst - - - - - # --------------- BREAK LOOP IF ACK HAS BEEN RECEIVED elif static.ARQ_ACK_RECEIVED: transfer_rates = helpers.calculate_transfer_rate() @@ -389,11 +398,6 @@ def arq_transmit(data_out): print("ARQ_RX_ACK_TIMEOUT " + str(static.ARQ_RX_ACK_TIMEOUT)) break - print("static.ARQ_ACK_RECEIVED" + str(static.ARQ_ACK_RECEIVED)) - print("static.ARQ_FRAME_ACK_RECEIVED" + str(static.ARQ_FRAME_ACK_RECEIVED)) - print("static.ARQ_N_SENT_FRAMES" + str(static.ARQ_N_SENT_FRAMES)) - print("static.ARQ_TX_N_TOTAL_ARQ_FRAMES" + str(static.ARQ_TX_N_TOTAL_ARQ_FRAMES)) - # --------------------------------WAITING AREA FOR FRAME ACKs @@ -424,7 +428,7 @@ def arq_transmit(data_out): # -------------------------BREAK TX BUFFER LOOP IF ALL PACKETS HAVE BEEN SENT AND WE GOT A FRAME ACK elif static.ARQ_N_SENT_FRAMES == static.TX_BUFFER_SIZE and static.ARQ_FRAME_ACK_RECEIVED: - logging.log(25, "ARQ | RX | FRAME ACK RECEIVED - DATA TRANSMITTED! :-)") + logging.log(25, "ARQ | RX | FRAME ACK! - DATA TRANSMITTED! :-)") break elif not static.ARQ_FRAME_ACK_RECEIVED and time.time() > frameacktimeout: # == False and static.ARQ_RX_FRAME_TIMEOUT == True: @@ -485,6 +489,8 @@ def burst_rpt_received(data_in): missing = missing_area[i:i + 2] static.ARQ_RPT_FRAMES.insert(0, missing) +def frame_nack_received(): + print("NAK RECEIVED :-/") # ############################################################################################################ # ARQ DATA CHANNEL HANDLER @@ -506,6 +512,10 @@ def open_dc_and_transmit(data_out, mode, n_frames): # wait until data channel is open while not static.ARQ_READY_FOR_DATA: time.sleep(0.01) + + #on a new transmission we reset the timer + static.ARQ_START_OF_TRANSMISSION = int(time.time()) + # transmit data arq_transmit(data_out) @@ -513,7 +523,7 @@ def open_dc_and_transmit(data_out, mode, n_frames): async def arq_open_data_channel(mode): - print(type(mode)) + if mode == 0: static.ARQ_DATA_CHANNEL_MODE = get_best_mode_for_transmission() print(static.ARQ_DATA_CHANNEL_MODE) @@ -575,7 +585,7 @@ def arq_received_data_channel_opener(data_in): modem.transmit_signalling(connection_frame) - modem.transmit_signalling(connection_frame) + #modem.transmit_signalling(connection_frame) while static.CHANNEL_STATE == 'SENDING_SIGNALLING': time.sleep(0.01) diff --git a/modem.py b/modem.py index 23e3a750..9f993c95 100644 --- a/modem.py +++ b/modem.py @@ -233,8 +233,6 @@ class RF(): static.MYCALLSIGN_CRC8 + \ payload_data - # print(arqframe) - buffer = bytearray(static.FREEDV_DATA_PAYLOAD_PER_FRAME) # create TX buffer buffer[:len(arqframe)] = arqframe # set buffersize to length of data which will be send @@ -250,10 +248,12 @@ class RF(): for n in range(0, len(static.ARQ_RPT_FRAMES)): + missing_frame = int.from_bytes(static.ARQ_RPT_FRAMES[n], "big") # ---------------------------BUILD ARQ BURST --------------------------------------------------------------------- frame_type = 10 + missing_frame # static.ARQ_TX_N_FRAMES_PER_BURST + frame_type = bytes([frame_type]) payload_data = bytes(static.TX_BUFFER[static.ARQ_N_SENT_FRAMES + missing_frame - 1]) @@ -272,6 +272,7 @@ class RF(): static.MYCALLSIGN_CRC8 + \ payload_data + buffer = bytearray(static.FREEDV_DATA_PAYLOAD_PER_FRAME) # create TX buffer buffer[:len(arqframe)] = arqframe # set buffersize to length of data which will be send @@ -304,7 +305,7 @@ class RF(): # -------------------------------------------------------------------------------------------------------- def receive(self, mode): - force = True + force = False self.c_lib.freedv_open.restype = ctypes.POINTER(ctypes.c_ubyte) freedv = self.c_lib.freedv_open(mode) @@ -328,7 +329,7 @@ class RF(): bytes_out = bytes_out() # get pointer to bytes_out while static.FREEDV_RECEIVE == True: - time.sleep(0.05) + time.sleep(0.01) # stuck in sync counter stuck_in_sync_counter = 0 @@ -354,16 +355,15 @@ class RF(): time.sleep(0.01) # refresh vars, so the correct parameters of the used mode are set - static.FREEDV_DATA_BYTES_PER_FRAME = bytes_per_frame - static.FREEDV_DATA_PAYLOAD_PER_FRAME = bytes_per_frame - 2 + if mode == static.ARQ_DATA_CHANNEL_MODE: + static.FREEDV_DATA_BYTES_PER_FRAME = bytes_per_frame + static.FREEDV_DATA_PAYLOAD_PER_FRAME = bytes_per_frame - 2 nin = self.c_lib.freedv_nin(freedv) #nin = int(nin*(static.AUDIO_SAMPLE_RATE_RX/static.MODEM_SAMPLE_RATE)) data_in = self.stream_rx.read(nin, exception_on_overflow=False) - - - + static.AUDIO_RMS = audioop.rms(data_in, 2) # self.c_lib.freedv_rawdatarx.argtype = [ctypes.POINTER(ctypes.c_ubyte), data_bytes_out, data_in] # check if really neccessary nbytes = self.c_lib.freedv_rawdatarx(freedv, bytes_out, data_in) # demodulate audio @@ -384,16 +384,17 @@ class RF(): stuck_in_sync_counter = 0 stuck_in_sync_10_counter = 0 - if stuck_in_sync_counter >= 66 and stuck_in_sync_10_counter >= 2: + elif stuck_in_sync_counter >= 66 and stuck_in_sync_10_counter >= 2: logging.critical("MODEM | stuck in sync #2") self.c_lib.freedv_set_sync(freedv, 0) # FORCE UNSYNC stuck_in_sync_counter = 0 stuck_in_sync_10_counter = 0 # ----------------------------------- - #self.calculate_ber(freedv) + self.calculate_snr(freedv) # forward data only if broadcast or we are the receiver # bytes_out[1:2] == callsign check for signalling frames, bytes_out[6:7] == callsign check for data frames, bytes_out[1:2] == b'\x01' --> broadcasts like CQ + # we could also create an own function, which returns True. In this case we could add callsign blacklists and so on if nbytes == bytes_per_frame and bytes(bytes_out[1:2]) == static.MYCALLSIGN_CRC8 or bytes(bytes_out[6:7]) == static.MYCALLSIGN_CRC8 or bytes(bytes_out[1:2]) == b'\x01': self.calculate_ber(freedv) self.calculate_snr(freedv) @@ -409,9 +410,6 @@ class RF(): n_frames_per_burst = int.from_bytes(bytes(bytes_out[1:2]), "big") #self.c_lib.freedv_set_frames_per_burst(freedv_data, n_frames_per_burst); - - - if 50 >= frametype >= 10: if frame != 3 or force == True: @@ -442,6 +440,11 @@ class RF(): logging.debug("REPEAT REQUEST RECEIVED....") data_handler.burst_rpt_received(bytes_out[:-2]) + # FRAME NAK + elif frametype == 63: + logging.debug("FRAME NAK RECEIVED....") + data_handler.frame_nack_received(bytes_out[:-2]) + # CQ FRAME elif frametype == 200: logging.debug("CQ RECEIVED....") @@ -532,4 +535,7 @@ class RF(): self.c_lib.freedv_get_modem_stats(freedv,byref(modem_stats_sync), byref(modem_stats_snr)) modem_stats_snr = modem_stats_snr.value - static.SNR = int(modem_stats_snr) + try: + static.SNR = int(modem_stats_snr) + except: + static.SNR = 0