I am using Pyserial and PyQtgraph to plot live data. The connection between the device that i am reading the data from (an arduino) and my pc works fine, i mean that i can read the data. The problem comes that when I disconnect the device, data is still drawing in the plot. And, if I let it keep reading, after a while, the plot crashes and i have to start over again.
I was reading some posts and I have found this:
implementing pyqtgraph for live data graphing
So, i think the problem is that in my code, the data is append to a list and then is plot, and that makes it slow and maybe that is why it crashes.
This is my code:
class MyApplication(QtGui.QApplication):
def __init__(self, *args, **kwargs):
super(MyApplication, self).__init__(*args, **kwargs)
self.t = QTime()
self.t.start()
self.data = deque()
self.cnt = 0
self.win = pg.GraphicsWindow()
self.plot = self.win.addPlot(title='Timed data')
self.curve = self.plot.plot()
self.tmr = QTimer()
self.tmr.timeout.connect(self.update)
self.tmr.start(100)
self.cnt = 0
print "Opening port"
self.raw=serial.Serial("com4",9600)
print "Port is open"
def update(self):
line = self.raw.read()
ardString = map(ord, line)
for number in ardString:
numb = float(number/77.57)
self.cnt += 1
x = self.cnt/20
self.data.append({'x': x , 'y': numb})
x = [item['x'] for item in self.data]
y = [item['y'] for item in self.data]
self.curve.setData(x=x, y=y)
How can i modify my code to use the code written in the post of above? Or how can I draw the data that is coming without append it to a list?
Sorry, but i am new with PyQtGraph
and I am a confused right now. HOpe you can help me.
---------- EDIT ---------
I´ve tried a simpler code like this one:
import serial
import numpy
import matplotlib.pyplot as plt
print "Opening port"
port = "com4"
arduinoData = serial.Serial(port, 9600)
while True:
if arduinoData.inWaiting()>0:
print "Reading data"
arduinoString = arduinoData.read(arduinoData.inWaiting())
bytes = map(ord, arduinoString)
for byte in bytes:
print byte
else:
print "There is no data"
So, after it shows the data in the command prompt, I disconnect the device, and I can see that the data are still showing for a few seconds. Then, the "There is no data" text appears. So, what could be the problem? I know that, it is buffered data, but it seems to me that it is the same that is happening with the other code.
---------- EDIT 2 ---------
I finally accomplish to do what i need. Thank you @busfault for all your help and patience.
This is the code for the update
method:
def update(self):
line = self.raw.read([1])
ardString = map(ord, line)
for number in ardString:
numb = float(number/77.57)
self.data.append(numb)
self.yData.append(numb)
if len (self.yData)>300 :
self.yData = []
self.raw.flush()
self.curve.setData(self.yData)
What i do now is that the data goes to two different lists: self.yData
and self.data
.
In self.yData
I can only append up to 300 data items(this is random, I could have chosen 500), and then I flush all the data and "clear" the list to start again.
Whit this I can see live data with no delay and save all of them in another place safe.