HongHuVPA.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 VPAAction
  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<VPAAction, string> _Command2Msg = new Dictionary<VPAAction, string>()
  45. {
  46. //Action
  47. { VPAAction.Align , "ALGN" },//旋转到预设的站点方向
  48. { VPAAction.AlignWithAngle, "MOVT REL" },//旋转到指定的角度
  49. { VPAAction.Home , "HOME" },//Home 初始化时 error时用
  50. { VPAAction.Reset , "RSET" },//重置 error时用
  51. { VPAAction.Scan , "SCAN" },//扫描整个Wafer参数
  52. { VPAAction.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($"SETM.VPAPort");
  75. _serialport = new AsyncSerialPort(port, 9600, 8, Parity.None, StopBits.One, _newLine, _IsAsciiMode);
  76. WaferManager.Instance.SubscribeLocation(_module, 1);
  77. _serialport.Open();
  78. _serialport.OnDataChanged += OnReceiveData;
  79. _state = RState.Init;
  80. _thread = new PeriodicJob(50, OnTimer, "OnTimer->VPA");
  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. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Ro{_ROffset} To{_TOffset}");
  150. }
  151. else
  152. {
  153. _state = RState.Failed;
  154. //分两种 1、不按格式的未知错误 2、有错误码的
  155. if (_catchErrorCode.IsMatch(handlemsg))
  156. {
  157. int errorcode = Convert.ToInt32(_catchErrorCode.Match(handlemsg).Value);
  158. if (_ErrorCode2Msg.ContainsKey(errorcode))
  159. {
  160. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, _ErrorCode2Msg[errorcode]);
  161. }
  162. else
  163. {
  164. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"未知错误码{errorcode}");
  165. }
  166. }
  167. else
  168. {
  169. LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "未收录相关错误");
  170. }
  171. }
  172. break;
  173. }
  174. }
  175. }
  176. //发送消息
  177. private bool SendMessage(string msg, params string[] args)
  178. {
  179. _state = RState.Running;
  180. if (args.Length > 0)//含参
  181. {
  182. foreach (string arg in args)
  183. msg = msg + " " + arg;
  184. LOG.WriteSingeLine(eEvent.EV_DEVICE_INFO, _module, $"Send Command to HongHu VPA: {msg}");
  185. return _serialport.Write(msg + _newLine);
  186. }
  187. else//空参
  188. {
  189. LOG.WriteSingeLine(eEvent.EV_DEVICE_INFO, _module, $"Send Command to HongHu VPA: {msg}");
  190. return _serialport.Write(msg + _newLine);
  191. }
  192. }
  193. public bool Home()
  194. {
  195. return SendMessage(_Command2Msg[VPAAction.Home]);
  196. }
  197. public bool Align()
  198. {
  199. if (!CanSendCommand())
  200. return false;
  201. return SendMessage(_Command2Msg[VPAAction.Align]);
  202. }
  203. public bool AlignWithAngle(float angle)
  204. {
  205. if (!CanSendCommand())
  206. return false;
  207. int ang = (int)Math.Floor(angle);//不能用Convert.toInt32("angle") 其遵守四舍五入 此处需向上取整
  208. return SendMessage(_Command2Msg[VPAAction.AlignWithAngle], ang.ToString());
  209. }
  210. public bool ReSet()
  211. {
  212. if (!IsError)
  213. return false;
  214. return SendMessage(_Command2Msg[VPAAction.Reset]);
  215. }
  216. public bool SCAN()
  217. {
  218. if (!CanSendCommand())
  219. return false;
  220. return SendMessage(_Command2Msg[VPAAction.Scan]);
  221. }
  222. public bool QueryOffset()
  223. {
  224. if (!CanSendCommand())
  225. return false;
  226. return SendMessage(_Command2Msg[VPAAction.RsLT]);
  227. }
  228. public bool CanSendCommand()
  229. {
  230. if (Status == RState.Init)
  231. {
  232. LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "VPA is not homed, please home first.");
  233. return false;
  234. }
  235. else if (Status == RState.Running)
  236. {
  237. LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "VPA is busy, please wait a minute");
  238. return false;
  239. }
  240. else if (Status == RState.Failed || Status == RState.Timeout)
  241. {
  242. LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "VPA has a error, please check and fix the hardware issue and home it");
  243. return false;
  244. }
  245. return true;
  246. }
  247. }
  248. }