AdTecGeneratorMockPMA.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MECF.Framework.Simulator.Core.Driver;
  6. namespace Venus_Simulator.Devices
  7. {
  8. class AdTecGeneratorMockPMA : SerialPortDeviceSimulator
  9. {
  10. public enum GeneratorStatus
  11. {
  12. Unknown,
  13. OFF,
  14. ON,
  15. ERROR
  16. }
  17. public static GeneratorStatus _simGeneratorStatus;
  18. private const string EOF = "\r";
  19. private const char MSG_DELIMITER = '_';
  20. private const string MOCKUP_PORT = "COM37";
  21. private string RFPower = "0";
  22. public AdTecGeneratorMockPMA() : base(MOCKUP_PORT, -1, EOF, MSG_DELIMITER)
  23. {
  24. _simGeneratorStatus = GeneratorStatus.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. string sRes = string.Empty;
  31. if (message.Contains(EOF))
  32. {
  33. message = message.Remove(message.Length - 1);
  34. }
  35. string[] msgs = message.Split('\r');
  36. foreach(string msg in msgs)
  37. {
  38. if (msg.Contains(" W"))
  39. {
  40. RFPower = msg.Split(' ')[0].ToString().PadLeft(5, '0');
  41. if (_simGeneratorStatus == GeneratorStatus.ON)
  42. {
  43. sRes = $"2010000 12345 {RFPower} 00010 45678\r";
  44. }
  45. }
  46. switch (msg)
  47. {
  48. case "Q":
  49. if (_simGeneratorStatus == GeneratorStatus.ON)
  50. {
  51. sRes = $"2010000 12345 {RFPower} 00010 45678\r";
  52. }
  53. else if (_simGeneratorStatus == GeneratorStatus.OFF)
  54. {
  55. sRes = "2000000 12345 00000 00000 45678\r";
  56. }
  57. break;
  58. case "G":
  59. _simGeneratorStatus = GeneratorStatus.ON;
  60. sRes = "\r";
  61. break;
  62. case "S":
  63. _simGeneratorStatus = GeneratorStatus.OFF;
  64. sRes = "\r";
  65. break;
  66. default:
  67. break;
  68. }
  69. //Thread.Sleep(200);
  70. sRes += "\r";
  71. OnWriteMessage(sRes);
  72. }
  73. }
  74. }
  75. }