SerialGraph.py 888 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import time
import numpy as np
import serial

ser=serial.Serial('/dev/ttyUSB0',115200)
app = QtGui.QApplication([])
win = pg.GraphicsWindow()
p1 = win.addPlot()
curve1 = p1.plot()

y1=[]
x1=[]
indx = 0

17

18
def update():
19
    global curve1, indx, y1,x1
20 21 22 23
    while (ser.inWaiting() == 0):
        pass
    readData= float(ser.readline())
    y1.append(readData)
24 25 26 27 28
    print(readData)
    indx+=1
    if len(y1)>99:
       del y1[0]
       indx=100
29
    else:
30
        x1.append(indx)
31 32 33

    curve1.setData(x1,y1)
    p1.setXRange(0, 100, padding=0)
34
    p1.setYRange(0,1023,padding=0)
35 36 37 38 39 40 41 42 43 44 45 46 47
    app.processEvents()

timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)




if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_'):
        QtGui.QApplication.instance().exec_()