You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GNU Radio 3.10 Python回调函数无法触发问题求助

GNU Radio 3.10 OOT模块回调函数不触发问题

我正在创建一个GNU Radio 3.10 OOT(Out-of-Tree)模块测试回调功能,但修改参数时Python回调函数无法触发。

已参考相关教程和文档修改代码,添加了set_selector回调函数。预期流图运行时,修改GUI中的selector参数会触发回调并打印内容,但实际未生效。已执行make、sudo make install、sudo ldconfig重新编译,且GRC中block参数selector显示下划线(表明已关联回调)。请问回调未触发的原因是什么?是否遗漏了注册步骤?

Python代码

import numpy as np
from gnuradio import gr

class addSubSelect(gr.sync_block):
    """
    docstring for block addSubSelect
    """
    def __init__(self, selector=True):
        gr.sync_block.__init__(self,
            name="addSubSelect",
            in_sig=[np.complex64, np.complex64],
            out_sig=[np.complex64])

        self.selector = selector

    def set_selector(self, selector):
        print("IN CALLBACK")

    def work(self, input_items, output_items):
        in0 = input_items[0]
        in1 = input_items[1]

        if (self.selector):
            output_items[0][:] = in0 + in1
        else:
            output_items[0][:] = in0 - in1

        return len(output_items[0])

YAML代码

id: callbackTest_addSubSelect
label: addSubSelect
category: '[callbackTest]'

templates:
  imports: from gnuradio import callbackTest
  make: callbackTest.addSubSelect(${selector})
  callbacks:
  - set_selector(${selector})
parameters:
- id: selector
  label: Add (True) or Subtract (False) selector
  dtype: bool
  default: True

inputs:
- label: in0
  domain: stream
  dtype: complex
- label: in1
  domain: stream
  dtype: complex

outputs:
- label: out0
  domain: stream
  dtype: complex

file_format: 1

问题原因与修复方案

回调未触发的核心原因是未显式向GNU Radio运行时注册回调函数,同时回调函数未更新块的内部状态,导致参数变更无法生效。

1. 显式注册回调函数

GNU Radio 3.10的Python块需要通过add_callback方法将参数与回调函数绑定,否则运行时无法识别该方法为参数变更的处理函数。在块的__init__方法中添加注册代码:

2. 更新内部状态

回调函数需要同步更新类的self.selector属性,否则参数变更后,work函数仍会使用旧值。

修改后的Python代码:

import numpy as np
from gnuradio import gr

class addSubSelect(gr.sync_block):
    """
    docstring for block addSubSelect
    """
    def __init__(self, selector=True):
        gr.sync_block.__init__(self,
            name="addSubSelect",
            in_sig=[np.complex64, np.complex64],
            out_sig=[np.complex64])

        self.selector = selector
        # 注册回调:将参数"selector"与set_selector方法绑定
        self.add_callback("selector", self.set_selector)

    def set_selector(self, selector):
        print("IN CALLBACK - selector updated to:", selector)
        self.selector = selector  # 同步更新内部状态

    def work(self, input_items, output_items):
        in0 = input_items[0]
        in1 = input_items[1]

        if self.selector:
            output_items[0][:] = in0 + in1
        else:
            output_items[0][:] = in0 - in1

        return len(output_items[0])

3. 后续验证步骤

  1. 重新编译安装模块:make && sudo make install && sudo ldconfig
  2. 重启GRC,确保加载最新模块
  3. 在终端运行GRC(而非图形界面双击启动),以便查看回调的打印输出
  4. 运行流图并修改selector参数,此时终端应打印回调信息,且输出信号会随参数切换加法/减法逻辑

内容的提问来源于stack exchange,提问作者Projectile Fish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 05:45:30