#!/usr/bin/env python
#A file sink that uses tags to save files.
# The sink uses a tag with the key 'burst' to trigger the saving of the burst data to a new file.
# If the value of this tag is True, it will open a new file and start writing all incoming data to it.
# If the tag is False, it will close the file (if already opened). The file names are based on the
# time when the burst tag was seen. If there is an 'rx_time' tag (standard with UHD sources),
# that is used as the time. If no 'rx_time' tag is found, the new time is calculated based off the
#sample rate of the block.
from gnuradio import gr, gr_unittest, blocks
import os, struct
#####os 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不
#####同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作 ,
#####关于python的os模块http://www.cnblogs.com/wayneye/archive/2010/05/03/1726865.html
class test_tag_file_sink(gr_unittest.TestCase):
def setUp(self):
self.tb = gr.top_block()
def tearDown(self):
self.tb = None
def test_001(self):##建立流图
src_data = ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
trg_data = (-1, -1, 1, 1, -1, -1, 1, 1, -1, -1)
src = blocks.vector_source_i(src_data)
trg = blocks.vector_source_s(trg_data)
op = blocks.burst_tagger(gr.sizeof_int)
snk = blocks.tagged_file_sink(gr.sizeof_int, 1)
self.tb.connect(src, (op,0))
self.tb.connect(trg, (op,1))
self.tb.connect(op, snk)
self.tb.run()
# Tagged file sink gets 2 burst tags at index 2 and index 5.
# Creates two new files, each with two integers in them from
# src_data at these indexes (3,4) and (7,8).
file0 = "file{0}_0_2.00000000.dat".format(snk.unique_id())
file1 = "file{0}_1_6.00000000.dat".format(snk.unique_id())
# Open the files and read in the data, then remove the files
# to clean up the directory.
outfile0 = file(file0, 'rb')#读取二进制文件,python中的文件读写:http://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html
outfile1 = file(file1, 'rb')
data0 = outfile0.read(8)
data1 = outfile1.read(8)
outfile0.close()
outfile1.close()
os.remove(file0)
os.remove(file1)
# Convert the 8 bytes from the files into a tuple of 2 ints.
####python用struct处理二进制数据:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html
idata0 = struct.unpack('ii', data0)
idata1 = struct.unpack('ii', data1)
self.assertEqual(idata0, (3, 4))##python中的测试模块
self.assertEqual(idata1, (7, 8))
if __name__ == '__main__':
gr_unittest.run(test_tag_file_sink, "test_tag_file_sink.xml")
#gr_unittest 是一个扩展的标准 Python 模块单元测试。gr_unittest 增加了对检查浮点
#和复数的元组的近似相等的支持。unittest 使用 Python 的反射机制来发现所有运行的方法和运行
#它们。unittest 包装每次调用 test_*来匹配建立和拆除的调用。当我们运行测试,在这种秩序中
#gr_unittest.main 要调用 SETUP,test_001和 tearDown。
gnuradio学习之qa_tag_file_sink.py
最新推荐文章于 2023-12-14 14:45:09 发布