HongHuVPA.cs 8.2 KB

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