简体   繁体   English

从PLC启动TwinCAT 3任务

[英]TwinCAT 3 Task Start/Stop from PLC

I need to run some code every time the PLC starts. 每当PLC启动时,我都需要运行一些代码。 This code should only be run once and then never again until the PLC is restarted. 该代码仅应运行一次,然后再运行一次,直到重新启动PLC。 I initialize some global variables and validate the persistent data before allowing the main PLC to run. 在允许主PLC运行之前,我初始化了一些全局变量并验证了持久数据。 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? 有没有办法启动/停止其他PLC任务? I noticed TwinCAT doesn't support initialization and shutdown interrupts for PLC tasks. 我注意到TwinCAT不支持PLC任务的初始化和关闭中断。

TwinCAT has a 'PlcTaskSystemInfo' struct containing a boolean for FirstCycle. TwinCAT具有“ PlcTaskSystemInfo”结构,其中包含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. 我不知道一种启动/停止单个PLC任务的方法。 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. 但也许可以像下面的代码一样简单,该代码仅在PLC启动时运行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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