merged trondeau/digital-wip2 r4193:4730 into trunk - improves digital receiver and...
[debian/gnuradio] / gnuradio-examples / python / hier / digital / benchmark_tx.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005, 2006 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 2, 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 class my_graph(gr.hier_block2):
40     def __init__(self, mod_class, options):
41         gr.hier_block2.__init__(self, "my_graph",
42                                 gr.io_signature(0,0,0), # Input signature
43                                 gr.io_signature(0,0,0)) # Output signature
44         self.txpath = transmit_path(mod_class, options)
45         self.define_component("txpath", self.txpath)
46
47
48 # /////////////////////////////////////////////////////////////////////////////
49 #                                   main
50 # /////////////////////////////////////////////////////////////////////////////
51
52 def main():
53
54     def send_pkt(payload='', eof=False):
55         return top_block.txpath.send_pkt(payload, eof)
56
57     def rx_callback(ok, payload):
58         print "ok = %r, payload = '%s'" % (ok, payload)
59
60     mods = modulation_utils.type_1_mods()
61
62     parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
63     expert_grp = parser.add_option_group("Expert")
64
65     parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
66                       default='gmsk',
67                       help="Select modulation from: %s [default=%%default]"
68                             % (', '.join(mods.keys()),))
69
70     parser.add_option("-s", "--size", type="eng_float", default=1500,
71                       help="set packet size [default=%default]")
72     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
73                       help="set megabytes to transmit [default=%default]")
74     parser.add_option("","--discontinuous", action="store_true", default=False,
75                       help="enable discontinous transmission (bursts of 5 packets)")
76
77     transmit_path.add_options(parser, expert_grp)
78
79     for mod in mods.values():
80         mod.add_options(expert_grp)
81
82     fusb_options.add_options(expert_grp)
83     (options, args) = parser.parse_args ()
84
85     if len(args) != 0:
86         parser.print_help()
87         sys.exit(1)
88
89     if options.tx_freq is None:
90         sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
91         parser.print_help(sys.stderr)
92         sys.exit(1)
93
94     r = gr.enable_realtime_scheduling()
95     if r != gr.RT_OK:
96         print "Warning: failed to enable realtime scheduling"
97
98     # Create an instance of a hierarchical block
99     top_block = my_graph(mods[options.modulation], options)
100     
101     # Create an instance of a runtime, passing it the top block
102     runtime = gr.runtime(top_block)
103     runtime.start()
104
105     # generate and send packets
106     nbytes = int(1e6 * options.megabytes)
107     n = 0
108     pktno = 0
109     pkt_size = int(options.size)
110
111     while n < nbytes:
112         send_pkt(struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff))
113         n += pkt_size
114         sys.stderr.write('.')
115         if options.discontinuous and pktno % 5 == 4:
116             time.sleep(1)
117         pktno += 1
118         
119     send_pkt(eof=True)
120     runtime.wait()
121
122 if __name__ == '__main__':
123     try:
124         main()
125     except KeyboardInterrupt:
126         pass