IoHeartbeat.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. namespace Venus_RT.Devices.IODevices
  10. {
  11. /// <summary>
  12. /// 心跳包机制
  13. ///
  14. /// 功能:
  15. /// 1.C#送出给PLC,如果PLC检测到C#心跳信号停止,则判定C#程序运行异常,从而触发安全逻辑动作。
  16. /// 2.C#检测PLC返回的心跳包信号,如果检测到PLC心跳信号停止,则判定PLC程序运行异常,从而进行报警处理。
  17. /// </summary>
  18. public class IoHeartbeat : BaseDevice, IDevice
  19. {
  20. private readonly int PLC_Heart_Beat_Timeout_ms = 1000 * 120;
  21. private readonly AIAccessor _ai;
  22. private readonly AOAccessor _ao;
  23. private readonly DeviceTimer _updateTimer = new DeviceTimer(); //更新 AO信号 给PLC
  24. private readonly int MAX = 0x7FFF;
  25. private float _prevAiValue;
  26. private readonly DeviceTimer _connectTimer = new DeviceTimer();
  27. private readonly R_TRIG _trigConnectionLost = new R_TRIG();
  28. // --------------------------Constructor-----------------------
  29. //
  30. public IoHeartbeat(string module, XmlElement node, string ioModule = "")
  31. {
  32. base.Module = module;
  33. base.Name = node.GetAttribute("id");
  34. base.Display = node.GetAttribute("display");
  35. base.DeviceID = node.GetAttribute("schematicId");
  36. _ai = ParseAiNode("ai", node, ioModule);
  37. _ao = ParseAoNode("ao", node, ioModule);
  38. }
  39. public bool Initialize()
  40. {
  41. _updateTimer.Start(500);
  42. _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
  43. return true;
  44. }
  45. public void Monitor()
  46. {
  47. if (Math.Abs(_prevAiValue - _GetRealFloat(_ai)) > 0.01)
  48. {
  49. _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
  50. }
  51. _trigConnectionLost.CLK = _connectTimer.IsTimeout();
  52. if (_trigConnectionLost.Q)
  53. {
  54. LOG.Write(eEvent.ERR_PLC_HEARTBEAT_FAIL, ModuleHelper.Converter(Module), Display);
  55. }
  56. _prevAiValue = _GetRealFloat(_ai);
  57. //如果计时到达,则翻转心跳信号
  58. if (_updateTimer.IsTimeout())
  59. {
  60. float beat_val = _GetRealFloat(_ao);
  61. beat_val++;
  62. if (beat_val >= MAX)
  63. {
  64. beat_val = 0;
  65. }
  66. _SetRealFloat(_ao, beat_val);
  67. _updateTimer.Start(3000); //500 ms
  68. }
  69. }
  70. public void Terminate() { }
  71. public void Reset()
  72. {
  73. _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
  74. _trigConnectionLost.RST = true;
  75. }
  76. }
  77. }