PendulumValveMockPMC.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using MECF.Framework.Simulator.Core.Driver;
  3. namespace Venus_Simulator.Devices
  4. {
  5. class PendulumValveMockPMC : 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 = "COM78";
  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 PendulumValveMockPMC() : 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. if(_bHold == false)
  31. {
  32. _pressure += _rd.Next(-10, 10);
  33. _position += _rd.Next(-50, 50);
  34. }
  35. if(_simPendulumValveStatus == PendulumValveStatus.OFF)
  36. {
  37. _position = 0;
  38. }
  39. string sRes = string.Empty;
  40. string str = message.Trim();
  41. switch(str)
  42. {
  43. case "P:":
  44. sRes = string.Format("P:{0:D8}\r\n", _pressure);
  45. break;
  46. case "A:":
  47. sRes = string.Format("A:{0:D6}\r\n", _position);
  48. break;
  49. case "C:":
  50. _bHold = false;
  51. _simPendulumValveStatus = PendulumValveStatus.OFF;
  52. sRes = "C:\r\n";
  53. break;
  54. case "O":
  55. _bHold = false;
  56. _simPendulumValveStatus = PendulumValveStatus.ON;
  57. sRes = "O:\r\n";
  58. break;
  59. case "H:":
  60. _bHold = true;
  61. sRes = "H:\r\n";
  62. break;
  63. case "i:30":
  64. sRes = "i:3012100000\r\n";
  65. break;
  66. default:
  67. {
  68. if (str.Contains("S:"))
  69. {
  70. int pressure;
  71. if (int.TryParse(str.Substring(2, 8), out pressure))
  72. {
  73. _pressure = pressure;
  74. }
  75. _bHold = false;
  76. sRes = "S:\r\n";
  77. }
  78. else if (str.StartsWith("R:"))
  79. {
  80. int position;
  81. if (int.TryParse(str.Substring(2, 6), out position))
  82. {
  83. _position = position;
  84. }
  85. _bHold = false;
  86. sRes = "R:\r\n";
  87. }
  88. else
  89. return;
  90. }
  91. break;
  92. }
  93. OnWriteMessage(sRes);
  94. }
  95. }
  96. }