Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Execute custom object commands in python script

2014-01-08
2014-01-09
  • dsDeveloper - 2014-01-08

    Good day,

    I am trying to call and execute custom objects using python script and hope someone could help me out with that.
    Currently with OnDriverLoad() I add an object to ScriptEngine which I call companyCommand. This companyCommand object
    implements the IScripExecutor interface (C#) and has the function "execute".

    Now if I want to call that function in a pythong script which I passed to the .exe, how do I need to proceed? Is it enough to add

    companyCommand.execute(...)
    

    and also pass the GUID of the object or do I need to call it in a special way? Any imports necessary? I couldn't find a tutorial
    where this is shown.

    Best regards and Thank you,

    dsDeveloper

    P.S.

    I will be able to provide more information on the implementation when I am back at the office tomorrow.

     
  • Anonymous - 2014-01-08

    Originally created by: M.Schaber

    Hi, dsDeveloper,

    First, this seems to be an Automation Platform question, which is best directed to the developer contact you got during your Automation Platform training.

    The developer network has an article How to make a scriptable plugin which explains the basics of creating scriptable plugins. (If you don't have an account yet, create one, and contact your 3S Sales Rep. to enable access to the AP section.)

    The IScriptExecutor interface is implemented by the Script Engine, and other Automation Platform plugins use it to trigger the execution of scripts. It is not to be implemented by objects which are provided for the scripts - those object should just provide the interface to be used by the scripts.

    In your IScriptDriver, during the OnDriverLoad() method, just pass in the objects you want to be accessible for the script:

    [TypeGuid("{insert-your-guid-here}")]
    public class ScriptDriverSystem : IScriptDriver
    {
        public void OnDriverLoad(IScriptExecutor executor)
        {
            // The script can access an instance of our CompanyObject() class by the name
            // company_object and call all its methods:
            executor.ProvideObjectForScript("company_object", new CompanyObject());
            // Some of our methods need the MyEnum parameter, so we provide the enum to the script.
            // Enums and other type objects which need to be accessible by the script (e. G. for instantiation and 
            // calling of static methods) need to be passed through PrepareType - it creates a python type out of
            // the .NET Type object.
            executor.ProvideObjectForScript("MyEnum", executor.Engine.PrepareType(typeof(MyEnum)));
            // You can also provide functions directly, by creating delegates:
            executor.ProvideObjectForScript("company_function", new System.Action<int>(SomeMethod));
        }
        public void SomeMethod(int i) { do something here... }
    }
    public enum MyEnum { ... }
    public class CompanyObject { ... }
    
     
  • dsDeveloper - 2014-01-09

    Thank you for your reply, Schaber. I checked the website and my implementation and it seemed to be correct. My issue was
    the way I passed arguments to a function of the company object. Python uses lists and an array was expected. I was using
    params in the C# code, so just passing the arguments one by one was the solution. Now it works as it should (at least this step).

    Thank you again for your effort!

    Best regards,

    dsDeveloper

     

Log in to post a comment.