TruPlasmaRF.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 MECF.Framework.Common.Communications;
  9. using MECF.Framework.Common.DataCenter;
  10. using MECF.Framework.Common.Device.Bases;
  11. using MECF.Framework.Common.Equipment;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Text.RegularExpressions;
  17. using System.Threading.Tasks;
  18. using Venus_Core;
  19. namespace Venus_RT.Devices
  20. {
  21. public class TruPlasmaRF : RfPowerBase
  22. {
  23. private readonly AsyncSerialPort _serial;
  24. public TruPlasmaRF(ModuleName mod, VenusDevice device) : base(mod.ToString(), device.ToString())
  25. {
  26. var portNum = SC.GetStringValue(device == VenusDevice.Rf ? $"{mod}.Rf.Port" : $"{mod}.BiasRf.Port");
  27. _serial = new AsyncSerialPort(portNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\r\r");
  28. }
  29. public override bool Initialize()
  30. {
  31. base.Initialize();
  32. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  33. if (_serial.Open())
  34. {
  35. _serial.OnDataChanged += SerialPortDataReceived;
  36. _serial.OnErrorHappened += SerialPortErrorOccurred;
  37. }
  38. else
  39. {
  40. LOG.Write(eEvent.ERR_RF, Module, "Tru 射频发生器串口无法打开");
  41. return false;
  42. }
  43. return true;
  44. }
  45. private void SerialPortDataReceived(string str)
  46. {
  47. }
  48. private void SerialPortErrorOccurred(string obj)
  49. {
  50. LOG.Write(eEvent.ERR_RF, Module, $"Tru 射频串口出错, [{obj}]");
  51. }
  52. public override void SetPower(float val)
  53. {
  54. var power = !_scEnableCalibration.BoolValue ? val : CalibrationData(val, true);
  55. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x06, 0x00, 0x01, 0x00, 0x04 };
  56. byte[] valueBytes = BitConverter.GetBytes((int)val);
  57. baseBytes.AddRange(valueBytes);
  58. baseBytes = CRC16(baseBytes.ToArray());
  59. baseBytes.Add(0x55);
  60. _serial.Write(baseBytes.ToArray());
  61. }
  62. public override bool SetPowerOnOff(bool on, out string str)
  63. {
  64. str = "";
  65. var _chamber = DEVICE.GetDevice<JetPMBase>(Module);
  66. if (on && !_chamber.CheckGeneratorAndHVInterlock(VenusDevice.Rf))
  67. {
  68. return false;
  69. }
  70. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6F, 0x00, 0x01, 0x00, 0x07 };
  71. if (on == true)
  72. {
  73. baseBytes.AddRange(new List<byte> { 0x01, 0x00, 0x00, 0x00 });
  74. }
  75. else
  76. {
  77. baseBytes.AddRange(new List<byte> { 0x00, 0x00, 0x00, 0x00 });
  78. }
  79. baseBytes = CRC16(baseBytes.ToArray());
  80. baseBytes.Add(0x55);
  81. _serial.Write(baseBytes.ToArray());
  82. return true;
  83. }
  84. public override void SetPulseMode(bool on)
  85. {
  86. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6A, 0x01, 0x01, 0x00, 0x04 };
  87. if (on == true)
  88. {
  89. baseBytes.AddRange(new List<byte> { 0x01, 0x00, 0x00, 0x00 });
  90. }
  91. else
  92. {
  93. baseBytes.AddRange(new List<byte> { 0x00, 0x00, 0x00, 0x00 });
  94. }
  95. baseBytes = CRC16(baseBytes.ToArray());
  96. baseBytes.Add(0x55);
  97. _serial.Write(baseBytes.ToArray());
  98. }
  99. public override void SetPulseRateFreq(int nFreq)
  100. {
  101. if (nFreq > 0)
  102. {
  103. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6C, 0x01, 0x01, 0x00, 0x04 };
  104. byte[] valueBytes = BitConverter.GetBytes(nFreq);
  105. baseBytes.AddRange(valueBytes);
  106. baseBytes = CRC16(baseBytes.ToArray());
  107. baseBytes.Add(0x55);
  108. _serial.Write(baseBytes.ToArray());
  109. }
  110. else
  111. {
  112. LOG.Write(eEvent.ERR_RF, Module, $"{Name},SetPulseRateFreq() parameter error: {nFreq}");
  113. }
  114. }
  115. public override void SetPulseDutyCycle(int percentage)
  116. {
  117. if (percentage >= 10 && percentage <= 90)
  118. {
  119. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6D, 0x01, 0x01, 0x00, 0x04 };
  120. byte[] valueBytes = BitConverter.GetBytes(percentage);
  121. baseBytes.AddRange(valueBytes);
  122. baseBytes = CRC16(baseBytes.ToArray());
  123. baseBytes.Add(0x55);
  124. _serial.Write(baseBytes.ToArray());
  125. }
  126. else
  127. {
  128. LOG.Write(eEvent.ERR_RF, Module, $"{Name},SetPulseDutyCycle() parameter error: {percentage}");
  129. }
  130. }
  131. #region 霍廷格RF协议crc-16/CCITT-FALSE校验
  132. private bool CRC16(byte[] buffer, ref byte[] ResCRC16) // crc-16/CCITT-FALSE,判断校验
  133. {
  134. bool status = false;
  135. ushort crc = 0xFFFF;
  136. int size = buffer.Length; //计算待计算的数据长度
  137. int i = 0;
  138. if (size > 0)
  139. {
  140. while (size-- > 0)
  141. {
  142. crc = (ushort)((crc >> 8) | (crc << 8));
  143. crc ^= buffer[i++];
  144. crc ^= (ushort)(((byte)crc) >> 4);
  145. crc ^= (ushort)(crc << 12);
  146. crc ^= (ushort)((crc & 0xff) << 5);
  147. }
  148. }
  149. //判断输入的ResCRC16与计算出来的是否一致
  150. if (ResCRC16[0] == (byte)((crc >> 8) & 0xff) && ResCRC16[1] == (byte)(crc & 0xff))
  151. {
  152. status = true;
  153. }
  154. return status;
  155. }
  156. private List<byte> CRC16(byte[] buffer) // crc-16/CCITT-FALSE,补全两个字节
  157. {
  158. ushort crc = 0xFFFF;
  159. int size = buffer.Length; //计算待计算的数据长度
  160. int i = 0;
  161. if (size > 0)
  162. {
  163. while (size-- > 0)
  164. {
  165. crc = (ushort)((crc >> 8) | (crc << 8));
  166. crc ^= buffer[i++];
  167. crc ^= (ushort)(((byte)crc) >> 4);
  168. crc ^= (ushort)(crc << 12);
  169. crc ^= (ushort)((crc & 0xff) << 5);
  170. }
  171. }
  172. var byteList = buffer.ToList();
  173. byteList.Add((byte)(crc & 0xff));
  174. byteList.Add((byte)((crc >> 8) & 0xff));
  175. return byteList;
  176. }
  177. #endregion
  178. }
  179. }