#!/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模块https://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中的文件读写:https://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处置二进制数据:https://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。
#!/usr/bin/env python
#A file sink that uses tags