Hello all, 
I am new to pyqtgraph, and I can plot data from com port in real time, but 
I can't exit the window to run the rest of the code. I've tried to 
implement a while loop += 1 and a set number of "samples" n, to no avail. 
Any suggestions would be highly appretiated 

# -*- coding: utf-8 -*-
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
from numpy import arange, sin, cos, pi
import pyqtgraph as pg
import sys
import serial
import time



def bytes_to_int(bytes):                        # Bytes to integers
    result = 0
    for b in bytes:
        result = result * 256 + int(b)
    return result

class Plot2D():
    def __init__(self):
        self.traces = dict()

        #QtGui.QApplication.setGraphicsSystem('raster')
        self.app = QtGui.QApplication([])
        #mw = QtGui.QMainWindow()
        #mw.resize(800,800)

        self.win = pg.GraphicsWindow(title="SeƱal")
        self.win.resize(1000,600)
        self.win.setWindowTitle('Voltaje')

        # Enable antialiasing for prettier plots
        pg.setConfigOptions(antialias=True)

        self.canvas = self.win.addPlot(title="Medicion")

    def start(self):
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()

    def trace(self,name,dataset_x,dataset_y):
        if name in self.traces:
            self.traces[name].setData(dataset_x,dataset_y)
        else:
            self.traces[name] = self.canvas.plot(pen='y')


val_vol = []
val_time = []
n = 40
count = 0
#i = 0
while (count <= n):
## Start Qt event loop unless running in interactive mode or using pyside.
    if __name__ == '__main__':
        p = Plot2D()
        #i = 0
        count = 0

        def update():
            global p, count
            ser = serial.Serial(port='com11', baudrate=9600)
            rawvol = ser.read()
            vol = bytes_to_int(rawvol)
            val_vol.append(vol)
            val_time.append(time.clock())
            p.trace("sin",val_time,val_vol)
            #i += 0.1
            print(val_vol)
            count += 1

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

        p.start()
        sys.exit()
        print(count)


 

-- 
You received this message because you are subscribed to the Google Groups 
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/a6854a07-2992-4347-af18-f082f8c4cc33%40googlegroups.com.

Reply via email to