Codesys runtime on Rpi and using a python script

ramas
2022-02-17
2022-02-18
  • ramas - 2022-02-17

    Hey guys so I am trying to run a python script during codesys runtime but i am getting a lot of errors in the POU, there are red underlines, I do not understand what i am doing wrong here.
    I have added the necessary libraries to run python while codesys runtime and followed the necessary steps, The python file should be copied into /var/opt/codesys and there is a log file that will also be created to log in the current time. However there seems to be a problem with my POU or the libraries are not syncing or something.

    If i could get some help form you guys that would be much appreciated:
    Here is the code:

    PROGRAM _PythonExecute
    VAR
        dwCopySize: DWORD;
        dutResult : RTS_IEC_RESULT;
        szCommand : STRING(200);
        fbTimer : TON;
        szStdOout : STRING(1000);
        fbTimer1 : TON;
        Command : STRING(200);
    END_VAR
    
    (*
       /etc/CODESYSControl.cfg:
       [SysProcess]
       Command=AllowAll 
    *)
    
    IF NOT GVL.init THEN
    
        //Copy the scripts to local
        SysFile.SysFileCopy('/var/opt/codesys/hallo.py'
                            ,'/var/opt/codesys/PlcLogic/Application/hallo.py'
                            ,ADR(dwCopySize));
    
    
        //Change the permission that can execute
        SysProcess_Implementation.SysProcessExecuteCommand2(pszCommand:='chmod +x *.py'
                                                            , pszStdOut:=szStdOout
                                                            , udiStdOutLen:= SIZEOF(szStdOout)
                                                            , pResult := ADR(dutResult));
    
        //Init is finished
        GVL.init:=TRUE;
    
    END_IF
    
    //Flash Timer
    fbTimer1(IN:= TRUE, PT:=T#1S); 
    
    //Command 
    Command:= 'sudo python hallo.py';
    
    //Timer is ON
    IF fbTimer1.Q  THEN
            //Reset it and flash again
            fbTimer1(IN:= FALSE);
            //Execute
            SysProcess.SysProcessExecuteCommand2(pszCommand:=Command
                                                , pszStdOut:=szStdOout
                                                , udiStdOutLen:= SIZEOF(szStdOout)
                                                , pResult := ADR(dutResult));
    END_IF
    

    Could someone please tell me what I am doing wrong here.

     

    Last edit: ramas 2022-02-17
  • eschwellinger

    eschwellinger - 2022-02-17

    call it without sudo then it should work.

     

    Last edit: eschwellinger 2022-02-17
    • ramas - 2022-02-18

      Thank you for the feedback i will try it without sudo!, But i think there is something wrong with the code in itself. Which would explain why there are red lines as you can see in the attachment "POU.png".

       
    • ramas - 2022-02-18

      Thank you for the feedback i will try it without sudo!, But i think there is something wrong with the code in itself. Which would explain why there are red lines as you can see in the attachment "POU.png".

       
    • ramas - 2022-02-18

      Thank you but that did not work. SysFile.SysFileCopy is being called as a function, i can see it in the Call tree but the file is not being copied into the directory. I wonder why

       
  • hapeter - 2022-02-18

    sometimes we have to clean our glasses ;-) In the CODESYS Development System there are two Windows, one for declaring the variables, one for the programcode. The later is minimized in your screenshot below the line 48, I can only see one line with the red "1". Move your programcode in this window and the red underlining should disapear.

     
    • ramas - 2022-02-18

      Yes thank you hapeter that was the problem, I am relatively new to codesys hence the rather amateur questions.

       

Log in to post a comment.