KashiyamaPump.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Xml;
  6. using Aitex.Core.Common.DeviceData;
  7. using Aitex.Core.RT.DataCenter;
  8. using Aitex.Core.RT.Device;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.IOCore;
  11. using Aitex.Core.RT.Log;
  12. using Aitex.Core.RT.OperationCenter;
  13. using Aitex.Core.RT.SCCore;
  14. using Aitex.Core.RT.Tolerance;
  15. using Aitex.Core.Util;
  16. using MECF.Framework.Common.Communications;
  17. using MECF.Framework.Common.DataCenter;
  18. using MECF.Framework.Common.Device.Bases;
  19. using MECF.Framework.Common.Equipment;
  20. using Venus_Core;
  21. using static System.ValueTuple;
  22. namespace Venus_RT.Devices
  23. {
  24. class KashiyamaPump : PumpBase
  25. {
  26. public enum KashiyamaPumpState { ON = 0, OFF, Connected, Disconnected, Unknown, ERROR }
  27. private readonly Dictionary<int, int> _JIS7_CODE = new Dictionary<int, int>
  28. {
  29. {0, 0x30 },
  30. {1, 0x31 },
  31. {2, 0x32 },
  32. {3, 0x33 },
  33. {4, 0x34 },
  34. {5, 0x35 },
  35. {6, 0x36 },
  36. {7, 0x37 },
  37. {8, 0x38 },
  38. {9, 0x39 },
  39. {10,0x41 },
  40. {11,0x42 },
  41. {12, 0x43 },
  42. {13, 0x44 },
  43. {14, 0x45 },
  44. {15, 0x46 },
  45. };
  46. private readonly byte STX = 0x02;
  47. private readonly string _PortNum = "COM91";
  48. private readonly AsyncSerialPort _serial;
  49. public KashiyamaPumpState StatusDry { get; set; }
  50. public KashiyamaPump(ModuleName mod) : base(mod.ToString(), VirgoDevice.MainPump.ToString(), "Kashiyama Pump", "")
  51. {
  52. _PortNum = SC.GetStringValue($"{mod}.DryPump.Port");
  53. StatusDry = KashiyamaPumpState.Unknown;
  54. _serial = new AsyncSerialPort(_PortNum, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r\n", false);
  55. }
  56. public override bool Initialize()
  57. {
  58. base.Initialize();
  59. if (!_serial.Open())
  60. {
  61. StatusDry = KashiyamaPumpState.Disconnected;
  62. EV.PostAlarmLog(this.Module, "Kashiyama Pump串口无法打开");
  63. return false;
  64. }
  65. StatusDry = KashiyamaPumpState.Connected;
  66. _serial.OnBinaryDataChanged += OnPortBinaryDataChanged;
  67. return true;
  68. }
  69. private (int hiFCS, int loFCS) CaculateFCS(byte[] data, int length)
  70. {
  71. int hiFCS = 0, loFCS = 0;
  72. for (int i = 0; i < data.Length; i++)
  73. {
  74. hiFCS ^= data[i] >> 4;
  75. loFCS ^= data[i] ^ 0x0F;
  76. }
  77. return (hiFCS, loFCS);
  78. }
  79. private (bool bValid, byte command) FCSValidation(byte [] data)
  80. {
  81. if (data[0] != STX || data.Length < 7)
  82. {
  83. return (false, 0);
  84. }
  85. var fcs = CaculateFCS(data, data.Length - 2);
  86. if ((_JIS7_CODE[fcs.hiFCS] != data[data.Length - 4]) || (_JIS7_CODE[ fcs.loFCS] != data[data.Length - 3]))
  87. {
  88. return (false, 0);
  89. }
  90. return (true, data[1]);
  91. }
  92. private void OnPortBinaryDataChanged(byte[] obj)
  93. {
  94. var result = FCSValidation(obj);
  95. if(result.bValid)
  96. {
  97. switch(result.command)
  98. {
  99. case 0x42: // 'B': status
  100. break;
  101. case 0x58: // 'X': analog data
  102. break;
  103. }
  104. }
  105. else
  106. {
  107. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  108. {
  109. LOG.Info($"[{Module}] Kashiyama Pump 数据无效:[{System.Text.Encoding.ASCII.GetString(obj)}]");
  110. }
  111. }
  112. }
  113. static IEnumerable<TResult>
  114. GenerateSequence<TState, TResult> (
  115. TState seed,
  116. Func<TState, TState> Generator,
  117. Func<TState, TResult> resultSelector)
  118. {
  119. var state = seed;
  120. while (true)
  121. {
  122. yield return resultSelector(state);
  123. state = Generator(state);
  124. }
  125. }
  126. void test()
  127. {
  128. var fibonacci = GenerateSequence(
  129. (current: 0, next: 1),
  130. pair => (pair.next, pair.current + pair.next),
  131. pair => pair.current);
  132. }
  133. }
  134. }