diff -r 3f123b64d367 -r 4cbd3d410230 pygpibtoolkit/plotter/qgpib_plotter.py --- a/pygpibtoolkit/plotter/qgpib_plotter.py Fri May 18 01:07:28 2018 +0200 +++ b/pygpibtoolkit/plotter/qgpib_plotter.py Sun Jun 03 18:04:11 2018 +0200 @@ -19,9 +19,10 @@ import time from PyQt5 import QtWidgets, QtGui, QtCore, QtPrintSupport, uic -from PyQt5.QtCore import Qt +from PyQt5.QtCore import Qt, pyqtSignal from pygpibtoolkit.plotter.hpgl_qt import QHPGLPlotterWidget +from pygpibtoolkit.plotter.gpib_plotter import GPIBplotter from pygpibtoolkit.qt5.qpreferences import IntItem, UnicodeItem, ColorItem from pygpibtoolkit.qt5.qpreferences import BoolItem @@ -40,14 +41,10 @@ os.path.join(HERE, "qhpgl_plotter.ui"), from_imports=True, import_from='pygpibtoolkit.plotter',) except Exception as e: - print(e) - from pygpibtoolkit.plotter.qhpgl_plotter_ui import Ui_MainWindow as form_class + from pygpibtoolkit.plotter.qhpgl_plotter_ui import ( + Ui_MainWindow as form_class) -if "-m" in sys.argv: - from pygpibtoolkit.plotter.gpib_plotter_mockup import GPIBplotter -else: - from pygpibtoolkit.plotter.gpib_plotter import GPIBplotter class Preferences(AbstractPreferences): @@ -97,7 +94,8 @@ autodisplay = BoolItem( default=True, name="Auto display", - description="Automatically display a new plot as soon as it is received", + description=( + "Automatically display a new plot as soon as it is received"), group="Misc") @@ -151,7 +149,8 @@ self._plots_list = QtCore.QStringListModel() self.plotsView.setModel(self._plots_list) self.plotsView.activated.connect(self.currentPlotChanged) - self.plotsView.selectionModel().currentChanged.connect(self.currentPlotChanged) + self.plotsView.selectionModel().currentChanged.connect( + self.currentPlotChanged) self.setReceivingLed() def currentPlotChanged(self, index, old_index=None): @@ -170,8 +169,8 @@ self.close() def closeEvent(self, event): + # if self.promptForSave(): if 1: - #if self.promptForSave(): self._prefs._pos = self.pos() self._prefs._size = self.size() self._prefs._appState = self.saveState() @@ -193,7 +192,6 @@ def openFiles(self, filenames): ok = False for filename in filenames: - print('FNAE=', filename) if os.path.exists(filename): data = open(filename).read() name = os.path.basename(filename) @@ -260,9 +258,10 @@ def initializeGPIB(self): self._online = False try: - self.gpib_plotter = QGPIBplotter(device=self._prefs.device, - address=self._prefs.address, - ) + self.gpib_plotter = GPIBplotter( + device=self._prefs.device, + address=self._prefs.address, + ) self.captureThread = GPIBReceiver(self.gpib_plotter) self.captureThread.plotReceived.connect(self.plotReceived) self.captureThread.plotStarted.connect(self.plotStarted) @@ -278,9 +277,9 @@ self.initializeGPIB() if self.gpib_plotter is None: QtWidgets.QMessageBox.critical( - self, self.tr("GPIB error"), - self.tr("Unable to initialize GPIB connection." - "
Please check your GPIB dongle and settings.")) + self, "GPIB error", + "Unable to initialize GPIB connection." + "
Please check your GPIB dongle and settings.") self._online = False self.setCaptureLed() return @@ -308,57 +307,37 @@ self.receivingButton.setIcon(icn) -class QGPIBplotter(GPIBplotter): - def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, - address=5): - super().__init__(device, baudrate, timeout, address) - self.emitter = None - - def plotStarted(self): - if self.emitter: - self.emitter.emit(SIGNAL('plotStarted()')) - # self.emitter.msleep(1) - class GPIBReceiver(QtCore.QThread): + plotStarted = pyqtSignal() + plotReceived = pyqtSignal(int) + def __init__(self, cnx, parent=None): super().__init__(parent) self.gpibplotter = cnx - self.gpibplotter.emitter = self + self.gpibplotter.plot_started_cb = self.plotStarted.emit - self._cancelmutex = QtCore.QMutex() self._cancel = False # self._nreceived = 0 self._plotsmutex = QtCore.QMutex() self._plots = [] - self._startstopmutex = QtCore.QMutex() - self._startstop = QtCore.QWaitCondition() self._capturing = False def cancel(self): - self._cancelmutex.lock() self._cancel = True - self._cancelmutex.unlock() def startCapture(self): - self._startstop.wakeOne() + self._capturing = True def stopCapture(self): - self._startstopmutex.lock() self._capturing = False - self._startstopmutex.unlock() def run(self): - while 1: - self._cancelmutex.lock() - if self._cancel: - return - self._cancelmutex.unlock() - self._startstopmutex.lock() + while not self._cancel: if not self._capturing: - self._startstop.wait(self._startstopmutex) - self._capturing = True - self._startstopmutex.unlock() + self.msleep(100) + continue + plot = self.gpibplotter.load_plot(wait_timeout=0.1) timestamp = time.time() if plot: @@ -366,7 +345,8 @@ self._plots.append((plot, timestamp)) n = len(self._plots) self._plotsmutex.unlock() - self.emit(SIGNAL('plotReceived(int)'), n-1) + + self.plotReceived.emit(n - 1) self.msleep(10) def getPlot(self, num): @@ -378,6 +358,8 @@ def main(): + global GPIBplotter + import optparse opt = optparse.OptionParser( 'A simple PyQt4 HP7470A GPIB plotter emulator for USB-GPIB bundle ' @@ -393,6 +375,8 @@ help="Verbose mode",) options, argv = opt.parse_args(sys.argv) + if options.mockup: + from pygpibtoolkit.plotter.gpib_plotter_mockup import GPIBplotter a = QtWidgets.QApplication(argv) w = QtHPGLPlotter()