def connect(self): if not self.libLoaded: #from artisanlib.s7client import S7Client from snap7.common import load_library as load_snap7_library # first load shared lib if needed platf = str(platform.system()) if platf in ['Windows', 'Linux'] and artisanlib.util.appFrozen(): libpath = os.path.dirname(sys.executable) if platf == 'Linux': snap7dll = os.path.join(libpath, "libsnap7.so") else: # Windows: snap7dll = os.path.join(libpath, "snap7.dll") load_snap7_library( snap7dll) # will ensure to load it only once self.libLoaded = True if self.libLoaded and self.plc is None: # create a client instance from artisanlib.s7client import S7Client self.plc = S7Client() # next reset client instance if not yet connected to ensure a fresh start if self.plc is not None and not self.isConnected(): try: self.plc.disconnect() except: pass with suppress_stdout_stderr(): time.sleep(0.4) try: self.plc.connect(self.host, self.rack, self.slot, self.port) time.sleep(0.4) except Exception: pass if self.isConnected(): self.sendmessage( QApplication.translate("Message", "S7 Connected", None)) time.sleep(0.4) else: time.sleep(0.6) try: self.plc.disconnect() except: pass # we try a second time with suppress_stdout_stderr(): time.sleep(0.4) self.plc.connect(self.host, self.rack, self.slot, self.port) time.sleep(0.4) if self.isConnected(): self.sendmessage( QApplication.translate("Message", "S7 Connected", None) + " (2)") time.sleep(0.4) self.updateActiveRegisters()
def connect(self): from artisanlib.s7client import S7Client from snap7.common import load_library as load_snap7_library # first load shared lib if needed platf = str(platform.system()) if platf in ['Windows', 'Linux'] and artisanlib.util.appFrozen(): libpath = os.path.dirname(sys.executable) if platf == 'Linux': snap7dll = os.path.join(libpath, "libsnap7.so") else: # Windows: snap7dll = os.path.join(libpath, "snap7.dll") load_snap7_library(snap7dll) # will ensure to load it only once # next reset client instance if not yet connected to ensure a fresh start if self.plc and not self.plc.get_connected(): self.plc = None # connect if not yet connected if self.plc is None: self.plc = S7Client() with suppress_stdout_stderr(): time.sleep(0.4) self.plc.connect(self.host, self.rack, self.slot, self.port) time.sleep(0.4) if self.plc.get_connected(): self.sendmessage( QApplication.translate("Message", "S7 Connected", None)) time.sleep(0.7) else: time.sleep(0.6) self.plc = S7Client() # we try a second time with suppress_stdout_stderr(): time.sleep(0.4) self.plc.connect(self.host, self.rack, self.slot, self.port) time.sleep(0.4) if self.plc.get_connected(): self.sendmessage( QApplication.translate("Message", "S7 Connected", None) + " (2)") time.sleep(0.7)