How to run a python script with codesys GUI

ramas
2022-02-09
2022-02-09
  • ramas - 2022-02-09

    Hey Guys,
    I am very new to Codesys i find it very useful for PLC applications.
    I have designed the GUI for an appplication in Codesys how do I relate the GUI to a python script.
    For example: I have a button or a bunch of buttons designed through codesys. These buttons when clicked should call python functions. This application will be run on a Raspberry pie as the PLC.
    I really could not find any material online to do such a thing. COuld someone help me and point me in the right direction maybe show me an example or post a link maybe for reference.

    Thank you in advance.

     
  • ramas - 2022-02-09

    Could someone share their knowledge in this topic please, Thank you

     
  • snguyen - 2022-02-09

    Hi, I do not know if there is a specific library for this, but you can run linux command line from CodeSys, meaning you can call your python script too.

    First, you need to edit a codesys file in the RPI to allow codesys to run command lines :

    sudo nano /etc/CODESYSControl.cfg
    

    in the file "CODESYSControl.cfg", add this :

    [SysProcess]
    command=AllowAll
    

    Then, in CodeSys, you need the have the SysProcess library in your project. You can then call your python script as follows :

    commandString:STRING:='python yourScript.py' (*This is your console command call*)
    stdout:STRING(1000) := ''; (*this is the return message of the command*)
    Result:Sysprocess.RTS_IEC_RESULT; (*This returns an error code if something was wrong*)
    ---------------
    SysProcess.SysProcessExecuteCommand2(pszCommand:=commandString, pszStdOut:=stdout, udiStdOutLen:= SIZEOF(stdout),pResult := ADR(Result));
    

    Note that the CodeSys cycle will stop until the command has return. If your script takes time, you can either add '&' at the end of your command so the function returns immediately and the script runs in background (but you won't have your return string), or put your call on a separate core (codesys can use 4 core on the RPI, meaning 4 simultaneous code running)

     

    Last edit: snguyen 2022-02-09

Log in to post a comment.