From e2cc9f74b538e1698f3f7744599aab2d0af1d574 Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Fri, 20 Dec 2019 10:43:45 +0100 Subject: [PATCH] autogeneration of iodevs allow a shortcut for generating iodevs: If 'uri' is given in the config file for a module with HasIodev, it will autogenerate its communicator. The iodevClass attribute on the module class determines the class for the iodev to be generated. Change-Id: I4e82a57a33218fd159cec9f4c95171365c55d94a Reviewed-on: https://forge.frm2.tum.de/review/c/sine2020/secop/playground/+/22068 Reviewed-by: Markus Zolliker Tested-by: JenkinsCodeReview --- cfg/ls370test.cfg | 25 ++++++++++--------------- secop/stringio.py | 17 ++++++++++++++--- secop_psi/ls370res.py | 3 +++ 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/cfg/ls370test.cfg b/cfg/ls370test.cfg index 1004f36..6ade7f7 100644 --- a/cfg/ls370test.cfg +++ b/cfg/ls370test.cfg @@ -6,21 +6,16 @@ type = tcp bindto = 0.0.0.0 bindport = 5000 +[module lsmain] +class = secop_psi.ls370res.Main +description = main control of Lsc controller +uri = localhost:4567 + [module res] class = secop_psi.ls370res.ResChannel vexc = '2mV' -.channel = 3 -.description = resistivity -.main = lsmain -.iodev = lscom - -[module lsmain] -class = secop_psi.ls370res.Main -.description = main control of Lsc controller -.iodev = lscom - -[module lscom] -class = secop_psi.ls370res.StringIO -.uri=localhost:4567 -.description = serial communicator to an LS 370 -.visibility = 3 +channel = 3 +description = resistivity +main = lsmain +# the auto created iodev from lsmain: +iodev = lsmain_iodev diff --git a/secop/stringio.py b/secop/stringio.py index 4ac9860..9eb91b0 100644 --- a/secop/stringio.py +++ b/secop/stringio.py @@ -59,7 +59,7 @@ class StringIO(Communicator): } parameters = { 'timeout': - Parameter('timeout', datatype=FloatRange(0), default=1), + Parameter('timeout', datatype=FloatRange(0), default=2), 'wait_before': Parameter('wait time before sending', datatype=FloatRange(), default=0), 'is_connected': @@ -236,7 +236,7 @@ class StringIO(Communicator): if self.wait_before: time.sleep(self.wait_before) if garbage is None: # read garbage only once - garbage = '' + garbage = b'' data = self.readWithTimeout(0) while data: garbage += data @@ -276,12 +276,23 @@ class HasIodev(Module): """Mixin for modules using a communicator""" properties = { 'iodev': Attached(), + 'uri': Property('uri for auto creation of iodev', StringType(), default=''), } + def __init__(self, name, logger, opts, srv): + super().__init__(name, logger, opts, srv) + if self.uri: + opts = {'uri': self.uri, 'description': 'communication device for %s' % name, + 'export': False} + ioname = name + '_iodev' + iodev = self.iodevClass(ioname, self.log.getChild(ioname), opts, srv) + srv.modules[ioname] = iodev + self.setProperty('iodev', ioname) + def initModule(self): try: self._iodev.read_is_connected() - except CommunicationFailedError: + except (CommunicationFailedError, AttributeError): pass super().initModule() diff --git a/secop_psi/ls370res.py b/secop_psi/ls370res.py index 7a5477a..76c773f 100644 --- a/secop_psi/ls370res.py +++ b/secop_psi/ls370res.py @@ -57,6 +57,7 @@ for bit, text in enumerate('CS_OVL VCM_OVL VMIX_OVL R_OVER R_UNDER T_OVER T_UNDE class StringIO(secop.stringio.StringIO): identification = [('*IDN?', 'LSCI,MODEL370,.*')] + wait_before = 0.05 class Main(HasIodev, Module): @@ -72,6 +73,7 @@ class Main(HasIodev, Module): } pollerClass = Poller + iodevClass = StringIO def analyze_scan(self, channel, autoscan): return dict(channel=channel, autoscan=autoscan) @@ -96,6 +98,7 @@ class ResChannel(HasIodev, Readable): for val in [2, 6.32, 20, 63.2, 200, 632]))} pollerClass = Poller + iodevClass = StringIO properties = { 'channel':