Run python script in Codesys

eamonn
2018-03-29
2023-03-23
  • eamonn - 2018-03-29

    Hi,

    I have a python script that sends a sms message to my phone when its run.
    I want this code to run when a led in codesys turns on.

    I can't figure out how to do this. I know that codesys can run python by using tools, scripting, execute script.

    I just want the code to run automatically to alert me of an alarm.

    Thanks for the help.

     
  • eschwellinger

    eschwellinger - 2018-03-30

    Hi,
    this sms you want to send on an alarm or IO triggered by the plc and send by the plc
    not in the CODESYS developmentsystem?
    Because this python scription here is all in the CODESYS developmentsystem not in the plc.

    If you need this for the plc side,
    this example here should show how this could work:
    (But this works only if the plc side could execute python...)
    l viewtopic.php?f=23&t=6247&p=19932#p19941 l

    BR
    Edwin

     
  • eamonn - 2018-04-05

    Thanks for the help.

    I am using the PLC to run the python code. It can run the code on it's own.

    I cant seem to get my code to run. I don't have the hardware to test yours but I used your example to make my code.
    It seems to run the code but it does not do anything. I have tested my python code on its own and it does work.

     
  • eschwellinger

    eschwellinger - 2018-04-07

    Hi,
    so on which plc do you execute this?

    BR
    Edwin

     
  • eamonn - 2018-04-11

    I am running it on a raspberry pi 3 model B.
    I am trying to have the single file run when a bool value = true.
    I can't seem to get it to work through Codesys though.

    Edit:

    Never mind I got it to work.

    Thanks for all the help though.

     
  • rKtiC - 2018-04-15

    Could you specify how you made it work? I am trying to run a Python Script on a Raspberry 3 B with the _ and it does not do anything. Running the script via the command line works fine.
    I would appreciate any help.

     
  • eamonn - 2018-04-21

    Here is the code that made it work.

    This runs my python file sms_warning on the raspberry pi.

    Make sure you do this

    /etc/CODESYSControl.cfg:
    [SysProcess]
    Command=AllowAll

    This can be done on the pi by using the command: sudo nano /etc/CODESYSControl.cfg

    hope this helps.

    IMG: Capture.PNG

     
  • eschwellinger

    eschwellinger - 2018-04-23

    Hi,
    one small remark:
    I would recommend to move from /etc/CODESYSControl.cfg to /etc/CODESYSControl_User.cfg:

    [SysProcess] Command=AllowAll

    This has the advanatage that on an runtime update ( for example from 3.5SP12 to 3.5SP13) this setting
    will be retained.

    BR
    Edwin

     
  • Peter.skokanek - 2018-06-12

    Hello,
    I tried to use your examples to run my python script. But there is no /var/opt/codesys directory. I can find my PlcLogic/Application directory only directly in root directory. Where is a problem. Is it possible to set it somewhere in development system?

     
  • eschwellinger

    eschwellinger - 2018-06-12

    Hi,
    you need to update the Pi runtime Version to have it in
    /var/opt/codesys...

    BR
    Edwin

     
  • jorgev - 2018-08-02

    Hi all,

    I would like to use this feature in Codesys (script lang) to transfer variable values between the PLC and a python script.

    First, I am trying to execute a simple python script using codesys with a RPI 3 B+ as PLC. I followed the example posted for the unicorn hat as reference. However, I do not have the hardware to test it so I developed a simple code "print()" to verify the code is running.
    Below the steps I have followed:
    1. Add my script as external file in my CODESYS program.

    1. Manually copy to the RPI my python script to the directory /var/opt/codesys/. I have also included
      [SysProcess]Command=AllowAll **
      in both files
      /etc/CODESYSControl.cfg:/etc/CODESYSControl_User.cfg:**

    2. Download the Codesys Program to the RPI and run the program from CODESYS.

    Although I do not receive any error nor compilation or execution, I can not observe my code running. Is there any step I am missing or I should skip?

    Thanks in advance

    IMG: interpreter_py.PNG

     
  • agrobot - 2019-04-23

    I have the same problem, load the program well without errors, but do nothing, could someone solve this problem?

     
  • jar.pekar - 2019-09-27

    Hi gueys ,

    im still heaving problems with executing scripts.
    I did all the stuffs like .

    Im able only run commands like Echo xxx >>log.txt or reboot etc.

    If i run python command i get reply 01 , some general error.

    Can anyone help ? I looks like im in deadend . Last idea is reinstal RPI image.
    and that is what i dont wanna do now

    Thx lot for reply

     
  • tommy99 - 2019-12-12

    Hi guys, I've followed all your instructions but I can't visualize anything on my Adafruit dipsplay (ILI9341 driver).
    My python script is copied correctly from /var/opt/codesys/PlcLogic/Applicarion to /var/opt/codesys so I don't know what I'm doing wrong. Advices?

    P.S. obviously if I load my program from Python idle in my RPi, it works properly.

    EDIT: resolved. I tried to change "python" to "python3" in the code and it works!

     
  • epolicar - 2020-07-26

    Hi guys,
    I followed all the instructions in this thread about executing external python scripts from Codesys. I was successful in running a blink LED script. However, I am unable to run a script containing just a simple print('Hello World'). Any ideas?

     
  • alien - 2021-04-27
     

    Last edit: alien 2021-05-19
  • thebushyone - 2023-03-23

    Did anyone get to the bottom of this problem? The Python scripts only execute certain commands within them when executed from Codesys. Direct from a the LXTerminal everything work fine.

    [SysProcess] AllowAll in both file positions and python scrips in the /var/opt/codesys file

    Thanks in advance

     
  • thebushyone - 2023-03-23

    Did anyone get to the bottom of this problem? The Python scripts only execute certain commands within them when executed from Codesys. Direct from a the LXTerminal everything work fine.

    [SysProcess] AllowAll in both file positions and python scrips in the /var/opt/codesys file

    Thanks in advance

     

Log in to post a comment.