def next_chunk(self): """ :returns: The next chunk of data points in the form of a SignalWave :rtype: :class:`tendril.utils.types.signalbase.SignalWave` """ rval = copy.copy(self.point_buffer) self.point_buffer = SignalWave(rval.unitclass, points=deque([rval.pop()], maxlen=rval._buffer_size), spacing=rval._spacing, ts0=rval._ts0, buffer_size=rval._buffer_size, use_point_ts=rval._use_point_ts) return rval
def reset_buffer(self, unitclass=DummyUnit): """ Resets the point buffer to a new :class:`tendril.utils.types.signalbase.SignalWave` with the unitclass as specified by the parameter. Any data presently within it will be lost. :param unitclass: Class of Unit that the Wave points are composed of. """ # logger.debug("Resetting buffer to type : " + repr(unitclass)) self.point_buffer = SignalWave(unitclass, spacing=TimeDelta(microseconds=100000), ts0=timestamp_factory.now(), buffer_size=1000, use_point_ts=False)
def next_chunk(self): """ :returns: The next chunk of data points in the form of a SignalWave :rtype: :class:`tendril.utils.types.signalbase.SignalWave` """ rval = copy.copy(self.point_buffer) self.point_buffer = SignalWave( rval.unitclass, points=deque([rval.pop()], maxlen=rval._buffer_size), spacing=rval._spacing, ts0=rval._ts0, buffer_size=rval._buffer_size, use_point_ts=rval._use_point_ts ) return rval
class TendrilProtocol2200087(InstProtocol2200087): """ This subclasses the twisted protocol from :mod:`driver2200087.runner` which handles serial communications with 2200087 multimeters. It produces :class:`tendril.utils.types.signalbase.SignalPoint` and :class:`tendril.utils.types.signalbase.SignalWave` objects instead of strings and deque objects, which contain Type objects from the :mod:`tendril.utils.types` module. This protocol exists and operates within the context of a twisted reactor. Applications themselves built on twisted should be able to simply import this protocol (or its factory). Synchronous / non-twisted applications should directly use the :class:`driver2200087.runner.InstInterface2200087` class instead, and pass this protocol's factory to modify its behavior. :param port: Port on which the device is connected. Default '/dev/ttyUSB0'. :type port: str :param buffer_size: Length of the point buffer in the protocol. :type buffer_size: int """ def __init__(self, port, buffer_size): InstProtocol2200087.__init__(self, port=port, buffer_size=buffer_size) def reset_buffer(self, unitclass=DummyUnit): """ Resets the point buffer to a new :class:`tendril.utils.types.signalbase.SignalWave` with the unitclass as specified by the parameter. Any data presently within it will be lost. :param unitclass: Class of Unit that the Wave points are composed of. """ # logger.debug("Resetting buffer to type : " + repr(unitclass)) self.point_buffer = SignalWave(unitclass, spacing=TimeDelta(microseconds=100000), ts0=timestamp_factory.now(), buffer_size=1000, use_point_ts=False) def connectionLost(self, reason=connectionDone): """ This function is called by twisted when the connection to the serial transport is lost. """ if repr(reason) == repr(connectionDone): return else: print "Lost Connection to Device" print reason def next_chunk(self): """ :returns: The next chunk of data points in the form of a SignalWave :rtype: :class:`tendril.utils.types.signalbase.SignalWave` """ rval = copy.copy(self.point_buffer) self.point_buffer = SignalWave(rval.unitclass, points=deque([rval.pop()], maxlen=rval._buffer_size), spacing=rval._spacing, ts0=rval._ts0, buffer_size=rval._buffer_size, use_point_ts=rval._use_point_ts) return rval def _get_point(self, string): """ Processes a string returned by :mod:`driver2200087.serialDecoder` and converts it into a :class:`tendril.utils.types.signalbase.SignalPoint` instance composed of the correct Unit class. :return: SignalPoint composed of the correct type and value from the string :rtype: :class:`tendril.utils.types.signalbase.SignalPoint` .. seealso:: :data:`rex_list` """ if string is None: return SignalPoint(DummyUnit, None) for rex in rex_list: m = rex[1].match(string.strip()) if m is not None: if rex[2] is not None: rstring = rex[2](m) else: rstring = m.group('string') try: return SignalPoint(rex[0], rstring) except (InvalidOperation, ValueError): # logger.error("Unable to make unit from string : " + # rstring + " : " + repr(rex[0])) return SignalPoint(rex[0], 0) raise ValueError("String not recognized : " + string) def frame_received(self, frame): """ Re-implements the Base class's frame_received function, producing SignalPoints instead. When a signal point of a different type as the point buffer is encountered, it resets the point buffer and initializes it to the new type. .. seealso:: :meth:`_get_point` """ frame = [byte.encode('hex') for byte in frame] chunk = ' '.join(frame) string = self._frame_processor(chunk) point = self._get_point(string) if not point.unitclass == self.point_buffer.unitclass: self.reset_buffer(point.unitclass) self.point_buffer.add_point(point)
class TendrilProtocol2200087(InstProtocol2200087): """ This subclasses the twisted protocol from :mod:`driver2200087.runner` which handles serial communications with 2200087 multimeters. It produces :class:`tendril.utils.types.signalbase.SignalPoint` and :class:`tendril.utils.types.signalbase.SignalWave` objects instead of strings and deque objects, which contain Type objects from the :mod:`tendril.utils.types` module. This protocol exists and operates within the context of a twisted reactor. Applications themselves built on twisted should be able to simply import this protocol (or its factory). Synchronous / non-twisted applications should directly use the :class:`driver2200087.runner.InstInterface2200087` class instead, and pass this protocol's factory to modify its behavior. :param port: Port on which the device is connected. Default '/dev/ttyUSB0'. :type port: str :param buffer_size: Length of the point buffer in the protocol. :type buffer_size: int """ def __init__(self, port, buffer_size): InstProtocol2200087.__init__(self, port=port, buffer_size=buffer_size) def reset_buffer(self, unitclass=DummyUnit): """ Resets the point buffer to a new :class:`tendril.utils.types.signalbase.SignalWave` with the unitclass as specified by the parameter. Any data presently within it will be lost. :param unitclass: Class of Unit that the Wave points are composed of. """ # logger.debug("Resetting buffer to type : " + repr(unitclass)) self.point_buffer = SignalWave(unitclass, spacing=TimeDelta(microseconds=100000), ts0=timestamp_factory.now(), buffer_size=1000, use_point_ts=False) def connectionLost(self, reason=connectionDone): """ This function is called by twisted when the connection to the serial transport is lost. """ if repr(reason) == repr(connectionDone): return else: print "Lost Connection to Device" print reason def next_chunk(self): """ :returns: The next chunk of data points in the form of a SignalWave :rtype: :class:`tendril.utils.types.signalbase.SignalWave` """ rval = copy.copy(self.point_buffer) self.point_buffer = SignalWave( rval.unitclass, points=deque([rval.pop()], maxlen=rval._buffer_size), spacing=rval._spacing, ts0=rval._ts0, buffer_size=rval._buffer_size, use_point_ts=rval._use_point_ts ) return rval def _get_point(self, string): """ Processes a string returned by :mod:`driver2200087.serialDecoder` and converts it into a :class:`tendril.utils.types.signalbase.SignalPoint` instance composed of the correct Unit class. :return: SignalPoint composed of the correct type and value from the string :rtype: :class:`tendril.utils.types.signalbase.SignalPoint` .. seealso:: :data:`rex_list` """ if string is None: return SignalPoint(DummyUnit, None) for rex in rex_list: m = rex[1].match(string.strip()) if m is not None: if rex[2] is not None: rstring = rex[2](m) else: rstring = m.group('string') try: return SignalPoint(rex[0], rstring) except (InvalidOperation, ValueError): # logger.error("Unable to make unit from string : " + # rstring + " : " + repr(rex[0])) return SignalPoint(rex[0], 0) raise ValueError("String not recognized : " + string) def frame_received(self, frame): """ Re-implements the Base class's frame_received function, producing SignalPoints instead. When a signal point of a different type as the point buffer is encountered, it resets the point buffer and initializes it to the new type. .. seealso:: :meth:`_get_point` """ frame = [byte.encode('hex') for byte in frame] chunk = ' '.join(frame) string = self._frame_processor(chunk) point = self._get_point(string) if not point.unitclass == self.point_buffer.unitclass: self.reset_buffer(point.unitclass) self.point_buffer.add_point(point)