Beckhoff#Execute the Window application in TwinCAT3

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

NETIDT_AmsNetIdThe AmsNetID of Target TwinCAT PC.Input “Empty String” if you want to execute the local PC application.
PATHSTRT_MaxStringThe path of application
DIRNAMET_MaxStringThe Directory of the Application
COMNDLINET_MaxStringThe Command
STARTBOOLRise Edge to Execute
TMOUTTIMETimeout

VAR_OUTPUT

BUSYBOOLTrue=Executing
ERRBOOLTrue=Error
ERRIDUDINTError 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

Footer_Basic

Please Support some devices for my blog

Amazon Gift List

Find ME

Twitter:@3threes2
Email:soup01threes*gmail.com (* to @)
YoutubeChannel:https://www.youtube.com/channel/UCQ3CHGAIXZAbeOC_9mjQiWQ

シェアする

  • このエントリーをはてなブックマークに追加

フォローする