From 4bd58b34bfd8b46e171f8670028bab540dc9e0e9 Mon Sep 17 00:00:00 2001 From: Suyash458 Date: Thu, 23 Feb 2017 13:12:41 +0530 Subject: [PATCH] Fixed QtWindow closing event and restarting plot. Works with Py3 too. --- SocketPlot-Test.py | 28 +++++++++++++++------------- SoftOscilloscope.py | 19 ++++++++++++++----- readme.md | 4 ++-- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/SocketPlot-Test.py b/SocketPlot-Test.py index 1c7d52a..d0c076a 100644 --- a/SocketPlot-Test.py +++ b/SocketPlot-Test.py @@ -1,6 +1,8 @@ import asyncore, socket, time import numpy as np from collections import deque +from SoftOscilloscope import SocketClientPlot +from multiprocessing import Process data = np.linspace(-np.pi, np.pi, 50) @@ -14,31 +16,31 @@ def __init__(self, address): def handle_accept(self): global data - client,addr = self.accept() - print "Connected to " + str(addr) + client, addr = self.accept() + print("Connected to " + str(addr)) while(1): try: - a = round(50 * np.sin(data[0]), 3) - b = round(20*data[0], 3) - client.send(str(a) + "," + str(b) + '\n') + a = str(round(50 * np.sin(data[0]), 3)) + b = str(round(20*data[0], 3)) + client.send((a + "," + b + '\n').encode()) data = np.roll(data, 1) time.sleep(0.05) - except Exception, message: - print message - self.close() - break + except Exception as e: + print(e) + return def handle_close(self): self.close() - print "Closing server." + print("Closing server.") + if __name__ == '__main__': address = ('0.0.0.0',5000) server = Server(address) try: - print "Server listening on " + str(address) - asyncore.loop(0.2,use_poll = True) + print("Server listening on " + str(address)) + asyncore.loop(0.2, use_poll=True) except KeyboardInterrupt: - print "Closing server." + print("Closing server.") server.close() \ No newline at end of file diff --git a/SoftOscilloscope.py b/SoftOscilloscope.py index 802b6b3..86d45e9 100644 --- a/SoftOscilloscope.py +++ b/SoftOscilloscope.py @@ -2,20 +2,27 @@ from pyqtgraph.Qt import QtGui, QtCore import pyqtgraph as pg import numpy as np +import signal class BasePlot(object): def __init__(self, stream, **kwargs): self.stream = stream - self.app = QtGui.QApplication([]) + try: + self.app = QtGui.QApplication([]) + except RuntimeError: + self.app = QtGui.QApplication.instance() self.view = pg.GraphicsView() self.layout = pg.GraphicsLayout(border=(100,100,100)) + self.view.closeEvent = self.handle_close_event + self.layout.closeEvent = self.handle_close_event self.view.setCentralItem(self.layout) self.view.show() - self.view.setWindowTitle('pyqtgraph example: GraphicsLayout') + self.view.setWindowTitle('Software Oscilloscope') self.view.resize(800,600) self.plot_list = [] def open_stream(self): + print("Opening Stream") self.stream.open() def close_stream(self): @@ -24,10 +31,13 @@ def close_stream(self): if hasattr(self.stream, 'flushOutput'): self.stream.flushOutput() self.stream.close() - print "Stream closed" + print("Stream closed") def handle_close_event(self, event): + print("Here") self.close_stream() + self.app.exit() + print("App exited") def plot_init(self): for i in xrange(20): @@ -58,7 +68,7 @@ def start(self): timer.timeout.connect(self.update) timer.start(0) if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): - QtGui.QApplication.instance().exec_() + self.app.exec_() class SerialPlot(BasePlot): def __init__(self, com_port, baud_rate, **kwargs): @@ -70,7 +80,6 @@ def __init__(self, com_port, baud_rate, **kwargs): class SocketClientPlot(BasePlot): def __init__(self, address, port, **kwargs): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.socket.settimeout(5) self.socket_params = (address, port) self.socket.connect((address, port)) self.stream = self.socket.makefile() diff --git a/readme.md b/readme.md index 6c95f93..15b74ed 100644 --- a/readme.md +++ b/readme.md @@ -1,9 +1,9 @@ # Software Oscilloscope -An ongoing python project which takes in data from any stream(Serial port, TCP socket or any generic stream) and plot it in real time using PyQtGraph. The stream must implement open(), close() and readline() methods +A python project which takes in data from any stream(Serial port, TCP socket or any generic stream) and plots it in real time using PyQtGraph. The stream must implement open(), close() and readline() methods to work with the package. ## Installation -* Requires Python 2 +* Works with Python 2/3 * Clone the repo or download the zip * Install VC++ for Python from [here](https://www.microsoft.com/en-in/download/details.aspx?id=44266) * `cd` to the folder