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 external Phyton script with GPIO pin, PLC function ?

jinx
2015-05-22
2015-05-23
  • jinx - 2015-05-22

    Hi,
    I browsed through the forum but I couldn't find a sollution for this.
    I would like to run a Phyton script (or bash script) based on a condition within PLC programm (ie. If GPIO pin 27 is high, I want to run a Phyton script).

    I found some suggestions using Sysprocess.library and SysTypes.library, and use "Sys Process Execute Command", but I can not find these two libraries at all ( l viewtopic.php?f=21&t=5714 l )

    Hope someone can help.
    Best regards,
    Gregor

     
  • eschwellinger

    eschwellinger - 2015-05-23

    Hi,
    1. for adding syslibs you need to switch to professional (this is obsolete for 3.5SP7 ).
    2. then you need to add sysprocess library and Systypes Interfaces2 library (just type part of the name into the search field see screenshot)
    3. check my example (add the command to /etc/CODESYSControl.cfg)
    BR
    Edwin

    IMG: Add_the_Command_CODESYSControl.cfg.jpg

    IMG: addSysprocess.jpg

    IMG: GPIO21Mapping.jpg

    IMG: Professional.jpg

    PiFaceIoDrv_LadderExample_andSysProcess.project [104.28 KiB]

     
  • jinx - 2015-05-23

    Hi Edwin,
    thank you for your answer.

    I managed to install libraries: "SysProcess 3.5.5.0" and "SysTypes2 Interfaces".

    I am a bit confused, what and how I should write into CODESYScontrol.cfg

    I have a working script (only an example) which I can run with this command:

    sudo python \home\GPIO27_ON.py
    

    What should I write into .cfg file for this command to be used from within PLC program?

    I prepared this in .cfg:

    [SysProcess]
    Command.0=shutdown
    Command.1=GPIO27_ON.py
    

    I moved file to \usr\bin\ folder. I made it executable.

    In Programm I define xGumb as virtual button.
    This is my code:

    xRisingTrigger(CLK:=xGumb);
    IF xRisingTrigger.QΒ  THEN 
    Β  Β _('GPIO27_ON.py',ADR(Result));
    END_IF
    

    I get Result value 25 when I trigger xGumb. Script is not executed.

    Gregor

     
  • eschwellinger

    eschwellinger - 2015-05-23

    Hi,
    could not really se the problem at the moment
    could you please upload the CODESYSControl.cfg .. (check if there is a dublicate entry of the SysProcess section)

    Error 25 means:
    define ERR_NO_ACCESS_RIGHTS 0x0019 / No access rights for this operation /

    BR
    Edwin

     
  • jinx - 2015-05-23

    Hi Edwin,
    attached is CODESYSControl.cfg (I had to rename it to .txt because forum doesn't allow .cfg files to be uploaded).
    There is no duplicate.

    Could this be a problem with ROOT privilege to run python scripts?

    BR,
    Gregor

    CODESYSControl.txt [1.04 KiB]

     
  • jinx - 2015-05-23

    Running script...

    BR,
    Gregor

    IMG: trying to run script.jpg

     
  • eschwellinger

    eschwellinger - 2015-05-23

    Hi,

    Change the config file to:

    [SysProcess]
    Command.0=shutdown
    Command.1=python

    (after doing this a restart of the runtime is needed)

    and then call in CODESYS
    Sys..Process...ExecuteC..('python GPIO27_ON.py',ADR(Result));
    BR
    Edwin

    IMG: executepython.jpg

     
  • jinx - 2015-05-23

    Hi Edwin,
    I finnaly got it working!
    I had to give the full path to run scripts!

    IF xRisingTrigger.QΒ  THEN 
    Β  Β _('python /usr/bin/GPIO27_ON.py',ADR(Result));
    END_IF
    

    Thank you!
    BR, Gregor

     

Log in to post a comment.