示例#1
0
	def pconnect(self):
		try:
			self.driver.disconnect()
		except: # Don't care if couldn't disconnect properly
			pass

		self.driver = Driver(readTimeout=self.config.timeout, writeTimeout=self.config.timeout)

		retry_connection = True

		# TODO replace reconnection loop with a reconnect timer and signal (emit a reconnect signal which
		# will start a timer and start the connection process when timer ends.
		while retry_connection and self.getRunning():
			try:
				if len(self.config.probes) == 0:
					# If no probes specified in config. Get from search.
					(prefix, self.count) = self.driver.setup()
					self.config.probes = ["%s%02d" % (prefix, i) for i in range(1, self.count+1)]
				else:
					(prefix, self.count) = self.driver.setup(probes=self.config.probes)
				self.setZeroValues(probe=False)
				retry_connection = False
				self.status("Connected.")
			except SerialException:
				self.status("Connection failed. Check hardware. Retrying in %d seconds." % self.config.retry)
				sleep(self.config.retry)
			except ValueError:
				self.status("Setup failure. Retrying in %d seconds." % self.config.retry)
				sleep(self.config.retry)
示例#2
0
class DataPooler(QtCore.QThread):
	"""Poll device for values."""

	def __init__(self, config, *args):
		QtCore.QThread.__init__(self, *args)
		self.running_mutex = QtCore.QMutex()
		self.zero_mutex = QtCore.QMutex()
		self.config = config
		self.running = True

	def status(self, message):
		self.emit(QtCore.SIGNAL("status"), message)

	def pconnect(self):
		try:
			self.driver.disconnect()
		except: # Don't care if couldn't disconnect properly
			pass

		self.driver = Driver(readTimeout=self.config.timeout, writeTimeout=self.config.timeout)

		retry_connection = True

		# TODO replace reconnection loop with a reconnect timer and signal (emit a reconnect signal which
		# will start a timer and start the connection process when timer ends.
		while retry_connection and self.getRunning():
			try:
				if len(self.config.probes) == 0:
					# If no probes specified in config. Get from search.
					(prefix, self.count) = self.driver.setup()
					self.config.probes = ["%s%02d" % (prefix, i) for i in range(1, self.count+1)]
				else:
					(prefix, self.count) = self.driver.setup(probes=self.config.probes)
				self.setZeroValues(probe=False)
				retry_connection = False
				self.status("Connected.")
			except SerialException:
				self.status("Connection failed. Check hardware. Retrying in %d seconds." % self.config.retry)
				sleep(self.config.retry)
			except ValueError:
				self.status("Setup failure. Retrying in %d seconds." % self.config.retry)
				sleep(self.config.retry)

	def run(self):
		"""Probe polling loop."""

		self.pconnect()
		while self.getRunning():
			for i in range(1, self.count+1):
				self.running_mutex.lock()
				try:
					result = self.driver.read("1", i)
				except OSError:
					self.status("Disconnected.")
					self.pconnect()
				except OrbitError:
					self.status("Read Error.")
					self.pconnect()
				if isinstance(result, float):
					if isinstance(self.zero[i-1], float):
						result -= self.zero[i-1]
					result = float("%.*f" % (self.config.precision, result))
				self.running_mutex.unlock()
				self.emit(QtCore.SIGNAL("dataRetrieved"), i, result)
			sleep(self.config.delay)
		self.driver.disconnect()

	def setZeroValues(self, probe=True):
		self.zero_mutex.lock()
                try:
                        if probe:
                        	self.zero = [self.driver.read("1", i) for i in range(1, self.count+1)]
                        else:
                                self.zero = [0.0 for i in range(1, self.count+1)]
                except AttributeError:
                        self.status("Error settings zero point. Please try again.")
		self.zero_mutex.unlock()

	def getRunning(self):
		"""Get application running status."""

		self.running_mutex.lock()
		running = self.running
		self.running_mutex.unlock()
		return running

	def exit(self):
		"""Stop polling device."""

		self.running_mutex.lock()
		self.running = False
		self.running_mutex.unlock()