Exemple #1
0
    def run(self):
        while True:
            for port in self.ports:
                if port in self.found: continue
                try:
                    serial = SerialInterface(port, 115200)
                    serial.connectToArduino()
                    if serial.isConnected():
                        serial.serial_conn.timeout = 0.2 # read timeout of 200 millisecond
                        samples = serial.getSamples(20)
                        
                        if samples:
                            serial.serial_conn.timeout = None
                            self.foundDevice.emit(port, serial)
                            self.found.append(port)
                        else:
                            serial.disconnect()

                except Exception, e:
                    #print port, e
                    continue
                finally:
                    if serial.isConnected() and not port in self.found:
class MainWindow(QtGui.QMainWindow):

	def __init__(self):
		QtGui.QMainWindow.__init__(self, None)
		uic.loadUi('mainwindow.ui', self)
		self.resize(700, 500)

		self.plotWindow = PlotWindow()
		self.stackedPlots.addWidget(self.plotWindow)
		self.stackedPlots.setCurrentIndex(0)

		self.actionRefresh.triggered[bool].connect(self.refreshList)
		self.actionConnect.triggered[bool].connect(self.connectFromList)
		self.actionDisconnect.triggered[bool].connect(self.disconnectFromCurrentDevice)
		self.actionLock.toggled[bool].connect(self.hold)
		self.actionViewAll.toggled[bool].connect(self.autoSet)
		self.actionExport.triggered[bool].connect(self.exportData)
		self.actionQuit.triggered[bool].connect(self.quitApp)
		self.actionAbout.triggered[bool].connect(self.aboutApp)

		self.listDevices.itemSelectionChanged.connect(self.deviceListChanged)

		self.actionViewAll.setChecked(True)
		self.autoSet(self.actionViewAll.isChecked())

		self.ifaces = []
		self.port = None
		self.baud = 115200
		self.iface = None
		self.iface_thread = None

		self.initDeviceFinder()
		self.disableControls()

		self.deviceListChanged()


		self.statusBar().showMessage('Ready.')
		
	def deviceListChanged(self):
		if self.listDevices.count() > 0:
			self.actionConnect.setEnabled(True)
			self.enableControls()
		else:
			self.actionConnect.setEnabled(False)
			self.disableControls()

	def disableControls(self):
		self.plotWindow.setEnabled(False)
		self.actionDisconnect.setEnabled(False)
		self.actionLock.setEnabled(False)
		self.actionViewAll.setEnabled(False)
		self.actionExport.setEnabled(False)

	def enableControls(self):
		self.plotWindow.setEnabled(True)
		self.actionDisconnect.setEnabled(True)
		self.actionLock.setEnabled(True)
		self.actionViewAll.setEnabled(True)
		self.actionExport.setEnabled(True)

	def initDeviceFinder(self):
		self.device_finder = DeviceFinder()
		self.device_finder.foundDevice[str, SerialInterface].connect(self.gotNewDevice)
		self.device_finder.start()

	def gotNewDevice(self, port, iface):
		self.port = port
		self.listDevices.addItem(QtGui.QListWidgetItem(QtGui.QIcon('icons/device.png'), port))
		self.ifaces.append(iface)

		self.statusBar().showMessage('Found %d device(s).' % self.listDevices.count())

	def startSampling(self):
		self.iface_thread.start()

	def stopSampling(self):
		self.iface_thread.stop__()
		self.iface_thread.exit()

	def connectDevice(self):
		if not self.iface:
			self.iface = self.ifaces[self.listDevices.currentRow()]

		if self.iface:
			if not self.iface.isConnected():
				self.iface = SerialInterface(self.port, self.baud)
				self.iface.connectToArduino()

			if not self.iface.serial_conn.isOpen():
				self.iface.serial_conn.open()
				
			if self.iface.isConnected():
				if self.iface_thread:
					del self.iface_thread
			
				self.iface_thread = SerialThread(self.iface)
				self.iface_thread.n_samples = 1000
				self.iface_thread.gotSamples[tuple, int].connect(self.plotWindow.plotSamples)
				#self.iface_thread.deviceDisconnected.connect(self.disconnectFromCurrentDevice)

				self.startSampling()

				return True

		return False

	def refreshList(self):
		self.statusBar().showMessage('Searching...')

		for i in range(0, self.listDevices.count()):
			port = self.listDevices.item(i).text()
			self.device_finder.found.remove(port)

		self.listDevices.clear()
		self.ifaces = []

	def connectFromList(self):
		self.actionConnect.setEnabled(False)
		self.actionDisconnect.setEnabled(True)
		self.actionRefresh.setEnabled(False)

		#self.connectDevice(self.listDevices.currentItem().text())
		if self.connectDevice():
			self.enableControls()
		else:
			QtGui.QMessageBox.critical(self, 'Error', 'Cannot connect to the device, refresh the list')

	def disconnectFromCurrentDevice(self):
		self.actionConnect.setEnabled(True)
		self.actionDisconnect.setEnabled(False)
		self.actionRefresh.setEnabled(True)
		self.disableControls()

		try:
			self.stopSampling()
			self.iface.disconnect()
		except:
			pass

		self.iface = None
		self.plotWindow.plot_item_a.clear()
		self.plotWindow.plot_item_p.clear()

	def exportData(self):
		self.plotWindow.scene().showExportDialog()

	def hold(self, checked):
		if checked:
			self.stopSampling()
		else:
			self.connectDevice()
			self.startSampling()

	def autoSet(self, checked):
		if checked:
			self.plotWindow.plot_item_a.enableAutoRange()
			self.plotWindow.plot_item_p.enableAutoRange()
		else:
			self.plotWindow.plot_item_a.disableAutoRange()
			self.plotWindow.plot_item_p.disableAutoRange()


	def quitApp(self):
		try:
			self.disconnectFromCurrentDevice()
		except:
			pass

		QtGui.QApplication.exit(0)

	def aboutApp(self):
		QtGui.QMessageBox.about(self, 'About',
							    """<b>Spectrum Analyzer</b><br>Computer Peripherals Interface Project<br><b>By:</b><br>Sherif Adel Radwan<br>Mahmoud Sayed Zainhom<br>Abdelrahman Ghanem Abdelrady""")
	
	def closeEvent(self, event):
		if self.iface:
			self.iface.disconnect()
Exemple #3
0
class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self, None)
        uic.loadUi('mainwindow.ui', self)
        self.resize(700, 500)

        self.plotWindow = PlotWindow()
        self.stackedPlots.addWidget(self.plotWindow)
        self.stackedPlots.setCurrentIndex(0)

        self.actionRefresh.triggered[bool].connect(self.refreshList)
        self.actionConnect.triggered[bool].connect(self.connectFromList)
        self.actionDisconnect.triggered[bool].connect(
            self.disconnectFromCurrentDevice)
        self.actionLock.toggled[bool].connect(self.hold)
        self.actionViewAll.toggled[bool].connect(self.autoSet)
        self.actionExport.triggered[bool].connect(self.exportData)
        self.actionQuit.triggered[bool].connect(self.quitApp)
        self.actionAbout.triggered[bool].connect(self.aboutApp)

        self.listDevices.itemSelectionChanged.connect(self.deviceListChanged)

        self.actionViewAll.setChecked(True)
        self.autoSet(self.actionViewAll.isChecked())

        self.ifaces = []
        self.port = None
        self.baud = 115200
        self.iface = None
        self.iface_thread = None

        self.initDeviceFinder()
        self.disableControls()

        self.deviceListChanged()

        self.statusBar().showMessage('Ready.')

    def deviceListChanged(self):
        if self.listDevices.count() > 0:
            self.actionConnect.setEnabled(True)
            self.enableControls()
        else:
            self.actionConnect.setEnabled(False)
            self.disableControls()

    def disableControls(self):
        self.plotWindow.setEnabled(False)
        self.actionDisconnect.setEnabled(False)
        self.actionLock.setEnabled(False)
        self.actionViewAll.setEnabled(False)
        self.actionExport.setEnabled(False)

    def enableControls(self):
        self.plotWindow.setEnabled(True)
        self.actionDisconnect.setEnabled(True)
        self.actionLock.setEnabled(True)
        self.actionViewAll.setEnabled(True)
        self.actionExport.setEnabled(True)

    def initDeviceFinder(self):
        self.device_finder = DeviceFinder()
        self.device_finder.foundDevice[str, SerialInterface].connect(
            self.gotNewDevice)
        self.device_finder.start()

    def gotNewDevice(self, port, iface):
        self.port = port
        self.listDevices.addItem(
            QtGui.QListWidgetItem(QtGui.QIcon('icons/device.png'), port))
        self.ifaces.append(iface)

        self.statusBar().showMessage('Found %d device(s).' %
                                     self.listDevices.count())

    def startSampling(self):
        self.iface_thread.start()

    def stopSampling(self):
        self.iface_thread.stop__()
        self.iface_thread.exit()

    def connectDevice(self):
        if not self.iface:
            self.iface = self.ifaces[self.listDevices.currentRow()]

        if self.iface:
            if not self.iface.isConnected():
                self.iface = SerialInterface(self.port, self.baud)
                self.iface.connectToArduino()

            if not self.iface.serial_conn.isOpen():
                self.iface.serial_conn.open()

            if self.iface.isConnected():
                if self.iface_thread:
                    del self.iface_thread

                self.iface_thread = SerialThread(self.iface)
                self.iface_thread.n_samples = 1000
                self.iface_thread.gotSamples[tuple, int].connect(
                    self.plotWindow.plotSamples)
                #self.iface_thread.deviceDisconnected.connect(self.disconnectFromCurrentDevice)

                self.startSampling()

                return True

        return False

    def refreshList(self):
        self.statusBar().showMessage('Searching...')

        for i in range(0, self.listDevices.count()):
            port = self.listDevices.item(i).text()
            self.device_finder.found.remove(port)

        self.listDevices.clear()
        self.ifaces = []

    def connectFromList(self):
        self.actionConnect.setEnabled(False)
        self.actionDisconnect.setEnabled(True)
        self.actionRefresh.setEnabled(False)

        #self.connectDevice(self.listDevices.currentItem().text())
        if self.connectDevice():
            self.enableControls()
        else:
            QtGui.QMessageBox.critical(
                self, 'Error',
                'Cannot connect to the device, refresh the list')

    def disconnectFromCurrentDevice(self):
        self.actionConnect.setEnabled(True)
        self.actionDisconnect.setEnabled(False)
        self.actionRefresh.setEnabled(True)
        self.disableControls()

        try:
            self.stopSampling()
            self.iface.disconnect()
        except:
            pass

        self.iface = None
        self.plotWindow.plot_item_a.clear()
        self.plotWindow.plot_item_p.clear()

    def exportData(self):
        self.plotWindow.scene().showExportDialog()

    def hold(self, checked):
        if checked:
            self.stopSampling()
        else:
            self.connectDevice()
            self.startSampling()

    def autoSet(self, checked):
        if checked:
            self.plotWindow.plot_item_a.enableAutoRange()
            self.plotWindow.plot_item_p.enableAutoRange()
        else:
            self.plotWindow.plot_item_a.disableAutoRange()
            self.plotWindow.plot_item_p.disableAutoRange()

    def quitApp(self):
        try:
            self.disconnectFromCurrentDevice()
        except:
            pass

        QtGui.QApplication.exit(0)

    def aboutApp(self):
        QtGui.QMessageBox.about(
            self, 'About',
            """<b>Spectrum Analyzer</b><br>Computer Peripherals Interface Project<br><b>By:</b><br>Sherif Adel Radwan<br>Mahmoud Sayed Zainhom<br>Abdelrahman Ghanem Abdelrady"""
        )

    def closeEvent(self, event):
        if self.iface:
            self.iface.disconnect()