Exemplo n.º 1
0
	def getversion(self):
		self.switchmode(GSProtocolHandler.MODE_PC)
		self._conn.write("v")

		versionstr = self._conn.waitforline(2)
		if versionstr is None:
			# Timeout, no response
			raise CommunicationException("timeout", "Timeout waiting for version reply.")
		versionstr = versionstr[1]

		result = { "Mode": None }
		if versionstr == "Standard":
			result["Mode"] = GSProtocolHandler.MODE_STANDARD
		elif GSProtocolHandlerVers2._version_pc_regex.match(versionstr):
			result["Mode"] = GSProtocolHandler.MODE_PC
			result["version"] = GSProtocolHandlerVers2._version_pc_regex[1]
			result["serial"] = int(GSProtocolHandlerVers2._version_pc_regex[2])
			result["buffill"] = int(GSProtocolHandlerVers2._version_pc_regex[3], 16)
			day = int(GSProtocolHandlerVers2._version_pc_regex[4])
			mon = int(GSProtocolHandlerVers2._version_pc_regex[5])
			year = int(GSProtocolHandlerVers2._version_pc_regex[6]) + 2000
			hour = int(GSProtocolHandlerVers2._version_pc_regex[7])
			mint = int(GSProtocolHandlerVers2._version_pc_regex[8])
			sec = int(GSProtocolHandlerVers2._version_pc_regex[9])
			result["datetime"] = datetime.datetime(year, mon, day, hour, mint, sec)
		else:
			raise CommunicationException("unparsable", "Unparsable version string '%s'." % (versionstr))
		return result
Exemplo n.º 2
0
	def switchmode(self, newmode):
		assert(newmode in GSProtocolHandler.VALID_MODES)
		if self._currentmode == newmode:
			# Already done!
			self._log.debug("Skipping mode switch %s to %s" % (str(self._currentmode), newmode))
			return
		self._log.info("Switching mode from %s to %s" % (str(self._currentmode), newmode))

		if newmode == GSProtocolHandler.MODE_STANDARD:
			# Switch to standard mode, end PC or online mode
			self._conn.write("X")

			# We need a loop here because the GS may be in online mode and may
			# respond with another interval before acknowledging our request to
			# leave the mode
			while True:
				datagram = self._conn.waitforline(1, 3.0)
				if datagram is None:
					raise CommunicationException("timeout", "No appropriate response within defined time when trying to switch to standard mode.")

				if datagram in [ "PC-Mode beendet", "Online-Mode beendet" ]:
					# Standard mode now active
					if datagram == "Online-Mode beendet":
						# The Gamma Scout online frequently chokes when leaving
						# the online mode. It sends at least one \x00 after the
						# last \r\n which means the interpretation of the next
						# message would be garbled if we do not clear the
						# buffer here. Additionally, it takes a rather long
						# time before being responsive to the next command
						# again. We solve both by waiting a bit (until it is
						# responsive again) and clearing the buffer afterwards
						# (to get rid of any excess \x00)
						time.sleep(0.5)
						self._conn.clearrxbuf()
					break
		elif newmode == GSProtocolHandler.MODE_PC:
			if self._currentmode == GSProtocolHandler.MODE_ONLINE:
				# Switch to standard mode first
				self.switchmode(GSProtocolHandler.MODE_STANDARD)

			# Switch to PC mode
			self._conn.write("P")
			self._conn.expectresponse("PC-Mode gestartet", 3)
		elif newmode == GSProtocolHandler.MODE_ONLINE:
			if self._currentmode == GSProtocolHandler.MODE_PC:
				# Switch to standard mode first
				self.switchmode(GSProtocolHandler.MODE_STANDARD)

			# Switch to PC mode
			self._conn.write("O")
			datagram = self._conn.waitforline(1)
			if (datagram is None) or (not datagram.startswith("S")):
				raise CommunicationException("unparsable", "Unparsable response when trying to enter online mode; expected \"S...\" but got '%s'" % (str(datagram)))

		self._currentmode = newmode
Exemplo n.º 3
0
	def expectresponse(self, string, timeout = 1.0):
		assert(isinstance(string, str))

		datagram = self._rxbuf.waitforline(2, timeout * self._args["timeout_factor"])
		if datagram is None:
			raise CommunicationException("timeout", "Waiting for response datagram '%s' timed out after %.1f secs." % (string, timeout))

		if datagram[0] != "":
			raise CommunicationException("unparsable", "Waiting for first response datagram returned '%s' while expecting empty string." % (str(datagram)))
		if datagram[1] != string:
			raise CommunicationException("unparsable", "Waiting for second response datagram returned '%s' while expecting '%s'." % (str(datagram), string))
Exemplo n.º 4
0
	def initmode(self):
		# We have no idea in which state the Gamma Scout is at the moment, so
		# we need to find out which one it is. We can always switch to Standard
		# mode, but this will cause no reaction if it is already in standard
		# mode (and therefore cause a delay because of the subsequent timeout).
		# Since assuming it is in default mode is a good assumption, we'll try
		# to get a version reply first and only switch to standard mode if that
		# fails
		self._currentmode = None
		self._conn.write("v")
		versionstr = self._conn.waitforline(2)
		if (versionstr is None) or (versionstr[1] != "Standard"):
			# This didn't work. Is it maybe a v1 device that we try to speak to
			# using a v2 protocol?
			if versionstr is not None:
				match = GSProtocolHandlerVers2._version_v1_regex.match(versionstr[1])
				if match is not None:
					# Set connection to None to avoid trying to close it
					self._conn.close()
					self._conn = None
					raise CommunicationException("feature", "You are trying to communicate with a v1 Gamma Scout (version %s) using the v2 protocol. Please select protocol version v1." % (match.groupdict()["version"]))
			self._log.info("Initial GS mode determination failed, returned %s." % (str(versionstr)))
			self.switchmode(GSProtocolHandler.MODE_STANDARD)
		else:
			self._log.debug("Initial GS mode determination succeeded, GS in standard mode.")
			self._currentmode = GSProtocolHandler.MODE_STANDARD
Exemplo n.º 5
0
    def getversion(self):
        self._conn.write("v")
        versionstr = self._conn.waitforline(2)
        if versionstr is None:
            # Timeout, no response
            raise CommunicationException("timeout",
                                         "Timeout waiting for version reply.")
        versionstr = versionstr[1]

        result = {"Mode": GSProtocolHandler.MODE_PC}
        if GSProtocolHandlerVers1._version_pc_regex.match(versionstr):
            result["version"] = GSProtocolHandlerVers1._version_pc_regex[1]
        else:
            if versionstr in ["Standard", "ONLINE"]:
                raise CommunicationException(
                    "feature",
                    "You are trying to communicate with a v2 Gamma Scout using the v1 protocol. Please select protocol version v2."
                )
            raise CommunicationException(
                "unparsable", "Unparsable version string '%s'." % (versionstr))
        return result
Exemplo n.º 6
0
	def readconfig(self):
		self.switchmode(GSProtocolHandler.MODE_PC)
		self._conn.write("c")
		linecnt = 0
		result = { }
		log = [ ]
		while True:
			linecnt += 1
			nextmsg = self._conn.waitforline()
			if nextmsg is None:
				break
			if linecnt == 2:
				if not GSProtocolHandlerVers2._config_firstline_regex.match(nextmsg):
					raise CommunicationException("unparsable", "First configuration data line format unexpected (received '%s')." % (nextmsg))
				log.append(int(GSProtocolHandlerVers2._config_firstline_regex[1], 16))
				log.append(int(GSProtocolHandlerVers2._config_firstline_regex[2], 16))
				nextmsg = GSProtocolHandlerVers2._config_firstline_regex[3]

			if linecnt >= 2:
				logdata = [ int(nextmsg[2 * i : 2 * i + 2], 16) for i in range(len(nextmsg) // 2) ]
				log += logdata
		return bytes(log)
Exemplo n.º 7
0
	def readlog(self):
		self.switchmode(GSProtocolHandler.MODE_PC)
		buffill = self.getversion()["buffill"]
		self._conn.write("b")
		self._conn.expectresponse("GAMMA-SCOUT Protokoll")

		log = [ ]
		linecnt = 0
		while True:
			linecnt += 1
			nextmsg = self._conn.waitforline()
			if nextmsg is None:
				break
			if (len(nextmsg) % 2) != 0:
				raise CommunicationException("unparsable", "Protocol line was not a multiple of two bytes (%d bytes received)." % (len(nextmsg)))

			logdata = [ int(nextmsg[2 * i : 2 * i + 2], 16) for i in range(len(nextmsg) // 2) ]
			calcchksum = GSProtocolHandlerVers2._linechecksum(logdata)
			if calcchksum != logdata[-1]:
				# Warn about this only, cannot do anything anyways
				print("Warning: Log line %d has checksum error, calculated 0x%x, transmitted 0x%x." % (linecnt, calcchksum, logdata[-1]), file = sys.stderr)

			log += logdata[:-1]
		return (buffill, bytes(log))
Exemplo n.º 8
0
 def switchmode(self, newmode):
     # Not possible in v1
     raise CommunicationException(
         "feature",
         "Gamma Scout Basic does not support switching the mode.")
Exemplo n.º 9
0
 def readconfig(self):
     raise CommunicationException(
         "feature",
         "Gamma Scout Basic does not support reading configuration block.")
Exemplo n.º 10
0
	def expectresponse(self, *args):
		raise CommunicationException("feature", "This command is not possible with the --nodevice parameter set.")
Exemplo n.º 11
0
	def write(self, data):
		raise CommunicationException("feature", "This command is not possible with the --nodevice parameter set.")