Commit 9534feb524c6af8580a6466707b4841ca3834be1
0 parents
Exists in
master
first commit
Showing 1 changed file with 123 additions and 0 deletions Side-by-side Diff
FPC1000_viewer.py
| ... | ... | @@ -0,0 +1,123 @@ |
| 1 | +#!/usr/bin/env python | |
| 2 | + | |
| 3 | +# -*- coding: utf-8 -*- | |
| 4 | + | |
| 5 | +from pyqtgraph.Qt import QtGui, QtCore | |
| 6 | +import argparse, numpy, pyqtgraph, socket, sys | |
| 7 | + | |
| 8 | +#============================================================================== | |
| 9 | + | |
| 10 | +# Default filename | |
| 11 | +IP = '192.168.0.11' | |
| 12 | +PORT = 5555 | |
| 13 | + | |
| 14 | +#============================================================================== | |
| 15 | + | |
| 16 | +def parse(): | |
| 17 | + """ | |
| 18 | + Specific parsing procedure for Allan Deviation plotting tool. | |
| 19 | + :returns: populated namespace (parser) | |
| 20 | + """ | |
| 21 | + parser = argparse.ArgumentParser(description = 'R&S FPC1000 spectrum viewer', | |
| 22 | + epilog = 'Example: \'./FPC1000_viewer.py -ip \'192.168.0.2\' -p 1234\' plot current spectrum from given device') | |
| 23 | + | |
| 24 | + parser.add_argument('-ip', | |
| 25 | + action='store', | |
| 26 | + dest='ip', | |
| 27 | + default=IP, | |
| 28 | + help='device IP (default '+IP+')') | |
| 29 | + | |
| 30 | + parser.add_argument('-p', | |
| 31 | + action='store', | |
| 32 | + dest='port', | |
| 33 | + default=PORT, | |
| 34 | + help='device port (default '+str(PORT)+')') | |
| 35 | + | |
| 36 | + args = parser.parse_args() | |
| 37 | + return args | |
| 38 | + | |
| 39 | +#============================================================================== | |
| 40 | + | |
| 41 | +def send(sock, command): | |
| 42 | + sock.send("%s\n"%command) | |
| 43 | + | |
| 44 | +#============================================================================== | |
| 45 | + | |
| 46 | +def read(sock): | |
| 47 | + ans = '' | |
| 48 | + nb_data_list = [] | |
| 49 | + nb_data = '' | |
| 50 | + try: | |
| 51 | + while ans != '\n': | |
| 52 | + ans = sock.recv(1) | |
| 53 | + nb_data_list.append(ans) # Return the number of data | |
| 54 | + list_size = len(nb_data_list) | |
| 55 | + for j in range (0, list_size): | |
| 56 | + nb_data = nb_data+nb_data_list[j] | |
| 57 | + return nb_data | |
| 58 | + except socket.timeout: | |
| 59 | + print "Socket timeout error when reading." | |
| 60 | + | |
| 61 | +#============================================================================== | |
| 62 | + | |
| 63 | +def get_spectrum(sock): | |
| 64 | + send(sock, 'FREQ:STAR?') | |
| 65 | + x1 = read(sock) | |
| 66 | + x1 = float(x1.replace('\n','')) | |
| 67 | + send(sock, 'FREQ:STOP?') | |
| 68 | + x2 = read(sock) | |
| 69 | + x2 = float(x2.replace('\n','')) | |
| 70 | + send(sock, 'TRAC?') | |
| 71 | + y = read(sock) | |
| 72 | + y = numpy.array(y.replace('\n','').split(','), dtype='float') | |
| 73 | + x = numpy.linspace(x1, x2, len(y)) | |
| 74 | + return (x, y) | |
| 75 | + | |
| 76 | +#============================================================================== | |
| 77 | + | |
| 78 | +def main(): | |
| 79 | + """ | |
| 80 | + Main script | |
| 81 | + """ | |
| 82 | + # Parse command line | |
| 83 | + args = parse() | |
| 84 | + # ip | |
| 85 | + ip = str(args.ip) | |
| 86 | + # port | |
| 87 | + port = int(args.port) | |
| 88 | + | |
| 89 | + try: | |
| 90 | + sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM,socket.IPPROTO_TCP) | |
| 91 | + sock.settimeout(10.0) | |
| 92 | + sock.connect((ip, port)) | |
| 93 | + | |
| 94 | + app = QtGui.QApplication([]) | |
| 95 | + | |
| 96 | + win = pyqtgraph.GraphicsWindow(title='Basic FPC1000 viewer (%s:%i)'%(ip, port)) | |
| 97 | + | |
| 98 | + pyqtgraph.setConfigOptions(antialias=True) | |
| 99 | + | |
| 100 | + global curve, ptr, p1 | |
| 101 | + p1 = win.addPlot(title="Spectrum") | |
| 102 | + curve = p1.plot(pen='y') | |
| 103 | + ptr = 0 | |
| 104 | + def update(): | |
| 105 | + global curve, ptr, p1 | |
| 106 | + (x_data, y_data) = get_spectrum(sock) | |
| 107 | + curve.setData(x = x_data, y = y_data) | |
| 108 | + if ptr == 0: | |
| 109 | + p1.enableAutoRange('xy', False) ## stop auto-scaling after the first data set is plotted | |
| 110 | + ptr += 1 | |
| 111 | + timer = QtCore.QTimer() | |
| 112 | + timer.timeout.connect(update) | |
| 113 | + timer.start(500) | |
| 114 | + | |
| 115 | + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): | |
| 116 | + QtGui.QApplication.instance().exec_() | |
| 117 | + | |
| 118 | + except: | |
| 119 | + pass | |
| 120 | + | |
| 121 | + ## Start Qt event loop unless running in interactive mode or using pyside. | |
| 122 | +if __name__ == '__main__': | |
| 123 | + main() |