PendulumValveMockPMA.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.ServiceModel.Channels;
  3. using MECF.Framework.Simulator.Core.Driver;
  4. namespace Venus_Simulator.Devices
  5. {
  6. class PendulumValveMockPMA : SerialPortDeviceSimulator
  7. {
  8. public enum PendulumValveStatus
  9. {
  10. Unknown,
  11. OFF,
  12. ON,
  13. ERROR
  14. }
  15. private const string EOF = "\r";
  16. private const char MSG_DELIMITER = '_';
  17. private const string MOCKUP_PORT = "COM65";
  18. private int _pressure = 120;
  19. private int _position = 500;
  20. private bool _bHold = false;
  21. private Random _rd = new Random();
  22. PendulumValveStatus _simPendulumValveStatus;
  23. public PendulumValveMockPMA() : base(MOCKUP_PORT, -1, EOF, MSG_DELIMITER)
  24. {
  25. _simPendulumValveStatus = PendulumValveStatus.Unknown;
  26. }
  27. protected override void ProcessUnsplitMessage(string message)
  28. {
  29. if (string.IsNullOrEmpty(message))
  30. throw new ArgumentException("Hardware command message is invalid");
  31. var items = message.Split('\r');
  32. foreach (var item in message.Split('\r'))
  33. {
  34. if (item == "")
  35. {
  36. continue;
  37. }
  38. string item2 = item + "\r";
  39. if (_bHold == false)
  40. {
  41. //_pressure += _rd.Next(-10, 10);
  42. //_position += _rd.Next(-50, 50);
  43. }
  44. if (_simPendulumValveStatus == PendulumValveStatus.OFF)
  45. {
  46. _position = 0;
  47. }
  48. string sRes = string.Empty;
  49. string str = item2.Trim();
  50. switch (str)
  51. {
  52. case "P:":
  53. sRes = string.Format("P:{0:D8}\r\n", _pressure);
  54. break;
  55. case "A:":
  56. sRes = string.Format("A:{0:D6}\r\n", _position);
  57. break;
  58. case "C:":
  59. _bHold = false;
  60. _simPendulumValveStatus = PendulumValveStatus.OFF;
  61. sRes = "C:\r\n";
  62. break;
  63. case "O:":
  64. _bHold = false;
  65. _simPendulumValveStatus = PendulumValveStatus.ON;
  66. sRes = "O:\r\n";
  67. break;
  68. case "H:":
  69. _bHold = true;
  70. sRes = "H:\r\n";
  71. break;
  72. case "i:30":
  73. string data = _simPendulumValveStatus == PendulumValveStatus.OFF ? "3" : "4";
  74. sRes = $"i:301{data}100000\r\n";
  75. break;
  76. case "i:02A04":
  77. sRes = $"i:02B041000.1\r\n";
  78. break;
  79. case "i:02A05":
  80. sRes = $"i:02B051001.2\r\n";
  81. break;
  82. default:
  83. {
  84. if (str.Contains("S:"))
  85. {
  86. int pressure;
  87. if (int.TryParse(str.Substring(2, 8), out pressure))
  88. {
  89. _pressure = pressure;
  90. }
  91. _bHold = false;
  92. sRes = "S:\r\n";
  93. }
  94. else if (str.StartsWith("R:"))
  95. {
  96. int position;
  97. if (int.TryParse(str.Substring(2, 6), out position))
  98. {
  99. _position = position;
  100. }
  101. _bHold = false;
  102. sRes = "R:\r\n";
  103. }
  104. else
  105. return;
  106. }
  107. break;
  108. }
  109. OnWriteMessage(sRes);
  110. }
  111. }
  112. }
  113. }