HongHuVPA.cs 8.0 KB

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