1from optparse import OptionParser 2 3import Initializer 4from InputHandlerThread import InputHandlerThread 5 6Initializer.init_path() 7 8from gnuradio import blocks 9from gnuradio import gr 10import osmosdr 11 12 13class top_block(gr.top_block): 14 15 def __init__(self, sample_rate, frequency, freq_correction, rf_gain, if_gain, bb_gain, bandwidth, port): 16 gr.top_block.__init__(self, "Top Block") 17 18 self.sample_rate = sample_rate 19 self.rf_gain = rf_gain 20 self.port = port 21 self.if_gain = if_gain 22 self.frequency = frequency 23 self.freq_correction = freq_correction 24 self.bb_gain = bb_gain 25 self.bandwidth = bandwidth 26 27 self.osmosdr_sink_0 = osmosdr.sink( 28 args="numchan=" + str(1) + " " + 'hackrf' 29 ) 30 self.osmosdr_sink_0.set_time_unknown_pps(osmosdr.time_spec_t()) 31 self.osmosdr_sink_0.set_sample_rate(sample_rate) 32 self.osmosdr_sink_0.set_center_freq(frequency, 0) 33 self.osmosdr_sink_0.set_freq_corr(freq_correction, 0) 34 self.osmosdr_sink_0.set_gain(rf_gain, 0) 35 self.osmosdr_sink_0.set_if_gain(if_gain, 0) 36 self.osmosdr_sink_0.set_bb_gain(bb_gain, 0) 37 self.osmosdr_sink_0.set_antenna('', 0) 38 self.osmosdr_sink_0.set_bandwidth(bandwidth, 0) 39 self.blocks_udp_source_0 = blocks.udp_source(gr.sizeof_gr_complex * 1, '127.0.0.1', port, 65536, False) 40 41 self.connect((self.blocks_udp_source_0, 0), (self.osmosdr_sink_0, 0)) 42 43 def get_sample_rate(self): 44 return self.sample_rate 45 46 def set_sample_rate(self, sample_rate): 47 self.sample_rate = sample_rate 48 self.osmosdr_sink_0.set_sample_rate(self.sample_rate) 49 50 def get_rf_gain(self): 51 return self.rf_gain 52 53 def set_rf_gain(self, rf_gain): 54 self.rf_gain = rf_gain 55 self.osmosdr_sink_0.set_gain(self.rf_gain, 0) 56 57 def get_port(self): 58 return self.port 59 60 def set_port(self, port): 61 self.port = port 62 63 def get_if_gain(self): 64 return self.if_gain 65 66 def set_if_gain(self, if_gain): 67 self.if_gain = if_gain 68 self.osmosdr_sink_0.set_if_gain(self.if_gain, 0) 69 70 def get_frequency(self): 71 return self.frequency 72 73 def set_frequency(self, frequency): 74 self.frequency = frequency 75 self.osmosdr_sink_0.set_center_freq(self.frequency, 0) 76 77 def get_freq_correction(self): 78 return self.freq_correction 79 80 def set_freq_correction(self, freq_correction): 81 self.freq_correction = freq_correction 82 self.osmosdr_sink_0.set_freq_corr(self.freq_correction, 0) 83 84 def get_direct_sampling_mode(self): 85 return self.direct_sampling_mode 86 87 def set_direct_sampling_mode(self, direct_sampling_mode): 88 self.direct_sampling_mode = direct_sampling_mode 89 90 def get_channel_index(self): 91 return self.channel_index 92 93 def set_channel_index(self, channel_index): 94 self.channel_index = channel_index 95 96 def get_bb_gain(self): 97 return self.bb_gain 98 99 def set_bb_gain(self, bb_gain): 100 self.bb_gain = bb_gain 101 self.osmosdr_sink_0.set_bb_gain(self.bb_gain, 0) 102 103 def get_bandwidth(self): 104 return self.bandwidth 105 106 def set_bandwidth(self, bandwidth): 107 self.bandwidth = bandwidth 108 self.osmosdr_sink_0.set_bandwidth(self.bandwidth, 0) 109 110 def get_antenna_index(self): 111 return self.antenna_index 112 113 def set_antenna_index(self, antenna_index): 114 self.antenna_index = antenna_index 115 116 117if __name__ == '__main__': 118 parser = OptionParser(usage='%prog: [options]') 119 parser.add_option('-s', '--sample-rate', dest='sample_rate', default=100000) 120 parser.add_option('-f', '--frequency', dest='frequency', default=433000) 121 parser.add_option('-g', '--gain', dest='rf_gain', default=30) 122 parser.add_option('-i', '--if-gain', dest='if_gain', default=30) 123 parser.add_option('-b', '--bb-gain', dest='bb_gain', default=30) 124 parser.add_option('-w', '--bandwidth', dest='bandwidth', default=250000) 125 parser.add_option('-c', '--freq-correction', dest='freq_correction', default=0) 126 parser.add_option('-d', '--direct-sampling', dest='direct_sampling', default=0) 127 parser.add_option('-n', '--channel-index', dest='channel_index', default=0) 128 parser.add_option('-a', '--antenna-index', dest='antenna_index', default=0) 129 parser.add_option('-p', '--port', dest='port', default=1234) 130 131 (options, args) = parser.parse_args() 132 tb = top_block(int(options.sample_rate), int(options.frequency), int(options.freq_correction), int(options.rf_gain), 133 int(options.if_gain), int(options.bb_gain), int(options.bandwidth), int(options.port)) 134 iht = InputHandlerThread(tb) 135 iht.start() 136 tb.start() 137 tb.wait() 138