MicrowaveGeneratorMockPMA.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Threading;
  3. using MECF.Framework.Simulator.Core.Driver;
  4. namespace EfemDualSimulator.Devices
  5. {
  6. class MicrowaveGeneratorMockPMA : 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 ushort SetPower = 0;
  20. public MicrowaveGeneratorMockPMA(string com) : base(com, -1, EOF, MSG_DELIMITER, false)
  21. {
  22. _simGeneratorStatus = GeneratorStatus.Unknown;
  23. }
  24. protected override void ProcessUnsplitMessage(byte[] message)
  25. {
  26. if(message == null || message.Length == 0)
  27. throw new ArgumentException("Microwave hardware command message is invalid");
  28. var byte0 = message[0];
  29. var byte1 = message[1];
  30. if(byte1 == 0x03)
  31. {
  32. var commandBytes = new byte[] { message[3], message[2] };
  33. var address = BitConverter.ToUInt16(commandBytes, 0);
  34. var lenBytes = new byte[] { message[5], message[4] };
  35. var len = BitConverter.ToUInt16(lenBytes, 0);
  36. var response = new byte[5 + len * 2];
  37. response[0] = byte0;
  38. response[1] = byte1;
  39. response[2] = Convert.ToByte(len);
  40. ushort data = 0;
  41. if (address == MicrowareRFCommand.PowerOnOrOff)
  42. {
  43. data = (ushort)(_simGeneratorStatus == GeneratorStatus.ON ? 1 : 0);
  44. }
  45. else if (address == MicrowareRFCommand.PowerSetPoint ||
  46. address == MicrowareRFCommand.ForwardPower ||
  47. address == MicrowareRFCommand.ReflectedPower)
  48. {
  49. data = SetPower;
  50. }
  51. response[3] = BitConverter.GetBytes(data)[1];
  52. response[4] = BitConverter.GetBytes(data)[0];
  53. var crc = CRC16(response);
  54. response[5] = crc[0];
  55. response[6] = crc[1];
  56. OnWriteMessage(response);
  57. }
  58. else if(byte1 == 0x06)
  59. {
  60. var commandBytes = new byte[] { message[3], message[2] };
  61. var address = BitConverter.ToUInt16(commandBytes, 0);
  62. var dataBytes = new byte[] { message[5], message[4] };
  63. var data = BitConverter.ToUInt16(dataBytes, 0);
  64. if (address == MicrowareRFCommand.PowerOnOrOff)
  65. {
  66. _simGeneratorStatus = data == 1 ? GeneratorStatus.ON : GeneratorStatus.OFF;
  67. }
  68. else if(address == MicrowareRFCommand.PowerSetPoint)
  69. {
  70. SetPower = data;
  71. }
  72. OnWriteMessage(message);
  73. }
  74. //if (string.IsNullOrEmpty(message))
  75. // throw new ArgumentException("Hardware command message is invalid");
  76. //string sRes = string.Empty;
  77. //if (message.Contains(EOF))
  78. //{
  79. // message = message.Remove(message.Length - 1);
  80. //}
  81. //switch (message)
  82. //{
  83. // case "Q":
  84. // if (_simGeneratorStatus == GeneratorStatus.ON)
  85. // {
  86. // sRes = $"2010000 12345 0{SetPower} 00100 45678\r";
  87. // }
  88. // else if (_simGeneratorStatus == GeneratorStatus.OFF)
  89. // {
  90. // sRes = "2000000 12345 00000 00000 45678\r";
  91. // }
  92. // break;
  93. // case "G":
  94. // _simGeneratorStatus = GeneratorStatus.ON;
  95. // sRes = "\r";
  96. // break;
  97. // case "S":
  98. // _simGeneratorStatus = GeneratorStatus.OFF;
  99. // sRes = "\r";
  100. // break;
  101. // default:
  102. // if (message.EndsWith("W"))
  103. // {
  104. // SetPower = message.TrimEnd('W').Trim();
  105. // }
  106. // break;
  107. //}
  108. ////string[] strs = message.Split(MSG_DELIMITER);
  109. ////Thread.Sleep(2 * 1000);
  110. //sRes += "\r";
  111. }
  112. public static byte[] CRC16(byte[] data)
  113. {
  114. ushort crc = 0xFFFF;
  115. for (int i = 0; i < data.Length; i++)
  116. {
  117. crc = (ushort)(crc ^ (data[i]));
  118. for (int j = 0; j < 8; j++)
  119. {
  120. crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
  121. }
  122. }
  123. byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
  124. byte lo = (byte)(crc & 0x00FF); //低位置
  125. return new byte[] { hi, lo };
  126. }
  127. }
  128. static class MicrowareRFCommand
  129. {
  130. public const ushort PowerOnOrOff = 0x0081; //1:开 0:关 RW
  131. public const ushort PowerSetPoint = 0x0082; //功率设定值(W) RW
  132. public const ushort State = 0x008C; //设备故障 R
  133. public const ushort Reset = 0x009F; //清除异常 RW
  134. public const ushort Warning = 0x00A0; //整机预警 R
  135. public const ushort ForwardPower = 0x00A1; //整机输出功率 R
  136. public const ushort ReflectedPower = 0x00A2; //整机反射功率 R
  137. }
  138. }