2

So currently, I am trying to pick up this code that an intern left off for this project. I have a batch file that is run when clicked, and will execute a powershell script that will convert a generated CSV file from collected data to an Excel file. I am trying to get this process to happen automatically when a button is clicked in the HMI, but am not sure how to call a batch file in Codesys.

I have tried calling the batch file in different places of my code. Nothing will run it when the button is clicked.

FUNCTION ScriptExecute 
VAR
    szStdOout : STRING(100);    
    dutResult : sysfile.RTS_IEC_RESULT;
    szCommand: STRING;
END_VAR
szCommand := '/home/cds-apps/PlcLogic/data/ psExecuter.bat';
SysProcess.SysProcessExecuteCommand2(pszCommand:=szCommand, pszStdOut:=szStdOout, udiStdOutLen:= SIZEOF(szStdOout), pResult := ADR(dutResult));

No errors come up with this code, I just want to find a way for the batch file to be executed automatically when the user clicks the specific button. Currently the user would have to manually go into the file explorer and run the batch file themselves.

  • `FUNCTION ScriptExecute` should return an error. It should be `FUNCTION ScriptExecute : BOOL` or something like this. Actually It should create an error during compilation. – Sergey Romanov Sep 01 '19 at 08:08
  • Try to add your bat fie to `c:\windows\system32` folder, or much better add `/home/cds-apps/PlcLogic/data/` to PATH and then only use `szCommand := 'psExecuter.bat';` also try to run this command in terminal and confirm it is working in a terminal. – Sergey Romanov Sep 01 '19 at 08:15

1 Answers1

1

This is my tested code with codesys V3 on a Windows 10 machine:

Declaration part:

PROGRAM PLC_PRG
VAR
    pRtsIecResult   : POINTER TO SysProcess.RTS_IEC_RESULT;
    bStart          : BOOL;
END_VAR

Implementation part:

IF bStart 
THEN

    bStart := FALSE;

    SysProcess.SysProcessCreate2(
        pszApplication  := 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe',
        pszCommandLine  := '-executionpolicy remotesigned -File C:\Users\fboid\Desktop\HelloWorld.ps1',
        ulFlags         := SysProcess.SYSPROCESS_CREATEFLAG_INTERACTIVE,
        pResult         := pRtsIecResult
    );

END_IF

As you can see I used the SysProcessCreate2 function of the SysProcess-library. Because the functions in the SysProcess-library are operating system dependent, they may not work on all platforms.

The powershell script I executed was a very simple one:

Write-Host 'Hello World!'
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") 
Filippo Boido
  • 1,136
  • 7
  • 11
  • Did not notice the spacing, but it does not seem to affect anything. And this is all being executed on windows. So with SysProcessCreate, I would be able to run that external bath file through Codesys HMI when a button is clicked? – QueenLexi13 Aug 30 '19 at 15:36
  • I thought you were using linux because of the path with /home in it and because SysProcessExecuteCommand2 is used on Linux. – Filippo Boido Aug 30 '19 at 16:09
  • Thank you! This is code from another interns work, and he is a big Linux fan so that may be why. I am only familiar with windows. When I try the code you provided I get all kinds of errors. RTS_HANDLE seems to not exist, so would that mean I am missing a library? – QueenLexi13 Aug 30 '19 at 16:42
  • Yes, you need to insert the SysProcess library into your project. – Filippo Boido Aug 30 '19 at 23:16
  • Your code example is С++, but @QueenLexi13 asked in ST. Maybe you had to explain that you mean to create C++ based function block library that then can be used in ST and how to do all that. – Sergey Romanov Sep 01 '19 at 08:07
  • In the old documentation it's documented like this. I had no time to search the new one (Probably it's still the same notation). In the SysProcess library you don't have for example char *pszApplication but reference to String.. – Filippo Boido Sep 02 '19 at 04:17
  • So after writing the code for SysProcessCreate, do I need a SysProcess Execute as well? Or should the SysProcessCreate run the batch file for the script as well? – QueenLexi13 Sep 06 '19 at 17:16
  • I updated the answer and inserted a working example. – Filippo Boido Sep 07 '19 at 08:44