From c8f30582a55aa56a65d8f8b08a93ceed3ccf5e2c Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Thu, 2 Mar 2023 09:24:29 +0100 Subject: [PATCH] default settings on the IO class allow to define default settings on the IO class: - a default 'port' may be given for tcp - defaults like 'baudrate' or 'parity' might be given for serial connections this avoids explicit settings in the config file in case the settings can not be changed or have a typical value other than the defaults in serial.Serial Change-Id: I990f47d63e785f8cc48c4af197944a8eebe91fb4 Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/30555 Reviewed-by: Georg Brandl Tested-by: Jenkins Automated Tests Reviewed-by: Markus Zolliker --- doc/source/reference.rst | 1 + frappy/io.py | 10 ++++++++-- frappy/lib/asynconn.py | 43 +++++++++++++++++++++------------------- frappy_demo/lakeshore.py | 1 + 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/doc/source/reference.rst b/doc/source/reference.rst index f144949..a469432 100644 --- a/doc/source/reference.rst +++ b/doc/source/reference.rst @@ -102,6 +102,7 @@ Communication .. autoclass:: frappy.io.IOBase :show-inheritance: + :members: default_settings .. autoclass:: frappy.io.StringIO :show-inheritance: diff --git a/frappy/io.py b/frappy/io.py index 37bffd4..b83390e 100644 --- a/frappy/io.py +++ b/frappy/io.py @@ -112,6 +112,12 @@ class IOBase(Communicator): wait_before = Parameter('wait time before sending', datatype=FloatRange(), default=0) is_connected = Parameter('connection state', datatype=BoolType(), readonly=False, default=False) pollinterval = Parameter('reconnect interval', datatype=FloatRange(0), readonly=False, default=10) + #: a dict of default settings for a device, e.g. for a LakeShore 336: + #: + #: ``default_settings = {'port': 7777, 'baudrate': 57600, 'parity': 'O', 'bytesize': 7}`` + #: + #: port is used in case of tcp, the others for serial over USB + default_settings = {} _reconnectCallbacks = None _conn = None @@ -243,7 +249,7 @@ class StringIO(IOBase): def connectStart(self): if not self.is_connected: uri = self.uri - self._conn = AsynConn(uri, self._eol_read) + self._conn = AsynConn(uri, self._eol_read, default_settings=self.default_settings) self.is_connected = True for command, regexp in self.identification: reply = self.communicate(command) @@ -354,7 +360,7 @@ class BytesIO(IOBase): def connectStart(self): if not self.is_connected: uri = self.uri - self._conn = AsynConn(uri, b'') + self._conn = AsynConn(uri, b'', default_settings=self.default_settings) self.is_connected = True for request, expected in self.identification: replylen, replypat = make_regexp(expected) diff --git a/frappy/lib/asynconn.py b/frappy/lib/asynconn.py index cdc374d..97cfc37 100644 --- a/frappy/lib/asynconn.py +++ b/frappy/lib/asynconn.py @@ -32,6 +32,7 @@ import ast import select import socket import time +import re from frappy.errors import CommunicationFailedError, ConfigError from frappy.lib import closeSocket, parseHostPort, tcpSocket @@ -51,15 +52,14 @@ class AsynConn: scheme = None SCHEME_MAP = {} connection = None # is not None, if connected - defaultport = None - def __new__(cls, uri, end_of_line=b'\n'): + def __new__(cls, uri, end_of_line=b'\n', default_settings=None): scheme = uri.split('://')[0] iocls = cls.SCHEME_MAP.get(scheme, None) if not iocls: # try tcp, if scheme not given try: - host_port = parseHostPort(uri, cls.defaultport) + host_port = parseHostPort(uri, None) except (ValueError, TypeError, AssertionError): if 'COM' in uri: raise ValueError("the correct uri for a COM port is: " @@ -72,8 +72,9 @@ class AsynConn: uri = 'tcp://%s:%d' % host_port return object.__new__(iocls) - def __init__(self, uri, end_of_line=b'\n'): + def __init__(self, uri, end_of_line=b'\n', default_settings=None): self.end_of_line = end_of_line + self.default_settings = default_settings or {} self._rxbuffer = b'' def __del__(self): @@ -172,7 +173,7 @@ class AsynTcp(AsynConn): # should be the case always uri = uri[6:] try: - self.connection = tcpSocket(uri, self.defaultport, self.timeout) + self.connection = tcpSocket(uri, self.default_settings.get('port'), self.timeout) except (ConnectionRefusedError, socket.gaierror, socket.timeout) as e: # indicate that retrying might make sense raise CommunicationFailedError(str(e)) from None @@ -218,7 +219,7 @@ class AsynSerial(AsynConn): uri syntax:: - serial://?[