IoHeartbeat.cs 2.9 KB

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