def Start(self): pyro.initServer() self.daemon = pyro.Daemon(host=self.ip_addr, port=self.port) self.plcobj = PLCObject(self.workdir, self.daemon, self.argv, self.statuschange, self.evaluator, self.pyruntimevars) uri = self.daemon.connect(self.plcobj, "PLCObject") print "Pyro port :", self.port print "Pyro object's uri :", uri print "Current working directory :", self.workdir # Configure and publish service # Not publish service if localhost in address params if (self.servicename is not None and self.ip_addr is not None and self.ip_addr != "localhost" and self.ip_addr != "127.0.0.1"): print "Publishing service on local network" self.servicepublisher = ServicePublisher.ServicePublisher() self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port) if self.autostart: self.plcobj.AutoLoad() if self.plcobj.GetPLCstatus()[0] != "Empty": self.plcobj.StartPLC() sys.stdout.flush() self.daemon.requestLoop()
def Start(self): self.plcobj = PLCObject(self.workdir, self.daemon, self.argv, self.statuschange, self.evaluator, self.pyruntimevars) uri = self.daemon.connect(self.plcobj, "PLCObject") print(_("Pyro port :"), self.port) print(_("Pyro object's uri :"), uri) # Beremiz IDE detects daemon start by looking # for self.workdir in the daemon's stdout. # Therefore don't delete the following line print(_("Current working directory :"), self.workdir) # Configure and publish service # Not publish service if localhost in address params if self.servicename is not None and \ self.ip_addr is not None and \ self.ip_addr != "localhost" and \ self.ip_addr != "127.0.0.1": print(_("Publishing service on local network")) self.servicepublisher = ServicePublisher.ServicePublisher() self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port) self.plcobj.AutoLoad() if self.plcobj.GetPLCstatus()[0] != "Empty": if self.autostart: self.plcobj.StartPLC() self.plcobj.StatusChange() sys.stdout.flush()
def KMSGPollThreadProc(): """ Logs Kernel messages starting with EtherCAT Uses GLibc wrapper to Linux syscall "klogctl" Last 4 KB are polled, and lines compared to last captured line to detect new lines """ libc = ctypes.CDLL("libc.so.6") klog = libc.klogctl klog.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_int] klog.restype = ctypes.c_int s = ctypes.create_string_buffer(4 * 1024) last = None while not StopKMSGThread: bytes_to_read = klog(3, s, len(s) - 1) log = s.value[:bytes_to_read - 1] if last: log = log.rpartition(last)[2] if log: last = log.rpartition('\n')[2] for lvl, msg in re.findall( r'<(\d)>\[\s*\d*\.\d*\]\s*(EtherCAT\s*.*)$', log, re.MULTILINE): PLCObject.LogMessage( LogLevelsDict[{ "4": "WARNING", "3": "CRITICAL" }.get(lvl, "DEBUG")], msg) time.sleep(0.5)
def SDOThreadProc(*params): global Result, SDOProc if params[0] == "upload": cmdfmt = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x" else: cmdfmt = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s" command = cmdfmt % params[1:] SDOProc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) res = SDOProc.wait() output = SDOProc.communicate()[0] if params[0] == "upload": Result = None if res == 0: if params[2] in ["float", "double"]: Result = float(output) elif params[2] in ["string", "octet_string", "unicode_string"]: Result = output else: hex_value, dec_value = output.split() if int(hex_value, 16) == int(dec_value): Result = int(dec_value) else: Result = res == 0 SDOAnswered() if res != 0: PLCObject.LogMessage(LogLevelsDict["WARNING"], "%s : %s" % (command, output))
def __init__(self, servicename, ip_addr, port, workdir, argv, statuschange=None, evaluator=default_evaluator, pyruntimevars=None): self.continueloop = True self.daemon = None self.servicename = servicename self.ip_addr = ip_addr self.port = port self.workdir = workdir self.argv = argv self.servicepublisher = None self.statuschange = statuschange self.evaluator = evaluator self.pyruntimevars = pyruntimevars self.plcobj = PLCObject(self)
class Server(): def __init__(self, servicename, ip_addr, port, workdir, argv, autostart=False, statuschange=None, evaluator=default_evaluator, pyruntimevars=None): self.continueloop = True self.daemon = None self.servicename = servicename self.ip_addr = ip_addr self.port = port self.workdir = workdir self.argv = argv self.plcobj = None self.servicepublisher = None self.autostart = autostart self.statuschange = statuschange self.evaluator = evaluator self.pyruntimevars = pyruntimevars def Loop(self): while self.continueloop: self.Start() def Restart(self): self.Stop() def Quit(self): self.continueloop = False if self.plcobj is not None: self.plcobj.StopPLC() self.plcobj.UnLoadPLC() self.Stop() def Start(self): pyro.initServer() self.daemon = pyro.Daemon(host=self.ip_addr, port=self.port) self.plcobj = PLCObject(self.workdir, self.daemon, self.argv, self.statuschange, self.evaluator, self.pyruntimevars) uri = self.daemon.connect(self.plcobj, "PLCObject") print _("Pyro port :"), self.port print _("Pyro object's uri :"), uri # Beremiz IDE detects daemon start by looking # for self.workdir in the daemon's stdout. # Therefore don't delete the following line print _("Current working directory :"), self.workdir # Configure and publish service # Not publish service if localhost in address params if self.servicename is not None and \ self.ip_addr is not None and \ self.ip_addr != "localhost" and \ self.ip_addr != "127.0.0.1": print _("Publishing service on local network") self.servicepublisher = ServicePublisher.ServicePublisher() self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port) self.plcobj.AutoLoad() if self.plcobj.GetPLCstatus()[0] != "Empty": if self.autostart: self.plcobj.StartPLC() self.plcobj.StatusChange() sys.stdout.flush() self.daemon.requestLoop() self.daemon.sock.close() def Stop(self): if self.plcobj is not None: self.plcobj.StopPLC() if self.servicepublisher is not None: self.servicepublisher.UnRegisterService() self.servicepublisher = None self.daemon.shutdown(True)
class Server(object): def __init__(self, servicename, ip_addr, port, workdir, argv, statuschange=None, evaluator=default_evaluator, pyruntimevars=None): self.continueloop = True self.daemon = None self.servicename = servicename self.ip_addr = ip_addr self.port = port self.workdir = workdir self.argv = argv self.servicepublisher = None self.statuschange = statuschange self.evaluator = evaluator self.pyruntimevars = pyruntimevars self.plcobj = PLCObject(self) def _to_be_published(self): return self.servicename is not None and \ self.ip_addr is not None and \ self.ip_addr != "localhost" and \ self.ip_addr != "127.0.0.1" def PrintServerInfo(self): print(_("Pyro port :"), self.port) # Beremiz IDE detects LOCAL:// runtime is ready by looking # for self.workdir in the daemon's stdout. print(_("Current working directory :"), self.workdir) if self._to_be_published(): print(_("Publishing service on local network")) sys.stdout.flush() def PyroLoop(self, when_ready): while self.continueloop: Pyro.config.PYRO_MULTITHREADED = 0 pyro.initServer() self.daemon = pyro.Daemon(host=self.ip_addr, port=self.port) # pyro never frees memory after connection close if no timeout set # taking too small timeout value may cause # unwanted diconnection when IDE is kept busy for long periods self.daemon.setTimeout(60) self.daemon.connect(self.plcobj, "PLCObject") if self._to_be_published(): self.servicepublisher = ServicePublisher.ServicePublisher() self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port) when_ready() self.daemon.requestLoop() self.daemon.sock.close() def Restart(self): self._stop() def Quit(self): self.continueloop = False if self.plcobj is not None: self.plcobj.StopPLC() self.plcobj.UnLoadPLC() self._stop() def _stop(self): if self.plcobj is not None: self.plcobj.StopPLC() if self.servicepublisher is not None: self.servicepublisher.UnRegisterService() self.servicepublisher = None self.daemon.shutdown(True) def AutoLoad(self): self.plcobj.AutoLoad() if self.plcobj.GetPLCstatus()[0] == "Stopped": if autostart: self.plcobj.StartPLC() self.plcobj.StatusChange()