Pyqtgraph and Pyserial

128 views
Skip to first unread message

WILMER CONTRERAS SEPULVEDA

unread,
Jul 6, 2019, 7:04:00 AM7/6/19
to pyqtgraph
Hi everyone

I am Wilmer C, i am trying to plot some data which come from a serial port (I send it through Bluetooth using an Bluetooth HC05 and an arduino), but my code doesn't work. 

I am trying to plot in in real time, the frecuency of the data is about 800Hz, I design an easy function which take the data from the serial port and send it to the update funcion whics is in charge to plot, but sometime the graph just doesn't plot nothing and sometimes it plot something but get block when are more than 20000 points.

I receive the data from the serial port in a string form as: "S1: 34 S2: 345 S3: 245", for this reason i make the things that appear in the function

I leave the program here, and if something can help me, please don't hesitate.


from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time
import serial

app = QtGui.QApplication([])

p = pg.plot()
p.setWindowTitle('live plot from serial')
curve = p.plot()

data = [0]
raw=serial.Serial("COM8",115200)
def leer():
    while True:
        line = raw.readline()
        line=str(line)
        line=line.split(' ')
        if len(line)>4:
            line=line[2]
            line=int(line)
            return(line)
        else:
            continue
    
def update():
    global curve, data
    data.append(int(line))
    xdata = np.array(data, dtype='float64')
    curve.setData(xdata)
    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_VERSION'):
        QtGui.QApplication.instance().exec_()
raw.close()


Thank you so much!

vas...@gmail.com

unread,
Jul 6, 2019, 12:36:44 PM7/6/19
to pyqt...@googlegroups.com
Hi.

1) you are not calling your leer() function anywhere.
2) it is much better to make an application with threads (but maybe just fix for #1 will resolve the problem)


--
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 pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/998c60d2-1cab-4aaf-8e3c-06ceb9ebc42d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marc Le Roy

unread,
Jul 6, 2019, 1:16:23 PM7/6/19
to pyqtgraph
I would add that this line of code is highly inefficient since a new array of increasing size is created at each update:

xdata = np.array(data, dtype='float64')

I would allocate a large array at initialization, and if required increase its size using numpy.ndarray.resize with a strategy avoiding to do it too frequently.

Marc

vas...@gmail.com

unread,
Jul 6, 2019, 4:29:50 PM7/6/19
to pyqt...@googlegroups.com
Hi again,

I have no your serial device, but your app should looks like:
import pyqtgraph as pg
import random

app = pg.Qt.QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('live plot from random')

curve = p.plot()
data = [0]

while p.isVisible():
    line = random.randint(0, 255)    
    data.append(int(line))
    xdata = np.array(data, dtype='int32')
    curve.setData(xdata)
    app.processEvents()




So you could start with this code:
import numpy as np
import pyqtgraph as pg
import serial

app = pg.Qt.QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('live plot from serial')
curve = p.plot()
data = [0]
raw=serial.Serial("COM8",115200)

while p.isVisible():

    line = raw.readline()
    line=str(line)
    line=line.split(' ')
    if len(line)>4:
        line=line[2]
        line=int(line)
    else:
        continue  
   
    data.append(int(line))
    xdata = np.array(data, dtype='int32')
    curve.setData(xdata)
    app.processEvents()



Next thing is probably to implement Marc's idea of initialization of a larger array.

Cheers,
Vasilije



--
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 pyqtgraph+...@googlegroups.com.

WILMER CONTRERAS SEPULVEDA

unread,
Jul 6, 2019, 6:12:27 PM7/6/19
to pyqt...@googlegroups.com

image.pngHi Guys, how are you?

I am so sorry, this is the new version of my code, I am trying to plot just one graph with the data which come from the serial port (PORT8),
This is the code:

from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph.ptime import time

import numpy as np
import pyqtgraph as pg

import serial

app = QtGui.QApplication([])


p = pg.plot()
p.setWindowTitle('live plot from serial')
curve = p.plot()
data = [0]
raw=serial.Serial("COM8",115200)

def leer():
    while True:

        line = raw.readline()
        line=str(line)
        line=line.split(' ')
        if len(line)>4:
            line=line[2]
            line=int(line)
            return(line)
        else:
            continue
   
def update():
    global curve, data
    data.append(int(leer()))
    curve.setData(data)
    app.processEvents()

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


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

And I always get the same answer from the program: 
image.png

For more options, visit https://groups.google.com/d/optout.


--
Wilmer Contreras Sepulveda
Grupo de Investigación en Desarrollo de Microelectronica Aplicada
Universidad Francisco de Paula Santander


Virus-free. www.avast.com

vas...@gmail.com

unread,
Jul 6, 2019, 7:07:01 PM7/6/19
to pyqt...@googlegroups.com
Nobody has the com8 device to try your code. Just start to make the app it this way, please:

import numpy as np
import pyqtgraph as pg
import serial

app = pg.Qt.QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('live plot from serial')
curve = p.plot()

data = [0]
raw=serial.Serial("COM8",115200)

while p.isVisible():
    line = raw.readline()
    line=str(line)
    line=line.split(' ')
    if len(line)>4:
        line=line[2]
        line=int(line)
    else:
        continue  
   
    data.append(line)

    xdata = np.array(data, dtype='int32')
    curve.setData(xdata)
    app.processEvents() 

WILMER CONTRERAS SEPULVEDA

unread,
Jul 6, 2019, 8:24:58 PM7/6/19
to pyqt...@googlegroups.com
Hi, I did it, but is still not working:

image.png

Virus-free. www.avast.com


For more options, visit https://groups.google.com/d/optout.

vas...@gmail.com

unread,
Jul 6, 2019, 8:29:50 PM7/6/19
to pyqt...@googlegroups.com
Now is definitely problem in reading and converting the string from the device.
Add new line after "line=str(line)":
print("line received:", line)
and you could see is your string properly formatted

WILMER CONTRERAS SEPULVEDA

unread,
Jul 6, 2019, 8:51:20 PM7/6/19
to pyqt...@googlegroups.com
Huumm, I don't know because I receive an string from the function (Somethin like that:  b'39 727 992 1017\r\n') I use the method split(' ') to separate it by space and after I convert it to integer with int(), so from a function I can take the numbers to send to the pyqtgraph.

But I don't know what appends it doesn't work

Virus-free. www.avast.com


For more options, visit https://groups.google.com/d/optout.

vas...@gmail.com

unread,
Jul 6, 2019, 9:10:53 PM7/6/19
to pyqt...@googlegroups.com
Make a lots your own debug (print) lines and you will see where is the problem. Also you could try to print data, or xdata variable. When there is not print, application for some reason stops and wait for something, as:

import numpy as np
import pyqtgraph as pg
import serial

app = pg.Qt.QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('live plot from serial')
curve = p.plot()

data = [0]
raw=serial.Serial("COM8",115200)

while p.isVisible():
    line = raw.readline()
    print("raw line:", line)
    line=str(line)
    print("str line:", line)
    line=line.split(' ')
    print("splitted line:", line)
    if len(line)>4:
        print("line>4:", line)
        line=line[2]
        print("line[2]:", line)
        line=int(line)
        print("int(line)", line)
    else:
        print("line<4:", line)  
   
    data.append(int(line))
    xdata = np.array(data, dtype='float64')
    curve.setData(xdata)
    print("now will refresh the plot")
    app.processEvents()  

       

 
       


WILMER CONTRERAS SEPULVEDA

unread,
Jul 6, 2019, 9:40:39 PM7/6/19
to pyqt...@googlegroups.com
Hi, I modified a few your code in order to work:
import numpy as np
import pyqtgraph as pg
import serial

app = pg.Qt.QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('live plot from serial')
curve = p.plot()

data = [0]
raw=serial.Serial("COM8",115200)

while p.isVisible():
    line = raw.readline()
    print("raw line:", line)
    line=str(line)
    print("str line:", line)
    line=line.split(' ')
    print("splitted line:", line)
    if len(line)>4:
        print("line>4:", line)
        line=line[2]
        print("line[2]:", line)
        line=int(line)
        print("int(line)", line)
        data.append(int(line))
        xdata = np.array(data, dtype='float64')
        curve.setData(xdata)
        print("now will refresh the plot")
        app.processEvents()  
    else:
        print("line<4:", line)  
   
        
And this is the result (still without work):

image.png

Virus-free. www.avast.com


For more options, visit https://groups.google.com/d/optout.

WILMER CONTRERAS SEPULVEDA

unread,
Jul 6, 2019, 9:44:44 PM7/6/19
to pyqt...@googlegroups.com
But, I don't know why the line is less than four if the function line.split(' ') through four values?

This is the all result:

===================== RESTART: C:\python\example2222.py =====================
raw line: b'1010\r\n'
str line: b'1010\r\n'
splitted line: ["b'1010\\r\\n'"]
line<4: ["b'1010\\r\\n'"]
raw line: b'46 725 1010 1010\r\n'
str line: b'46 725 1010 1010\r\n'
splitted line: ["b'46", '725', '1010', "1010\\r\\n'"]
line<4: ["b'46", '725', '1010', "1010\\r\\n'"]
raw line: b'47 728 1014 1013\r\n'
str line: b'47 728 1014 1013\r\n'
splitted line: ["b'47", '728', '1014', "1013\\r\\n'"]
line<4: ["b'47", '728', '1014', "1013\\r\\n'"]
raw line: b'50 726 1014 1012\r\n'
str line: b'50 726 1014 1012\r\n'
splitted line: ["b'50", '726', '1014', "1012\\r\\n'"]
line<4: ["b'50", '726', '1014', "1012\\r\\n'"]
raw line: b'29 728 1014 1018\r\n'
str line: b'29 728 1014 1018\r\n'
splitted line: ["b'29", '728', '1014', "1018\\r\\n'"]
line<4: ["b'29", '728', '1014', "1018\\r\\n'"]
raw line: b'32 729 1018 1016\r\n'
str line: b'32 729 1018 1016\r\n'
splitted line: ["b'32", '729', '1018', "1016\\r\\n'"]
line<4: ["b'32", '729', '1018', "1016\\r\\n'"]
raw line: b'39 732 1023 1015\r\n'
str line: b'39 732 1023 1015\r\n'
splitted line: ["b'39", '732', '1023', "1015\\r\\n'"]
line<4: ["b'39", '732', '1023', "1015\\r\\n'"]
raw line: b'27 735 1015 1011\r\n'
str line: b'27 735 1015 1011\r\n'
splitted line: ["b'27", '735', '1015', "1011\\r\\n'"]
line<4: ["b'27", '735', '1015', "1011\\r\\n'"]
raw line: b'42 730 1016 1017\r\n'
str line: b'42 730 1016 1017\r\n'
splitted line: ["b'42", '730', '1016', "1017\\r\\n'"]
line<4: ["b'42", '730', '1016', "1017\\r\\n'"]
raw line: b'43 729 1018 1017\r\n'
str line: b'43 729 1018 1017\r\n'
splitted line: ["b'43", '729', '1018', "1017\\r\\n'"]
line<4: ["b'43", '729', '1018', "1017\\r\\n'"]
raw line: b'35 727 1023 1021\r\n'
str line: b'35 727 1023 1021\r\n'
splitted line: ["b'35", '727', '1023', "1021\\r\\n'"]
line<4: ["b'35", '727', '1023', "1021\\r\\n'"]
raw line: b'40 729 1015 1009\r\n'
str line: b'40 729 1015 1009\r\n'
splitted line: ["b'40", '729', '1015', "1009\\r\\n'"]
line<4: ["b'40", '729', '1015', "1009\\r\\n'"]
raw line: b'36 724 1018 1019\r\n'
str line: b'36 724 1018 1019\r\n'
splitted line: ["b'36", '724', '1018', "1019\\r\\n'"]
line<4: ["b'36", '724', '1018', "1019\\r\\n'"]
raw line: b'32 726 1018 1017\r\n'
str line: b'32 726 1018 1017\r\n'
splitted line: ["b'32", '726', '1018', "1017\\r\\n'"]
line<4: ["b'32", '726', '1018', "1017\\r\\n'"]
raw line: b'35 728 1016 1021\r\n'
str line: b'35 728 1016 1021\r\n'
splitted line: ["b'35", '728', '1016', "1021\\r\\n'"]
line<4: ["b'35", '728', '1016', "1021\\r\\n'"]
raw line: b'40 731 1017 1015\r\n'
str line: b'40 731 1017 1015\r\n'
splitted line: ["b'40", '731', '1017', "1015\\r\\n'"]
line<4: ["b'40", '731', '1017', "1015\\r\\n'"]
raw line: b'18 728 1013 1016\r\n'
str line: b'18 728 1013 1016\r\n'
splitted line: ["b'18", '728', '1013', "1016\\r\\n'"]
line<4: ["b'18", '728', '1013', "1016\\r\\n'"]
raw line: b'43 731 1012 1017\r\n'
str line: b'43 731 1012 1017\r\n'
splitted line: ["b'43", '731', '1012', "1017\\r\\n'"]
line<4: ["b'43", '731', '1012', "1017\\r\\n'"]
raw line: b'43 730 1019 1017\r\n'
str line: b'43 730 1019 1017\r\n'
splitted line: ["b'43", '730', '1019', "1017\\r\\n'"]
line<4: ["b'43", '730', '1019', "1017\\r\\n'"]
raw line: b'58 728 1003 1014\r\n'
str line: b'58 728 1003 1014\r\n'
splitted line: ["b'58", '728', '1003', "1014\\r\\n'"]
line<4: ["b'58", '728', '1003', "1014\\r\\n'"]
raw line: b'29 730 1014 1009\r\n'
str line: b'29 730 1014 1009\r\n'
splitted line: ["b'29", '730', '1014', "1009\\r\\n'"]
line<4: ["b'29", '730', '1014', "1009\\r\\n'"]
raw line: b'46 730 1006 1014\r\n'
str line: b'46 730 1006 1014\r\n'
splitted line: ["b'46", '730', '1006', "1014\\r\\n'"]
line<4: ["b'46", '730', '1006', "1014\\r\\n'"]
raw line: b'45 732 1022 1012\r\n'
str line: b'45 732 1022 1012\r\n'
splitted line: ["b'45", '732', '1022', "1012\\r\\n'"]
line<4: ["b'45", '732', '1022', "1012\\r\\n'"]
raw line: b'47 731 1019 1017\r\n'
str line: b'47 731 1019 1017\r\n'
splitted line: ["b'47", '731', '1019', "1017\\r\\n'"]
line<4: ["b'47", '731', '1019', "1017\\r\\n'"]
raw line: b'29 732 1021 1014\r\n'
str line: b'29 732 1021 1014\r\n'
splitted line: ["b'29", '732', '1021', "1014\\r\\n'"]
line<4: ["b'29", '732', '1021', "1014\\r\\n'"]
raw line: b'37 731 1008 1016\r\n'
str line: b'37 731 1008 1016\r\n'
splitted line: ["b'37", '731', '1008', "1016\\r\\n'"]
line<4: ["b'37", '731', '1008', "1016\\r\\n'"]
raw line: b'50 731 1012 1017\r\n'
str line: b'50 731 1012 1017\r\n'
splitted line: ["b'50", '731', '1012', "1017\\r\\n'"]
line<4: ["b'50", '731', '1012', "1017\\r\\n'"]
raw line: b'41 732 1019 1017\r\n'
str line: b'41 732 1019 1017\r\n'
splitted line: ["b'41", '732', '1019', "1017\\r\\n'"]
line<4: ["b'41", '732', '1019', "1017\\r\\n'"]
raw line: b'29 729 1018 1019\r\n'
str line: b'29 729 1018 1019\r\n'
splitted line: ["b'29", '729', '1018', "1019\\r\\n'"]
line<4: ["b'29", '729', '1018', "1019\\r\\n'"]
raw line: b'32 731 1013 1008\r\n'
str line: b'32 731 1013 1008\r\n'
splitted line: ["b'32", '731', '1013', "1008\\r\\n'"]
line<4: ["b'32", '731', '1013', "1008\\r\\n'"]
raw line: b'49 728 1019 1012\r\n'
str line: b'49 728 1019 1012\r\n'
splitted line: ["b'49", '728', '1019', "1012\\r\\n'"]
line<4: ["b'49", '728', '1019', "1012\\r\\n'"]
raw line: b'37 731 1019 1017\r\n'
str line: b'37 731 1019 1017\r\n'
splitted line: ["b'37", '731', '1019', "1017\\r\\n'"]
line<4: ["b'37", '731', '1019', "1017\\r\\n'"]
raw line: b'29 728 1018 1017\r\n'
str line: b'29 728 1018 1017\r\n'
splitted line: ["b'29", '728', '1018', "1017\\r\\n'"]
line<4: ["b'29", '728', '1018', "1017\\r\\n'"]
raw line: b'32 729 1015 1011\r\n'
str line: b'32 729 1015 1011\r\n'
splitted line: ["b'32", '729', '1015', "1011\\r\\n'"]
line<4: ["b'32", '729', '1015', "1011\\r\\n'"]
raw line: b'51 729 1011 1009\r\n'
str line: b'51 729 1011 1009\r\n'
splitted line: ["b'51", '729', '1011', "1009\\r\\n'"]
line<4: ["b'51", '729', '1011', "1009\\r\\n'"]
raw line: b'55 727 1011 1009\r\n'
str line: b'55 727 1011 1009\r\n'
splitted line: ["b'55", '727', '1011', "1009\\r\\n'"]
line<4: ["b'55", '727', '1011', "1009\\r\\n'"]
raw line: b'44 726 1011 1009\r\n'
str line: b'44 726 1011 1009\r\n'
splitted line: ["b'44", '726', '1011', "1009\\r\\n'"]
line<4: ["b'44", '726', '1011', "1009\\r\\n'"]
raw line: b'48 727 1013 1012\r\n'
str line: b'48 727 1013 1012\r\n'
splitted line: ["b'48", '727', '1013', "1012\\r\\n'"]
line<4: ["b'48", '727', '1013', "1012\\r\\n'"]
raw line: b'48 726 1014 1013\r\n'
str line: b'48 726 1014 1013\r\n'
splitted line: ["b'48", '726', '1014', "1013\\r\\n'"]
line<4: ["b'48", '726', '1014', "1013\\r\\n'"]
raw line: b'50 726 1014 1011\r\n'
str line: b'50 726 1014 1011\r\n'
splitted line: ["b'50", '726', '1014', "1011\\r\\n'"]
line<4: ["b'50", '726', '1014', "1011\\r\\n'"]
raw line: b'49 728 1011 1010\r\n'
str line: b'49 728 1011 1010\r\n'
splitted line: ["b'49", '728', '1011', "1010\\r\\n'"]
line<4: ["b'49", '728', '1011', "1010\\r\\n'"]
raw line: b'47 729 1011 1012\r\n'
str line: b'47 729 1011 1012\r\n'
splitted line: ["b'47", '729', '1011', "1012\\r\\n'"]
line<4: ["b'47", '729', '1011', "1012\\r\\n'"]
raw line: b'45 727 1014 1012\r\n'
str line: b'45 727 1014 1012\r\n'
splitted line: ["b'45", '727', '1014', "1012\\r\\n'"]
line<4: ["b'45", '727', '1014', "1012\\r\\n'"]
raw line: b'56 728 1010 1009\r\n'
str line: b'56 728 1010 1009\r\n'
splitted line: ["b'56", '728', '1010', "1009\\r\\n'"]
line<4: ["b'56", '728', '1010', "1009\\r\\n'"]
raw line: b'39 727 1014 1012\r\n'
str line: b'39 727 1014 1012\r\n'
splitted line: ["b'39", '727', '1014', "1012\\r\\n'"]
line<4: ["b'39", '727', '1014', "1012\\r\\n'"]
raw line: b'40 727 1008 1008\r\n'
str line: b'40 727 1008 1008\r\n'
splitted line: ["b'40", '727', '1008', "1008\\r\\n'"]
line<4: ["b'40", '727', '1008', "1008\\r\\n'"]
raw line: b'62 724 1000 1015\r\n'
str line: b'62 724 1000 1015\r\n'
splitted line: ["b'62", '724', '1000', "1015\\r\\n'"]
line<4: ["b'62", '724', '1000', "1015\\r\\n'"]
raw line: b'56 728 1009 1012\r\n'
str line: b'56 728 1009 1012\r\n'
splitted line: ["b'56", '728', '1009', "1012\\r\\n'"]
line<4: ["b'56", '728', '1009', "1012\\r\\n'"]
raw line: b'38 728 1015 1010\r\n'
str line: b'38 728 1015 1010\r\n'
splitted line: ["b'38", '728', '1015', "1010\\r\\n'"]
line<4: ["b'38", '728', '1015', "1010\\r\\n'"]
raw line: b'41 726 1010 1010\r\n'
str line: b'41 726 1010 1010\r\n'
splitted line: ["b'41", '726', '1010', "1010\\r\\n'"]
line<4: ["b'41", '726', '1010', "1010\\r\\n'"]
raw line: b'50 724 1011 1010\r\n'
str line: b'50 724 1011 1010\r\n'
splitted line: ["b'50", '724', '1011', "1010\\r\\n'"]
line<4: ["b'50", '724', '1011', "1010\\r\\n'"]
raw line: b'51 732 1014 1012\r\n'
str line: b'51 732 1014 1012\r\n'
splitted line: ["b'51", '732', '1014', "1012\\r\\n'"]
line<4: ["b'51", '732', '1014', "1012\\r\\n'"]
raw line: b'38 722 1014 1013\r\n'

Virus-free. www.avast.com

WILMER CONTRERAS SEPULVEDA

unread,
Jul 6, 2019, 9:45:49 PM7/6/19
to pyqt...@googlegroups.com
Ok I understand, maybe the decision should to be if len(line)>3 
let me try

Virus-free. www.avast.com

WILMER CONTRERAS SEPULVEDA

unread,
Jul 6, 2019, 9:51:25 PM7/6/19
to pyqt...@googlegroups.com
Right Now it work, but just a couple of seconds and I get my main problem, after a few seconds the functions just crash 
look at it:

image.png

Virus-free. www.avast.com

vas...@gmail.com

unread,
Jul 6, 2019, 10:07:47 PM7/6/19
to pyqt...@googlegroups.com
Please change
if len(line)>4:
to
if len(line)>=4:

WILMER CONTRERAS SEPULVEDA

unread,
Jul 6, 2019, 10:10:39 PM7/6/19
to pyqt...@googlegroups.com
I did, and it worked, but just a coup6of seconds it crash

vas...@gmail.com

unread,
Jul 6, 2019, 10:22:39 PM7/6/19
to pyqt...@googlegroups.com
Maybe to use independent variable (not all should be named 'line') for every step:

import numpy as np
import pyqtgraph as pg
import serial

app = pg.Qt.QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('live plot from serial')
curve = p.plot()

data = [0]
int_line = 0

raw=serial.Serial("COM8",115200)

while p.isVisible():
    raw_line = raw.readline()
    print("raw line:", raw_line)
    str_line = str(raw_line)
    print("str line:", str_line)
    line_splitted = str_line.split(' ')
    print("splitted line:", line_splitted)
   
    if len(line_split) >= 4:
        print("line_splitted >=4:", line)
        line2 = line_splitted[2]
        print("line[2]:", line2)
        int_line=int(line2)
        print("int(line):", int_line)
    else:
        print("line <=3 :", line_split)
        int_line = 0
   
    if int_line != 0:
        print("int_line != 0:", int_line)
        data.append(int_line)

        xdata = np.array(data, dtype='int32')
        curve.setData(xdata)
        print("data is set")

    app.processEvents()  

       

 
       


WILMER CONTRERAS SEPULVEDA

unread,
Jul 6, 2019, 10:51:03 PM7/6/19
to pyqt...@googlegroups.com
again it crash
image.png

Virus-free. www.avast.com


For more options, visit https://groups.google.com/d/optout.

vas...@gmail.com

unread,
Jul 6, 2019, 11:11:00 PM7/6/19
to pyqt...@googlegroups.com
Maybe is problem that you run your script from IDLE.
1) you could save your script somewhere on disk (for example, as my_script.py) and run in with python command from command prompt (dont forget to change the directory to the directory of your script), as python my_script.py
2) you could install some text editor with integrated python execution as Geany https://download.geany.org/geany-1.35_setup.exe, load script in Geany and Execute it.

WILMER CONTRERAS SEPULVEDA

unread,
Jul 7, 2019, 10:04:13 AM7/7/19
to pyqt...@googlegroups.com
With both ways the answer is the same, I think that the problem is in the pytograph method but I don't know

image.png

Virus-free. www.avast.com


For more options, visit https://groups.google.com/d/optout.

WILMER CONTRERAS SEPULVEDA

unread,
Jul 7, 2019, 10:23:10 AM7/7/19
to pyqt...@googlegroups.com
I make the next code, and it works (with the same bug), but I need have a continuous plot of the data and not as in this code where the data is plot by groups:


from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np
import serial

ser = serial.Serial('COM8', baudrate='115200')


data=[]
data1=[]
data2=[]

for i in range(1,50):
    data.append(np.random.randint(1,90))
    data1.append(np.random.randint(50))
    data2.append(np.random.randint(60,100))

win=pg.GraphicsWindow(title='MAIL')
pg.setConfigOptions(antialias=True)
p1=win.addPlot(title='SENSOR1', row=0, col=0)
p1.plot(data1, pen=(255,255,0))
p1.showGrid(x=True,y=True)

p2=win.addPlot(title='SENSOR2',row=1, col=0)
p2.plot(data2, pen=(0,0,255))
p2.showGrid(x=True, y=True)

p3= win.addPlot(title='SENSOR3',row=0, col=1)
p3.plot(data, pen=(255,0,0))
p3.showGrid(x=True, y=True)

app = QtGui.QApplication([])

p6 = win.addPlot(title="SENSOR4",row=1, col=1)
curve = p6.plot(pen='y')
data= np.zeros(100)
data12=[]
global contador
contador=list(range(1,100))
global fiftis

def updape():
   
    global curve, data
    for h in range(100):
        try:
            datos=ser.readline()
            datos=str(datos)
            datos=datos.split(' ')
            datos=int(datos[1])
            data[h]=datos
        except:
            pass
    curve.setData(data)
    app.processEvents() ## force complete redraw for every plot
   
   
timer = QtCore.QTimer()
timer.timeout.connect(updape)

timer.start(50)


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


ser.close()
image.png

Virus-free. www.avast.com
Reply all
Reply to author
Forward
0 new messages