Hey all,
trying to send a text file using B200 and receive it using B210 or PLUTO
over OFDM Tx/Rx blocks.
the setup works fine, but I need this setup to be repeated for 100
different text files. So, I modified the python code to read, send and
transmit the files one by one. the code works good for the first two
(sometimes three) files and after that the files written at receiver side
are all of size zero byte. I used another SDR on a nearby machine and could
detect that there is transmission going there.
attached is :
- the flow graph to illustrate the idea
-python code files (main one is ofdmrx_processor)

I wonder if it is related to buffering issues, you kind help is appreciated.

Moath Sulaiman
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#
# SPDX-License-Identifier: GPL-3.0
#
# GNU Radio Python Flow Graph
# Title: FullDupx_OFDM_forCode
# Author: Moath Sulaiman
# Description: send and receive a file using OFDM over USRPs
# GNU Radio version: v3.11.0.0git-393-ga07ced54

from packaging.version import Version as StrictVersion
from PyQt5 import Qt
# from gnuradio import qtgui
from gnuradio import digital

from gnuradio import blocks
import pmt,os
from gnuradio import gr
from gnuradio.filter import firdes
from gnuradio.fft import window
import sys
import signal
from PyQt5 import Qt
from argparse import ArgumentParser
from gnuradio.eng_arg import eng_float, intx
from gnuradio import eng_notation
from gnuradio import uhd
import time
import numpy as np
from gnuradio import iio



class ofdm_rxblock(gr.top_block):

    def __init__(self,fname):

        gr.top_block.__init__(self, "ofdm_block_code", catch_exceptions=True)

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 1500000
        self.rf_rate = rf_rate = 1500000
        self.rand = rand = np.random.randint(0, 2, 100)
        self.packet_len = packet_len = 48
        self.len_tag_key = len_tag_key = "packet_len"
        self.fft_len = fft_len = 64
        self.cfreq = cfreq = int(2.8e9)
        self.BW = BW = 2e6

        GR_FIXED_BUFFER_SIZE = 128e6



        ##################################################
        # Blocks
        ##################################################



############# Rx Part 30D3F3D ######################################

#########  B200 series receiver ######################
        # self.uhd_usrp_source_0 = uhd.usrp_source(
        #     ",".join(('serial=30D3F3D', '')),
        #     uhd.stream_args(
        #         cpu_format="fc32",
        #         args='',
        #         channels=list(range(0,1)),
        #     ),
        # )
        # self.uhd_usrp_source_0.set_samp_rate(samp_rate)
        # self.uhd_usrp_source_0.set_time_unknown_pps(uhd.time_spec(0))
        # self.uhd_usrp_source_0.set_center_freq(cfreq, 0)
        # self.uhd_usrp_source_0.set_antenna("TX/RX", 0)
        # self.uhd_usrp_source_0.set_bandwidth(BW, 0)
        # self.uhd_usrp_source_0.set_gain(75, 0)        

################ PLUTO Receiver ##################


        self.iio_pluto_source_0 = iio.fmcomms2_source_fc32('ip:192.168.2.1' if 'ip:192.168.2.1' else iio.get_pluto_uri(), [True, True], 32768)
        self.iio_pluto_source_0.set_len_tag_key('packet_len')
        self.iio_pluto_source_0.set_frequency(cfreq)
        self.iio_pluto_source_0.set_samplerate(samp_rate)
        self.iio_pluto_source_0.set_gain_mode(0, 'slow_attack')
        self.iio_pluto_source_0.set_gain(0, 64)
        self.iio_pluto_source_0.set_quadrature(True)
        self.iio_pluto_source_0.set_rfdc(True)
        self.iio_pluto_source_0.set_bbdc(True)
        self.iio_pluto_source_0.set_filter_params('Auto', '', 0, 0)


############# OFDM Rcvr #####################


        self.digital_ofdm_rx_0 = digital.ofdm_rx(
            fft_len=fft_len, cp_len=(fft_len//4),
            frame_length_tag_key='frame_'+"rx_len",
            packet_length_tag_key="rx_len",
            occupied_carriers=((-4,-3,-2,-1,1,2,3,4),),
            pilot_carriers=((-6,-5,5,6),),
            pilot_symbols=((-1,1,-1,1),),
            sync_word1=None,
            sync_word2=None,
            bps_header=1,
            bps_payload=2,
            debug_log=False,
            scramble_bits=False)

        # self.digital_ofdm_rx_0.set_min_output_buffer(65536)
        # self.digital_ofdm_rx_0.set_max_output_buffer(int(65536))


        os.chdir(r'//home/moath/Documents/OFDM/ofdm_block/RandVector/Code/files/Rx_data')
        
        # self.blocks_vector_source_x_0_0 = blocks.vector_source_f(rand, True, 1, [])
        # self.blocks_sub_xx_0 = blocks.sub_ff(1)
        # self.blocks_float_to_char_1 = blocks.float_to_char(1, 1)

        # outfile = fname.replace('txt','out')
        # self.blocks_file_sink_0 = blocks.file_sink(gr.sizeof_char*1, outfile, False)
        # self.blocks_file_sink_0.set_unbuffered(False)
        # self.blocks_char_to_float_1 = blocks.char_to_float(1, 1)


        outfile = fname.replace('txt','dat')

        self.blocks_file_sink_1 = blocks.file_sink(gr.sizeof_char*1, outfile, False)#Data after OFDM directly
        self.blocks_file_sink_1.set_unbuffered(False)
        self.blocks_file_sink_1.set_min_output_buffer(65536)
        # # self.blocks_file_sink_1.set_max_output_buffer(int(2e6))


        ##################################################
        # Rx Connections
        ##################################################
        #self.connect((self.uhd_usrp_source_0, 0), (self.digital_ofdm_rx_0, 0))
        self.connect((self.iio_pluto_source_0, 0), (self.digital_ofdm_rx_0, 0))

        self.connect((self.digital_ofdm_rx_0, 0), (self.blocks_file_sink_1, 0))        
        # self.connect((self.digital_ofdm_rx_0, 0), (self.blocks_char_to_float_1, 0))
        # self.connect((self.blocks_char_to_float_1, 0), (self.blocks_sub_xx_0, 0))
        # self.connect((self.blocks_vector_source_x_0_0, 0), (self.blocks_sub_xx_0, 1))
        # self.connect((self.blocks_sub_xx_0, 0), (self.blocks_float_to_char_1, 0))        
        # self.connect((self.blocks_float_to_char_1, 0), (self.blocks_file_sink_0, 0))



#########################################################

    def closeEvent(self, event):
        self.settings = Qt.QSettings("GNU Radio", "FullDupx_OFDM_forCode")
        self.settings.setValue("geometry", self.saveGeometry())
        self.stop()
        self.wait()

        event.accept()

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.uhd_usrp_sink_0.set_samp_rate(self.samp_rate)
        self.uhd_usrp_source_0.set_samp_rate(self.samp_rate)

    def get_rf_rate(self):
        return self.rf_rate

    def set_rf_rate(self, rf_rate):
        self.rf_rate = rf_rate

    def get_rand(self):
        return self.rand

    def set_rand(self, rand):
        self.rand = rand
        self.blocks_vector_source_x_0.set_data(self.rand, [])
        self.blocks_vector_source_x_0_0.set_data(self.rand, [])

    def get_packet_len(self):
        return self.packet_len

    def set_packet_len(self, packet_len):
        self.packet_len = packet_len
        self.blocks_stream_to_tagged_stream_0.set_packet_len(self.packet_len)
        self.blocks_stream_to_tagged_stream_0.set_packet_len_pmt(self.packet_len)

    def get_len_tag_key(self):
        return self.len_tag_key

    def set_len_tag_key(self, len_tag_key):
        self.len_tag_key = len_tag_key

    def get_fft_len(self):
        return self.fft_len

    def set_fft_len(self, fft_len):
        self.fft_len = fft_len

    def get_cfreq(self):
        return self.cfreq

    def set_cfreq(self, cfreq):
        self.cfreq = cfreq
        self.uhd_usrp_sink_0.set_center_freq(self.cfreq, 0)
        self.uhd_usrp_source_0.set_center_freq(self.cfreq, 0)

    def get_BW(self):
        return self.BW

    def set_BW(self, BW):
        self.BW = BW
        self.uhd_usrp_sink_0.set_bandwidth(self.BW, 0)
        self.uhd_usrp_source_0.set_bandwidth(self.BW, 0)




def main(top_block_cls=ofdm_rxblock, options=None):

    print("***********************************\n")
    print("You are inside main function within the ofdm_rxblock_code file\n**************************")

    tb = top_block_cls()

    tb.start()
    try:
        raw_input('Press Enter to go for next file: ')
    except EOFError:
        pass
    tb.stop()
    tb.wait()
    
if __name__ == '__main__':
    main()
#################################################################
# Title: FullDupx_OFDM_forCode
# Author: Moath Sulaiman
# Description: send and receive a file using OFDM over USRPs
# GNU Radio version: v3.11.0.0git-393-ga07ced54
#################################################################

from packaging.version import Version as StrictVersion
from PyQt5 import Qt
# from gnuradio import qtgui
from gnuradio import digital

from gnuradio import blocks
import pmt
from gnuradio import gr
from gnuradio.filter import firdes
from gnuradio.fft import window
import sys
import signal
from PyQt5 import Qt
from argparse import ArgumentParser
from gnuradio.eng_arg import eng_float, intx
from gnuradio import eng_notation
from gnuradio import uhd
import time
import numpy as np



class ofdm_txblock(gr.top_block):

    def __init__(self,fname):

        gr.top_block.__init__(self, "ofdm_block_code", catch_exceptions=True)


        
        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 1500000
        self.rf_rate = rf_rate = 1500000
        self.rand = rand = np.random.randint(0, 2, 100)
        self.packet_len = packet_len = 48
        self.len_tag_key = len_tag_key = "packet_len"
        self.fft_len = fft_len = 64
        self.cfreq = cfreq = int(2.8e9)
        self.BW = BW = 2e6
        
        #GR_FIXED_BUFFER_SIZE = 128e6


        ##################################################
        # Blocks
        ##################################################

###### Tx Part ###########################

        self.digital_ofdm_tx_0 = digital.ofdm_tx(
            fft_len=fft_len,
            cp_len=(fft_len//4),
            packet_length_tag_key=len_tag_key,
            occupied_carriers=((-4,-3,-2,-1,1,2,3,4),),
            pilot_carriers=((-6,-5,5,6),),
            pilot_symbols=((-1,1,-1,1),),
            sync_word1=None,
            sync_word2=None,
            bps_header=1,
            bps_payload=2,
            rolloff=0,
            debug_log=False,
            scramble_bits=False)

        self.uhd_usrp_sink_0 = uhd.usrp_sink(
            ",".join(('serial=318A0AA', '')),
            uhd.stream_args(
                cpu_format="fc32",
                args='',
                channels=list(range(0,1)),
            ),
            "",
        )
        self.uhd_usrp_sink_0.set_samp_rate(samp_rate)
        self.uhd_usrp_sink_0.set_time_unknown_pps(uhd.time_spec(0))
        self.uhd_usrp_sink_0.set_center_freq(cfreq, 0)
        self.uhd_usrp_sink_0.set_antenna("TX/RX", 0)
        self.uhd_usrp_sink_0.set_bandwidth(BW, 0)
        self.uhd_usrp_sink_0.set_gain(75, 0)

        self.blocks_add_xx_0 = blocks.add_vff(1)
        self.blocks_vector_source_x_0 = blocks.vector_source_f(rand, True, 1, [])
        self.blocks_stream_to_tagged_stream_0 = blocks.stream_to_tagged_stream(gr.sizeof_char, 1, packet_len, len_tag_key)
        self.blocks_multiply_const_vxx_0_0 = blocks.multiply_const_cc(0.05)
        self.blocks_char_to_float_0 = blocks.char_to_float(1, 1)
        
        self.blocks_file_source_0 = blocks.file_source(gr.sizeof_char*1, fname, True, 0, 0)
        self.blocks_file_source_0.set_begin_tag(pmt.PMT_NIL)        
        self.blocks_float_to_char_0 = blocks.float_to_char(1, 1)



        ##################################################
        # Tx Connections
        ##################################################
        self.connect((self.blocks_file_source_0, 0), (self.blocks_char_to_float_0, 0))
        self.connect((self.blocks_char_to_float_0, 0), (self.blocks_add_xx_0, 0))
        self.connect((self.blocks_vector_source_x_0, 0), (self.blocks_add_xx_0, 1))
        self.connect((self.blocks_add_xx_0, 0), (self.blocks_float_to_char_0, 0))
        self.connect((self.blocks_float_to_char_0, 0), (self.blocks_stream_to_tagged_stream_0, 0))
        self.connect((self.blocks_stream_to_tagged_stream_0, 0), (self.digital_ofdm_tx_0, 0))
        self.connect((self.digital_ofdm_tx_0, 0), (self.blocks_multiply_const_vxx_0_0, 0))
        self.connect((self.blocks_multiply_const_vxx_0_0, 0), (self.uhd_usrp_sink_0, 0))

############# Rx Part serial= 318A0AA , 30D3F3D ######################################

        
        
#########################################################

    def closeEvent(self, event):
        self.settings = Qt.QSettings("GNU Radio", "FullDupx_OFDM_forCode")
        self.settings.setValue("geometry", self.saveGeometry())
        self.stop()
        self.wait()

        event.accept()

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.uhd_usrp_sink_0.set_samp_rate(self.samp_rate)
        self.uhd_usrp_source_0.set_samp_rate(self.samp_rate)

    def get_rf_rate(self):
        return self.rf_rate

    def set_rf_rate(self, rf_rate):
        self.rf_rate = rf_rate

    def get_rand(self):
        return self.rand

    def set_rand(self, rand):
        self.rand = rand
        self.blocks_vector_source_x_0.set_data(self.rand, [])
        self.blocks_vector_source_x_0_0.set_data(self.rand, [])

    def get_packet_len(self):
        return self.packet_len

    def set_packet_len(self, packet_len):
        self.packet_len = packet_len
        self.blocks_stream_to_tagged_stream_0.set_packet_len(self.packet_len)
        self.blocks_stream_to_tagged_stream_0.set_packet_len_pmt(self.packet_len)

    def get_len_tag_key(self):
        return self.len_tag_key

    def set_len_tag_key(self, len_tag_key):
        self.len_tag_key = len_tag_key

    def get_fft_len(self):
        return self.fft_len

    def set_fft_len(self, fft_len):
        self.fft_len = fft_len

    def get_cfreq(self):
        return self.cfreq

    def set_cfreq(self, cfreq):
        self.cfreq = cfreq
        self.uhd_usrp_sink_0.set_center_freq(self.cfreq, 0)
        self.uhd_usrp_source_0.set_center_freq(self.cfreq, 0)

    def get_BW(self):
        return self.BW

    def set_BW(self, BW):
        self.BW = BW
        self.uhd_usrp_sink_0.set_bandwidth(self.BW, 0)
        self.uhd_usrp_source_0.set_bandwidth(self.BW, 0)




def main(top_block_cls=ofdm_txblock, options=None):

    print("***********************************\n")
    print("You are inside main function within the ofdm_block_code file\n**************************")

    tb = top_block_cls()

    tb.start()
    try:
        raw_input('Press Enter to go for next file: ')
    except EOFError:
        pass
    tb.stop()
    tb.wait()
    
if __name__ == '__main__':
    main()
import numpy as np
import gnuradio
# import scipy
import csv, time, os, glob, sys
from ofdmrx_block_code import ofdm_block2




    ##########################################################
    # read file names to a list
    # for each file transmit it using block of OFDM, retransmit repeatdely for 15 sec
    # after 5 sec of transmittion, start receiving and dump the received data to a file.
    # repeat for the next input file.

    ####################################################################




##########################  Tx Part ############################

if __name__ == "__main__":
    
    #read file names
    # /home/moath/Documents/OFDM/ofdm_block/RandVector/Code/files
    # os.chdir(r'/home/moath/Documents/OFDM/ofdm_block/RandVector/Code/')
    # tx_files=glob.glob('*.txt')
    # # fn,tmp,thr =my_files[0].split("_")
        
    # print("There are ",len(tx_files)," file; first one is: ", tx_files[0]) # to print the number of files in the directory and the name for the first file.
    # print("-----------------------")
    
    for index in range(1,101):
        tb=ofdm_block(index)
        tb.start()
        time.sleep(3)
        tb.stop()
        tb.wait()
        print("Sleeping for a while!")
        time.sleep(7)


##########################  Rx part #############################


# def write_csv(writecsvfile,write_csv_data):
#     with open(writecsvfile,'a') as real_handle:
#         writer = csv.writer(real_handle)
#         writer.writerow(write_csv_data)


# def split_iq_writecsv(iqchunk,txr, rxr, mod):
#     filename = 'Txr_'+txr+'-Rvr_'+rxr+'-'+mod + '.csv'
#     for index, value in enumerate(range(0,iqchunk.shape[1]+1,1024)):
#         sliced_sample = iqchunk[0][value:value+1024]
#         if sliced_sample.shape[0] == 1024:
#             write_csv(filename, sliced_sample.tolist())
#             sample_count = index+1
#         else: 
#             print("Alert : Discarding sample slice !!!!")
#     return sample_count
    
# def data_processor(data_file, transmitter, receiver, modulation):
#     data_list_nn = []
#     dtype_all = scipy.dtype([('raw-iq0', scipy.complex64)])
#     iqdata = np.fromfile(open(data_file),dtype = dtype_all)
#     no_of_data = iqdata.shape[0]
#     print("Data collected: ", no_of_data*2)
#     xdata = np.concatenate([iqdata['raw-iq0'].real.reshape(no_of_data,1),iqdata['raw-iq0'].imag.reshape(no_of_data,1)],axis = 1)
#     trans_x_orig = xdata.reshape(1,no_of_data*2)
#     sample_count = split_iq_writecsv(trans_x_orig, transmitter, receiver ,modulation)
#     print("Number of samples collected: ", sample_count)
#     return sample_count


# #############################################################

# if __name__ == "__main__":
    
    
#     ##########################################################
#     # read file names to a list
#     # for each file transmit it using block of OFDM, retransmit repeatdely for 15 sec
#     # after 5 sec of transmittion, start receiving and dump the received data to a file.
#     # repeat for the next input file.

#     ####################################################################3

#     #read file names
#     os.chdir(r'/home/moath/emitter/Copy_results')
#     tx_files=glob.glob('*.txt')
#     # fn,tmp,thr =my_files[0].split("_")
#     print(len(my_files), tx_files[0]) # to print the number of files in the directory and the name for the first file.
        


#     const_list = ['bpsk','qpsk','8_psk','16_qam','64_qam']
#     # tx_src=sys.argv[1]
#     # rx_source = sys.argv[2]
#     # constellation = sys.argv[3]
    
#     #for constellation in const_list:
#     total_samples = 0
#     rounds = 0

#     while total_samples < 100000:
#         if constellation == '64_qam':
#             tb = top_block(rx_source, constellation,'qam')
#         elif constellation != '64_qam': 
#             tb = top_block(rx_source, constellation,'generic')
#         rounds +=1
#         print("-----------------------")
#         print("Receiving round (",rounds,") with total samples of:",total_samples+1,': ',constellation)
#         print("-----------------------")

#         tb.start()
#         time.sleep(5)
#         tb.stop()
#         tb.wait()
#         no_of_samples = data_processor('iqdump.dat', tx_src, rx_source, constellation)
#         total_samples = total_samples + no_of_samples
#         print("total_samples=  ", total_samples)

  • Fwd: mult... GNU Radio, the Free & Open-Source Toolkit for Software Radio

Reply via email to