From ce0142f48e898eb75dfee0323cd347b4791f443b Mon Sep 17 00:00:00 2001 From: Akira Naru Takizawa Date: Sat, 22 Feb 2020 10:34:20 +0100 Subject: [PATCH] Value of memory setting in .value property The type of returned value could be int or str. So we can get the settings including IP or device_path of Modbus devices. resolve #2 --- revpimodio2/device.py | 14 +++++++++++--- revpimodio2/io.py | 28 ++++++++++++++++++++++++++++ revpimodio2/modio.py | 3 --- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/revpimodio2/device.py b/revpimodio2/device.py index 0f33178..8013abf 100644 --- a/revpimodio2/device.py +++ b/revpimodio2/device.py @@ -302,7 +302,15 @@ class Device(object): for key in sorted(dict_io, key=lambda x: int(x)): # Neuen IO anlegen - if bool(dict_io[key][7]) or isinstance(self, Base): + if iotype == MEM: + # Memory setting + io_new = MemIO( + self, dict_io[key], + iotype, + "little", + False + ) + elif bool(dict_io[key][7]) or isinstance(self, Base): # Bei Bitwerten oder Base IOBase verwenden io_new = IOBase( self, dict_io[key], iotype, "little", False @@ -317,7 +325,7 @@ class Device(object): "little", False ) - elif isinstance(self, Gateway) and iotype != MEM: + elif isinstance(self, Gateway): # Ersetzbare IOs erzeugen io_new = IntIOReplaceable( self, dict_io[key], @@ -1127,5 +1135,5 @@ class Virtual(Gateway): # Nachträglicher Import -from .io import IOBase, IntIO, IntIOCounter, IntIOReplaceable +from .io import IOBase, IntIO, IntIOCounter, IntIOReplaceable, MemIO from revpimodio2 import INP, OUT, MEM diff --git a/revpimodio2/io.py b/revpimodio2/io.py index 045984d..07efc6d 100644 --- a/revpimodio2/io.py +++ b/revpimodio2/io.py @@ -1244,3 +1244,31 @@ class StructIO(IOBase): frm = property(_get_frm) signed = property(_get_signed) value = property(get_structvalue, set_structvalue) + + +class MemIO(IOBase): + """ + Erstellt einen IO für die Memory Werte in piCtory. + + Dieser Typ ist nur für lesenden Zugriff vorgesehen und kann verschiedene + Datentypen über .value zurückgeben. Damit hat man nun auch Zugriff + auf Strings, welche in piCtory vergeben werden. + """ + + def get_variantvalue(self): + val = bytes(self._defaultvalue) + + if self._bitlength == 256: + # STRING + try: + val = val.strip(b'\x00').decode("ASCII") + except Exception: + pass + return val + + else: + # INT + return int.from_bytes(val, self._byteorder, signed=self._signed) + + defaultvalue = property(get_variantvalue) + value = property(get_variantvalue) diff --git a/revpimodio2/modio.py b/revpimodio2/modio.py index 8a0d143..e410ca8 100644 --- a/revpimodio2/modio.py +++ b/revpimodio2/modio.py @@ -1040,9 +1040,6 @@ class RevPiModIO(object): # Inputs vom Bus einlesen dev._ba_devdata[dev._slc_inp] = bytesbuff[dev._slc_inpoff] - # Mems vom Bus lesen - dev._ba_devdata[dev._slc_mem] = bytesbuff[dev._slc_memoff] - dev._filelock.release() return True