TruPlasmaRF.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 CommunityToolkit.HighPerformance.Buffers;
  11. using MECF.Framework.Common.Communications;
  12. using MECF.Framework.Common.DataCenter;
  13. using MECF.Framework.Common.Device.Bases;
  14. using MECF.Framework.Common.Equipment;
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.Linq;
  20. using System.ServiceModel.Channels;
  21. using Venus_Core;
  22. namespace Venus_RT.Devices
  23. {
  24. public class TruPlasmaRF : RfPowerBase
  25. {
  26. private readonly AsyncSerialPort _serial;
  27. private List<byte> buffer = new List<byte>(4096);
  28. public TruPlasmaRF(ModuleName mod, VenusDevice device) : base(mod.ToString(), device.ToString())
  29. {
  30. this.Status = GeneratorStatus.Unknown;
  31. var portNum = SC.GetStringValue(device == VenusDevice.Rf ? $"{mod}.Rf.Port" : $"{mod}.BiasRf.Port");
  32. _serial = new AsyncSerialPort(portNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One,"/r", false);
  33. }
  34. public override bool Initialize()
  35. {
  36. base.Initialize();
  37. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  38. if (_serial.Open())
  39. {
  40. _serial.OnBinaryDataChanged += SerialPortDataReceived;
  41. _serial.OnErrorHappened += SerialPortErrorOccurred;
  42. ResetCommand();
  43. }
  44. else
  45. {
  46. LOG.Write(eEvent.ERR_RF, Module, "Tru 射频发生器串口无法打开");
  47. return false;
  48. }
  49. return true;
  50. }
  51. public new AITRfData DeviceData =>
  52. new AITRfData
  53. {
  54. Module = Module,
  55. DeviceName = Name,
  56. ScalePower = ScalePower,
  57. ForwardPower = ForwardPower,
  58. ReflectPower = ReflectPower,
  59. IsRfOn = IsPowerOn,
  60. PowerSetPoint = PowerSetPoint,
  61. };
  62. private void SerialPortDataReceived(byte[] rawMessage)
  63. {
  64. try
  65. {
  66. buffer.AddRange(rawMessage);
  67. while (buffer.Count >= 18) //至少包含帧头(2字节)、长度(1字节)、校验位(1字节);根据设计不同而不同
  68. {
  69. //2.1 查找数据头
  70. if (buffer[0] == 0x06) //传输数据有帧头,用于判断
  71. {
  72. //得到完整的数据,复制到ReceiveBytes中进行校验
  73. byte[] ReceiveBytes = new byte[13];
  74. byte[] ReadReceiveBytes = new byte[18];
  75. buffer.CopyTo(0, ReceiveBytes, 0, 13);
  76. buffer.CopyTo(0, ReadReceiveBytes, 0, 18);
  77. if ((ReceiveBytes[12] == 0x55)&& (ReceiveBytes[1] == 0xAA)) //校验失败,最后一个字节是校验位
  78. {
  79. parsecmd(ReceiveBytes);
  80. buffer.RemoveRange(0, 13);
  81. }
  82. else if ((ReadReceiveBytes[17] == 0x55) && (ReceiveBytes[1] == 0xAA))
  83. {
  84. parsecmd(ReadReceiveBytes);
  85. buffer.RemoveRange(0, 18);
  86. }
  87. else
  88. {
  89. buffer.Clear();
  90. LOG.Write(eEvent.ERR_RF, Module, $"rf通讯错误");
  91. }
  92. ///执行对数据进行处理操作RunReceiveDataCallback(ReceiveBytes);
  93. }
  94. else //帧头不正确时,记得清除
  95. {
  96. //buffer.RemoveAt(0);
  97. }
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. buffer.Clear();
  103. LOG.WriteExeption(ex);
  104. }
  105. }
  106. public GeneratorStatus Status { get; set; }
  107. private bool GetBitValue(byte value, int bit)
  108. {
  109. return (value & (byte)Math.Pow(2, bit)) > 0 ? true : false;
  110. }
  111. public override bool IsPowerOn
  112. {
  113. get => Status == GeneratorStatus.ON;
  114. set { }
  115. }
  116. public void parsecmd(byte[] message)
  117. {
  118. try
  119. {
  120. IsPowerOn = GetBitValue(message[4], 4);
  121. if (GetBitValue(message[4], 4))
  122. {
  123. Status = GeneratorStatus.ON;
  124. }
  125. else
  126. {
  127. Status = GeneratorStatus.OFF;
  128. }
  129. switch (message[5])
  130. {
  131. case 0x01://ParamRead
  132. if (message.Length == 18 && message[6]==0x14)
  133. {
  134. int DataValue = BitConverter.ToInt32(new byte[] { message[11], message[12], message[13], message[14] }, 0);
  135. ReflectPower = DataValue;
  136. }else if (message.Length == 18 && message[6] == 0x12)
  137. {
  138. int DataValue1 = BitConverter.ToInt32(new byte[] { message[11], message[12], message[13], message[14] }, 0);
  139. ForwardPower = DataValue1;
  140. }
  141. break;
  142. case 0x02://ParamWrite
  143. break;
  144. case 0xFE://TelegramError
  145. LOG.Write(eEvent.ERR_RF, Module, "Telegram structure is not correct");
  146. break;
  147. default:
  148. // 默认代码块
  149. break;
  150. }
  151. switch (message[9])
  152. {
  153. case 0x26:
  154. Status = GeneratorStatus.OFF;
  155. IsPowerOn = false;
  156. break;
  157. case 0x24:
  158. LOG.Write(eEvent.ERR_RF, Module, "Telegram structure is not correct");
  159. break;
  160. default:
  161. // 默认代码块
  162. break;
  163. }
  164. }
  165. catch (Exception ex)
  166. { }
  167. }
  168. private void SerialPortErrorOccurred(string obj)
  169. {
  170. LOG.Write(eEvent.ERR_RF, Module, $"Tru 射频串口出错, [{obj}]");
  171. }
  172. public override void SetPower(float val)
  173. {
  174. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x06, 0x00, 0x01, 0x00, 0x04 };
  175. byte[] valueBytes = BitConverter.GetBytes((int)val);
  176. baseBytes.AddRange(valueBytes);
  177. baseBytes = CRC16(baseBytes.ToArray());
  178. baseBytes.Add(0x55);
  179. _serial.Write(baseBytes.ToArray());
  180. }
  181. public override void Monitor()
  182. {
  183. readpi();
  184. readpr();
  185. }
  186. public void getcontrol()
  187. {
  188. byte[] getincontrol = new byte[] { 0xAA, 0x02, 0x06, 0x00, 0x05, 0x01, 0x00, 0x00, 0xFF, 0x34, 0x8A, 0x55 };
  189. _serial.Write(getincontrol.ToArray());
  190. }
  191. public void releasecontrol()
  192. {
  193. byte[] getincontrol = new byte[] { 0xAA, 0x02, 0x06, 0x00, 0x05, 0x02, 0x00, 0x00, 0xFF, 0xE8, 0x11, 0x55 };
  194. _serial.Write(getincontrol.ToArray());
  195. }
  196. public void readpr()//reflect
  197. {
  198. byte[] getincontrol = new byte[] { 0xAA, 0x02, 0x06, 0x00, 0x01, 0x14, 0x00, 0x01, 0xFF, 0xE1, 0x97, 0x55 };
  199. _serial.Write(getincontrol.ToArray());
  200. }
  201. public void readpi()//forward
  202. {
  203. byte[] getincontrola = new byte[] { 0xAA, 0x02, 0x06, 0x00, 0x01, 0x12, 0x00, 0x01, 0xFF, 0x78, 0xB0, 0x55 };
  204. _serial.Write(getincontrola.ToArray());
  205. }
  206. public void ResetCommand()
  207. {
  208. LOG.Write(eEvent.ERR_RF, Module, $"Send rest");
  209. byte[] getincontrol = new byte[] { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x4D, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x30, 0x72, 0x55 };
  210. _serial.Write(getincontrol.ToArray());
  211. }
  212. public override void Reset()
  213. {
  214. byte[] getincontrol = new byte[] { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x4D, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x30, 0x72, 0x55 };
  215. _serial.Write(getincontrol.ToArray());
  216. }
  217. public override bool SetPowerOnOff(bool on, out string str)
  218. {
  219. str = "";
  220. var _chamber = DEVICE.GetDevice<JetPMBase>(Module);
  221. if (on && !_chamber.CheckGeneratorAndHVInterlock(VenusDevice.Rf))
  222. {
  223. return false;
  224. }
  225. getcontrol();
  226. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6F, 0x00, 0x01, 0x00, 0x07 };
  227. if (on == true)
  228. {
  229. baseBytes.AddRange(new List<byte> { 0x01, 0x00, 0x00, 0x00 });
  230. }
  231. else
  232. {
  233. baseBytes.AddRange(new List<byte> { 0x00, 0x00, 0x00, 0x00 });
  234. }
  235. baseBytes = CRC16(baseBytes.ToArray());
  236. baseBytes.Add(0x55);
  237. _serial.Write(baseBytes.ToArray());
  238. if (on == false)
  239. {
  240. releasecontrol();
  241. }
  242. return true;
  243. }
  244. public override void SetPulseMode(bool on)
  245. {
  246. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6A, 0x01, 0x01, 0x00, 0x04 };
  247. if (on == true)
  248. {
  249. baseBytes.AddRange(new List<byte> { 0x01, 0x00, 0x00, 0x00 });
  250. }
  251. else
  252. {
  253. baseBytes.AddRange(new List<byte> { 0x00, 0x00, 0x00, 0x00 });
  254. }
  255. baseBytes = CRC16(baseBytes.ToArray());
  256. baseBytes.Add(0x55);
  257. _serial.Write(baseBytes.ToArray());
  258. }
  259. public override void SetPulseRateFreq(int nFreq)
  260. {
  261. if (nFreq > 0)
  262. {
  263. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6C, 0x01, 0x01, 0x00, 0x04 };
  264. byte[] valueBytes = BitConverter.GetBytes(nFreq);
  265. baseBytes.AddRange(valueBytes);
  266. baseBytes = CRC16(baseBytes.ToArray());
  267. baseBytes.Add(0x55);
  268. _serial.Write(baseBytes.ToArray());
  269. }
  270. else
  271. {
  272. LOG.Write(eEvent.ERR_RF, Module, $"{Name},SetPulseRateFreq() parameter error: {nFreq}");
  273. }
  274. }
  275. public override void SetPulseDutyCycle(int percentage)
  276. {
  277. if (percentage >= 10 && percentage <= 90)
  278. {
  279. List<byte> baseBytes = new List<byte>() { 0xAA, 0x02, 0x0B, 0x00, 0x02, 0x6D, 0x01, 0x01, 0x00, 0x04 };
  280. byte[] valueBytes = BitConverter.GetBytes(percentage);
  281. baseBytes.AddRange(valueBytes);
  282. baseBytes = CRC16(baseBytes.ToArray());
  283. baseBytes.Add(0x55);
  284. _serial.Write(baseBytes.ToArray());
  285. }
  286. else
  287. {
  288. LOG.Write(eEvent.ERR_RF, Module, $"{Name},SetPulseDutyCycle() parameter error: {percentage}");
  289. }
  290. }
  291. #region 霍廷格RF协议crc-16/CCITT-FALSE校验
  292. private bool CRC16(byte[] buffer, ref byte[] ResCRC16) // crc-16/CCITT-FALSE,判断校验
  293. {
  294. bool status = false;
  295. ushort crc = 0xFFFF;
  296. int size = buffer.Length; //计算待计算的数据长度
  297. int i = 0;
  298. if (size > 0)
  299. {
  300. while (size-- > 0)
  301. {
  302. crc = (ushort)((crc >> 8) | (crc << 8));
  303. crc ^= buffer[i++];
  304. crc ^= (ushort)(((byte)crc) >> 4);
  305. crc ^= (ushort)(crc << 12);
  306. crc ^= (ushort)((crc & 0xff) << 5);
  307. }
  308. }
  309. //判断输入的ResCRC16与计算出来的是否一致
  310. if (ResCRC16[0] == (byte)((crc >> 8) & 0xff) && ResCRC16[1] == (byte)(crc & 0xff))
  311. {
  312. status = true;
  313. }
  314. return status;
  315. }
  316. private List<byte> CRC16(byte[] buffer) // crc-16/CCITT-FALSE,补全两个字节
  317. {
  318. ushort crc = 0xFFFF;
  319. int size = buffer.Length; //计算待计算的数据长度
  320. int i = 0;
  321. if (size > 0)
  322. {
  323. while (size-- > 0)
  324. {
  325. crc = (ushort)((crc >> 8) | (crc << 8));
  326. crc ^= buffer[i++];
  327. crc ^= (ushort)(((byte)crc) >> 4);
  328. crc ^= (ushort)(crc << 12);
  329. crc ^= (ushort)((crc & 0xff) << 5);
  330. }
  331. }
  332. var byteList = buffer.ToList();
  333. byteList.Add((byte)(crc & 0xff));
  334. byteList.Add((byte)((crc >> 8) & 0xff));
  335. return byteList;
  336. }
  337. #endregion
  338. }
  339. static class TruPlasmaMatchMessage
  340. {
  341. public const string PRESET = "G";
  342. public const string AUTO = "L";
  343. public const string MANUAL = "M";
  344. public const string PRESET_MEM = "P";
  345. public const string START_QUERY = "S3";
  346. public const string STOP_QUERY = "SP";
  347. public const string WRITE_POS = "$APGR";
  348. public const string READ_POS = "$APRR";
  349. }
  350. class TruPlasmaMatch : RfMatchBase
  351. {
  352. private readonly AsyncSerialPort _serial;
  353. private const ushort S3_HEAD_LENGTH = 2;
  354. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  355. private int QUERY_INTERVAL = 1000;
  356. [Subscription("MatchWorkMode")]
  357. public EnumRfMatchTuneMode WorkMode { get; set; }
  358. public float C1 { get; set; }
  359. public float C2 { get; set; }
  360. //[Subscription("VPP")]
  361. public ushort VPP { get; set; }
  362. public new AITMatchData DeviceData
  363. {
  364. get
  365. {
  366. return new AITMatchData
  367. {
  368. Module = Module,
  369. DeviceName = Name,
  370. WorkMode = WorkMode.ToString(),
  371. C1 = TunePosition1,
  372. C2 = TunePosition2,
  373. VPP = "",
  374. DCBias = DCBias.ToString()
  375. };
  376. }
  377. }
  378. public TruPlasmaMatch(ModuleName mod, VenusDevice device) : base(mod.ToString(), device.ToString())
  379. {
  380. var portNum = SC.GetStringValue($"{mod}.{device}.Port");
  381. _serial = new AsyncSerialPort(portNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "/r", false);
  382. intervalTime = 100;
  383. }
  384. ~TruPlasmaMatch()
  385. {
  386. _serial?.Close();
  387. }
  388. public override bool Initialize()
  389. {
  390. base.Initialize();
  391. if (_serial.Open())
  392. {
  393. _serial.OnBinaryDataChanged += SerialBinaryPortDataReceived;
  394. _serial.OnErrorHappened += SerialPortErrorOccurred;
  395. }
  396. else
  397. {
  398. LOG.Write(eEvent.ERR_RF, Module, "Match 串口无法打开");
  399. return false;
  400. }
  401. DATA.Subscribe($"{Module}.{Name}.C1", () => TunePosition1);
  402. DATA.Subscribe($"{Module}.{Name}.C2", () => TunePosition2);
  403. DATA.Subscribe($"{Module}.{Name}.WorkMode", () => WorkMode.ToString());
  404. OP.Subscribe($"{Module}.{Name}.SetC1", (func, args) =>
  405. {
  406. return true;
  407. });
  408. OP.Subscribe($"{Module}.{Name}.SetC2", (func, args) =>
  409. {
  410. return true;
  411. });
  412. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC1}", (out string reason, int time, object[] param) =>
  413. {
  414. SetMatchPositionC1((float)Convert.ToDouble(param[0]), out reason);
  415. return true;
  416. });
  417. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPositionC2}", (out string reason, int time, object[] param) =>
  418. {
  419. SetMatchPositionC2((float)Convert.ToDouble(param[0]), out reason);
  420. return true;
  421. });
  422. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  423. {
  424. SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason);
  425. return true;
  426. });
  427. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  428. {
  429. SetMatchMode((string)param[0] == "Auto" ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual, out reason);
  430. return true;
  431. });
  432. return true;
  433. }
  434. public override void Monitor()
  435. {
  436. }
  437. public override void Terminate()
  438. {
  439. }
  440. public override void Reset()
  441. {
  442. }
  443. /// <summary>
  444. ///
  445. /// </summary>
  446. /// <param name="c1,c2">百分比数字</param>
  447. /// <param name="c2"></param>
  448. ///
  449. private void executeMatchPostion(float c1, float c2)
  450. {
  451. SetPosition(c1, c2);
  452. }
  453. public override void SetMatchPosition(float c1, float c2, out string reason)
  454. {
  455. executeMatchPostion(c1, c2);
  456. reason = "";
  457. }
  458. public void SetPresetMode(RfMatchPresetMode mode)
  459. {
  460. }
  461. // -----------------------Private Method-------------------------
  462. //
  463. private void SerialBinaryPortDataReceived(byte[] message)
  464. {
  465. if (message.Count()<10)
  466. {
  467. LOG.Write(eEvent.ERR_RF, Module, "收到 Match 数据为空");
  468. return;
  469. }
  470. else
  471. {
  472. if (message[0] ==0x1D && message[8]==0x60)
  473. {
  474. TunePosition1 = BitConverter.ToSingle(new byte[] { message[10], message[11], message[12], message[13] }, 0) * 100;
  475. TunePosition2 = BitConverter.ToSingle(new byte[] { message[14], message[15], message[16], message[17] }, 0) * 100;
  476. switch (message[7])
  477. {
  478. case 0x01:
  479. this.WorkMode = EnumRfMatchTuneMode.Manual;
  480. break;
  481. case 0x02:
  482. this.WorkMode = EnumRfMatchTuneMode.Auto;
  483. break;
  484. case 0x20:
  485. this.WorkMode = EnumRfMatchTuneMode.Undefined;
  486. break;
  487. default:
  488. break;
  489. }
  490. }
  491. }
  492. }
  493. private void SerialPortErrorOccurred(string str)
  494. {
  495. LOG.Write(eEvent.ERR_RF, Module, $"AdTec Match error [{str}]");
  496. }
  497. private void SetPosition(float c1val, float c2val)
  498. {
  499. List<byte> Len = new List<byte>() { 0x16, 0xE9};
  500. List<byte> DstSrc = new List<byte>() { 0x00, 0x02, 0x00, 0x01};
  501. List<byte> Cmd = new List<byte> { 0x60, 0x40 };
  502. byte[] val1Bytes = BitConverter.GetBytes(c1val / 100);
  503. byte[] val2Bytes = BitConverter.GetBytes(c2val / 100);
  504. List<byte> Act = new List<byte>() { 0x00 };
  505. List<byte> Ctr123 = new List<byte> { 0x08, 0x00, 0x00 };
  506. List<byte> baseBytes = new List<byte>() { };
  507. baseBytes.AddRange(Len);
  508. baseBytes.AddRange(DstSrc);
  509. baseBytes.AddRange(Cmd);
  510. baseBytes.AddRange(val1Bytes);
  511. baseBytes.AddRange(val2Bytes);
  512. baseBytes.AddRange(Act);
  513. baseBytes.AddRange(Ctr123);
  514. int a = 0;
  515. for (int i=2; i< baseBytes.Count;i++)
  516. {
  517. a+=(Int16)baseBytes[i];
  518. }
  519. byte []ackture= new byte[2];
  520. byte[]ackbyte= BitConverter.GetBytes(a);
  521. ackture[0] = ackbyte[1];
  522. ackture[1] = ackbyte[0];
  523. baseBytes.AddRange(ackture);
  524. _serial.Write(baseBytes.ToArray());
  525. }
  526. public override bool SetMatchMode(EnumRfMatchTuneMode enumRfMatchTuneMode, out string reason)
  527. {
  528. reason = string.Empty;
  529. return true;
  530. }
  531. private void SetWorkMode(EnumRfMatchTuneMode mode)
  532. {
  533. }
  534. private void SetPresetMemory(byte gear)
  535. {
  536. }
  537. }
  538. }