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