IoHeartbeat.cs 2.7 KB

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