HongHuVPA.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.RT.SCCore;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Communications;
  5. using MECF.Framework.Common.Equipment;
  6. using MECF.Framework.Common.SubstrateTrackings;
  7. using MECF.Framework.RT.ModuleLibrary.VceModules;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO.Ports;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading.Tasks;
  15. using Venus_Core;
  16. namespace Venus_RT.Devices.PreAligner
  17. {
  18. public class HongHuVPA : IPreAlign
  19. {
  20. private AsyncSerialPort _serialport;//串口
  21. private string _newLine = "\r";//结束符号
  22. private object _locker = new object();//锁变量
  23. private bool _IsAsciiMode = true;
  24. private PeriodicJob _thread;
  25. //private string _CurrentMsg;
  26. private RState _state;
  27. private Regex _catchErrorCode = new Regex(@"(?<=_ERR )(.*)");
  28. private Regex _checkData = new Regex(@"(?<=DATA)(.*)");
  29. private LinkedList<string> _lstMessage = new LinkedList<string>();
  30. private ModuleName _module;
  31. private enum AlignerAction
  32. {
  33. Home,
  34. Align,
  35. AlignWithAngle,
  36. Reset,
  37. Scan,
  38. RsLT
  39. }
  40. private Dictionary<int, string> _ErrorCode2Msg = new Dictionary<int, string>()
  41. {
  42. { 16 , "" },
  43. };
  44. private Dictionary<AlignerAction, string> _Command2Msg = new Dictionary<AlignerAction, string>()
  45. {
  46. //Action
  47. { AlignerAction.Align , "ALGN" },//旋转到预设的站点方向
  48. { AlignerAction.AlignWithAngle, "MOVT REL" },//旋转到指定的角度
  49. { AlignerAction.Home , "HOME" },//Home 初始化时 error时用
  50. { AlignerAction.Reset , "RSET" },//重置 error时用
  51. { AlignerAction.Scan , "SCAN" },//扫描整个Wafer参数
  52. { AlignerAction.RsLT , "RSLT" }
  53. //Read
  54. //{ "" , "RQCD" },
  55. //{ "" , "RQCCDPOS" },
  56. //{ "" , "RQID" },
  57. //{ "" , "RQPS" },
  58. //Set
  59. //{ "" , "" },
  60. //Welding
  61. //{ "" , "SVCD" },
  62. };
  63. public ModuleName Module => _module;
  64. public bool IsConnect => _serialport.IsOpen();
  65. public RState Status => _state;//状态
  66. public bool IsError => _state == RState.Failed || _state == RState.Timeout;
  67. private int _ROffset = 0;
  68. private int _TOffset = 0;
  69. private bool _IsOverRange = false;
  70. public int ROffset => _ROffset;
  71. public int TOffset => _TOffset;
  72. public bool IsOverRange => _IsOverRange;
  73. public HongHuVPA(ModuleName module)
  74. {
  75. _module = module;
  76. string port = SC.GetStringValue($"{module}.AlignerPort");
  77. _serialport = new AsyncSerialPort(port, 9600, 8, Parity.None, StopBits.One, _newLine, _IsAsciiMode);
  78. WaferManager.Instance.SubscribeLocation(ModuleName.Aligner1, 1);
  79. _serialport.Open();
  80. _serialport.OnDataChanged += OnReceiveData;
  81. _state = RState.Init;
  82. _thread = new PeriodicJob(50, OnTimer, "OnTimer->Aligner1");
  83. _thread.Start();
  84. }
  85. //收到新消息时的处理
  86. private void OnReceiveData(string obj)
  87. {
  88. lock (_locker)
  89. {
  90. if (string.IsNullOrEmpty(_newLine))//没有CR
  91. {
  92. _lstMessage.AddLast(obj);//将消息添加到最后
  93. return;
  94. }
  95. string[] array = obj.Split(_newLine.ToCharArray());//按照cr分开通讯数据
  96. foreach (string text in array)
  97. {
  98. if (!string.IsNullOrEmpty(text))
  99. {
  100. _lstMessage.AddLast(text + _newLine);//存进list中等待处理
  101. }
  102. }
  103. }
  104. }
  105. //定时器处理新信息为状态
  106. private bool OnTimer()
  107. {
  108. //线程锁
  109. lock (_locker)
  110. {
  111. //采用ascii码模式处理
  112. if (_IsAsciiMode)
  113. {
  114. //存在尚未处理的信息
  115. while (_lstMessage.Count > 0)
  116. {
  117. //获取头上的数据
  118. string handlemsg = _lstMessage.First.Value;
  119. Handlemessage(handlemsg);
  120. _lstMessage.RemoveFirst();
  121. }
  122. }
  123. //采用binary模式处理
  124. else
  125. {
  126. }
  127. }
  128. return true;
  129. }
  130. //处理单条信息
  131. private void Handlemessage(string handlemsg)
  132. {
  133. //需要按类型进行处理 Action需要将其error
  134. bool IsAction = true;
  135. handlemsg = handlemsg.Trim();
  136. if (IsAction)
  137. {
  138. switch (handlemsg)
  139. {
  140. //正确执行
  141. case "_RDY":
  142. _state = RState.End;
  143. break;
  144. //返回
  145. default:
  146. if (_checkData.IsMatch(handlemsg))
  147. {
  148. string[] data = handlemsg.Split(' ');
  149. _ROffset = Convert.ToInt32(data[2]);
  150. _TOffset = Convert.ToInt32(data[3]);
  151. if (data[5] == "Y")
  152. {
  153. _IsOverRange = true;
  154. LOG.Write(eEvent.WARN_DEVICE_INFO, Module, $"Wafer offset is over range");
  155. }
  156. if (data[5] == "N")
  157. {
  158. _IsOverRange = false;
  159. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Wafer offset is in range");
  160. }
  161. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Ro{_ROffset} To{_TOffset}");
  162. }
  163. else
  164. {
  165. _state = RState.Failed;
  166. //分两种 1、不按格式的未知错误 2、有错误码的
  167. if (_catchErrorCode.IsMatch(handlemsg))
  168. {
  169. int errorcode = Convert.ToInt32(_catchErrorCode.Match(handlemsg).Value);
  170. if (_ErrorCode2Msg.ContainsKey(errorcode))
  171. {
  172. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, _ErrorCode2Msg[errorcode]);
  173. }
  174. else
  175. {
  176. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"未知错误码{errorcode}");
  177. }
  178. }
  179. else
  180. {
  181. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "未收录相关错误");
  182. }
  183. }
  184. break;
  185. }
  186. }
  187. }
  188. //发送消息
  189. private bool SendMessage(string msg, params string[] args)
  190. {
  191. _state = RState.Running;
  192. if (args.Length > 0)//含参
  193. {
  194. foreach (string arg in args)
  195. msg = msg + " " + arg;
  196. LOG.WriteSingeLine(eEvent.EV_DEVICE_INFO, _module, $"Send Command to HongHu Aligner: {msg}");
  197. return _serialport.Write(msg + _newLine);
  198. }
  199. else//空参
  200. {
  201. LOG.WriteSingeLine(eEvent.EV_DEVICE_INFO, _module, $"Send Command to HongHu Aligner: {msg}");
  202. return _serialport.Write(msg + _newLine);
  203. }
  204. }
  205. public bool Home()
  206. {
  207. return SendMessage(_Command2Msg[AlignerAction.Home]);
  208. }
  209. public bool Align()
  210. {
  211. if (!CanSendCommand())
  212. return false;
  213. return SendMessage(_Command2Msg[AlignerAction.Align]);
  214. }
  215. public bool AlignWithAngle(float angle)
  216. {
  217. if (!CanSendCommand())
  218. return false;
  219. int ang = (int)Math.Floor(angle);//不能用Convert.toInt32("angle") 其遵守四舍五入 此处需向上取整
  220. return SendMessage(_Command2Msg[AlignerAction.AlignWithAngle], ang.ToString());
  221. }
  222. public bool ReSet()
  223. {
  224. if (!IsError)
  225. return false;
  226. return SendMessage(_Command2Msg[AlignerAction.Reset]);
  227. }
  228. public bool SCAN()
  229. {
  230. if (!CanSendCommand())
  231. return false;
  232. return SendMessage(_Command2Msg[AlignerAction.Scan]);
  233. }
  234. public bool QueryOffset()
  235. {
  236. if (!CanSendCommand())
  237. return false;
  238. return SendMessage(_Command2Msg[AlignerAction.RsLT]);
  239. }
  240. public bool CanSendCommand()
  241. {
  242. if (Status == RState.Init)
  243. {
  244. LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "Aligner is not homed, please home first.");
  245. return false;
  246. }
  247. else if (Status == RState.Running)
  248. {
  249. LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "Aligner is busy, please wait a minute");
  250. return false;
  251. }
  252. else if (Status == RState.Failed || Status == RState.Timeout)
  253. {
  254. LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "Aligner has a error, please check and fix the hardware issue and home it");
  255. return false;
  256. }
  257. return true;
  258. }
  259. }
  260. }