Join us today!
Running Windows Commands in TwinCAT
Since the TwinCAT runs on Windows, you can execute Windows commands in TwinCAT using the NT_StartProcess function block. To use this function block, you would need to add the Tc2_Utilities library in the project.
We will show how to open a txt file in TwinCAT using this function block. Declare the variables below in MAIN program:
fbStartWindowsCommand : NT_StartProcess; // Start Windows Command sPath : T_MaxString := 'C:\Windows\System32\cmd.exe' ; sDirName : T_MaxString := 'C:\Windows\System32' ; sCommandLine : T_MaxString := '/C %HOMEPATH%\Desktop\info.txt'; //text file path bStartWindowsCommand : BOOL;
sPath is the file path of the cmd.exe (command line program)
sCommandLine is the windows command you would like to execute in TwinCAT. To format the string command properly, you would need to use
'/C' and a space in your string command.
The string command below opens the info.txt file
sCommandLine : T_MaxString := '/C %HOMEPATH%\Desktop\info.txt'; // opens the info txt file
And the command below, copies the info.txt file from folderA to folder B. You can learn more about Windows commands here.
sCommandLine : T_MaxString := '/C "copy %HOMEPATH%\Desktop\folderA\info.txt %HOMEPATH%\Desktop\folderB';
Add the implementation below in MAIN program body and set the bStartWindowsCommand to TRUE.
//========================================================= //open the info.txt file using windows command fbStartWindowsCommand( NETID:= '', PATHSTR:= sPath, DIRNAME:= sDirName, COMNDLINE:= sCommandLine, START:= bStartWindowsCommand, TMOUT:= T#5S, BUSY=> , ERR=> , ERRID=> ); //=========================================================
In case you want to say thank you !)
We'd be very grateful if you could share this community with your colleagues and friends. You can also buy us a coffee to keep us fueled 😊 This is the best way to say thank you to this project and support your community.
twinControls - https://twincontrols.com/
In case you want to say thank you !)
We'd be very grateful if you could share this community with your colleagues and friends. You can also buy us a coffee to keep us fueled 😊 This is the best way to say thank you to this project and support your community.
twinControls - https://twincontrols.com/
To close a file by using the window title :
windows command => taskkill /F /FI "WindowTitle eq [window title name]" /T'
sCommandLine := '/C taskkill /F /FI "WindowTitle eq info - Notepad" /T';
In case you want to say thank you !)
We'd be very grateful if you could share this community with your colleagues and friends. You can also buy us a coffee to keep us fueled 😊 This is the best way to say thank you to this project and support your community.
twinControls - https://twincontrols.com/
My blog: codingbytes
My code: https://github.com/benhar-dev
- 17 Forums
- 267 Topics
- 942 Posts
- 4 Online
- 722 Members