HongHuVPA.cs 9.5 KB

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