Regarding connecting blocks

Hi
I am new to python and I need to know about adding new blocks.
I modified the benchmark_tx and rx.py files, so that the receiver will
get the file from sender and store it in a txt file which is passed as
an arguement. I also added few lines of code so that the receiver
calculates the number of error bits in each packet it receives.

The program is as follows:
#!/usr/bin/env python

Copyright 2005,2006,2007,2009 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 Foundation; 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.

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 gr, gru, modulation_utils
from gnuradio import usrp
from gnuradio import eng_notation
from gnuradio.eng_option import eng_option
from optparse import OptionParser

import random
import struct
import sys
import time
import binascii

from current dir

import usrp_receive_path
import operator
#import os
#print os.getpid()
#raw_input('Attach and press enter: ')

class my_top_block(gr.top_block):
def init(self, demodulator, rx_callback, options):
gr.top_block.init(self)

Set up receive path

self.rxpath = usrp_receive_path.usrp_receive_path(demodulator,
rx_callback, options)

self.connect(self.rxpath)

/////////////////////////////////////////////////////////////////////////////

main

/////////////////////////////////////////////////////////////////////////////

global n_rcvd, n_right

def main():
global n_rcvd, n_right, bi, pattern, a, x, t, coun, tbin, i, l, abin

n_rcvd = 0
n_right = 0
coun=l=0
#FILE = open(“payload.dat”, “a”)

def rx_callback(ok, payload):
global n_rcvd, n_right, coun, l
(pktno,) = struct.unpack(’!H’, payload[0:2])
n_rcvd += 1
if ok:
n_right += 1

print “ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d” % (
ok, pktno, n_rcvd, n_right)
a =ord(payload[2:options.size])
abin=bin(a)[2:]
FILE.write(payload[2:options.size])
#a=ord(payload[2:options.size])
print ('a = ’ , a)
print(‘abin=’,abin)
x =ord(‘a’)
#print(‘x=b=’, x)
t= operator.xor(a,x)
#print(“t=”, t)
tbin= bin(t)[2:]
#print('tbin= ',tbin)
i=0
l=len(abin)+l

while i<len(tbin):
if tbin[i]==‘1’:
coun=coun+1
i=i+1
print(“total error bits so far=”,coun)
print(“total bits=”, l)
b=float(coun)/float(l)
print(“b=”,b)
demods = modulation_utils.type_1_demods()

Create Options Parser:

parser = OptionParser (option_class=eng_option,
conflict_handler=“resolve”)
expert_grp = parser.add_option_group(“Expert”)

parser.add_option("-m", “–modulation”, type=“choice”,
choices=demods.keys(),
default=‘gmsk’,
help=“Select modulation from: %s [default=%%default]”
% (’, '.join(demods.keys()),))
parser.add_option("-s", “–size”, type=“eng_float”, default=1500,
help=“set packet size [default=%default]”)

usrp_receive_path.add_options(parser, expert_grp)

for mod in demods.values():
mod.add_options(expert_grp)

(options, args) = parser.parse_args ()

filename = args[0];
FILE = open(filename, “a”)

if len(args) != 1:
parser.print_help(sys.stderr)
sys.exit(1)

if options.rx_freq is None:
sys.stderr.write(“You must specify -f FREQ or --freq FREQ\n”)
parser.print_help(sys.stderr)
sys.exit(1)

build the graph

tb = my_top_block(demods[options.modulation], rx_callback, options)

r = gr.enable_realtime_scheduling()
if r != gr.RT_OK:
print “Warning: Failed to enable realtime scheduling.”

tb.start() # start flow graph

tb.wait() # wait for it to finish
FILE.close()

if name == ‘main’:
try:
main()
except KeyboardInterrupt:
pass

I am now trying to find the received signal power in receiver node and
I found many websites mention this particulat piece of code to be
added to the program:

alpha = 0.001
thresh = 30 # in dB, will have to adjust
self.probe = gr.probe_avg_mag_sqrd_cf(thresh,alpha)
self.power_sink = gr.file_sink(gr.sizeof_float, “rxpower.dat”)
self.connect(self.probe, self.power_sink)

But I am unable to connect this probe to the rxpath. whenever i add code
like:

self.connect(self,self.probe) # connects the input block to
the power probe
self.connect(self.probe, self.rxpath)# connects the power block
to the rece

I get error like,
ValueError: port number 0 exceeds max of (none)

I think this is something related to the number of ports in some block
am trying to connect. Can anyone help me with this?

On Mon, Jun 27, 2011 at 4:41 PM, shantharam balasubramanian <
[email protected]> wrote:

Copyright 2005,2006,2007,2009 Free Software Foundation, Inc.

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

from gnuradio import eng_notation
import operator

def main():
n_rcvd += 1
print(‘abin=’,abin)
if tbin[i]==‘1’:
conflict_handler=“resolve”)
usrp_receive_path.add_options(parser, expert_grp)
parser.print_help(sys.stderr)

try:
thresh = 30 # in dB, will have to adjust
to the rece

I get error like,
ValueError: port number 0 exceeds max of (none)

I think this is something related to the number of ports in some block
am trying to connect. Can anyone help me with this?

The object self.rx_path is from the class receiver_path (in
receive_path.py)
and has no outputs. You can see this in receive_path’s io_signature
line,
where the second line (the output) is set to 0.

What you can do, though, is connect an internal block from the receive
path
to your probe. Such as self.rx_path.channel_filter.

Tom