1、gunradio软件无线电发送接收字符程序GNU RADIO软件无线电benchmark程序发送接收文件测试笔记 在GNU RADIO软件无线电程序中,benchmark程序能够发射900MHZ,接收900MHZ的无线电信号。具体做法是,把两台装有Basic子板的USRP1母板别离连上两台电脑的USB端口上,接通电源,在ubuntu操作系统下,在其中一台电脑的终端提示符下键入:“ cd /usr/local/share/gnuradio/example/digital/进入benchmark程序的目录, 再键入:“./ -f 900M这时候,和这台电脑USB端口连接的USRP母板上的LFTX子
2、板通过电线就会发送一个900MHZ的无线电信号,电脑显示器显示:.表示正在发射无线电信号。这时,在另外的一台电脑上的终端提示符下键入:“ cd /usr/locaul/share/gnuradio/example/digital/进入benchmark程序的目录, 再键入:“./ -f 900M这时候,和这台电脑USB端口连接的USRP母板上的LFTX子板通过电线就会收到一个900MHZ的无线电信号,电脑显示器显示:ok=True pktno=1202 n_rcvd=1 n_right=0表明接收信号正常。 现在的问题是要从这个电脑发送一个文件到另外一个电脑,我查找了相关资料发现,benchm
3、ark具有发送文件的功能,但是开源程序里没有这项功能,需要用户依照自己的需要修改程序来达到发送接收文件的功能。我通过研究发觉,修改程序后,能够把这台电脑的文件的“hello“字符发送到另外一台 电脑的文件中。具体做法是,在发送端电脑的/usr/local/share/gnuradio/example/digital目录下新建,两个文本文件,在中输入hello并保留,然后在终端下输入:“vi 在vi编辑器下打开程序,在第124行 () # start flow graph,后输入 “f=open(/usr/local/share/gnuradio/examples/digital/,r)” “l
4、ines=()”两行新程序,第一行程序是以只读方式打开文件,第二行程序的作用是以每行阅读的方式读出中的文本内容,然后再赋值给lines数组,接着把第140行程序payload = (!H, pktno & 0xffff) + data 改为 payload = str ) + str(lines) 这步的作用是,去掉了打包发送pkno函数的功能,而加入发送lines函数和函数的功能。 此刻说一下函数是从哪来的。第一把程序的第95行至第100行,改成 (-E,-discontinuous, action=store_true, default=False, help=enable discont
5、inous transmission (bursts of 5 packets) (-W,-from_file,dest=from_file, default=None, help=use intput file for packet contents) (-T,-to_file,dest=to_file, default=None, help=Output file for modulated samples) (-n,-num,type=int,dest=num,default=1000, help=send message) (-e, -file,dest=filename, help=
6、write report to FILE,metavar=FILE) (-p, -pdbk, action=store_true, dest=pdcl, default=False, help=write pdbk data to oracle db) (-z, -zdbk, action=store_true, dest=zdcl, default=False, help=write zdbk data to oracle db) 简单说一下,在终端输入“benchmark_tx -f 900M -n 1500”时,程序以900MHZ的频率发送数组中的数字1500和lines数组,因为(-n
7、,-num,type=int,dest=num,default=1000, help=send message)定义了一个属性是num的函数通过提示符-n xxx来返回一个值给数组, 上面其他程序的作用是定义了在终端提示符下发送文件-W,接收文件-T,写入给数据库-p,读取数据库-z, 其他功能这里暂时不讨论。修改完成后保存文件,退出vi编辑器。 下来在接收端的电脑上的/usr/local/share/gnuradio/example/digital/narrowband目录下新建,两个空白文本文件,然后在终端下输入:“vi 在vi编辑器下打开程序,把第79行到第98行的内容改为global
8、n_rcvd, n_rightglobal n_datadef main(): global n_rcvd, n_right global n_data n_rcvd = 0 n_right = 0 n_data = str() def rx_callback(ok, payload): global n_rcvd, n_right global n_data (pktno,) = (!H, payload0:2) n_data = payload0:4096 f=open(/usr/local/share/gnuradio/examples/digital/,w) (payload) n_r
9、cvd += 1 if ok: n_right += 1 print ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d n_data = %5s % ( ok, pktno, n_rcvd, n_right, n_data)其中,n_data = payload0:4096定义变量n_data用来显示接收的数组和lines数组。 还有上述 f=open(/usr/local/share/gnuradio/examples/digital/narrowband/,w) (payload)两行程序实现了以写入方式打开文本文件,然后把接收的payload
10、数组写入中。保存修改退出vi编辑器后,在发送端的电脑/usr/local/share/gnuradio/example/digital的目录下键入./ -f 900M -n 1900后,电脑显示.表示发送正常,然后在接收端电脑/usr/local/share/gnuradio/example/digital的目录下键入./ -f 900M 电脑显示ok = True pktno = 12598 n_rcvd = 458 n_right = 458 n_data = 1900hellon表示接收正常。用“ctrl+c”键停止接收,打开接收端电脑/usr/local/share/gnuradio/
11、example/digital/的目录下的文件,发觉原先是空文件的,显现hellon显示,表示发送接收正常.说明发送端电脑上的文件中的“hello”字符已经传送到接收端电脑的文件中。 给发送端电脑的文件中添加字符,如把“hello”改成“hello python”,接收端点脑上的文件中显示“hello pythonn,说明同意正常,当中字符的长度超过3571个字节时,接收端就会显示乱码,因为每次程序只能发送低于3571字节的 文本,一旦文本超过了3571字节,发送的信息量太大就会使程序读入数据犯错,接收端就会收到乱码,要传送更多字节的文本就要修改程序,那个问题还在研究,此刻不做讨论。 下面测试
12、一下能不能用其他子板正常发送接收文本。把上面的两个Basic子板换成两个WBX子板,WBX子板上都连有天线。测试后发觉接收端的就会显示乱码和误码。例如 原先发送文本中的hello,接收到的文本却是为welp&*%。造成那个结果的缘故是WBX子板靠天线发送 信号,而LFRX子板是通过屏蔽线传输信号。WBX子板发送无线电后,由于空间中的干扰,无线电信号在传输中会产生衰减和转变,另外的WBX子板接收到的 无线电信号就会和发送的无线电信号不同。要想传输的内容不发生转变,就要修改程序,使每次只发送一个数据,如此接收就不容易产生误码和乱码。修改程序后,把中的payload = data + str(lin
13、es) + chr & 0xff) 改为 payload = str(lines) “作用是只把 lines的值赋给发送函数paload,相当于只发送了lines数组。同时把发送端电脑的文件里的“hello”改成“h”,只发送一个字符h,减少了传送的数据量,现在运行接收./ -f 900M 和发送./ -f 900M发觉接收端电脑的文本显示h;说明接收成功附件:源码#!/usr/bin/python#!/usr/bin/env python# Copyright 2020,2020,2021 Free Software Foundation, Inc.# # This file is part
14、 of GNU Radio# # GNU Radio is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 3, or (at your option)# any later version.# # GNU Radio is distributed in the hope that it will be us
15、eful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.# # You should have received a copy of the GNU General Public License# along with GNU Radio; see the file COPYING. If not,
16、write to# the Free Software Foundation, Inc., 51 Franklin Street,# Boston, MA 02110-1301, USA.# from gnuradio import gr, grufrom gnuradio import blocksfrom gnuradio import eng_notationfrom import eng_optionfrom optparse import OptionParser# From gr-digitalfrom gnuradio import digital# from current d
17、irfrom receive_path import receive_pathfrom uhd_interface import uhd_receiverimport structimport sys#import os#print ()#raw_input(Attach and press enter: )class my_top_block: def _init_(self, demodulator, rx_callback, options): if is not None): # Work-around to get the modulations bits_per_symbol ar
18、gs = (options) symbol_rate = / demodulator(*args).bits_per_symbol() = uhd_receiver, symbol_rate, , , , , , , , = elif is not None): Reading samples from %s.nn % ) = , else: No source defined, pulling samples from null source.nn) = # Set up receive path # do this after for any adjustments to the opti
19、ons that may # occur in the sinks (specifically the UHD sink) = receive_path(demodulator, rx_callback, options) , # oin(),) (,-from-file, default=None, help=input file of samples to demod) (parser, expert_grp) (parser) for mod in (): (expert_grp) (options, args) = () if len(args) != 0: (1) if is Non
20、e: if is None: You must specify -f FREQ or -freq FREQn) (1) # build the graph tb = my_top_block(demods, rx_callback, options) r = () if r != : print Warning: Failed to enable realtime scheduling. () # start flow graph () # wait for it to finishif _name_ = _main_: try: main() except KeyboardInterrupt
21、: pass源码#!/usr/bin/python#!/usr/bin/env python# Copyright 2020,2020,2021 Free Software Foundation, Inc.# # This file is part of GNU Radio# # GNU Radio is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foun
22、dation; either version 3, or (at your option)# any later version.# # GNU Radio is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details
23、.# # You should have received a copy of the GNU General Public License# along with GNU Radio; see the file COPYING. If not, write to# the Free Software Foundation, Inc., 51 Franklin Street,# Boston, MA 02110-1301, USA.# from gnuradio import grfrom gnuradio import blocksfrom gnuradio import eng_notat
24、ionfrom import eng_optionfrom optparse import OptionParser#diaoyong minglinghang chuli canshu# From gr-digitalfrom gnuradio import digital# from current dirfrom transmit_path import transmit_pathfrom uhd_interface import uhd_transmitterimport time, struct, sys#import os #print ()#raw_input(Attach an
25、d press enter)class my_top_block: def _init_(self, modulator, options): if is not None): # Work-around to get the modulations bits_per_symbol args = (options) symbol_rate = / modulator(*args).bits_per_symbol() = uhd_transmitter, symbol_rate, , , , , , , , = elif is not None): Saving samples to %s.nn
26、 % ) = , else: No sink defined, dumping samples to null sink.nn) = # do this after for any adjustments to the options that may # occur in the sinks (specifically the UHD sink) = transmit_path(modulator, options) , # oin(),) (-s, -size, type=eng_float, default=1500, help=set packet size default=%defa
27、ult) (-M, -megabytes, type=eng_float, default=, help=set megabytes to transmit default=%default) (-E,-discontinuous, action=store_true, default=False, help=enable discontinous transmission (bursts of 5 packets) (-W,-from_file,dest=from_file, default=None, help=use intput file for packet contents) (-
28、T,-to_file,dest=to_file, default=None, help=Output file for modulated samples) (-n,-num,type=int,dest=num,default=1000, help=send message) (-p, -pdbk, action=store_true, dest=pdcl, default=False, help=write pdbk data to oracle db) (-z, -zdbk, action=store_true, dest=zdcl, default=False, help=write zdbk data to oracle db) (parser, expert_grp) (parser) for mod in (): (expert_grp) (options, args) = () # if =True: # print pdcl is true # if =True: # print zdcl is true if len(args) != 0: () (1