IoHeartbeat.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 * 10;
  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. //public bool IsUnConnect;
  29. // --------------------------Constructor-----------------------
  30. //
  31. public IoHeartbeat(string module, XmlElement node, string ioModule = "")
  32. {
  33. base.Module = module;
  34. base.Name = node.GetAttribute("id");
  35. base.Display = node.GetAttribute("display");
  36. base.DeviceID = node.GetAttribute("schematicId");
  37. _ai = ParseAiNode("ai", node, ioModule);
  38. _ao = ParseAoNode("ao", node, ioModule);
  39. }
  40. public bool Initialize()
  41. {
  42. _updateTimer.Start(500);
  43. _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
  44. return true;
  45. }
  46. public void Monitor()
  47. {
  48. if (Math.Abs(_prevAiValue - _GetRealFloat(_ai)) > 0.01)
  49. {
  50. _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
  51. }
  52. _trigConnectionLost.CLK = _connectTimer.IsTimeout();
  53. if (_trigConnectionLost.Q)
  54. {
  55. //IsUnConnect = true;
  56. LOG.Write(eEvent.ERR_PLC_HEARTBEAT_FAIL, ModuleHelper.Converter(Module), Display);
  57. }
  58. _prevAiValue = _GetRealFloat(_ai);
  59. //如果计时到达,则翻转心跳信号
  60. if (_updateTimer.IsTimeout())
  61. {
  62. float beat_val = _GetRealFloat(_ao);
  63. beat_val++;
  64. if (beat_val >= MAX)
  65. {
  66. beat_val = 0;
  67. }
  68. _SetRealFloat(_ao, beat_val);
  69. _updateTimer.Start(3000); //500 ms
  70. }
  71. }
  72. public void Terminate() { }
  73. public void Reset()
  74. {
  75. _connectTimer.Start(PLC_Heart_Beat_Timeout_ms);
  76. _trigConnectionLost.RST = true;
  77. //IsUnConnect = false;
  78. }
  79. }
  80. }