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

Runing scripts outside CoDeSys

daniel-REC
2012-07-19
2016-09-07
  • daniel-REC - 2012-07-19

    Hi all,

    Could someone tell me is there any way to run a script that Codesys will interprete, not manually from Tools->Scripting, but from e.g. command line?

    I did manage to start Codesys application, login and run it through the command line by executing it with parameter:

    C:\Program Files\3S CoDeSys V3.4 SP3\CoDeSys\Common>start /wait CoDeSys.exe --profile="CoDeSys V3.4 SP3" --runscript="C:\runscript.py"

    Now runscript.py is looking like this:

    from future import print_function

    import os
    scriptdir = os.path.dirname("C:\TestStand\Systemtest\Libs\CoDeSys\Open_SysTS_FBC-EC.py") # Directory of our script.
    datadir = os.path.join(scriptdir, "Projekt") # Enter the subdirectory.
    projectpath = os.path.join(datadir, "test_no2.project") # And add the project name.

    proj = projects.open(projectpath)

    app = proj.active_application

    onlineapp = online.create_online_application(app)

    onlineapp.login(OnlineChangeOption.Try, True)

    if not onlineapp.application_state == ApplicationState.run:
    onlineapp.start()

    system.delay(1000)

    So the application is starting and working, but what if I want to e.g. stop the program running? I could manually execute another script from Codesys, but I want to do that by calling some batch file for example, that will stop Codesys.
    Please help me.

     
  • Anonymous - 2012-07-19

    Originally created by: M.Schaber

    Hi, Daniel_REC,
    [/quote]

    daniel_REC hat geschrieben:
    Could someone tell me is there any way to run a script that Codesys will interprete, not manually from Tools->Scripting, but from e.g. command line?
    I did manage to start Codesys application, login and run it through the command line by executing it with parameter:
    C:\Program Files\3S CoDeSys V3.4 SP3\CoDeSys\Common>start /wait CoDeSys.exe --profile="CoDeSys V3.4 SP3" --runscript="C:\runscript.py"

    So you already know how to start a script via command line. There are additional options like "--noUI" which are described in the Online Help, see the chapter about the "Script Language" in "Concepts and Basic Components".

    Also note that you don't need the "start /wait" when starting CoDeSys from within a batch file.

    daniel_REC hat geschrieben:
    Now runscript.py is looking like this:


    from __future__ import print_function
    import os
    scriptdir = os.path.dirname("C:\TestStand\Systemtest\Libs\CoDeSys\Open_SysTS_FBC-EC.py") # Directory of our script.
    datadir = os.path.join(scriptdir, "Projekt") # Enter the subdirectory.
    projectpath = os.path.join(datadir, "test_no2.project") # And add the project name.
    proj = projects.open(projectpath)
    app = proj.active_application
    onlineapp = online.create_online_application(app)
    onlineapp.login(OnlineChangeOption.Try, True)
    if not onlineapp.application_state == ApplicationState.run:
    Β  Β  onlineapp.start()
    system.delay(1000)
    

    So the application is starting and working, but what if I want to e.g. stop the program running? I could manually execute another script from Codesys, but I want to do that by calling some batch file for example, that will stop Codesys.
    Please help me.

    The application on the PLC will continue running when CoDeSys (the IDE) exists after executing the script.

    So you just create a different python script which just stops the application instead of starting it.

     
  • daniel-REC - 2012-07-19

    Thank You M.Schaber for quick answer, but You did write exactly the same solution that is in my post, and I did ask about HOW to start the next python script while Codesys is allready running? I can't do that with:
    C:\Program Files\3S CoDeSys V3.4 SP3\CoDeSys\Common>start /wait CoDeSys.exe --profile="CoDeSys V3.4 SP3" --runscript="C:\run_second_script.py"
    because it will open another Codesys application. Maybe I try to explain it like this:
    Codesys is running, script from parameter was executed -> after a while I want to stop Codesys by executing another script file -> Question: How to do it from command line?

     
  • Anonymous - 2012-07-19

    Originally created by: M.Schaber

    Hi, daniel_REC,

    daniel_REC hat geschrieben:
    Thank You M.Schaber for quick answer, but You did write exactly the same solution that is in my post, and I did ask about HOW to start the next python script while Codesys is allready running?
    I can't do that with:
    C:\Program Files\3S CoDeSys V3.4 SP3\CoDeSys\Common>start /wait CoDeSys.exe --profile="CoDeSys V3.4 SP3" --runscript="C:\run_second_script.py"
    because it will open another Codesys application. Maybe I try to explain it like this:
    Codesys is running, script from parameter was executed -> after a while I want to stop Codesys by executing another script file -> Question: How to do it from command line?

    I'm not really sure what you mean. With "Codesys", do you mean the runtime or the IDE?

     
  • daniel-REC - 2012-07-19

    Hm... now i don't know what You mean:)
    I want to do a really simple thing in theory (so I think). I have created project in Codesys application and i can run it using windows command line (cmd) by writing path to Codesys.exe and parameter after this (in parameter is path to script that starts project, login and run it). After the whole project is started the script file is finished working. And from this part I need help:
    I have another script that looks like this:


    from future import print_function

    proj = projects.primary
    app = proj.active_application
    onlineapp = online.create_online_application(app)
    onlineapp.logout()


    If I will execute this script by selecting in Codesys "Tools->Scripting->Execute Script File..." then it is working, the project is logout. But I want to do it in another way - I want to execute this script from windows command line. My question is, what should I write in cmd to make it work?

     
  • Anonymous - 2012-07-19

    Originally created by: M.Schaber

    Hi, Daniel,

    daniel_REC hat geschrieben:
    Hm... now i don't know what You mean:)

    The CoDeSys IDE is the integrated development environment, the tool you use to write IEC-61131 applications, and which also executes the IronPython scripts.

    The CoDeSys runtime is where your IEC applications are actually running. It may be a CoDeSys Control Win running on the same PC as your IDE (The little icon in your notification area), or running remote on some other machine (e. G. an Industrial PC or PLC).

    When you go "Online", you connect your IDE with your Runtime, and "download" the compiled application from the IDE to the Runtime, so it can be executed there.

    daniel_REC hat geschrieben:
    I want to do a really simple thing in theory (so I think). I have created project in Codesys application and i can run it using windows command line (cmd) by writing path to Codesys.exe and parameter after this (in parameter is path to script that starts project, login and run it). After the whole project is started the script file is finished working. And from this part I need help:
    I have another script that looks like this:


    from future import print_function
    proj = projects.primary
    app = proj.active_application
    onlineapp = online.create_online_application(app)
    onlineapp.logout()


    If I will execute this script by selecting in Codesys "Tools->Scripting->Execute Script File..." then it is working, the project is logout. But I want to do it in another way - I want to execute this script from windows command line. My question is, what should I write in cmd to make it work?

    Now, if your question is about starting and stopping IEC applications, then you do not need to keep the CoDeSys IDE UI open after your script has finished executing. You can insert a ```

    system.exit()

    ``` statement at the end of the python script to close the CoDeSys IDE after the Script is finished, or add "--noUI" to the command line which prevents the IDE UI from even showing at all, and automatically exits the IDE when the Script is finished.

    On the other hand, if your question is about shutting an already running IDE via command line, or tell an already running IDE to execute another script via command line: Both cases are not yet covered by scripting itsself, but you may use external tools like AutoIt.

    IMG: Bild

    IMG: runtime.png

     
  • daniel-REC - 2012-07-20

    Hello,

    So I'm talking about Codesys IDE application. Unfortunately I can't close it by system.exit() because it is still needed. I'm using it with TestStand and there are some test cases that requires to stop the runtime and logout to do some changes in modules parameters, and after that login again and start runtime. Now I'm doing it manually, but I was thinking that I can do it by executing script, and I can, but still the script I have to execute manually, so it is not automated system yet. For executing new script I could use Autoit like You did mention, but it is not so proffesional:)

     
  • Anonymous - 2012-07-20

    Originally created by: M.Schaber

    daniel_REC hat geschrieben:
    Hello,
    So I'm talking about Codesys IDE application. Unfortunately I can't close it by system.exit() because it is still needed. I'm using it with TestStand and there are some test cases that requires to stop the runtime and logout to do some changes in modules parameters, and after that login again and start runtime. Now I'm doing it manually, but I was thinking that I can do it by executing script, and I can, but still the script I have to execute manually, so it is not automated system yet. For executing new script I could use Autoit like You did mention, but it is not so proffesional:)

    Hmm. I don't know about TestStand, but maybe you can close the IDE after the first script, and just re-start it to do the second login, stop the runtime, logout, update, login and restart. If you use the same project, and you save it before closing, it should be able to reload the compile info, and login without any interruption to the IEC application.

     
  • daniel-REC - 2012-07-20

    Thank You, now it's working:)
    But in the future - CoDeSys developers maybe should implement some way to do this without closing IDE because starting it even without UI take some time, and when it's closed we can't check what is the actual variables status.
    The whole start/stop IDE for me was needed to import and replace some modules with different parameters, because I can't change it automaticaly. E.g. I have an analog input module and using DP-Parameters it can be switched between voltage and current measurement. Unfortunately this switch I can do only manually by stopping runtime, logging out, changing desired parameter by mouse click and then login and run. This is also another tip for developers to implement some way to change the module parameter with some programming code.

     
  • Anonymous - 2012-07-20

    Originally created by: M.Schaber

    Hi, Daniel,

    daniel_REC hat geschrieben:
    Thank You, now it's working:)
    But in the future - CoDeSys developers maybe should implement some way to do this without closing IDE because starting it even without UI take some time, and when it's closed we can't check what is the actual variables status.

    We did already think about something like a "CoDeSys Scripting Server" which can be used to trigger scripts or other actions in an running CoDeSys IDE instance. Technically, this is easy to implement. But there are some "higher-level" problems like the question which IPC mechanism to use (TCP, or windows messages, etc.), and what to do if several CoDeSys instances are running on the same machine or even in the same user session on that machine - which instance is the one to execute the commands?

    If you can live with your CoDeSys instance being blocked for user input, you can implement such a server yourself - see the topic Python and Threads

    Additionally, most runtimes support concurrent connections, so - at least in some cases - it should work to use one CoDeSys instance to monitor the variables, and another one to manipulate the application via scripting.

    daniel_REC hat geschrieben:
    The whole start/stop IDE for me was needed to import and replace some modules with different parameters, because I can't change it automaticaly. E.g. I have an analog input module and using DP-Parameters it can be switched between voltage and current measurement. Unfortunately this switch I can do only manually by stopping runtime, logging out, changing desired parameter by mouse click and then login and run. This is also another tip for developers to implement some way to change the module parameter with some programming code.

    We have plans to implement interfaces to configure device parameters via Python scripting, but I cannot give any concrete date yet. As a workaround, some of our customers remove() the object in question and import_native() a modified instance.

    But we currently also prepare a public version of our Test Manager (which existed as an internal product for several years now), which supports setting of device parameters. As of current planning, a beta release might be ready by the end of the year, but this is not fixed yet.

     
  • daniel-REC - 2012-07-23

    Hello,

    I,m trying now to speed up starting of Codesys IDE without UI and logout operation. Could you tell me can we modify that line:
    onlineapp.login(OnlineChangeOption.Try, True)
    to login without online change?

    I did try with:
    onlineapp.login(OnlineChangeOption.Never, False)
    but it's not working, instead of simple login with no changes - building application is started.

     
  • Anonymous - 2012-08-17

    Originally created by: M.Schaber

    Hi, Daniel,

    daniel_REC hat geschrieben:
    I,m trying now to speed up starting of Codesys IDE without UI and logout operation. Could you tell me can we modify that line:
    onlineapp.login(OnlineChangeOption.Try, True)
    to login without online change?
    I did try with:
    onlineapp.login(OnlineChangeOption.Never, False)
    but it's not working, instead of simple login with no changes - building application is started.

    OnlineChangeOption.Never usually forces a Download.

    Maybe you can try the OnlineChangeOption.Force, it is documented as follows: "Online change shall be forced. If not possible, the action is terminated with no change.".

     
  • esskaey - 2016-03-22

    Hi,

    I am trying to pass Parameters to my python script using Windows Batch commands. The command Looks like this:

    SET PARAMETER = 100
    C:\Program Files\3S CoDeSys V3.5 SP3\CoDeSys\Common>start /wait CoDeSys.exe --profile="CoDeSys V3.5 SP3" --runscript="C:\myscript.py" %PARAMETER%
    

    But I am not able to Access the Parameter in my list of arguments using sys.argv in my script.

    Can you probably provide an example on how to pass Parameters to my script.

    Best regards,

    SK

     
  • mkeller - 2016-03-22

    Hi SK.

    codesys_sig_forum hat geschrieben:
    Can you probably provide an example on how to pass Parameters to my script.

    Command line:

    CODESYS.exe --profile="CODESYS V3.5 SP8" --runscript="C:\Scripting\script_args.py" --scriptargs="-c --test=3"
    

    Python code:

    import sys
    for a in sys.argv:
    Β  Β print("Arg: " + a)
    

    The content of argv is "C:\Scripting\script_args.py", "-c" and "--test=3".

    BR
    Martin

     
  • Thomas233 - 2016-09-06

    Hello,
    is ther any documentation about all command line options for codesys.exe ?
    I can only find a small set in the Online Help (e.g. --noUI and --scriptargs are missing).

    Maybe there are more options to get codesys start or open projects faster? Like without loading libraries or without checking for errors in the Project.

    Regards
    Thomas

     
  • mkeller - 2016-09-06

    Hi .

    Thomas233 hat geschrieben:
    is ther any documentation about all command line options for codesys.exe ?
    I can only find a small set in the Online Help (e.g. --noUI and --scriptargs are missing).

    The page "Command Line Interface" contains all of the parameters as far as I know. Some AddOns have additional parameters but the online help only contains them if the AddOn is installed.

    Thomas233 hat geschrieben:
    Maybe there are more options to get codesys start or open projects faster? Like without loading libraries or without checking for errors in the Project.

    Why would you want to do that? We always keep an eye on the performance but sometimes you can't have a feature without losing some performance.

    BR
    Martin

     
  • Thomas233 - 2016-09-07

    M.Keller hat geschrieben:
    The page "Command Line Interface" contains all of the parameters as far as I know. Some AddOns have additional parameters but the online help only contains them if the AddOn is installed.

    Oh, i did not see the sub parameters. Maybe because other options like "ignorewhitespace","ignorecomments" and "ignoreproperties" are also no sub parameter of "compare".

    M.Keller hat geschrieben:
    Why would you want to do that? We always keep an eye on the performance but sometimes you can't have a feature without losing some performance.

    I have build a workaround to use missing script commands in SP7, which are only available in SP9. Therefore i run a script which opens another codesys instance with SP9, opens the project, does some work and saves and closes it again. But i found out this is really slow. Even without UI my script runs like more than 3 minutes.
    As my "work" are really simple tasks like embedding an extern obejct, i dont need all codesys features in the temporary second isntance.

     

Log in to post a comment.