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