- 数据采集的显示需要:
- 数据格式不同:解析格式
- 支持缩放,平移等
- 图片导出
- 丰富的展示方式
- 很自然地,从头开发是一个很LOW的选择,借助开源的力量和开放的环境,利用成熟的部分进行堆叠开发
- 技术栈
- MISA
- matplotlib
- pyqt
- python
-
matplotlib可以集成到QT中
- 很自然,和流畅的实现。开放合作才能共赢。
不要将有限的生命耗费在重复的事情上面
采集显示
- 代码量?<300行
import sys
import time
import numpy as np
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import mrq
import mrq.MRQ
import datetime
import time
from matplotlib.backends.qt_compat import QtCore, QtWidgets, is_pyqt5
if is_pyqt5():
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
else:
from matplotlib.backends.backend_qt4agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
class TestItem():
def __init__(self, deviceName = "", dev=None, ch=0, item = ""):
self.mCache = 1024
self.mDeviceName = deviceName
self.mHandle = dev
self.mCH = ch
self.mItem = item
self.mInfo=""
self.mName=""
self.mT =[]
self.mSubNames=[]
self.mSubVals=[ ]
def setItem( self, dev, ch, item ):
self.mDev = dev
self.mCH = ch
self.mItem = item
def setName( self, n ):
self.mName = n
def name( self ):
return self.mName
def setInfo( self, inf ):
self.mInfo = inf
def info( self ):
return self.mInfo
def shift( self, ary ):
vecLen = len( ary )
if ( vecLen >= self.mCache ):
ary = ary[ vecLen - self.mCache + 1 : ]
def sampleProc( self ):
if ( self.mHandle != None and len(self.mItem) > 0 ):
pass
else:
return
ret, val = self.mHandle.getREPORT_DATA( self.mCH, self.mItem )
if ( ret == 0 ):
self.deParseProc( val )
self.mT.append( datetime.datetime.now() )
def deParseProc( self, val ):
if ( self.mItem == "TORQUE" ):
# the first time
if ( len(self.mSubNames) < 1 ):
self.mSubNames.append( "SG" )
self.mSubNames.append( "SE" )
self.mSubVals.append( [ (val >> 8) & 0xff ] )
self.mSubVals.append( [ (val >> 0) & 0xff ] )
else:
self.shift( self.mSubVals[0] )
self.shift( self.mSubVals[1] )
self.mSubVals[0].append( (val >> 8) & 0xff )
self.mSubVals[1].append( (val >> 0) & 0xff )
else:
if ( len(self.mSubNames) < 1 ):
self.mSubNames.append( "" )
self.mSubVals.append( [ val ] )
else:
self.shift( self.mSubVals[0] )
self.mSubVals[0].append( val )
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3)))
layout.addWidget(dynamic_canvas)
# for ui
self.ui_deviceCombox = QtWidgets.QComboBox()
self.ui_chCombox = QtWidgets.QComboBox()
self.ui_itemCombox = QtWidgets.QComboBox()
layout.addWidget( self.ui_deviceCombox )
layout.addWidget( self.ui_chCombox )
layout.addWidget( self.ui_itemCombox )
hLayout = QtWidgets.QHBoxLayout()
layout.addLayout( hLayout )
self.ui_btnAdd = QtWidgets.QToolButton()
self.ui_btnDel = QtWidgets.QToolButton()
self.ui_btnStart = QtWidgets.QToolButton()
self.ui_btnStop = QtWidgets.QToolButton()
hLayout.addWidget(self.ui_btnAdd)
hLayout.addWidget(self.ui_btnDel)
hLayout.addWidget(self.ui_btnStart)
hLayout.addWidget(self.ui_btnStop)
self.addToolBar( NavigationToolbar(dynamic_canvas, self) )
self._dynamic_ax = dynamic_canvas.figure.subplots()
# self._dynamic_ax = dynamic_canvas.figure()
self._timer = dynamic_canvas.new_timer(
1000, [(self._update_canvas, (), {})])
# self._timer.start()
# member
self.setupMembers()
# setup
self.setupUi( None )
# build connection
self.buildConnection()
def setupMembers( self ):
self.m_items = []
self.m_datas = []
self.m_devices={} # deviceName: dev
def setupUi( self, dev ):
self.ui_deviceCombox.addItem( "device1" )
self.ui_deviceCombox.addItem( "device2" )
self.ui_chCombox.addItem( "CH1" )
self.ui_chCombox.addItem( "CH2" )
self.ui_chCombox.addItem( "CH3" )
self.ui_chCombox.addItem( "CH4" )
self.ui_itemCombox.addItem("TORQUE")
self.ui_btnAdd.setText( "Add" )
self.ui_btnDel.setText( "Remove" )
self.ui_btnStart.setText("Start")
self.ui_btnStop.setText("Stop")
def buildConnection( self ):
self.ui_btnAdd.clicked.connect( self.onAddClicked )
self.ui_btnDel.clicked.connect( self.onDelClicked )
self.ui_btnStart.clicked.connect( self.onStartClicked )
self.ui_btnStop.clicked.connect( self.onStopClicked )
def findItem( self, deviceName, ch, item ):
for item in self.m_items:
if ( item.mDeviceName == deviceName and item.mCH == ch and item.mItem == item ):
return item
return None
def addItem( self, deviceName, handle, ch, item ):
# check exist
if ( self.findItem( deviceName, ch, item ) ):
return
else:
pass
# add item
self.m_items.append( TestItem( deviceName, handle, ch, item) )
def delItem( self, deviceName, ch, item ):
item = self.findItem( deviceName, ch, item )
if ( item != None ):
self.m_items.remove( item )
def lutDevice( self, deviceName ):
# device ready
if ( deviceName in self.m_devices.keys() ):
return self.m_devices[ deviceName ]
# open device
self.m_devices[ deviceName ] = mrq.MRQ.MRQ( deviceName )
return self.m_devices[ deviceName ]
def onAddClicked( self ):
selDevice = self.lutDevice( self.ui_deviceCombox.currentText() )
self.addItem( self.ui_deviceCombox.currentText(),
selDevice,
self.ui_chCombox.currentIndex(),
self.ui_itemCombox.currentText() )
def onDelClicked( self ):
self.delItem( self.ui_deviceCombox.currentText(),
self.ui_chCombox.currentIndex(),
self.ui_itemCombox.currentText() )
def onStartClicked( self ):
self._timer.start()
self.ui_btnStart.setEnabled( False )
self.ui_btnStop.setEnabled( True )
def onStopClicked( self ):
self._timer.stop()
self.ui_btnStart.setEnabled( True )
self.ui_btnStop.setEnabled( False )
def _update_canvas(self):
self._dynamic_ax.clear()
self.sampleProc()
self.updatePlot()
def sampleProc( self ):
# sample
for subItem in self.m_items:
subItem.sampleProc( )
def updatePlot( self ):
for subItem in self.m_items:
for i in range( 0, len( subItem.mSubVals) ):
self._dynamic_ax.plot( ( subItem.mT ),
( subItem.mSubVals[i] ),
".",
label="%s.CH%d.%s.%s" % ( subItem.mDeviceName, subItem.mCH, subItem.mItem, subItem.mSubNames[i] ) )
self._dynamic_ax.legend()
self._dynamic_ax.figure.canvas.draw()
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
app.setWindowTitle( "Mega Plot" )
qapp.exec_()
网友评论