TruPlasmaRF.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Device;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.RT.Tolerance;
  8. using Aitex.Core.UI.Control;
  9. using Aitex.Core.Util;
  10. using MECF.Framework.Common.Communications;
  11. using MECF.Framework.Common.DataCenter;
  12. using MECF.Framework.Common.Device.Bases;
  13. using MECF.Framework.Common.Equipment;
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Net;
  19. using Venus_Core;
  20. namespace Venus_RT.Devices
  21. {
  22. public class TruPlasmaRF : RfPowerBase
  23. {
  24. private readonly AsyncSerialPort _serial;
  25. private static byte _ACK = 0x06;
  26. private static byte ParamRead = 0x01;
  27. private static byte ParamWrite = 0x02;
  28. private static byte TelegramError = 0xFE;
  29. private static byte MessageRead = 0x04;
  30. private static byte CtllnterfaceAct= 0x05;
  31. // private bool = 0x05;
  32. public TruPlasmaRF(ModuleName mod, VenusDevice device) : base(mod.ToString(), device.ToString())
  33. {
  34. var portNum = SC.GetStringValue(device == VenusDevice.Rf ? $"{mod}.Rf.Port" : $"{mod}.BiasRf.Port");
  35. _serial = new AsyncSerialPort(portNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\r\r");
  36. }
  37. public override bool Initialize()
  38. {
  39. base.Initialize();
  40. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  41. if (_serial.Open())
  42. {
  43. _serial.OnBinaryDataChanged += SerialPortDataReceived;
  44. _serial.OnErrorHappened += SerialPortErrorOccurred;
  45. }
  46. else
  47. {
  48. LOG.Write(eEvent.ERR_RF, Module, "Tru 射频发生器串口无法打开");
  49. return false;
  50. }
  51. return true;
  52. }
  53. public new AITRfData DeviceData =>
  54. new AITRfData
  55. {
  56. Module = Module,
  57. DeviceName = Name,
  58. ScalePower = ScalePower,
  59. ForwardPower = ForwardPower,
  60. ReflectPower = ReflectPower,
  61. IsRfOn = IsPowerOn,
  62. PowerSetPoint = PowerSetPoint,
  63. };
  64. private void SerialPortDataReceived(byte[] rawMessage)
  65. {
  66. LOG.Write(eEvent.INFO_RF, Module, $"RF byte"+rawMessage.ToString());
  67. try
  68. {
  69. if (rawMessage.Length==1&& rawMessage[1]== 0x06) //ACK 指令
  70. {
  71. }
  72. else if (rawMessage.Length > 1)
  73. {
  74. parsecmd(rawMessage);
  75. }
  76. }
  77. catch (Exception ex)
  78. {
  79. LOG.WriteExeption(ex);
  80. }
  81. }
  82. public void parsecmd(byte[] message)
  83. {
  84. switch (message[4])
  85. {
  86. case 0x01://ParamRead
  87. int DataValue = BitConverter.ToInt32(new byte[] { message[10], message[11], message[12], message[13] }, 0);
  88. ReflectPower = DataValue;
  89. break;
  90. case 0x02://ParamWrite
  91. DataValue = BitConverter.ToInt32(new byte[] { message[10] ,message[11], message[12], message[13] }, 0);
  92. ForwardPower = DataValue;
  93. break;
  94. case 0xFE://TelegramError
  95. LOG.Write(eEvent.ERR_RF, Module, "Telegram structure is not correct");
  96. break;
  97. default:
  98. // 默认代码块
  99. break;
  100. }
  101. }
  102. private void SerialPortErrorOccurred(string obj)
  103. {
  104. LOG.Write(eEvent.ERR_RF, Module, $"Tru 射频串口出错, [{obj}]");
  105. }
  106. public override void SetPower(float val)
  107. {
  108. //var power = !_scEnableCalibration.BoolValue ? val : CalibrationData(val, true);
  109. ForwardPower=val;
  110. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x06, 0x00, 0x01, 0x00, 0x04 };
  111. byte[] valueBytes = BitConverter.GetBytes((int)val);
  112. baseBytes.AddRange(valueBytes);
  113. baseBytes = CRC16(baseBytes.ToArray());
  114. baseBytes.Add(0x55);
  115. _serial.Write(baseBytes.ToArray());
  116. }
  117. public override bool SetPowerOnOff(bool on, out string str)
  118. {
  119. str = "";
  120. var _chamber = DEVICE.GetDevice<JetPMBase>(Module);
  121. if (on && !_chamber.CheckGeneratorAndHVInterlock(VenusDevice.Rf))
  122. {
  123. return false;
  124. }
  125. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6F, 0x00, 0x01, 0x00, 0x07 };
  126. if (on == true)
  127. {
  128. baseBytes.AddRange(new List<byte> { 0x01, 0x00, 0x00, 0x00 });
  129. }
  130. else
  131. {
  132. baseBytes.AddRange(new List<byte> { 0x00, 0x00, 0x00, 0x00 });
  133. }
  134. baseBytes = CRC16(baseBytes.ToArray());
  135. baseBytes.Add(0x55);
  136. _serial.Write(baseBytes.ToArray());
  137. return true;
  138. }
  139. public override void SetPulseMode(bool on)
  140. {
  141. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6A, 0x01, 0x01, 0x00, 0x04 };
  142. if (on == true)
  143. {
  144. baseBytes.AddRange(new List<byte> { 0x01, 0x00, 0x00, 0x00 });
  145. }
  146. else
  147. {
  148. baseBytes.AddRange(new List<byte> { 0x00, 0x00, 0x00, 0x00 });
  149. }
  150. baseBytes = CRC16(baseBytes.ToArray());
  151. baseBytes.Add(0x55);
  152. _serial.Write(baseBytes.ToArray());
  153. }
  154. public override void SetPulseRateFreq(int nFreq)
  155. {
  156. if (nFreq > 0)
  157. {
  158. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6C, 0x01, 0x01, 0x00, 0x04 };
  159. byte[] valueBytes = BitConverter.GetBytes(nFreq);
  160. baseBytes.AddRange(valueBytes);
  161. baseBytes = CRC16(baseBytes.ToArray());
  162. baseBytes.Add(0x55);
  163. _serial.Write(baseBytes.ToArray());
  164. }
  165. else
  166. {
  167. LOG.Write(eEvent.ERR_RF, Module, $"{Name},SetPulseRateFreq() parameter error: {nFreq}");
  168. }
  169. }
  170. public override void SetPulseDutyCycle(int percentage)
  171. {
  172. if (percentage >= 10 && percentage <= 90)
  173. {
  174. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6D, 0x01, 0x01, 0x00, 0x04 };
  175. byte[] valueBytes = BitConverter.GetBytes(percentage);
  176. baseBytes.AddRange(valueBytes);
  177. baseBytes = CRC16(baseBytes.ToArray());
  178. baseBytes.Add(0x55);
  179. _serial.Write(baseBytes.ToArray());
  180. }
  181. else
  182. {
  183. LOG.Write(eEvent.ERR_RF, Module, $"{Name},SetPulseDutyCycle() parameter error: {percentage}");
  184. }
  185. }
  186. #region 霍廷格RF协议crc-16/CCITT-FALSE校验
  187. private bool CRC16(byte[] buffer, ref byte[] ResCRC16) // crc-16/CCITT-FALSE,判断校验
  188. {
  189. bool status = false;
  190. ushort crc = 0xFFFF;
  191. int size = buffer.Length; //计算待计算的数据长度
  192. int i = 0;
  193. if (size > 0)
  194. {
  195. while (size-- > 0)
  196. {
  197. crc = (ushort)((crc >> 8) | (crc << 8));
  198. crc ^= buffer[i++];
  199. crc ^= (ushort)(((byte)crc) >> 4);
  200. crc ^= (ushort)(crc << 12);
  201. crc ^= (ushort)((crc & 0xff) << 5);
  202. }
  203. }
  204. //判断输入的ResCRC16与计算出来的是否一致
  205. if (ResCRC16[0] == (byte)((crc >> 8) & 0xff) && ResCRC16[1] == (byte)(crc & 0xff))
  206. {
  207. status = true;
  208. }
  209. return status;
  210. }
  211. private List<byte> CRC16(byte[] buffer) // crc-16/CCITT-FALSE,补全两个字节
  212. {
  213. ushort crc = 0xFFFF;
  214. int size = buffer.Length; //计算待计算的数据长度
  215. int i = 0;
  216. if (size > 0)
  217. {
  218. while (size-- > 0)
  219. {
  220. crc = (ushort)((crc >> 8) | (crc << 8));
  221. crc ^= buffer[i++];
  222. crc ^= (ushort)(((byte)crc) >> 4);
  223. crc ^= (ushort)(crc << 12);
  224. crc ^= (ushort)((crc & 0xff) << 5);
  225. }
  226. }
  227. var byteList = buffer.ToList();
  228. byteList.Add((byte)(crc & 0xff));
  229. byteList.Add((byte)((crc >> 8) & 0xff));
  230. return byteList;
  231. }
  232. #endregion
  233. }
  234. }