GNU Radio嵌入式Python块无法读取参数值的问题求助
GNU Radio嵌入式Python Block向量长度读取问题
我正在给GNU Radio的EmbeddedEmbedded Python Block编写代码,想要实现读取用户点击该块时弹出参数栏里设置的向量长度。但GNU Radio调度器要到流程后期才能获取用户输入的值,导致我的代码始终使用__init__函数里的默认向量长度,影响正常运行。
我试过用相关问题里Djgreene730提供的解决方案,但因为对GRC库和编程知识了解有限(我是电气工程师,非计算机相关专业),没法完成实现和调试。我能确定代码本身没问题,是GRC的机制导致的问题,目前只理解那段代码能找到流图变量的名称,但我需要的是变量的具体值。
我的目标是写一个函数,在流图里找到目标变量并提取它的值,这样就能在__init__函数里覆盖默认值。现在我只能每次修改向量长度时,在代码编辑器里改默认值,操作繁琐。最终想让这个流图对用户友好,不用IDE就能修改参数。
以下是我当前的代码(注:标注为“test code”的部分是Djgreene730提供的代码):
import numpy as np from gnuradio import gr import inspect # 原代码遗漏该导入,必须添加才能运行 #WindowSize = 1000000 # *** BEGINNING OF TEST CODE *** def get_top_variable(variable_name="WindowSize", default=None): ''' Returns the value of a variable from the flow graph. ''' # Let's go! try: ## Run Condition: GNURadio is starting the flowgraph top = inspect.currentframe().f_back.f_back.f_locals # Check if top has the variable name we're looking for if top.__contains__(variable_name): # Let the user know we found the variable print(f"[Block Debug] While starting, I found top variable '{variable_name}': type={type(top[variable_name])}, value={top[variable_name]}") # Return the value of the variable name return top[variable_name] ## Run Condition: Saving the flowgraph top = inspect.currentframe().f_back.f_back.f_back.f_back.f_back.f_locals # Make sure top has 'self' if top.__contains__("self") and \ (isinstance(top['self'], grc.gui.canvas.flowgraph.FlowGraph)) and \ (hasattr(top['self'], 'blocks')): # Get a list of all blocks block_names = [block.name for block in top['self'].blocks] # Find the index to the variable we need block_index = block_names.index(variable_name) # Store the result result = top['self'].blocks[block_index].params['value'].value # Let the user know we found the variable print(f"[Block Debug] While modifying the flowgraph, I found top variable '{variable_name}': type={type(result)}, value={result}") # Return the value of the variable we found return result # Something went wrong except Exception as e: print("[Block Debug]: Threw an exception", e) except: print("[Block Debug]: Threw an unknown exception.") # Return the default result return default # *** ENDING OF TEST CODE *** class blk(gr.sync_block): # other base classes are basic_block, decim_block, interp_block """Embedded Python Block example - a simple multiply const""" def __init__(self, window_size = get_top_variable("vlen", default=1)): # = 1 # only default arguments here # note on the window_size value: internal, need to get an external variable and sub it in # # *** TEST CODE BEGIN *** # Get packet length vlen = get_top_variable("vlen", default=1) print(f"[Block Debug] Setting input length using 'vlen': {vlen}, type={type(vlen)}") # # *** TEST CODE END *** """arguments to this function show up as parameters in GRC""" gr.sync_block.__init__( self, name='Modulation Monitor', # will show up in GRC in_sig=[(np.complex64, window_size)], out_sig=[(np.complex64, window_size)] ) # if an attribute with the same name as a parameter is found, # a callback is registered (properties work, too).
(已省略work()代码,该部分无问题)
内容的提问来源于stack exchange,提问作者Bogus Man
相关产品推荐
相关产品推荐

