ComRevtechMatch.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO.Ports;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using Aitex.Core.Common.DeviceData;
  9. using Aitex.Core.RT.DataCenter;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.IOCore;
  12. using Aitex.Core.RT.Log;
  13. using Aitex.Core.RT.OperationCenter;
  14. using Aitex.Core.RT.SCCore;
  15. using Aitex.Core.RT.Tolerance;
  16. using Aitex.Core.Util;
  17. using MECF.Framework.Common.Communications;
  18. using MECF.Framework.Common.DataCenter;
  19. using MECF.Framework.Common.Device.Bases;
  20. using MECF.Framework.Common.Equipment;
  21. using VirgoCommon;
  22. using VirgoRT.Modules;
  23. namespace VirgoRT.Devices
  24. {
  25. public class ComRevtechMatch : RevtechMatch
  26. {
  27. private readonly AsyncSerialPort _serial;
  28. private string _portNum;
  29. private bool _isException = false;
  30. public ComRevtechMatch(ModuleName mod, string name, string configName = "") : base(mod, name, configName)
  31. {
  32. _portNum = SC.GetStringValue($"{mod}.{(string.IsNullOrWhiteSpace(configName) ? name : configName)}.Port");
  33. _serial = new AsyncSerialPort(_portNum, 115200, 8, Parity.None, StopBits.One, "\n", true);
  34. _isException = false;
  35. //_serial.EnableLog = true;
  36. }
  37. public override bool Initialize()
  38. {
  39. base.Initialize();
  40. if (_serial.Open())
  41. {
  42. //_serial.OnBinaryDataChanged += OnDataChanged;
  43. _serial.OnDataChanged += SerialPortDataReceived;
  44. _serial.OnErrorHappened += SerialPortErrorOccurred;
  45. LOG.Info($"{Module} {Name} Revtech match port:[{_portNum}] connect");
  46. }
  47. else
  48. {
  49. EV.PostAlarmLog(this.Module, $"Revtech match port:[{_portNum}] open failed");
  50. return false;
  51. }
  52. return true;
  53. }
  54. protected override void SendCmd(string str)
  55. {
  56. base.SendCmd(str);
  57. _serial.Write(Encoding.ASCII.GetBytes(str + "\n"));
  58. }
  59. private void SerialPortErrorOccurred(string str)
  60. {
  61. EV.PostAlarmLog(Module, $"{Module} {Name} Revtech Match error [{str}]");
  62. }
  63. private void SerialPortDataReceived(string data)
  64. {
  65. try
  66. {
  67. string[] matchData = data.Split(new char[] { ',' });
  68. if (matchData.Length > 13)
  69. {
  70. if (matchData[0].Contains("MANUAL") || matchData[0].Contains("AUTO"))
  71. {
  72. WorkMode = matchData[0] == "MANUAL" ? EnumRfMatchTuneMode.Manual : EnumRfMatchTuneMode.Auto;
  73. TunePosition1 = Convert.ToSingle(matchData[8]);
  74. TunePosition2 = Convert.ToSingle(matchData[7]);
  75. VPP = (ushort)Convert.ToSingle(matchData[12]);
  76. DCBias = Convert.ToSingle(matchData[13]);
  77. }
  78. }
  79. _isException = false;
  80. }
  81. catch (Exception ex)
  82. {
  83. if(!_isException)
  84. LOG.Info($"{Module} {Name} ex={ex}");
  85. _isException = true;
  86. }
  87. }
  88. }
  89. }