hi.
i wanted to simulate a transceiver which receives some data and then
transmits its own and the data it got.
I have been modifying then benchmark_tx and rx program to do this and
I was able to simulate the transceiver I mentioned, by adding the
sender code in the "def_rxcallback" function.
I dont know if thats the right way to do, but it worked for me, after
importing and adding some code from benchmark_tx program.
But I am now trying to find a way to parse options for both the sender
and receiver. Since I have been modifying the benchmark_rx program for
this, the options I pass are only used for the receiver program alone.
So I am not able to send and receive using two different frequencies
or two different packet sizes for each.
Can anyone please help me with this?. I have attached my code here, so
that if anyone is free to go through it, can help me with the mistakes
I made.
thank you
#!/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
# from current dir
import usrp_transmit_path
import usrp_receive_path
#import os
#print os.getpid()
#raw_input('Attach and press enter')
class my_top_block(gr.top_block):
def __init__(self, modulator, demodulator, rx_callback, options):
gr.top_block.__init__(self)
self.rxpath = usrp_receive_path.usrp_receive_path(demodulator, rx_callback, options)
self.txpath = usrp_transmit_path.usrp_transmit_path(modulator, options)
self.connect(self.rxpath)
self.connect(self.txpath)
# /////////////////////////////////////////////////////////////////////////////
# main
# /////////////////////////////////////////////////////////////////////////////
global n_rcvd, n_right
def main():
global n_rcvd, n_right, re, a, l, coun
def send_pkt(payload='', eof=False):
return tb.txpath.send_pkt(payload, eof)
def carrier_sensed(self):
"""
Return True if the receive path thinks there's carrier
"""
return self.rxpath.carrier_sensed()
a=0
n_rcvd=0
n_right=0
coun=l=0
def rx_callback(ok, payload):
global n_rcvd, n_right, coun, l
(ide,pktno,) = struct.unpack('!cH', payload[0:3])
n_rcvd += 1
if ok:
n_right += 1
print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d ide=%1c" % (
ok, pktno, n_rcvd, n_right, ide)
if(ide == 'a' or ide == 'c'):
print("payload=",payload)
a =ord(payload[3:options.size])
abin=bin(a)[2:]
FILE1.write(payload[3: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)
ber1=float(coun)/float(l)
print("ber1=",ber1)
#this s the block seen in tx.py, i added it here and it seems to work fine except for the last pkt. sid, please remove the 're' variable and add #the condtion like, (if pkt header=last pkt header)
if ide=='c':
nbytes=1250
n = 0
pktno = 0
pkt_size = int(options.size)
# c='a'
# start_time=0
# last_time=0
while n < nbytes:
print("in while loop")
if options.from_file is None:
data = (pkt_size - 2) * chr(pktno & 0xff)
else:
data = source_file.read(pkt_size - 2)
if data == '':
break;
# if carrier_sensed():
# re=1
# if re==1:
print("before payload packing")
payload = struct.pack('!H', pktno & 0xffff) + data
send_pkt(payload)
n += len(payload)
a=a+1
sys.stderr.write('.')
if options.discontinuous and pktno % 5 == 4:
time.sleep(1)
pktno += 1
send_pkt(eof=True)
print("pkts hopefully sent", a)
demods = modulation_utils.type_1_demods()
mods = modulation_utils.type_1_mods()
parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
expert_grp = parser.add_option_group("Expert")
parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
default='gmsk',
help="Select modulation from: %s [default=%%default]"
% (', '.join(mods.keys()),))
parser.add_option("-s", "--size", type="eng_float", default=1500,
help="set packet size [default=%default]")
parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
help="set megabytes to transmit [default=%default]")
parser.add_option("","--discontinuous", action="store_true", default=False,
help="enable discontinous transmission (bursts of 5 packets)")
parser.add_option("","--from-file", default=None,
help="use file for packet contents")
# expert_grp.add_option("", "--rx-freq", type="eng_float", default=None,
# help="set Rx frequency to FREQ [default=%default]", metavar="FREQ")
# expert_grp.add_option("", "--tx-freq", type="eng_float", default=None,
# help="set transmit frequency to FREQ [default=%default]", metavar="FREQ")
usrp_receive_path.add_options(parser, expert_grp)
usrp_transmit_path.add_options(parser, expert_grp)
for mod in demods.values():
mod.add_options(expert_grp)
for mod in mods.values():
mod.add_options(expert_grp)
(options, args) = parser.parse_args ()
filename1 =args[0];
FILE1 = open(filename1, "a")
# if len(args) !=1:
# parser.print_help()
# sys.exit(1)
if options.tx_freq is None:
sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
parser.print_help(sys.stderr)
sys.exit(1)
if options.from_file is not None:
source_file = open(options.from_file, 'r')
tb = my_top_block(mods[options.modulation], demods[options.modulation], rx_callback, options)
print("no of pkts=",a)
r = gr.enable_realtime_scheduling()
if r != gr.RT_OK:
print "Warning: failed to enable realtime scheduling"
tb.start() # start flow graph
# generate and send packets
tb.wait() # wait for it to finish
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
_______________________________________________
Discuss-gnuradio mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/discuss-gnuradio