Created
August 18, 2015 18:45
-
-
Save michelp/9fabeb560bb1490e2196 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """\ | |
| pyrtlsdr server | |
| Usage: | |
| net.py <frequency> [options] | |
| net.py (-h | --help) | |
| net.py --version | |
| Options: | |
| -h --help Show this screen. | |
| -v --version Show version. | |
| -l --listen Pull from PUB | |
| -i --client Be a control client | |
| -d --device=<d> Device identifier [default: 0]. | |
| -s --sample-rate=<s> Sample rate in samples per second [default: 1000000]. | |
| -n --num-samples=<n> Number of samples to send per-frame [default: 16384]. | |
| -g --gain=<g> Set gain [default: auto]. | |
| -c --control-port=<c> Control port [default: 22996]. | |
| -p --pub-port=<p> Pub port [default: 22997]. | |
| """ | |
| import gevent | |
| from cPickle import dumps, loads | |
| from rtlsdr import RtlSdr | |
| from zmq import green as zmq | |
| def tune(device, frequency=None, sample_rate=None, gain=None): | |
| if frequency is not None: | |
| device.center_freq = frequency | |
| if sample_rate is not None: | |
| device.sample_rate = sample_rate | |
| if gain is not None: | |
| device.gain = gain | |
| def server(device, context, frequency, sample_rate, gain, num_samples, control_port, pub_port): | |
| tune(device, frequency, sample_rate, gain) | |
| control = context.socket(zmq.REP) | |
| pub = context.socket(zmq.PUB) | |
| control.bind('tcp://127.0.0.1:%s' % control_port) | |
| pub.bind('tcp://127.0.0.1:%s' % pub_port) | |
| def _control(): | |
| while True: | |
| tune(device, **control.recv_json()) | |
| def _pub(): | |
| while True: | |
| samples = device.read_samples(num_samples) | |
| print 'sending: ', len(samples) | |
| pub.send(dumps(samples)) | |
| gevent.joinall([gevent.spawn(_control), gevent.spawn(_pub)]) | |
| if __name__ == '__main__': | |
| from docopt import docopt | |
| args = docopt(__doc__, version='pyrtlsdr net 1.0') | |
| listen = args.pop('--listen') | |
| client = args.pop('--client') | |
| device = int(args.pop('--device')) | |
| frequency = int(args.pop('<frequency>')) | |
| sample_rate = int(args.pop('--sample-rate')) | |
| num_samples = int(args.pop('--num-samples')) | |
| gain = int(args.pop('--gain')) if args['--gain'] != 'auto' else args.pop('--gain') | |
| pub_port = int(args.pop('--pub-port')) | |
| control_port = int(args.pop('--control-port')) if args['--control-port'] else 22996 | |
| context = zmq.Context() | |
| if listen: | |
| sub = context.socket(zmq.SUB) | |
| sub.connect('tcp://127.0.0.1:%s' % pub_port) | |
| sub.setsockopt(zmq.SUBSCRIBE, '') | |
| def _listen(): | |
| while True: | |
| print 'uh' | |
| print len(loads(sub.recv())) | |
| gevent.joinall([gevent.spawn(_listen)]) | |
| elif client: | |
| control = context.socket(zmq.REQ) | |
| control.connect('tcp://127.0.0.1:%s' % control_port) | |
| control.send_json(**args) | |
| else: | |
| device = RtlSdr(device) | |
| server(device, context, frequency, sample_rate, gain, num_samples, control_port, pub_port) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment