Search code examples
gnuradio

Is there a way to rerun top_block via the run() function?


I have setup the python code to transmit a wav file over NBFM and it works great for sending the file once OR if I set the auto-repeat it will continue to send the message. However, I would like to call the top_block.run() function, wait than call it again and each time it would run the flow graph(the top_block).

Currently I have

    self.samp_rate = samp_rate = 44000

    ##################################################
    # Blocks
    ##################################################
    self.rational_resampler_xxx_0 = filter.rational_resampler_ccc(
            interpolation=2400000,
            decimation=88200,
            taps=None,
            fractional_bw=None,
    )

    self.osmosdr_sink_0 = osmosdr.sink( args="numchan=" + str(1) + " " + "" )
    self.osmosdr_sink_0.set_sample_rate(2400000)
    self.osmosdr_sink_0.set_center_freq(463025000, 0)
    self.osmosdr_sink_0.set_freq_corr(6, 0)
    self.osmosdr_sink_0.set_gain(0, 0)
    self.osmosdr_sink_0.set_if_gain(30, 0)
    self.osmosdr_sink_0.set_bb_gain(20, 0)
    self.osmosdr_sink_0.set_antenna("", 0)
    self.osmosdr_sink_0.set_bandwidth(10000, 0)

    self.blocks_wavfile_source_0 = blocks.wavfile_source("/home/ahmad/Test123.wav", True)
    self.blocks_multiply_const_vxx_0 = blocks.multiply_const_vff((2, ))
    self.analog_nbfm_tx_0 = analog.nbfm_tx(
        audio_rate=44100,
        quad_rate=88200,
        tau=75e-6,
        max_dev=5e3,
            )

    ##################################################
    # Connections
    ##################################################
    self.connect((self.analog_nbfm_tx_0, 0), (self.rational_resampler_xxx_0, 0))    
    self.connect((self.blocks_multiply_const_vxx_0, 0), (self.analog_nbfm_tx_0, 0))    
    self.connect((self.blocks_wavfile_source_0, 0), (self.blocks_multiply_const_vxx_0, 0))    
    self.connect((self.rational_resampler_xxx_0, 0), (self.osmosdr_sink_0, 0))    

tb = top_block_cls()
i = 0
while i < 5:
    tb.run()
    i = i + 1

How ever the above code only transmits once and rest of the time skips the run cmd, I think the blocks have some boolean which indicates that the work has been done? And it needs to be reset. Thank you for your time!


Solution

  • You can reuse a top block, but you need to think about the state of the blocks in it. In particular, most likely, your file source block is still sitting at end-of-file.

    Pick a solution:

    • Tell the file source block to start over or reopen the same file. (This is not available for the wavfile_source block, so you will have to use another solution.)
    • Replace it with a newly created file source block. (This approach has the advantage of generalizing to all types of source block.)

      To replace a block, while the flow graph is not running, use self.disconnect(...) (same parameters as self.connect(...)) to remove the connection from it, and then self.connect(...) the new block.

    • Create a new top_block_cls instead of reusing the previous one. (This ensures that no state from the previous run is carried through at all, but of course may take more time.)