开发过程模拟器逻辑
开发将模拟长期运行的任务的逻辑。
先决条件
设置默认外部代码编辑器。请参见设置默认代码编辑器。
- 开发过程模拟器逻辑
- 创建 ProcessVariableSimulator 对象:
- 在项目视图中,右键单击模型,然后选择 。
- 将光标悬停在对象上,选择 并输入ProcessVariableSimulator。
- 在属性中,通过选择 和变量添加四个变量。
- 对这些变量进行重命名,方法是将光标悬停在每个变量上,选择 并输入以下内容:
- IsRunning
- Message
- Step
- Value
- 选择IsRunning旁边的Int32,然后选择Boolean。
- 选择Message旁边的Int32,然后选择String。
- 开发 ProcessVariableSimulatorLogic 脚本:
- 右键单击ProcessVariableSimulator,然后选择 。
- 将光标悬停在 NetLogic 上,选择 并输入ProcessVariableSimulatorLogic
- 双击 NetLogic。外部代码编辑器将打开。
- 使用以下代码替换现有代码:using System.Threading; using UAManagedCore; public class ProcessVariableSimulatorLogic : FTOptix.NetLogic.BaseNetLogic { public override void Start() { var owner = (ProcessVariableSimulator)LogicObject.Owner; valueVariable = owner.ValueVariable; stepVariable = owner.StepVariable; messageVariable = owner.MessageVariable; isRunningVariable = owner.IsRunningVariable; increment = true; periodicTask = new PeriodicTask(IncrementDecrementTask, 500, LogicObject); periodicTask.Start(); longRunningTask = new LongRunningTask(MessageTask, LogicObject); longRunningTask.Start(); } public override void Stop() { periodicTask.Dispose(); periodicTask = null; longRunningTask.Dispose(); longRunningTask = null; } [FTOptix.NetLogic.ExportMethod] public void StartMethod() { periodicTask.Start(); isRunningVariable.Value = true; } [FTOptix.NetLogic.ExportMethod] public void StopMethod() { periodicTask.Cancel(); isRunningVariable.Value = false; } public void IncrementDecrementTask() { int currentValue = valueVariable.Value; int step = stepVariable.Value; if (increment) { currentValue += step; if (currentValue >= 100) { currentValue = 100; increment = false; } } else { currentValue -= step; if (currentValue <= 0) { currentValue = 0; increment = true; } } valueVariable.Value = currentValue; } public void MessageTask() { Thread.Sleep(2000); messageVariable.Value = "PHASE 1"; Thread.Sleep(2000); messageVariable.Value = "PHASE 2"; Thread.Sleep(2000); messageVariable.Value = "PHASE 3"; Thread.Sleep(2000); messageVariable.Value = "PHASE 4"; } private IUAVariable valueVariable; private IUAVariable stepVariable; private IUAVariable messageVariable; private IUAVariable isRunningVariable; private bool increment; private PeriodicTask periodicTask; private LongRunningTask longRunningTask; }
- 保存代码。
- 将 ProcessVariableSimulatorLogic 对象转换为对象类型:
- 在类型视图中,双击模型。
- 将ProcessVariableSImulator从项目视图拖到类型视图中。
- 通过将ProcessVariableSImulator从类型视图拖到项目视图中的模型文件夹中,创建 ProcessVariableSimulator1 对象。
- 配置 ProcessVariableSimulator1 对象:
- 在属性中,将IsRunning设置为真。
- 将Step设置为1
提供反馈