Join us today!

Running Windows Com...
 
Notifications
Clear all

Running Windows Commands in TwinCAT

4 Posts
2 Users
0 Reactions
1,070 Views
twinControls
Posts: 114
Admin
Topic starter
(@twincontrols)
Member
Joined: 2 years ago

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=> );
//========================================================= 

 

 

 

Reply
3 Replies
twinControls
Posts: 114
Admin
Topic starter
(@twincontrols)
Member
Joined: 2 years ago

Related : Executing Command-Line Commands in TwinCAT

Related: Logging to a network drive in TwinCAT

Reply
twinControls
Posts: 114
Admin
Topic starter
(@twincontrols)
Member
Joined: 2 years ago

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';

Reply
benhar-dev
Posts: 17
(@benhar-dev)
Eminent Member
Joined: 2 years ago
Reply
Share: