简体   繁体   中英

TwinCAT 3 Task Start/Stop from PLC

I need to run some code every time the PLC starts. This code should only be run once and then never again until the PLC is restarted. I initialize some global variables and validate the persistent data before allowing the main PLC to run. This is because the actions of the machine can be damaging if some of these variables are not setup correctly.

Is there a way to start/stop the other PLC tasks? I noticed TwinCAT doesn't support initialization and shutdown interrupts for PLC tasks.

TwinCAT has a 'PlcTaskSystemInfo' struct containing a boolean for FirstCycle. You can use that to run the initializing code only once.

VAR fbGetCurTaskIdx: GETCURTASKINDEX; (* Further example+explanation in Infosys *)

fbGetCurTaskIdx();
IF _TaskInfo[fbGetCurTaskIdx.index].FirstCycle THEN
  (* Initialization code here *)
ELSE
  (* Normal code here *)
END_IF;

I don't know of a way to start/stop individual PLC tasks. You can start/stop a runtime though.

But perhaps it can be as simple as this code below, which will only run when your PLC starts.

VAR initialized: BOOL := FALSE;

IF NOT initialized THEN
  (* Run your initialization code here *)
  initialized := TRUE;
END_IF

(* Rest of your program here *)

Edit:

I used a state machine inside the initialization code to help with the task allowed time issue.

Example:

VAR
  Initialized : BOOL := FALSE;
  Init_State  : UINT := 0;
END_VAR

IF NOT Initialized THEN
  (* Initialization State Machine *)
  CASE Init_State OF
    0: (* First step in initialization *)
       Init_State := Init_State + 1;
    1: (* Second step in initialization *)
       Init_State := Init_State + 1;
     .
     .
     .
    n: (* Last step in initialization *)
       Initialized := TRUE;
  END_CASE
END_IF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM