Skip to content

Commit

Permalink
Fixed QtWindow closing event and restarting plot. Works with Py3 too.
Browse files Browse the repository at this point in the history
  • Loading branch information
Suyash458 committed Feb 23, 2017
1 parent 89072f9 commit 4bd58b3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
28 changes: 15 additions & 13 deletions SocketPlot-Test.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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()

19 changes: 14 additions & 5 deletions SoftOscilloscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4bd58b3

Please sign in to comment.