gnuradio学习之qa_tag_file_sink.py

简介:
#!/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。



目录
相关文章
|
6月前
|
缓存 架构师 算法
【Conan 入门教程 】深入理解 Conan 2.X 中的 self.source_folder
【Conan 入门教程 】深入理解 Conan 2.X 中的 self.source_folder
104 1
|
12月前
|
资源调度 JavaScript Windows
yarn install命令报错解决办法-warning package-lock.json found.
yarn install命令报错解决办法-warning package-lock.json found.
312 0
CMake Error: The source “xxx“ does not match the source “yyy“ used to generate cache. Re-run cmake
CMake Error: The source “xxx“ does not match the source “yyy“ used to generate cache. Re-run cmake
859 0
|
2月前
|
缓存 Python
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-npf9报错
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-npf9报错
|
Docker 容器
Docker错误:inotify_add_watch(7, /dev/dm-4, 10) failed: No such file or directory
Docker错误:inotify_add_watch(7, /dev/dm-4, 10) failed: No such file or directory
244 1
|
4月前
|
Python
安装 supervisor报错执行python setup.py install时No local packages or working download links found for meld3
安装 supervisor报错执行python setup.py install时No local packages or working download links found for meld3
32 0
|
数据可视化 Python
常见的bug---5、在安装superset时候报错,Command “python setup.py egg_info“ failed with error code 1
常见的bug---5、在安装superset时候报错,Command “python setup.py egg_info“ failed with error code 1
|
6月前
|
JavaScript Java C++
【CMake 中的 aux_source_directory 命令深入解析】 aux_source_directory 命令从文件识别到最佳实践
【CMake 中的 aux_source_directory 命令深入解析】 aux_source_directory 命令从文件识别到最佳实践
652 0
|
数据安全/隐私保护
dpkg-buildpackage: error: debian/rules binary subprocess was killed by signal 2
dpkg-buildpackage: error: debian/rules binary subprocess was killed by signal 2
988 0
|
Ubuntu
问题解决:Ubuntu安装ROS依赖出现ERROR: the following packages/stacks could not have their rosdep keys resolvedt
问题解决:Ubuntu安装ROS依赖出现ERROR: the following packages/stacks could not have their rosdep keys resolvedt
1465 0