AdTecGeneratorMockPMA.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Threading;
  3. using MECF.Framework.Simulator.Core.Driver;
  4. namespace EfemDualSimulator.Devices
  5. {
  6. class AdTecGeneratorMockPMA : SerialPortDeviceSimulator
  7. {
  8. public enum GeneratorStatus
  9. {
  10. Unknown,
  11. OFF,
  12. ON,
  13. ERROR
  14. }
  15. public GeneratorStatus _simGeneratorStatus;
  16. private const string EOF = "\r";
  17. private const char MSG_DELIMITER = '_';
  18. private const string MOCKUP_PORT = "COM37";
  19. private string SetPower = "0000";
  20. public AdTecGeneratorMockPMA(string com) : base(com, -1, EOF, MSG_DELIMITER)
  21. {
  22. _simGeneratorStatus = GeneratorStatus.Unknown;
  23. }
  24. protected override void ProcessUnsplitMessage(string message)
  25. {
  26. if (string.IsNullOrEmpty(message))
  27. throw new ArgumentException("Hardware command message is invalid");
  28. string sRes = string.Empty;
  29. if (message.Contains(EOF))
  30. {
  31. message = message.Remove(message.Length - 1);
  32. }
  33. switch (message)
  34. {
  35. case "Q":
  36. if (_simGeneratorStatus == GeneratorStatus.ON)
  37. {
  38. sRes = $"2010000 12345 0{SetPower} 00100 45678\r";
  39. }
  40. else if (_simGeneratorStatus == GeneratorStatus.OFF)
  41. {
  42. sRes = "2000000 12345 00000 00000 45678\r";
  43. }
  44. break;
  45. case "G":
  46. _simGeneratorStatus = GeneratorStatus.ON;
  47. sRes = "\r";
  48. break;
  49. case "S":
  50. _simGeneratorStatus = GeneratorStatus.OFF;
  51. sRes = "\r";
  52. break;
  53. default:
  54. if (message.EndsWith("W"))
  55. {
  56. SetPower = message.TrimEnd('W').Trim();
  57. }
  58. break;
  59. }
  60. //string[] strs = message.Split(MSG_DELIMITER);
  61. //Thread.Sleep(2 * 1000);
  62. sRes += "\r";
  63. OnWriteMessage(sRes);
  64. }
  65. }
  66. }