SunWayVPA.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.EquipmentLibrary.HardwareUnits.Robot;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO.Ports;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Threading.Tasks;
  16. using Venus_Core;
  17. namespace Venus_RT.Devices.PreAligner
  18. {
  19. public class SunWayVPA : IPreAlign
  20. {
  21. private AsyncSerialPort _serialport;//串口
  22. private string _newLine = "\r\n";//结束符号
  23. private object _locker = new object();//锁变量
  24. private bool _IsAsciiMode = true;
  25. private PeriodicJob _thread;
  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 , "ALIGNER ALGN" },//旋转到预设的站点方向
  49. { AlignerAction.AlignWithAngle, "MOVT REL" },//旋转到指定的角度
  50. { AlignerAction.Home , "ALIGNER HOME" },//Home 初始化时 error时用
  51. { AlignerAction.Reset , "RSET" },//重置 error时用
  52. { AlignerAction.Scan , "ALIGNER SCAN" },//扫描整个Wafer参数
  53. { AlignerAction.RsLT , "ALIGNER RSLT" },
  54. { AlignerAction.ServeUp , "SET SERVOS ON"}
  55. //Read
  56. //{ "" , "RQCD" },
  57. //{ "" , "RQCCDPOS" },
  58. //{ "" , "RQID" },
  59. //{ "" , "RQPS" },
  60. //Set
  61. //{ "" , "" },
  62. //Welding
  63. //{ "" , "SVCD" },
  64. };
  65. public ModuleName Module => _module;
  66. public bool IsConnect => _serialport.IsConnected;
  67. public RState Status => _state;//状态
  68. public bool IsError => _state == RState.Failed || _state == RState.Timeout;
  69. private int _ROffset = 0;
  70. private int _TOffset = 0;
  71. private bool _IsOverRange = false;
  72. public int ROffset => _ROffset;
  73. public int TOffset => _TOffset;
  74. public bool IsOverRange => _IsOverRange;
  75. public SunWayVPA(ModuleName module)
  76. {
  77. _module = module;
  78. string port = SC.GetStringValue($"{module}.AlignerPort");
  79. _serialport = new AsyncSerialPort(port, 9600, 8, Parity.None, StopBits.One, _newLine, _IsAsciiMode);
  80. _serialport.Open();
  81. WaferManager.Instance.SubscribeLocation(ModuleName.Aligner1, 1);
  82. _serialport.OnDataChanged += OnReceiveData;
  83. _state = RState.Init;
  84. _thread = new PeriodicJob(50, OnTimer, "OnTimer->Aligner1");
  85. _thread.Start();
  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. 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. private bool SendMessage(string msg, params string[] args)
  189. {
  190. _state = RState.Running;
  191. if (args.Length > 0)//含参
  192. {
  193. foreach (string arg in args)
  194. msg = msg + " " + arg;
  195. LOG.WriteSingeLine(eEvent.EV_DEVICE_INFO, _module, $"Send Command to SunWay Aligner: {msg}");
  196. return _serialport.Write(msg + _newLine);
  197. }
  198. else//空参
  199. {
  200. LOG.WriteSingeLine(eEvent.EV_DEVICE_INFO, _module, $"Send Command to SunWay Aligner: {msg}");
  201. return _serialport.Write(msg + _newLine);
  202. }
  203. }
  204. public bool Align()
  205. {
  206. if (!CanSendCommand())
  207. return false;
  208. return SendMessage(_Command2Msg[AlignerAction.Align]);
  209. }
  210. public bool AlignWithAngle(float angle)
  211. {
  212. if (!CanSendCommand())
  213. return false;
  214. int ang = (int)Math.Floor(angle);//不能用Convert.toInt32("angle") 其遵守四舍五入 此处需向上取整
  215. return SendMessage(_Command2Msg[AlignerAction.AlignWithAngle], ang.ToString());
  216. }
  217. public bool Home()
  218. {
  219. return SendMessage(_Command2Msg[AlignerAction.Home]);
  220. }
  221. public bool QueryOffset()
  222. {
  223. if (!CanSendCommand())
  224. return false;
  225. return SendMessage(_Command2Msg[AlignerAction.RsLT]);
  226. }
  227. public bool ReSet()
  228. {
  229. if (!IsError)
  230. return false;
  231. return SendMessage(_Command2Msg[AlignerAction.Reset]);
  232. }
  233. public bool SCAN()
  234. {
  235. if (!CanSendCommand())
  236. return false;
  237. return SendMessage(_Command2Msg[AlignerAction.Scan]);
  238. }
  239. public bool ServeUp()
  240. {
  241. return SendMessage(_Command2Msg[AlignerAction.ServeUp]);
  242. }
  243. public bool CanSendCommand()
  244. {
  245. if (Status == RState.Init)
  246. {
  247. LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "Aligner is not homed, please home first.");
  248. return false;
  249. }
  250. else if (Status == RState.Running)
  251. {
  252. LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "Aligner is busy, please wait a minute");
  253. return false;
  254. }
  255. else if (Status == RState.Failed || Status == RState.Timeout)
  256. {
  257. LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "Aligner has a error, please check and fix the hardware issue and home it");
  258. return false;
  259. }
  260. return true;
  261. }
  262. }
  263. }