本文介绍了如何使用PyQtGraph提高速度并分割多个图块的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用STM32套件从串行端口读取数据.问题是我需要使用自己的时间戳来绘制ADC数据.也就是说x轴应该是我的RTC时间(为此使用ms),y轴应该是ADC数据.有一些用于绘图串行端口的程序,但是正如我所说,我需要为图形设置自己的时间.我为此尝试了matplotlib,但速度确实很慢.然后使用pyqtgraph和此脚本:

I am reading datas from serial port using an STM32 kit. Problem is that I need to use own timestamp for plot ADC datas. That is mean x-axis should be my RTC time(using ms for this) and y-axis is ADC datas. There are programs for plot serial port but as I said I need to set own time for graph. I tried matplotlib for this but it was really slow. Then have used pyqtgraph and this script:

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("/dev/ttyACM0",115200)
#raw.open()

def update():
    global curve, data
    line = raw.readline()
    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_()

这也很慢,但与mathplotlib相比却很快.现在,我无法找到如何将时间戳和ADC数据拆分为x,y之类的图.我的数据以';'分割.

This is slow too but fast compare with mathplotlib. Now I can't find how split my timestamp and ADC datas for plot like x,y. My datas are spliting with ';'.

感谢答案.

我更改了代码读取速度,看起来足够了解.但是知道它正在绘制一些小故障,例如时间戳在向前和向后跳跃或返回大量x轴数据.我正在监视串行GUI上的数据,但找不到任何错误的数据.我认为有些东西来自Python代码.我可以忽略绘图程序中的这些故障吗?

I changed my code reading speed looking enough for know. But know it is plotting some glitches like timetamp is jumping with forward and come back or very big numbers of x-axis datas. I am monitoring datas on a serial port GUI and I can't find any wrong data. Somethings is coming from Python code, i think. Can I ignore these glitches on plotting program?

现在输入代码:

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]
tdata = [0]
temp = [0]
datax = [0]
datay = [0]

temp = 0
now = 0
k = 0
raw=serial.Serial("/dev/ttyACM0",115200, timeout=None)
while p.isVisible():
    line = raw.readline().decode('utf-8').strip()
    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)
        tdata = line[0]
        data = line[1]
        print("line[0]; line[1]:", tdata, line)
        tdata = int(tdata)
        data = int(data)
        print("int(tdata)", tdata)
        print("int(line)", data)
        datax.append(int(tdata))
        datay.append(int(data))
        xdata = np.array(datax, dtype='float64')
        ydata = np.array(datay, dtype='float64')
        p.setXRange(tdata-500, tdata+500, padding=0)
        curve.setData(xdata, ydata)
        # p.setYRange(0 ,data+30, padding=0)
        print("now will refresh the plot")
        app.processEvents()
    else:
        print("line<4:", line)

推荐答案

拆分数据

使用Serial.readline()时,读取的数据末尾将包含换行符\n(请参阅Python统一换行符,如果从Windows发送则为\r\n\.)

When you use Serial.readline() the data read will contain the newline character \n at the end (see Python univeral newlines, could be \r\n\ if you send from Windows).

首先解码接收到的字节并删除换行符:

First decode the bytes received and remove the newline character:

data_string = r.readline().decode('utf-8').strip()

然后在:

data_split = data_string.split(':')

现在data_split是包含条目的列表

[packetCount, databuffer, sec, subsec]

,您可以将它们转换为浮点数或整数,然后将它们放入Numpy数组中.

and you can convert them to floats or integers and put them in the Numpy array.

速度提高

Serial.readline可能会降低您的代码速度.使用类似 https://stackoverflow.com/a/56632812/7919597 的东西.

Serial.readline might slow down your code. Use something like this https://stackoverflow.com/a/56632812/7919597 .

还可以考虑将数据移动到固定的numpy数组中,而不是每次使用xdata = np.array(data, dtype='float64')都创建一个新的数组.

Also consider shifting the data in a fixed numpy array instead of creating a new one each time with xdata = np.array(data, dtype='float64').

请参见在numpy数组中移动元素

我将这些功能与Thread之类的

import threading
import queue

q = queue.Queue()
thread = threading.Thread(target=read_from_port, args=(serial_port, q))
thread.start()

从串行端口读取.

PyQtGraph示例中有一个非常有用的示例:

There is a very helpful example in the PyQtGraph examples:

https://github.com/pyqtgraph/pyqtgraph/blob /develop/examples/scrollingPlots.py

显示了使用Numpy数组绘制滚动图的三种不同方法.

Is shows three different methods for plotting scrolling plots with Numpy arrays.

我最终使用方法1和3的组合,同时移动了从串行端口读取的位置.

I ended up using a combination of method 1 and 3, shifting as many places as were read from the serial port in the meantime.

这篇关于如何使用PyQtGraph提高速度并分割多个图块的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:09