HongHuVPA.cs 10 KB

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