gnuradio笔记[1]-内嵌python代码块

摘要

在GNU Radio中简单使用内嵌python代码块实现输出内容到文件.

超链接

解决无法编辑代码块内代码

原理简介

GNU Radio简介

[https://wiki.gnuradio.org/index.php?title=What_Is_GNU_Radio]
GNU Radio is a free & open-source software development toolkit that provides signal processing blocks to implement software radios. It can be used with readily-available low-cost external RF hardware to create software-defined radios, or without hardware in a simulation-like environment. It is widely used in research, industry, academia, government, and hobbyist environments to support both wireless communications research and real-world radio systems.
GNU Radio is a framework that enables users to design, simulate, and deploy highly capable real-world radio systems. It is a highly modular, "flowgraph"-oriented framework that comes with a comprehensive library of processing blocks that can be readily combined to make complex signal processing applications. GNU Radio has been used for a huge array of real-world radio applications, including audio processing, mobile communications, tracking satellites, radar systems, GSM networks, Digital Radio Mondiale, and much more - all in computer software. It is, by itself, not a solution to talk to any specific hardware. Nor does it provide out-of-the-box applications for specific radio communications standards (e.g., 802.11, ZigBee, LTE, etc.,), but it can be (and has been) used to develop implementations of basically any band-limited communication standard.

GNU Radio是一个免费开源的软件开发工具包,提供信号处理块来实现软件无线电。它可以与现成的低成本外部RF硬件一起使用,以创建软件定义的无线电,或者在没有硬件的情况下在类似模拟的环境中使用。它广泛应用于研究、工业、学术、政府和业余爱好者环境,以支持无线通信研究和现实世界的无线电系统。
GNU Radio是一个框架,使用户能够设计、模拟和部署高性能的现实无线电系统。它是一个高度模块化的、面向“流图”的框架,带有全面的处理块库,可以很容易地组合起来,制作复杂的信号处理应用程序。GNU Radio已被用于大量的现实无线电应用,包括音频处理、移动通信、跟踪卫星、雷达系统、GSM网络、数字无线电世界等等,所有这些都在计算机软件中。它本身并不是与任何特定硬件通信的解决方案。它也没有为特定的无线电通信标准(如802.11、ZigBee、LTE等)提供开箱即用的应用程序,但它可以(并且已经)用于开发基本上任何带限通信标准的实现。

GNU Radio使用基于流图的可视化方式开发,支持内嵌代码.

python代码块简介

[https://wiki.gnuradio.org/index.php/Embedded_Python_Block]
This block allows you to create a new (custom) block in Python without needing to make and install an Out of Tree (OOT) Module. When you add the block to your flowgraph, the pre-populated code simply takes the input stream and multiplies it by a constant. Note that the structure of this Python block matches the structure of an OOT Python block. It's essentially a Python OOT block built into a grc flowgraph.

"""
Embedded Python Blocks:

Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__  will
be the parameters. All of them are required to have default values!
"""

import numpy as np
from gnuradio import gr


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, example_param=1.0):  # only default arguments here
        """arguments to this function show up as parameters in GRC"""
        gr.sync_block.__init__(
            self,
            name='Embedded Python Block',   # will show up in GRC
            in_sig=[np.complex64],
            out_sig=[np.complex64]
        )
        # if an attribute with the same name as a parameter is found,
        # a callback is registered (properties work, too).
        self.example_param = example_param

    def work(self, input_items, output_items):
        """example: multiply with constant"""
        output_items[0][:] = input_items[0] * self.example_param
        return len(output_items[0])

参数:
The parameters for this block are simply those of your embedded Python block itself.

Note that every parameter needs a default value. For example, in the code above, note line:

def __init__(self, example_param=1.0):  # only default arguments here
#If you don't have any parameters, change that line of code to

def __init__(self):
#If you have a vector length equal to a variable of your block, note that the default value must be "hardcoded".

实现

连线

注意
输入输出类型要匹配,如都是byte,否则连线会变成红色.

代码块代码

"""
Embedded Python Block demo
"""

#  epy_block_0.py
#  created 10/17/2019

import numpy as np
from gnuradio import gr

import pmt

textboxValue = ""

class my_sync_block(gr.sync_block):
    """
    reads input from a message port
    outputs text
    """
    def __init__(self):
        gr.sync_block.__init__(self,
            name = "Embedded Python demo",
            in_sig = None,
            out_sig = [np.byte])
        self.message_port_register_in(pmt.intern('msg_in'))
        self.message_port_register_out(pmt.intern('clear_input'))
        self.set_msg_handler(pmt.intern('msg_in'), self.handle_msg)

    def handle_msg(self, msg):
        global textboxValue

        textboxValue = pmt.symbol_to_string (msg)
        # print (textboxValue)
    
    def work(self, input_items, output_items):
        global textboxValue

        # get length of string
        _len = len(textboxValue)
        if (_len > 0):
            # terminate with LF
            textboxValue += "\n"
            _len += 1
            # store elements in output array
            for x in range(_len):
                output_items[0][x] = ord(textboxValue[x])
            textboxValue = ""
            self.message_port_pub(pmt.intern('clear_input'), pmt.intern(''))
            return (_len)
        else:
            return (0)

效果

posted @ 2023-12-21 23:24  qsBye  阅读(78)  评论(0编辑  收藏  举报