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

如何判断GNU Radio的File Source已完成文件读取?

判断GNU Radio File Source是否完成文件读取的方法

以下两种方法可以实现你需要的文件读取完成判断:

方法1:通过文件位置对比判断

利用file_source的tell()方法获取当前读取的字节位置,结合文件总大小来判断是否读取完毕。

import os
from gnuradio import gr, blocks

# 初始化File Source(根据你的数据类型调整sizeof_*)
file_path = "input_file.bin"
file_source = blocks.file_source(gr.sizeof_gr_complex, file_path, repeat=False)

# 获取文件总字节数和单个数据项的字节大小
total_file_bytes = os.path.getsize(file_path)
item_byte_size = gr.sizeof_gr_complex

# 在流处理过程中检查
while True:
    # 执行你的流处理逻辑(比如调用流图的run_once()或处理数据)
    
    current_read_bytes = file_source.tell()
    # 当已读取字节数大于等于文件总字节数时,判定为读取完成
    if current_read_bytes >= total_file_bytes:
        print("done.")
        break

方法2:监听File Source的结束消息

GNU Radio的file_source在文件读取完成时,会通过done消息端口发送结束信号。你可以订阅这个消息来触发完成逻辑:

from gnuradio import gr, blocks, pmt

class MyFlowgraph(gr.top_block):
    def __init__(self):
        gr.top_block.__init__(self)
        
        # 初始化File Source
        self.file_source = blocks.file_source(gr.sizeof_gr_complex, "input_file.bin", repeat=False)
        # 订阅done消息
        self.file_source.msg_connect(self.file_source, "done", self, "handle_done")
        
        # 连接其他模块...
    
    def handle_done(self, msg):
        if pmt.equal(msg, pmt.intern("done")):
            print("done.")
            self.stop()  # 停止流图

# 运行流图
tb = MyFlowgraph()
tb.run()

注意:不要直接调用work()函数,该方法由GNU Radio的调度系统内部调用,手动调用会因参数不匹配报错。

内容的提问来源于stack exchange,提问作者Guy Meirson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 15:32:39