Fixed base class name.
[debian/gnuradio] / gnuradio-examples / python / hier / digital / benchmark_tx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005, 2006,2007 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 from gnuradio import gr, gru, modulation_utils
24 from gnuradio import usrp
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from optparse import OptionParser
28
29 import random, time, struct, sys
30
31 # from current dir
32 from transmit_path import transmit_path
33 import fusb_options
34
35 #import os 
36 #print os.getpid()
37 #raw_input('Attach and press enter')
38
39 # /////////////////////////////////////////////////////////////////////////////
40 #                                   main
41 # /////////////////////////////////////////////////////////////////////////////
42
43 def main():
44
45     def send_pkt(payload='', eof=False):
46         return top_block.send_pkt(payload, eof)
47
48     def rx_callback(ok, payload):
49         print "ok = %r, payload = '%s'" % (ok, payload)
50
51     mods = modulation_utils.type_1_mods()
52
53     parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
54     expert_grp = parser.add_option_group("Expert")
55
56     parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
57                       default='gmsk',
58                       help="Select modulation from: %s [default=%%default]"
59                             % (', '.join(mods.keys()),))
60
61     parser.add_option("-s", "--size", type="eng_float", default=1500,
62                       help="set packet size [default=%default]")
63     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
64                       help="set megabytes to transmit [default=%default]")
65     parser.add_option("","--discontinuous", action="store_true", default=False,
66                       help="enable discontinous transmission (bursts of 5 packets)")
67
68     transmit_path.add_options(parser, expert_grp)
69
70     for mod in mods.values():
71         mod.add_options(expert_grp)
72
73     fusb_options.add_options(expert_grp)
74     (options, args) = parser.parse_args ()
75
76     if len(args) != 0:
77         parser.print_help()
78         sys.exit(1)
79
80     if options.tx_freq is None:
81         sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
82         parser.print_help(sys.stderr)
83         sys.exit(1)
84
85     r = gr.enable_realtime_scheduling()
86     if r != gr.RT_OK:
87         print "Warning: failed to enable realtime scheduling"
88
89     # Create an instance of a hierarchical block
90     top_block = transmit_path(mods[options.modulation], options)
91     top_block.start()    
92
93     # generate and send packets
94     nbytes = int(1e6 * options.megabytes)
95     n = 0
96     pktno = 0
97     pkt_size = int(options.size)
98
99     while n < nbytes:
100         send_pkt(struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff))
101         n += pkt_size
102         sys.stderr.write('.')
103         if options.discontinuous and pktno % 5 == 4:
104             time.sleep(1)
105         pktno += 1
106         
107     send_pkt(eof=True)
108     top_block.wait()
109
110 if __name__ == '__main__':
111     try:
112         main()
113     except KeyboardInterrupt:
114         pass