""" This example demonstrates many of the 2D plotting capabilities in pyqtgraph. All of the plots may be panned/scaled by dragging with the left/right mouse buttons. Right click on any plot to show a context menu. """ import serial, struct import numpy as np import pyqtgraph as pg from pyqtgraph.Qt import QtCore if __name__ == '__main__': ser = serial.Serial("COM2", 115200, timeout=250) # 打开COM17,将波特率配置为115200,其余参数使用默认值 if ser.isOpen(): # 判断串口是否成功打开 print("打开串口成功。") # print(ser.name) # 输出串口号 else: print("打开串口失败。") app = pg.mkQApp("Plotting Example") #mw = QtWidgets.QMainwin1dow() #mw.resize(800,800) win1 = pg.GraphicsLayoutWidget(show=True, title="Basic plotting examples") win1.resize(1000,600) win1.setWindowTitle('pyqtgraph example: Plotting1') # Enable antialiasing for prettier plots pg.setConfigOptions(antialias=True) win1.nextRow() win1.nextCol() p6 = win1.addPlot(title="Updating plot") curve = p6.plot(pen=None, symbolBrush=(255,0,0), symbolPen='w', col=3, row=3,rowspan=2, colspan=5) lr = pg.LinearRegionItem([400,700]) lr.setZValue(-10) p6.addItem(lr) data = np.zeros(1024) # points = int(np.random.random()*1024) points = bytearray() ptr = 0 def update(): global curve, data, ptr, p6, ser, points points += ser.read_all() if len(points) > 2: for i in range(len(points)//2): data[struct.unpack('>H', points[2*i:2*i+2])[0]>>6] += 1 points = points[2*int(len(points)/2):] curve.setData(data) if ptr == 0: p6.enableAutoRange('xy', True) ## stop auto-scaling after the first data set is plotted ptr += 1 timer = QtCore.QTimer() timer.timeout.connect(update) timer.start(250) win1.nextCol() win1.nextRow() p9 = win1.addPlot(title="Zoom on selected region", col=1, row=3,rowspan=3, colspan=1) p9.plot(data, symbolBrush=(255,0,0), symbolPen='w') def updatePlot(): p9.setXRange(*lr.getRegion(), padding=0) def updateRegion(): lr.setRegion(p9.getViewBox().viewRange()[0]) lr.sigRegionChanged.connect(updatePlot) p9.sigXRangeChanged.connect(updateRegion) updatePlot() # win2 = pg.GraphicsLayoutWidget(show=True, title="Basic plotting examples") # win2.resize(1000,600) # win2.setWindowTitle('pyqtgraph example: Plotting') pg.exec() ser.close()