中断一个简单的 GNUradio 流程

Interrupting a simple GNUradio flow

我正在尝试在 python 中编写 GNUradio 脚本。我的最终目标是有一个例程,定期将 GNUradio 进程中的浮点结果写入串行端口。作为第一步,我想简单地暂停一个简单的例程以下代码通过声卡播放 1kHz 音调:

`#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: simpleTone
# Generated: Wed Jun 29 07:26:02 2016
##################################################

from gnuradio import analog
from gnuradio import audio
from gnuradio import blocks
from gnuradio import gr
import time

class simpleTone(gr.top_block):

    def __init__(self):
    gr.top_block.__init__(self)

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 32000

        ##################################################
        # Blocks
        ##################################################
        self.blocks_throttle_0 = blocks.throttle(gr.sizeof_float*1, samp_rate)
        self.audio_sink_0 = audio.sink(samp_rate, "", True)
        self.analog_sig_source_x_0 = analog.sig_source_f(samp_rate, analog.GR_COS_WAVE, 1000, 1, 0)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.analog_sig_source_x_0, 0), (self.blocks_throttle_0, 0))
        self.connect((self.blocks_throttle_0, 0), (self.audio_sink_0, 0))


if __name__ == '__main__':
    simpleTone().run()`

上面的代码工作正常,除了一些溢出和点击音频。但是,如果我进行以下替换:

`        
if __name__ == '__main__':
    simpleTone().start()
    time.sleep(3)
    simpleTone().stop()`

结果是文件运行,并在 3 秒后结束但没有产生音频。

我确定我错过了一些相当基本的东西,我们将不胜感激。谢谢

-埃德

这个问题也在这里提出:

http://lists.gnu.org/archive/html/discuss-gnuradio

并得到答复。如果有人遇到这个问题,我会在这里分享这个答案:

Your problem is that you're constructing three unrelated top blocks: you have three separate occurrences of "simpleTone()". Instead you need to create one and continue to use it, like so:

tb = simpleTone()
tb.start()
...
tb.stop()

You have another problem, too, which you will find after fixing the first one. .wait() means to wait for the flowgraph to finish all processing, and your flowgraph has no elements within it to finish such as a Head block, so the .stop() will never be reached.

Instead, you need to proceed like this:

tb = simpleTone()
tb.start()
# the flowgraph is now running independently
time.sleep(3)
tb.stop()
tb.wait()

The final .wait() is not actually necessary in this case -- what it does is wait for the flowgraph to finish, which will happen shortly after .stop() is called, but if you later wish to start the same top block again, you must have called .wait() before you call .start(), so always having a matched set of [start, stop, wait] or [start, wait, stop] is good practice.

这是您在 python 中创建对象的方式的问题。每次调用 'simpleTone()' 时,您实际上是在启动一个新对象。 class simpleTone 的。我同意之前的回答,即创建一个对象即可解决问题。