HongHuVPA.cs 8.0 KB

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