In this Tutorial I will explain how to execute a windows application from TwinCAT.
Insert Library
Reference>Add library.
Search TC2_Utilities and Insert it.
Confirm the Command Prompt Path
open the command line and enter where cmd to check your Command Prompt Path.
Function Block
We will use NT_StartProcess to implement these features.Of Course not just your local PC but also Remote PC.
VAR_INPUT
| NETID | T_AmsNetId | The AmsNetID of Target TwinCAT PC.Input “Empty String” if you want to execute the local PC application. |
| PATHSTR | T_MaxString | The path of application |
| DIRNAME | T_MaxString | The Directory of the Application |
| COMNDLINE | T_MaxString | The Command |
| START | BOOL | Rise Edge to Execute |
| TMOUT | TIME | Timeout |
VAR_OUTPUT
| BUSY | BOOL | True=Executing |
| ERR | BOOL | True=Error |
| ERRID | UDINT | Error ID |
プログラム
In this example, I will execute a program to create a text file and append the text to the last line of file.
VAR
| VAR // stCommand:STRING(512); stPrintingMessage:STRING:=’Hello,This is my message that write from TwinCAT PLC!’; NT_StartProcess:NT_StartProcess; // bTrigger:BOOL; bError:BOOL; iErrorID:UDINT; bReset:BOOL; END_VAR |
Code
| stCommand:=’/C ‘; stCommand:=CONCAT(stCommand,’echo ‘); stCommand:=CONCAT(stCommand,stPrintingMessage); stCommand:=CONCAT(stCommand,’>> ‘); stCommand:=CONCAT(stCommand,’C:\Users\root\myfile.txt’); NT_StartProcess( NETID:=” ,PATHSTR:=cStCommandPath ,COMNDLINE:=stCommand ); IF bReset THEN bError:=FALSE; iErrorID:=0; bReset:=FALSE; NT_StartProcess(Start:=False); END_IF; IF bTrigger AND NOT bError THEN NT_StartProcess(Start:=TRUE); IF NT_StartProcess.ERR THEN bError:=TRUE; iErrorID:=NT_StartProcess.ERRID; END_IF bTrigger:=FALSE; NT_StartProcess(Start:=False); END_IF |
Result
myfile.txt is created in the setting path.
And you can see the content is the same as the function block’s parameter.
Be Careful
The command’s length should not be longer than your String variable.It may cause the command can not 100% fully execute,
Sample Code
Please download by the following link:
https://github.com/soup01Threes/TwinCAT3/blob/main/TwinCAT%20Project_usingTerminal.tnzip