简体   繁体   English

在 GNU Radio Companion 中开发; 在单独的 Python 文件中调用顶部块的方法

[英]Develop in GNU Radio Companion; call top block's methods in separate Python file

I would like to...我想...

  • Make a flowgraph in GNU Radio Companion在 GNU Radio Companion 中制作流程图
  • Call methods from a separate Python file从单独的 Python 文件调用方法

For example, let's say I want to scan through center frequencies in this flowgraph:例如,假设我想扫描此流程图中的中心频率:

Osmocom source --> NBFM Receive --> Audio sink

I'm guessing that Message Passing would allow me to set the osmocom's center freq programmatically, but it would be easier (I think...?) to do something like this in Python:我猜消息传递将允许我以编程方式设置 osmocom 的中心频率,但在 Python 中做这样的事情会更容易(我认为......?):

freqs = [100e6, 101e6]
for freq in freqs:
    set_center_freq(freq)

What do you suggest?你有什么建议? ( I'm answering my own question, but I'm curious if others have suggested improvements! 🙂 ) 我正在回答我自己的问题,但我很好奇其他人是否提出了改进建议!🙂

You can import the top block from the Python file generated by GRC.您可以从 GRC 生成的 Python 文件中导入顶部块。 That way, when the file is regenerated, you don't lose your changes.这样,当文件重新生成时,您不会丢失所做的更改。

from my_generated_FM_receiver import my_generated_FM_receiver

tb = my_generated_FM_receiver()

There's a little more setup to be done to run the flowgraph.运行流程图需要做更多的设置。 (See here , lines 103-128, starting with def main(top_block_cls... ). (参见此处,第 103-128 行,从def main(top_block_cls... ) 开始。

If you want to access the tb variable, you can create a thread and pass tb as an argument, like so (this code is identical to the usual generated code except for the two added lines near the end):如果您想访问tb变量,您可以创建一个线程并将tb作为参数传递,如下所示(此代码与通常生成的代码相同,除了末尾附近添加的两行):



from threading import Thread


def set_up_qt_top_block_and_run_func_in_thread(top_block_cls, func_to_run):

    if StrictVersion("4.5.0") <= StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
        style = gr.prefs().get_string('qtgui', 'style', 'raster')
        Qt.QApplication.setGraphicsSystem(style)
    qapp = Qt.QApplication(sys.argv)

    tb = top_block_cls()
    tb.start()
    tb.show()

    def sig_handler(sig=None, frame=None):
        Qt.QApplication.quit()

    signal.signal(signal.SIGINT, sig_handler)
    signal.signal(signal.SIGTERM, sig_handler)

    timer = Qt.QTimer()
    timer.start(500)
    timer.timeout.connect(lambda: None)

    def quitting():
        tb.stop()
        tb.wait()
    qapp.aboutToQuit.connect(quitting)

    #######################
    ## this is my addition
    ##
    thread = Thread(target = func_to_run, args = (tb, ), daemon=True)
    thread.start()
    ## end my addition
    #######################
    qapp.exec_()
    

Here's how you could use it:以下是您可以使用它的方法:


# This is your GRC-generated Python module
from my_generated_FM_receiver import my_generated_FM_receiver


def runMe(tb):
    freqs = [100e6, 101e6]
    for freq in freqs:
        tb.osmosdr_source_0.set_center_freq(freq)

set_up_qt_top_block_and_run_func_in_thread(my_generated_FM_receiver, runMe)

PS: In GRC 3.9+, the Python Snippit may be better. PS:在 GRC 3.9+ 中, Python Snippit可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM