123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System;
- using System.Xml;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.IOCore;
- using Aitex.Core.RT.Log;
- using Aitex.Core.Util;
- namespace Aitex.Core.RT.Device.Unit
- {
- /// <summary>
- /// 心跳包机制
- ///
- /// 功能:
- /// 1.C#送出给PLC,如果PLC检测到C#心跳信号停止,则判定C#程序运行异常,从而触发安全逻辑动作。
- /// 2.C#检测PLC返回的心跳包信号,如果检测到PLC心跳信号停止,则判定PLC程序运行异常,从而进行报警处理。
- /// </summary>
- public class IoHeartbeat : BaseDevice, IDevice
- {
- private int PLC_Heart_Beat_Timeout_ms = 1000* 120;
- //IO
- private AIAccessor _ai = null;
- private AOAccessor _ao = null;
-
- private DeviceTimer _updateTimer = new DeviceTimer(); //更新 AO信号 给PLC
-
- private int MAX = 0x7FFF;
- private float _prevAiValue = 0;
- private DeviceTimer _connectTimer = new DeviceTimer();
- private R_TRIG _trigConnectionLost = new R_TRIG();
- public IoHeartbeat(string module, XmlElement node, string ioModule = "")
- {
- base.Module = module;
- base.Name = node.GetAttribute("id");
- base.Display = node.GetAttribute("display");
- base.DeviceID = node.GetAttribute("schematicId");
- _ai = ParseAiNode("ai", node, ioModule);
- _ao = ParseAoNode("ao", node, ioModule);
- }
-
- public bool Initialize()
- {
- _updateTimer.Start(500);
- _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
- return true;
- }
- public void Terminate()
- {
- }
- public void Monitor()
- {
- if (Math.Abs(_prevAiValue - _ai.Value) > 0.01)
- {
- _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
- }
- _trigConnectionLost.CLK = _connectTimer.IsTimeout();
- if (_trigConnectionLost.Q)
- {
- ////EV.PostMessage(Module, EventEnum.PlcHeartBeatFail, Module, Display);
- LOG.Write(eEvent.ERR_PLC_HEARTBEAT_FAIL, Module, Display);
- }
- _prevAiValue = _ai.Value;
-
- //如果计时到达,则翻转心跳信号
- if (_updateTimer.IsTimeout())
- {
- int value = (int)_ao.Value;
- value++;
- if (value >= MAX)
- {
- value = 0;
- }
- _ao.Value = (short)value;
- _updateTimer.Start(500); //500 ms
- }
- }
- public void Reset()
- {
- _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
- _trigConnectionLost.RST = true;
- }
- }
- }
|