PendulumValveMockPMB.cs 4.2 KB

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