IoHeartbeat.cs 2.6 KB

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