多线程 - 从串行读取,用 pyqtgraph 绘制

发布于 2025-01-13 06:36:04 字数 9349 浏览 4 评论 0原文

我试图从串口接收以 921600 波特率发送的不同 id 的消息,根据它们的 id 解析这些消息并将它们绘制在不同的图表中。首先,当我使用超过 4 个 id 的 pyqtgraph 进行绘图时,我开始丢失来自串行端口的数据。我希望这不会发生,因为它是一个线程。另外,当我拖放主窗口时,线程冻结,我再次丢失数据包。我应该如何进行这里?同样在这里,我在代码中创建了一个主窗口并将其嵌入,但是如果我需要使用 Qt 设计器创建一个界面并在其中添加多个 pyqtgraph,我该怎么办? 这是代码

from pyqtgraph.Qt import QtGui, QtCore
from numpy import *
import serial
import time
import threading, multiprocessing

read_data = []
cnt =0
row_count=0
currentrow= []
ser_bytes=[]
id_number = []
pkt_cnt=[]
totalpacket=0

windowWidth= 100
app = pg.mkQApp()
mw = QtGui.QMainWindow()
mw.resize(1000,250)
cw = QtGui.QWidget()
mw.setCentralWidget(cw)
l = QtGui.QVBoxLayout()
cw.setLayout(l)

pw1 = pg.PlotWidget(name='Plot1')
curve1 = pw1.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw1)
Val1 = linspace(0,0,windowWidth)
ptr1 = -windowWidth

pw2 = pg.PlotWidget(name='Plot2')
curve2 = pw2.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw2)
Val2 = linspace(0,0,windowWidth)
ptr2 = -windowWidth

pw3 = pg.PlotWidget(name='Plot3')
curve3 = pw3.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw3)
Val3 = linspace(0,0,windowWidth)
ptr3 = -windowWidth
#
pw4 = pg.PlotWidget(name='Plot4')
curve4 = pw4.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw4)
Val4 = linspace(0,0,windowWidth)
ptr4 = -windowWidth
#
pw5 = pg.PlotWidget(name='Plot5')
curve5 = pw5.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw5)
Val5 = linspace(0,0,windowWidth)
ptr5 = -windowWidth

pw6 = pg.PlotWidget(name='Plot6')
curve6 = pw6.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw6)
Val6 = linspace(0,0,windowWidth)
ptr6 = -windowWidth

mw.show()

def UpdateGraphnode1(queuenode1Bytes):
    global ptr1, Val1, curve1
    if not queuenode1Bytes.empty():
        valueofgraph = queuenode1Bytes.get(timeout=5)
        Val1[:-1] = Val1[1:]
        Val1[-1] = valueofgraph
        curve1.setData(Val1)
        curve1.setPos(ptr1,0)
        ptr1+=1
        curve1.setPen('w')

def ReadBytenode1(queueArraynode1,queuenode1Bytes):
    index = 0
    if not queueArraynode1.empty():
        arrbytes = queueArraynode1.get(timeout=5)
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode1Bytes.put(byteofarr)
             index =index + 1


def UpdateGraphnode2(queuenode2Bytes):
    global ptr2, Val2, curve2
    if not queuenode1Bytes.empty():
        valueofgraph = queuenode2Bytes.get(timeout=5)
        # print("node2", valueofgraph)
        Val2[:-1] = Val2[1:]
        Val2[-1] = valueofgraph
        curve2.setData(Val2)
        curve2.setPos(ptr2,0)
        ptr2+=1
        curve2.setPen('w')

def ReadBytenode2(queueArraynode2,queuenode2Bytes):
    index = 0
    if not queueArraynode2.empty():
        arrbytes = queueArraynode2.get(timeout=5)
        # print("len",len(arrbytes))
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode2Bytes.put(byteofarr)
             # print("byteofarr",byteofarr)
             index =index + 1

def UpdateGraphnode3(queuenode3Bytes):
    global ptr3, Val3, curve3
    if not queuenode3Bytes.empty():
        valueofgraph = queuenode3Bytes.get(timeout=5)
        # print("node3", valueofgraph)
        Val3[:-1] = Val3[1:]
        Val3[-1] = valueofgraph
        curve3.setData(Val3)
        curve3.setPos(ptr3,0)
        ptr3+=1
        curve3.setPen('w')

def ReadBytenode3(queueArraynode3,queuenode3Bytes):
    index = 0
    if not queueArraynode3.empty():
        arrbytes = queueArraynode3.get(timeout=5)
        # print("len",len(arrbytes))
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode3Bytes.put(byteofarr)
             # print("byteofarr",byteofarr)
             index =index + 1


def UpdateGraphnode4(queuenode4Bytes):
    global ptr4, Val4, curve4
    if not queuenode4Bytes.empty():
        valueofgraph = queuenode4Bytes.get(timeout=5)
        # print("node4",valueofgraph)
        Val4[:-1] = Val4[1:]
        Val4[-1] = valueofgraph
        curve4.setData(Val4)
        curve4.setPos(ptr4,0)
        ptr4+=1
        curve4.setPen('w')

def ReadBytenode4(queueArraynode4,queuenode4Bytes):
    index = 0
    if not queueArraynode4.empty():
        arrbytes = queueArraynode4.get(timeout=5)
        # print("len",len(arrbytes))
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode4Bytes.put(byteofarr)
             # print("byteofarr",byteofarr)
             index =index + 1

def UpdateGraphnode5(queuenode5Bytes):
    global ptr5, Val5, curve5
    if not queuenode5Bytes.empty():
        valueofgraph = queuenode5Bytes.get(timeout=5)
        # print("node5", valueofgraph)
        Val5[:-1] = Val5[1:]
        Val5[-1] = valueofgraph
        curve5.setData(Val5)
        curve5.setPos(ptr5,0)
        ptr5+=1
        curve5.setPen('w')

def ReadBytenode5(queueArraynode5,queuenode5Bytes):
    index = 0
    if not queueArraynode5.empty():
        arrbytes = queueArraynode5.get(timeout=5)
        # print("len",len(arrbytes))
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode5Bytes.put(byteofarr)
             # print("byteofarr",byteofarr)
             index =index + 1

def UpdateGraphnode6(queuenode6Bytes):
    global ptr6, Val6, curve6
    if not queuenode6Bytes.empty():
        valueofgraph = queuenode6Bytes.get(timeout=5)
        # print("node6", valueofgraph)
        Val6[:-1] = Val6[1:]
        Val6[-1] = valueofgraph
        curve6.setData(Val6)
        curve6.setPos(ptr6,0)
        ptr6+=1
        curve6.setPen('w')

def ReadBytenode6(queueArraynode6,queuenode6Bytes):
    index = 0
    if not queueArraynode6.empty():
        arrbytes = queueArraynode6.get(timeout=5)
        # print("len",len(arrbytes))
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode6Bytes.put(byteofarr)
             # print("byteofarr",byteofarr)
             index =index + 1


def ReadFunction(queue):
    global ser_bytes, id_number, pkt_cnt,ser_bytes_init, totalpacket
    ser_bytes_init = ser.read_until(expected=b"\xff\xff\xff", size=244)
    totalpacket = totalpacket + 1
    id_number = ser_bytes[0:1].hex()
    pkt_cnt = ser_bytes[2]
    queue.put(ser_bytes)

    if id_number == "31":
        queueArraynode1.put(ser_bytes)
    if id_number == "32":
        queueArraynode2.put(ser_bytes)
    if id_number == "33":
        queueArraynode3.put(ser_bytes)
    if id_number == "34":
        queueArraynode4.put(ser_bytes)
    if id_number == "35":
        queueArraynode5.put(ser_bytes)
    if id_number == "36":
        queueArraynode6.put(ser_bytes)





if __name__ == "__main__":
    portName = "COM6"
    baudrate = 921600
    ser = serial.Serial(portName, baudrate,timeout = None)
    ser.flushInput()

    start = time.time()
    queue = multiprocessing.Queue()
    queueArraynode1 = multiprocessing.Queue()
    queueArraynode2 = multiprocessing.Queue()
    queueArraynode3 = multiprocessing.Queue()
    queueArraynode4 = multiprocessing.Queue()
    queueArraynode5 = multiprocessing.Queue()
    queueArraynode6 = multiprocessing.Queue()
    queuenode1Bytes = multiprocessing.Queue()
    queuenode2Bytes = multiprocessing.Queue()
    queuenode3Bytes = multiprocessing.Queue()
    queuenode4Bytes = multiprocessing.Queue()
    queuenode5Bytes = multiprocessing.Queue()
    queuenode6Bytes = multiprocessing.Queue()
    stopped = threading.Event()


    while True:
        try:
            QtCore.QCoreApplication.processEvents()
            p1 = threading.Thread(target=ReadFunction, args=(queue,))
            p3 = threading.Thread(target=ReadBytenode1, args=(queueArraynode1,queuenode1Bytes,))
            p4 = threading.Thread(target=UpdateGraphnode1, args=(queuenode1Bytes,))
            p5 = threading.Thread(target=ReadBytenode2, args=(queueArraynode2, queuenode2Bytes,))
            p6 = threading.Thread(target=UpdateGraphnode2, args=(queuenode2Bytes,))
            p7 = threading.Thread(target=ReadBytenode3, args=(queueArraynode3, queuenode3Bytes,))
            p8 = threading.Thread(target=UpdateGraphnode3, args=(queuenode3Bytes,))
            p9 = threading.Thread(target=ReadBytenode4, args=(queueArraynode4, queuenode4Bytes,))
            p10 = threading.Thread(target=UpdateGraphnode4, args=(queuenode4Bytes,))
            p11 = threading.Thread(target=ReadBytenode5, args=(queueArraynode5, queuenode5Bytes,))
            p12 = threading.Thread(target=UpdateGraphnode5, args=(queuenode5Bytes,))
            p13 = threading.Thread(target=ReadBytenode6, args=(queueArraynode6, queuenode6Bytes,))
            p14 = threading.Thread(target=UpdateGraphnode6, args=(queuenode6Bytes,))

            p1.start()
            p3.start()
            p4.start()
            p5.start()
            p6.start()
            p7.start()
            p8.start()
            p9.start()
            p10.start()
            p11.start()
            p12.start()
            p13.start()
            p14.start()

            p1.join()



        except:

            print("Keyboard Interrupt")
            stop = time.time()
            break
    pg.QtGui.QApplication.exec_()```

I'm trying to receive messages sent with different id's at 921600 baudrate from the serial port, parse these messages according to their id's and plot them in different graphs. Firstly when I plot with more than 4 id pyqtgraph I start to miss data from serial port. I was expecting this not to happen since it's a thread. Also, when I drag-drop the main window, the threads freeze and I'm missing packets again. How should I proceed here? Also here, I created a mainwindow in the code and embedded it, but what should I do if I need to create an interface with Qt designer and add more than one pyqtgraph inside it?
Here is the code

from pyqtgraph.Qt import QtGui, QtCore
from numpy import *
import serial
import time
import threading, multiprocessing

read_data = []
cnt =0
row_count=0
currentrow= []
ser_bytes=[]
id_number = []
pkt_cnt=[]
totalpacket=0

windowWidth= 100
app = pg.mkQApp()
mw = QtGui.QMainWindow()
mw.resize(1000,250)
cw = QtGui.QWidget()
mw.setCentralWidget(cw)
l = QtGui.QVBoxLayout()
cw.setLayout(l)

pw1 = pg.PlotWidget(name='Plot1')
curve1 = pw1.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw1)
Val1 = linspace(0,0,windowWidth)
ptr1 = -windowWidth

pw2 = pg.PlotWidget(name='Plot2')
curve2 = pw2.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw2)
Val2 = linspace(0,0,windowWidth)
ptr2 = -windowWidth

pw3 = pg.PlotWidget(name='Plot3')
curve3 = pw3.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw3)
Val3 = linspace(0,0,windowWidth)
ptr3 = -windowWidth
#
pw4 = pg.PlotWidget(name='Plot4')
curve4 = pw4.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw4)
Val4 = linspace(0,0,windowWidth)
ptr4 = -windowWidth
#
pw5 = pg.PlotWidget(name='Plot5')
curve5 = pw5.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw5)
Val5 = linspace(0,0,windowWidth)
ptr5 = -windowWidth

pw6 = pg.PlotWidget(name='Plot6')
curve6 = pw6.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
l.addWidget(pw6)
Val6 = linspace(0,0,windowWidth)
ptr6 = -windowWidth

mw.show()

def UpdateGraphnode1(queuenode1Bytes):
    global ptr1, Val1, curve1
    if not queuenode1Bytes.empty():
        valueofgraph = queuenode1Bytes.get(timeout=5)
        Val1[:-1] = Val1[1:]
        Val1[-1] = valueofgraph
        curve1.setData(Val1)
        curve1.setPos(ptr1,0)
        ptr1+=1
        curve1.setPen('w')

def ReadBytenode1(queueArraynode1,queuenode1Bytes):
    index = 0
    if not queueArraynode1.empty():
        arrbytes = queueArraynode1.get(timeout=5)
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode1Bytes.put(byteofarr)
             index =index + 1


def UpdateGraphnode2(queuenode2Bytes):
    global ptr2, Val2, curve2
    if not queuenode1Bytes.empty():
        valueofgraph = queuenode2Bytes.get(timeout=5)
        # print("node2", valueofgraph)
        Val2[:-1] = Val2[1:]
        Val2[-1] = valueofgraph
        curve2.setData(Val2)
        curve2.setPos(ptr2,0)
        ptr2+=1
        curve2.setPen('w')

def ReadBytenode2(queueArraynode2,queuenode2Bytes):
    index = 0
    if not queueArraynode2.empty():
        arrbytes = queueArraynode2.get(timeout=5)
        # print("len",len(arrbytes))
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode2Bytes.put(byteofarr)
             # print("byteofarr",byteofarr)
             index =index + 1

def UpdateGraphnode3(queuenode3Bytes):
    global ptr3, Val3, curve3
    if not queuenode3Bytes.empty():
        valueofgraph = queuenode3Bytes.get(timeout=5)
        # print("node3", valueofgraph)
        Val3[:-1] = Val3[1:]
        Val3[-1] = valueofgraph
        curve3.setData(Val3)
        curve3.setPos(ptr3,0)
        ptr3+=1
        curve3.setPen('w')

def ReadBytenode3(queueArraynode3,queuenode3Bytes):
    index = 0
    if not queueArraynode3.empty():
        arrbytes = queueArraynode3.get(timeout=5)
        # print("len",len(arrbytes))
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode3Bytes.put(byteofarr)
             # print("byteofarr",byteofarr)
             index =index + 1


def UpdateGraphnode4(queuenode4Bytes):
    global ptr4, Val4, curve4
    if not queuenode4Bytes.empty():
        valueofgraph = queuenode4Bytes.get(timeout=5)
        # print("node4",valueofgraph)
        Val4[:-1] = Val4[1:]
        Val4[-1] = valueofgraph
        curve4.setData(Val4)
        curve4.setPos(ptr4,0)
        ptr4+=1
        curve4.setPen('w')

def ReadBytenode4(queueArraynode4,queuenode4Bytes):
    index = 0
    if not queueArraynode4.empty():
        arrbytes = queueArraynode4.get(timeout=5)
        # print("len",len(arrbytes))
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode4Bytes.put(byteofarr)
             # print("byteofarr",byteofarr)
             index =index + 1

def UpdateGraphnode5(queuenode5Bytes):
    global ptr5, Val5, curve5
    if not queuenode5Bytes.empty():
        valueofgraph = queuenode5Bytes.get(timeout=5)
        # print("node5", valueofgraph)
        Val5[:-1] = Val5[1:]
        Val5[-1] = valueofgraph
        curve5.setData(Val5)
        curve5.setPos(ptr5,0)
        ptr5+=1
        curve5.setPen('w')

def ReadBytenode5(queueArraynode5,queuenode5Bytes):
    index = 0
    if not queueArraynode5.empty():
        arrbytes = queueArraynode5.get(timeout=5)
        # print("len",len(arrbytes))
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode5Bytes.put(byteofarr)
             # print("byteofarr",byteofarr)
             index =index + 1

def UpdateGraphnode6(queuenode6Bytes):
    global ptr6, Val6, curve6
    if not queuenode6Bytes.empty():
        valueofgraph = queuenode6Bytes.get(timeout=5)
        # print("node6", valueofgraph)
        Val6[:-1] = Val6[1:]
        Val6[-1] = valueofgraph
        curve6.setData(Val6)
        curve6.setPos(ptr6,0)
        ptr6+=1
        curve6.setPen('w')

def ReadBytenode6(queueArraynode6,queuenode6Bytes):
    index = 0
    if not queueArraynode6.empty():
        arrbytes = queueArraynode6.get(timeout=5)
        # print("len",len(arrbytes))
        while index < 244:
             byteofarr = float(arrbytes[index])
             queuenode6Bytes.put(byteofarr)
             # print("byteofarr",byteofarr)
             index =index + 1


def ReadFunction(queue):
    global ser_bytes, id_number, pkt_cnt,ser_bytes_init, totalpacket
    ser_bytes_init = ser.read_until(expected=b"\xff\xff\xff", size=244)
    totalpacket = totalpacket + 1
    id_number = ser_bytes[0:1].hex()
    pkt_cnt = ser_bytes[2]
    queue.put(ser_bytes)

    if id_number == "31":
        queueArraynode1.put(ser_bytes)
    if id_number == "32":
        queueArraynode2.put(ser_bytes)
    if id_number == "33":
        queueArraynode3.put(ser_bytes)
    if id_number == "34":
        queueArraynode4.put(ser_bytes)
    if id_number == "35":
        queueArraynode5.put(ser_bytes)
    if id_number == "36":
        queueArraynode6.put(ser_bytes)





if __name__ == "__main__":
    portName = "COM6"
    baudrate = 921600
    ser = serial.Serial(portName, baudrate,timeout = None)
    ser.flushInput()

    start = time.time()
    queue = multiprocessing.Queue()
    queueArraynode1 = multiprocessing.Queue()
    queueArraynode2 = multiprocessing.Queue()
    queueArraynode3 = multiprocessing.Queue()
    queueArraynode4 = multiprocessing.Queue()
    queueArraynode5 = multiprocessing.Queue()
    queueArraynode6 = multiprocessing.Queue()
    queuenode1Bytes = multiprocessing.Queue()
    queuenode2Bytes = multiprocessing.Queue()
    queuenode3Bytes = multiprocessing.Queue()
    queuenode4Bytes = multiprocessing.Queue()
    queuenode5Bytes = multiprocessing.Queue()
    queuenode6Bytes = multiprocessing.Queue()
    stopped = threading.Event()


    while True:
        try:
            QtCore.QCoreApplication.processEvents()
            p1 = threading.Thread(target=ReadFunction, args=(queue,))
            p3 = threading.Thread(target=ReadBytenode1, args=(queueArraynode1,queuenode1Bytes,))
            p4 = threading.Thread(target=UpdateGraphnode1, args=(queuenode1Bytes,))
            p5 = threading.Thread(target=ReadBytenode2, args=(queueArraynode2, queuenode2Bytes,))
            p6 = threading.Thread(target=UpdateGraphnode2, args=(queuenode2Bytes,))
            p7 = threading.Thread(target=ReadBytenode3, args=(queueArraynode3, queuenode3Bytes,))
            p8 = threading.Thread(target=UpdateGraphnode3, args=(queuenode3Bytes,))
            p9 = threading.Thread(target=ReadBytenode4, args=(queueArraynode4, queuenode4Bytes,))
            p10 = threading.Thread(target=UpdateGraphnode4, args=(queuenode4Bytes,))
            p11 = threading.Thread(target=ReadBytenode5, args=(queueArraynode5, queuenode5Bytes,))
            p12 = threading.Thread(target=UpdateGraphnode5, args=(queuenode5Bytes,))
            p13 = threading.Thread(target=ReadBytenode6, args=(queueArraynode6, queuenode6Bytes,))
            p14 = threading.Thread(target=UpdateGraphnode6, args=(queuenode6Bytes,))

            p1.start()
            p3.start()
            p4.start()
            p5.start()
            p6.start()
            p7.start()
            p8.start()
            p9.start()
            p10.start()
            p11.start()
            p12.start()
            p13.start()
            p14.start()

            p1.join()



        except:

            print("Keyboard Interrupt")
            stop = time.time()
            break
    pg.QtGui.QApplication.exec_()```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2025-01-20 06:36:04

@ArthurKing我按照你说的再次编辑了代码。这是代码:

            import pyqtgraph as pg
    from pyqtgraph.Qt import QtGui, QtCore
    from numpy import *
    import serial
    import time
    import threading, multiprocessing

    read_data = []
    cnt =0
    ser_bytes=[]
    id_number = []
    pkt_cnt=[]
    totalpacket=0

    windowWidth= 100
    app = pg.mkQApp()
    mw = QtGui.QMainWindow()
    mw.resize(1000,250)
    cw = QtGui.QWidget()
    mw.setCentralWidget(cw)
    l = QtGui.QVBoxLayout()
    cw.setLayout(l)

    pw1 = pg.PlotWidget(name='Plot1')  ## giving the plots names allows us to link their axes together
    curve1 = pw1.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw1)
    Val1 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr1 = -windowWidth                      # set first x position

    pw2 = pg.PlotWidget(name='Plot2')  ## giving the plots names allows us to link their axes together
    curve2 = pw2.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw2)
    Val2 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr2 = -windowWidth                      # set first x position

    pw3 = pg.PlotWidget(name='Plot3')  ## giving the plots names allows us to link their axes together
    curve3 = pw3.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw3)
    Val3 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr3 = -windowWidth                      # set first x position
    #
    pw4 = pg.PlotWidget(name='Plot4')  ## giving the plots names allows us to link their axes together
    curve4 = pw4.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw4)
    Val4 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr4 = -windowWidth                      # set first x position
    #
    pw5 = pg.PlotWidget(name='Plot5')  ## giving the plots names allows us to link their axes together
    curve5 = pw5.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw5)
    Val5 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr5 = -windowWidth                      # set first x position

    pw6 = pg.PlotWidget(name='Plot6')  ## giving the plots names allows us to link their axes together
    curve6 = pw6.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw6)
    Val6 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr6 = -windowWidth                      # set first x position

    mw.show()

    def UpdateGraphnode(queuenodeBytes,Val,curve,ptr):
        global ptr1, Val1, curve1
        if not queuenodeBytes.empty():
            valueofgraph = queuenodeBytes.get(timeout=5)
            # print("node1", valueofgraph)
            Val[:-1] = Val[1:]
            Val[-1] = valueofgraph
            curve.setData(Val)
            curve.setPos(ptr,0)
            ptr+=1
            curve.setPen('w')

    def ReadBytenode(queueArraynode,queuenodeBytes):
        index = 0
        if not queueArraynode.empty():
            arrbytes = queueArraynode.get(timeout=5)
            # print("len",len(arrbytes))
            while index < 244:
                 byteofarr = float(arrbytes[index])
                 queuenodeBytes.put(byteofarr)
                 # print("byteofarr",byteofarr)
                 index =index + 1


    def ReadFunction(queue):
        global ser_bytes, id_number, pkt_cnt,ser_bytes_init, totalpacket
        ser_bytes_init = ser.read_until(expected=b"\xff\xff\xff", size=244)
        totalpacket = totalpacket + 1
        id_number = ser_bytes[0:1].hex()
        pkt_cnt = ser_bytes[2]
        queue.put(ser_bytes)

        if id_number == "31":
            queueArraynode1.put(ser_bytes)
        if id_number == "32":
            queueArraynode2.put(ser_bytes)
        if id_number == "33":
            queueArraynode3.put(ser_bytes)
        if id_number == "34":
            queueArraynode4.put(ser_bytes)
        if id_number == "35":
            queueArraynode5.put(ser_bytes)
        if id_number == "36":
            queueArraynode6.put(ser_bytes)




    if __name__ == "__main__":


        portName = "COM6"
        baudrate = 921600
        ser = serial.Serial(portName, baudrate,timeout = None)
        ser.flushInput()

        start = time.time()
        queue = multiprocessing.Queue()
        queueArraynode1 = multiprocessing.Queue()
        queueArraynode2 = multiprocessing.Queue()
        queueArraynode3 = multiprocessing.Queue()
        queueArraynode4 = multiprocessing.Queue()
        queueArraynode5 = multiprocessing.Queue()
        queueArraynode6 = multiprocessing.Queue()
        queuenode1Bytes = multiprocessing.Queue()
        queuenode2Bytes = multiprocessing.Queue()
        queuenode3Bytes = multiprocessing.Queue()
        queuenode4Bytes = multiprocessing.Queue()
        queuenode5Bytes = multiprocessing.Queue()
        queuenode6Bytes = multiprocessing.Queue()
        stopped = threading.Event()


        while True:
            try:
                QtCore.QCoreApplication.processEvents()
                p1 = threading.Thread(target=ReadFunction, args=(queue,))

                p3 = threading.Thread(target=ReadBytenode, args=(queueArraynode1,queuenode1Bytes,))
                p4 = threading.Thread(target=UpdateGraphnode, args=(queuenode1Bytes,Val1,curve1,ptr1,))

                p5 = threading.Thread(target=ReadBytenode, args=(queueArraynode2, queuenode2Bytes,))
                p6 = threading.Thread(target=UpdateGraphnode, args=(queuenode2Bytes,Val2,curve2,ptr2,))

                p7 = threading.Thread(target=ReadBytenode, args=(queueArraynode3, queuenode3Bytes,))
                p8 = threading.Thread(target=UpdateGraphnode, args=(queuenode3Bytes,Val3,curve3,ptr3,))

                p9 = threading.Thread(target=ReadBytenode, args=(queueArraynode4, queuenode4Bytes,))
                p10 = threading.Thread(target=UpdateGraphnode, args=(queuenode4Bytes,Val4,curve4,ptr4,))

                p11 = threading.Thread(target=ReadBytenode, args=(queueArraynode5, queuenode5Bytes,))
                p12 = threading.Thread(target=UpdateGraphnode, args=(queuenode5Bytes,Val5,curve5,ptr5,))

                p13 = threading.Thread(target=ReadBytenode, args=(queueArraynode6, queuenode6Bytes,))
                p14 = threading.Thread(target=UpdateGraphnode, args=(queuenode6Bytes,Val6,curve6,ptr6,))

                p1.start()
                p3.start()
                p4.start()
                p5.start()
                p6.start()
                p7.start()
                p8.start()
                p9.start()
                p10.start()
                p11.start()
                p12.start()
                p13.start()
                p14.start()
                p1.join()

            except:

                print("Keyboard Interrupt")
                stop = time.time()
                print("elapsed time:",stop-start)
                break
        pg.QtGui.QApplication.exec_()

@ArthurKing I edited the code again according to what you said. Here is the code:

            import pyqtgraph as pg
    from pyqtgraph.Qt import QtGui, QtCore
    from numpy import *
    import serial
    import time
    import threading, multiprocessing

    read_data = []
    cnt =0
    ser_bytes=[]
    id_number = []
    pkt_cnt=[]
    totalpacket=0

    windowWidth= 100
    app = pg.mkQApp()
    mw = QtGui.QMainWindow()
    mw.resize(1000,250)
    cw = QtGui.QWidget()
    mw.setCentralWidget(cw)
    l = QtGui.QVBoxLayout()
    cw.setLayout(l)

    pw1 = pg.PlotWidget(name='Plot1')  ## giving the plots names allows us to link their axes together
    curve1 = pw1.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw1)
    Val1 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr1 = -windowWidth                      # set first x position

    pw2 = pg.PlotWidget(name='Plot2')  ## giving the plots names allows us to link their axes together
    curve2 = pw2.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw2)
    Val2 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr2 = -windowWidth                      # set first x position

    pw3 = pg.PlotWidget(name='Plot3')  ## giving the plots names allows us to link their axes together
    curve3 = pw3.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw3)
    Val3 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr3 = -windowWidth                      # set first x position
    #
    pw4 = pg.PlotWidget(name='Plot4')  ## giving the plots names allows us to link their axes together
    curve4 = pw4.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw4)
    Val4 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr4 = -windowWidth                      # set first x position
    #
    pw5 = pg.PlotWidget(name='Plot5')  ## giving the plots names allows us to link their axes together
    curve5 = pw5.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw5)
    Val5 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr5 = -windowWidth                      # set first x position

    pw6 = pg.PlotWidget(name='Plot6')  ## giving the plots names allows us to link their axes together
    curve6 = pw6.plot(clear=True,pen={'color':'m', 'cosmetic':False, 'width': 2})
    l.addWidget(pw6)
    Val6 = linspace(0,0,windowWidth)          # create array that will contain the relevant time series
    ptr6 = -windowWidth                      # set first x position

    mw.show()

    def UpdateGraphnode(queuenodeBytes,Val,curve,ptr):
        global ptr1, Val1, curve1
        if not queuenodeBytes.empty():
            valueofgraph = queuenodeBytes.get(timeout=5)
            # print("node1", valueofgraph)
            Val[:-1] = Val[1:]
            Val[-1] = valueofgraph
            curve.setData(Val)
            curve.setPos(ptr,0)
            ptr+=1
            curve.setPen('w')

    def ReadBytenode(queueArraynode,queuenodeBytes):
        index = 0
        if not queueArraynode.empty():
            arrbytes = queueArraynode.get(timeout=5)
            # print("len",len(arrbytes))
            while index < 244:
                 byteofarr = float(arrbytes[index])
                 queuenodeBytes.put(byteofarr)
                 # print("byteofarr",byteofarr)
                 index =index + 1


    def ReadFunction(queue):
        global ser_bytes, id_number, pkt_cnt,ser_bytes_init, totalpacket
        ser_bytes_init = ser.read_until(expected=b"\xff\xff\xff", size=244)
        totalpacket = totalpacket + 1
        id_number = ser_bytes[0:1].hex()
        pkt_cnt = ser_bytes[2]
        queue.put(ser_bytes)

        if id_number == "31":
            queueArraynode1.put(ser_bytes)
        if id_number == "32":
            queueArraynode2.put(ser_bytes)
        if id_number == "33":
            queueArraynode3.put(ser_bytes)
        if id_number == "34":
            queueArraynode4.put(ser_bytes)
        if id_number == "35":
            queueArraynode5.put(ser_bytes)
        if id_number == "36":
            queueArraynode6.put(ser_bytes)




    if __name__ == "__main__":


        portName = "COM6"
        baudrate = 921600
        ser = serial.Serial(portName, baudrate,timeout = None)
        ser.flushInput()

        start = time.time()
        queue = multiprocessing.Queue()
        queueArraynode1 = multiprocessing.Queue()
        queueArraynode2 = multiprocessing.Queue()
        queueArraynode3 = multiprocessing.Queue()
        queueArraynode4 = multiprocessing.Queue()
        queueArraynode5 = multiprocessing.Queue()
        queueArraynode6 = multiprocessing.Queue()
        queuenode1Bytes = multiprocessing.Queue()
        queuenode2Bytes = multiprocessing.Queue()
        queuenode3Bytes = multiprocessing.Queue()
        queuenode4Bytes = multiprocessing.Queue()
        queuenode5Bytes = multiprocessing.Queue()
        queuenode6Bytes = multiprocessing.Queue()
        stopped = threading.Event()


        while True:
            try:
                QtCore.QCoreApplication.processEvents()
                p1 = threading.Thread(target=ReadFunction, args=(queue,))

                p3 = threading.Thread(target=ReadBytenode, args=(queueArraynode1,queuenode1Bytes,))
                p4 = threading.Thread(target=UpdateGraphnode, args=(queuenode1Bytes,Val1,curve1,ptr1,))

                p5 = threading.Thread(target=ReadBytenode, args=(queueArraynode2, queuenode2Bytes,))
                p6 = threading.Thread(target=UpdateGraphnode, args=(queuenode2Bytes,Val2,curve2,ptr2,))

                p7 = threading.Thread(target=ReadBytenode, args=(queueArraynode3, queuenode3Bytes,))
                p8 = threading.Thread(target=UpdateGraphnode, args=(queuenode3Bytes,Val3,curve3,ptr3,))

                p9 = threading.Thread(target=ReadBytenode, args=(queueArraynode4, queuenode4Bytes,))
                p10 = threading.Thread(target=UpdateGraphnode, args=(queuenode4Bytes,Val4,curve4,ptr4,))

                p11 = threading.Thread(target=ReadBytenode, args=(queueArraynode5, queuenode5Bytes,))
                p12 = threading.Thread(target=UpdateGraphnode, args=(queuenode5Bytes,Val5,curve5,ptr5,))

                p13 = threading.Thread(target=ReadBytenode, args=(queueArraynode6, queuenode6Bytes,))
                p14 = threading.Thread(target=UpdateGraphnode, args=(queuenode6Bytes,Val6,curve6,ptr6,))

                p1.start()
                p3.start()
                p4.start()
                p5.start()
                p6.start()
                p7.start()
                p8.start()
                p9.start()
                p10.start()
                p11.start()
                p12.start()
                p13.start()
                p14.start()
                p1.join()

            except:

                print("Keyboard Interrupt")
                stop = time.time()
                print("elapsed time:",stop-start)
                break
        pg.QtGui.QApplication.exec_()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文