CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Wed Apr 24, 2019 7:15 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Sep 16, 2015 10:09 am 
Offline

Joined: Wed Aug 26, 2015 3:26 pm
Posts: 8
Dear Forum!

Following up on http://forum.codesys.com/viewtopic.php?f=18&t=6115 (which I hijacked, sorry), I am wondering:

Is it possible to programmatically start a Python script via a plugin?

I. e. can I write a plugin that has a method that makes Codesys execute a Python script, e. g. based on its path? (I would then create another interface to that plugin, like a web service, in order to execute different Python scripts when feasible.)

Many thanks
Andreas Reiff


Top
   
PostPosted: Fri Sep 25, 2015 9:03 am 
Offline

Joined: Fri Jul 10, 2015 10:52 am
Posts: 35
I'm replying as a user so dont take my word as an answer.

What you could use is 'execfile(path_to_your_script);' this is similar to C include (it will put the content of the script/file you pass
to the line where it is executed.

With FIFO management you can create a python script (executed by codesys) which accepts pathes on your interface
and puts it in the FIFO, then an endless loop processes the the FIFO and calls the execfile with your scripts.

This would maybe the simpliest, I dont know if it works I've never tried the execfile in a loop but im using this method
to exec multiple python script to the codesys executed one.


Top
   
PostPosted: Fri Sep 25, 2015 9:09 am 
Offline

Joined: Fri Jul 10, 2015 10:52 am
Posts: 35
Also you can experiment with system execute command as described here:
viewtopic.php?f=18&t=6306

the GUID/names you need is:
974e36d0-d2ed-4dcc-9505-2e4f2fb9c70d - Execute Script File... - Array[str](('script', 'execute'))

so:
system.commands[GUID('974e36d0-d2ed-4dcc-9505-2e4f2fb9c70d')].execute(...)

or identical:
system.commands["script", "execute"].execute(...)

I dont know:
- if this can be execute without GUI (--noUI) execution
- if execute() takes an argument
- if it does not you have to use additional methods to pass the path of the script to codesys (maybe system.prompt_answers[...])

Sorry I dont have the time to experiment with these. But thought it can help you.


Top
   
PostPosted: Fri Sep 25, 2015 10:12 am 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi, Andreas,

andipandi wrote:
Dear Forum!

Following up on http://forum.codesys.com/viewtopic.php?f=18&t=6115 (which I hijacked, sorry), I am wondering:

Is it possible to programmatically start a Python script via a plugin?

I. e. can I write a plugin that has a method that makes Codesys execute a Python script, e. g. based on its path? (I would then create another interface to that plugin, like a web service, in order to execute different Python scripts when feasible.)


With programmatically, I think you mean the Automation Platform.

The ScriptEngine Plugin has an official Automation Platform API which allows to start your own scripts. (This is also what the Test Manager does within its "ExecuteScript" action.) This API provides more control than using the command execution.

The apis are defined in the ScriptEngine interface DLL.

There's also an article in the developer network which covers the more advanced topics like how you can add your own functions and objects to be called by Python.

HTH,
Markus

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
PostPosted: Tue Sep 29, 2015 2:08 pm 
Offline

Joined: Wed Aug 26, 2015 3:26 pm
Posts: 8
@etamgul
Many thanks, I probably was not clear enough in my description, I was wondering how to execute a script within Codesys - controlled from another program.

@Markus
Many thanks, that is what I am looking for.
Since from my understanding, the plugin is passive, I would then have to create my own listener in that plugin in order to control it from outside the Codesys application.

Your sample unfortunately is the other way round.. extending the script engine, but not controlling it.

(Sorry, I have not developed with the Codesys plugin concept yet.)


Top
   
PostPosted: Tue Sep 29, 2015 6:21 pm 
Offline

Joined: Wed Aug 26, 2015 3:26 pm
Posts: 8
Could that be

Code:
            var scriptEngine = (IScriptEngine)ComponentManager.Singleton.InstanceFactory.GetSystemInstance(typeof(IScriptEngine).FullName);
            if (scriptEngine == null)
                throw new InvalidOperationException("No ScriptEngine accessible error.");
            scriptEngine.Execute("abc.py");

?

Also, if I have this running in a plugin (that is the way to do it?) in a separate thread, can I just call the Execute method or is there some dependency on the main thread?

Many thanks


Top
   
PostPosted: Wed Sep 30, 2015 7:12 am 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi,

andipandi wrote:
Could that be

Code:
            var scriptEngine = (IScriptEngine)ComponentManager.Singleton.InstanceFactory.GetSystemInstance(typeof(IScriptEngine).FullName);
            if (scriptEngine == null)
                throw new InvalidOperationException("No ScriptEngine accessible error.");
            scriptEngine.Execute("abc.py");

?

Also, if I have this running in a plugin (that is the way to do it?) in a separate thread, can I just call the Execute method or is there some dependency on the main thread?

Many thanks

Yes, that source looks like a valid, minimal script execution.

For more control about the script, you can create an IScriptExecutor.

IronPython by itself does not depend on the main thread, it is (or should be) as threading capable as other .NET languages like C#.

However, the standard scripting API we provide to the scripts using the script drivers is inherently bound to the primary thread (UI thread), as it calls into Automation Platform APIs.

If you do not want to use that API, just use an IScriptExecutor and don't call the "LoadScriptDrivers()" Method. Then your scripts can still use the whole .NET and IronPython standard library, as well as any APIs you provide or any Assemblies the script loads directly.

HTH,
Markus

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 7 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited