IoHeartbeat.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Xml;
  3. using Aitex.Core.RT.Device;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.IOCore;
  6. using Aitex.Core.Util;
  7. using Aitex.Core.RT.Log;
  8. using MECF.Framework.Common.Equipment;
  9. using Aitex.Core.RT.SCCore;
  10. namespace Venus_RT.Devices.IODevices
  11. {
  12. /// <summary>
  13. /// 心跳包机制
  14. ///
  15. /// 功能:
  16. /// 1.C#送出给PLC,如果PLC检测到C#心跳信号停止,则判定C#程序运行异常,从而触发安全逻辑动作。
  17. /// 2.C#检测PLC返回的心跳包信号,如果检测到PLC心跳信号停止,则判定PLC程序运行异常,从而进行报警处理。
  18. /// </summary>
  19. public class IoHeartbeat : BaseDevice, IDevice
  20. {
  21. private readonly int PLC_Heart_Beat_Timeout_ms = 1000 * 10;
  22. private readonly AIAccessor _ai;
  23. private readonly AOAccessor _ao;
  24. private readonly DeviceTimer _updateTimer = new DeviceTimer(); //更新 AO信号 给PLC
  25. private readonly int MAX = 0x7FFF;
  26. private float _prevAiValue;
  27. private readonly DeviceTimer _connectTimer = new DeviceTimer();
  28. private readonly R_TRIG _trigConnectionLost = new R_TRIG();
  29. private bool _isSimulatorMode;
  30. //public bool IsUnConnect;
  31. // --------------------------Constructor-----------------------
  32. //
  33. public IoHeartbeat(string module, XmlElement node, string ioModule = "")
  34. {
  35. base.Module = module;
  36. base.Name = node.GetAttribute("id");
  37. base.Display = node.GetAttribute("display");
  38. base.DeviceID = node.GetAttribute("schematicId");
  39. _ai = ParseAiNode("ai", node, ioModule);
  40. _ao = ParseAoNode("ao", node, ioModule);
  41. _isSimulatorMode = SC.GetConfigItem("System.IsSimulatorMode").BoolValue;
  42. }
  43. public bool Initialize()
  44. {
  45. _updateTimer.Start(500);
  46. _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
  47. return true;
  48. }
  49. public void Monitor()
  50. {
  51. if (Math.Abs(_prevAiValue - _GetRealFloat(_ai)) > 0.01)
  52. {
  53. _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
  54. }
  55. _trigConnectionLost.CLK = _connectTimer.IsTimeout();
  56. if (_trigConnectionLost.Q)
  57. {
  58. //IsUnConnect = true;
  59. //LOG.Write(eEvent.ERR_PLC_HEARTBEAT_FAIL, ModuleHelper.Converter(Module), Display);
  60. if (!_isSimulatorMode)
  61. {
  62. if (Module == "TM")
  63. {
  64. LOG.Write(eEvent.ERR_TM_PLC_HEARTBEAT_FAIL, Module, Display);
  65. }
  66. else
  67. {
  68. LOG.Write(eEvent.ERR_PM_PLC_HEARTBEAT_FAIL, Module, Display);
  69. }
  70. }
  71. }
  72. _prevAiValue = _GetRealFloat(_ai);
  73. //如果计时到达,则翻转心跳信号
  74. if (_updateTimer.IsTimeout())
  75. {
  76. float beat_val = _GetRealFloat(_ao);
  77. beat_val++;
  78. if (beat_val >= MAX)
  79. {
  80. beat_val = 0;
  81. }
  82. _SetRealFloat(_ao, beat_val);
  83. _updateTimer.Start(3000); //500 ms
  84. }
  85. }
  86. public void Terminate() { }
  87. public void Reset()
  88. {
  89. _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
  90. _trigConnectionLost.RST = true;
  91. //IsUnConnect = false;
  92. }
  93. }
  94. }