HongHuVce.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. using Aitex.Core.Common;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.SCCore;
  4. using Aitex.Core.Util;
  5. using MECF.Framework.Common.Communications;
  6. using MECF.Framework.Common.Equipment;
  7. using MECF.Framework.Common.SubstrateTrackings;
  8. using MECF.Framework.RT.ModuleLibrary.VceModules;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO.Ports;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Threading.Tasks;
  16. using Venus_Core;
  17. namespace Venus_RT.Devices.VCE
  18. {
  19. //定义Vce动作
  20. public enum VceCommand
  21. {
  22. Home,
  23. DoorClose,
  24. DoorOpen,
  25. CheckGoto,
  26. Goto,
  27. GotoLP,
  28. Load,
  29. UnLoad,
  30. Map,
  31. ReadMap,
  32. }
  33. public enum VceMessageHead
  34. {
  35. Action,
  36. Read,
  37. Set,
  38. Petrify
  39. }
  40. public sealed class VceMessage
  41. {
  42. private Dictionary<VceCommand, string> _Command2Msg = new Dictionary<VceCommand, string>()
  43. {
  44. //Action
  45. {VceCommand.Home, "HM" },
  46. {VceCommand.Load, "LOAD" },
  47. {VceCommand.UnLoad, "UNLOAD"},
  48. {VceCommand.Map, "MP" },
  49. {VceCommand.CheckGoto, "GC" },
  50. {VceCommand.Goto, "GO" },
  51. {VceCommand.GotoLP, "LP" },
  52. //Read
  53. {VceCommand.ReadMap, "MI" },
  54. //Set
  55. };
  56. private Dictionary<VceMessageHead, string> _Type2Head = new Dictionary<VceMessageHead, string>()
  57. {
  58. {VceMessageHead.Action, "A"},
  59. {VceMessageHead.Read, "R"},
  60. {VceMessageHead.Set, "S"},
  61. {VceMessageHead.Petrify, "P"},
  62. };
  63. public VceMessageHead Head { get; set; }
  64. public VceCommand Command { get; set; }
  65. public string Param { get; set; }
  66. public string toString()
  67. {
  68. if (string.IsNullOrEmpty(Param))//含尾参
  69. return $"00,{_Type2Head[Head]},{_Command2Msg[Command]}";
  70. else//不含尾参 目前只允许一个
  71. return $"00,{_Type2Head[Head]},{_Command2Msg[Command]},{Param}";
  72. }
  73. }
  74. /// <summary>
  75. /// 泓浒Vce驱动 下发指令等
  76. /// </summary>
  77. public class HongHuVce : VceModuleBase
  78. {
  79. #region 私有变量
  80. private AsyncSerialPort _serialport;
  81. private string _portname;
  82. private string _newline = "\n\r";//终止符 0A 0D
  83. private object _locker = new object();
  84. private bool _IsAsciiMode;
  85. private LinkedList<string> _lstAsciiMsgs = new LinkedList<string>();
  86. private PeriodicJob _thread;
  87. private Regex _match_ReadMsg = new Regex(@"\d\d,X,.*");
  88. private Regex _matchErrorCode = new Regex(@"(?<=_BKGERR )(.*)");
  89. private ModuleName _moduleName;
  90. private RState _status;
  91. private string _currentMsg;
  92. private VceMessage _currentVceMessage;
  93. private bool _HasReceiveMsg;
  94. //待补充
  95. private Dictionary<string, string> _ErrorCode2Reason = new Dictionary<string, string>()
  96. {
  97. {"A6","设备动作,但是发生异常" },
  98. {"A8","设备检查互锁,发现无法执行" },
  99. };
  100. //
  101. #endregion
  102. #region 暴露变量
  103. public override bool IsConnected => _serialport.IsOpen();
  104. public override RState Status => _status;
  105. public override bool IsReady => _status == RState.Init || _status == RState.End;
  106. public override bool IsError => _status == RState.Failed || _status == RState.Timeout;
  107. public override bool IsInit => _status == RState.Init;
  108. private string[] _slotMap = new string[25];
  109. public string SlotMap
  110. {
  111. get
  112. {
  113. if (_slotMap == null)
  114. {
  115. _slotMap = new string[25];
  116. }
  117. for (int i = 0; i < _slotMap.Length; i++)
  118. {
  119. _slotMap[i] = ((int)WaferManager.Instance.GetWafer(_moduleName, i).Status).ToString();
  120. }
  121. return string.Join("", _slotMap);
  122. }
  123. }
  124. #endregion
  125. //传入slot数量
  126. public HongHuVce(int slot, ModuleName moduleName) : base(slot, moduleName)
  127. {
  128. _moduleName = moduleName;
  129. _IsAsciiMode = true;
  130. //_portname = SC.GetStringValue("VCE.Port");
  131. _portname = "COM162";
  132. _serialport = new AsyncSerialPort(_portname, 9600, 7, Parity.Even, StopBits.One, _newline, _IsAsciiMode);
  133. _serialport.Open();
  134. _status = RState.Init;
  135. _serialport.OnDataChanged += onDataChange;
  136. _thread = new PeriodicJob(50, fnTimer, "VCE", true);
  137. Action<ModuleName, int> _subscribeLoc = (ModuleName module, int waferCount) => {
  138. if (ModuleHelper.IsInstalled(module))
  139. {
  140. WaferManager.Instance.SubscribeLocation(module, waferCount);
  141. }
  142. };
  143. CarrierManager.Instance.SubscribeLocation(ModuleName.VCE1.ToString(), 1);
  144. _subscribeLoc(ModuleName.VCE1, slot);
  145. }
  146. /// <summary>
  147. /// 对处理过的数据list进行处理
  148. /// 将每条数据进行解析
  149. /// </summary>
  150. /// <returns></returns>
  151. private bool fnTimer()
  152. {
  153. lock (_locker)
  154. {
  155. //采用ascii传输
  156. if (_IsAsciiMode)
  157. {
  158. //有数据尚未处理
  159. while (_lstAsciiMsgs.Count > 0)
  160. {
  161. string _needHandle = _lstAsciiMsgs.First.Value;
  162. HandleSingleMsg(_needHandle);
  163. _lstAsciiMsgs.RemoveFirst();
  164. }
  165. }
  166. //采用binary
  167. else
  168. {
  169. }
  170. }
  171. return true;
  172. }
  173. /// <summary>
  174. /// 处理单条信息的函数
  175. /// 1、判断结束 2、判断错误
  176. /// </summary>
  177. /// <param name="msg">需要处理的单条回复</param>
  178. private void HandleSingleMsg(string msg)
  179. {
  180. //
  181. msg = msg.Trim();
  182. if (!string.IsNullOrEmpty(msg))
  183. {
  184. //action set petrify _BKGRDY结束
  185. switch (_currentVceMessage.Head)
  186. {
  187. case VceMessageHead.Action:
  188. case VceMessageHead.Set:
  189. case VceMessageHead.Petrify:
  190. switch (msg)
  191. {
  192. //设备收到 开始运行 目前状态在下发
  193. case "_RDY":
  194. LOG.Write(eEvent.EV_VCE_COMMON_INFO, _moduleName, $"vce start {_currentVceMessage.Head}");
  195. break;
  196. //设备执行完毕
  197. case "_BKGRDY":
  198. LOG.Write(eEvent.EV_VCE_COMMON_INFO, _moduleName, $"vce {_currentVceMessage.Head} over");
  199. _status = RState.End;
  200. break;
  201. //异常处理
  202. default:
  203. _status = RState.Failed;
  204. string reason;
  205. Errorhandle(msg, out reason);
  206. LOG.Write(eEvent.ERR_VCE_COMMON_Failed, _moduleName, reason);
  207. break;
  208. }
  209. break;
  210. case VceMessageHead.Read:
  211. //如果收到的信息符合
  212. if (_match_ReadMsg.IsMatch(msg))
  213. {
  214. //收到消息 用于结束
  215. _HasReceiveMsg = true;
  216. switch (_currentVceMessage.Command)
  217. {
  218. //处理wafer 信息为map数据
  219. case VceCommand.ReadMap:
  220. ReadMapData(msg);
  221. break;
  222. }
  223. }
  224. //_RDY查询结束
  225. else
  226. {
  227. if (msg == "_RDY")
  228. {
  229. if (_HasReceiveMsg)
  230. {
  231. _status = RState.End;
  232. }
  233. else
  234. {
  235. LOG.Write(eEvent.ERR_VCE_COMMON_Failed, _moduleName, $"Read Message is over but not receive msg! raw message:{_currentMsg}");
  236. _status = RState.Failed;
  237. }
  238. }
  239. else
  240. {
  241. _status = RState.Failed;
  242. LOG.Write(eEvent.ERR_VCE_COMMON_Failed, _moduleName, $"Read Message is invalid: receive message {msg} and send message {_currentMsg}");
  243. }
  244. }
  245. break;
  246. }
  247. }
  248. }
  249. private void ReadMapData(string msg)
  250. {
  251. string waferinfo = "";
  252. string[] waferitems = msg.Split(',');
  253. for (int i = 3; i < waferitems.Length - 1; i++)
  254. {
  255. //从16进制字符转义回二进制
  256. string wafersingleinfo = Convert.ToString(Convert.ToInt32(waferitems[i], 16), 2);
  257. if (wafersingleinfo.Length < 4)
  258. wafersingleinfo = wafersingleinfo.PadLeft(4, '0');//补位
  259. waferinfo = wafersingleinfo + waferinfo;//添加到数据中
  260. }
  261. //请将数据按照反向槽位进行解析存入到wafermanager中
  262. for (int i = waferinfo.Length - 1; i > 0; i--)
  263. {
  264. int slotnum = waferinfo.Length - i - 1;
  265. if (slotnum < 25)
  266. {
  267. if (waferinfo[i] == '1')
  268. {
  269. WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Normal);
  270. }
  271. else
  272. {
  273. WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Empty);
  274. }
  275. }
  276. }
  277. }
  278. private void Errorhandle(string msg,out string reason)
  279. {
  280. if (_matchErrorCode.IsMatch(msg))
  281. {
  282. //若是匹配
  283. //包含原因
  284. string errorcode = _matchErrorCode.Match(msg).Value;
  285. if (_ErrorCode2Reason.ContainsKey(errorcode))
  286. {
  287. reason = _ErrorCode2Reason[errorcode];
  288. }
  289. else
  290. {
  291. reason = "未找到相关Error Code";
  292. }
  293. }
  294. else
  295. {
  296. //若不匹配
  297. reason = "回复消息不符合标准格式";
  298. }
  299. }
  300. /// <summary>
  301. /// 处理新到的数据
  302. /// 利用linkedlist处理拆包 粘包情况
  303. /// </summary>
  304. /// <param name="newline">新到数据</param>
  305. private void onDataChange(string oneLineMessage)
  306. {
  307. lock (_locker)
  308. {
  309. if (string.IsNullOrEmpty(_newline))//没有CR
  310. {
  311. _lstAsciiMsgs.AddLast(oneLineMessage);//将消息添加到最后
  312. return;
  313. }
  314. string[] array = oneLineMessage.Split(_newline.ToCharArray());//按照cr分开通讯数据
  315. foreach (string text in array)
  316. {
  317. if (!string.IsNullOrEmpty(text))
  318. {
  319. _lstAsciiMsgs.AddLast(text + _newline);//存进list中等待处理
  320. }
  321. }
  322. }
  323. }
  324. public override bool HomeALL()
  325. {
  326. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Home ,Param = "ALL" };
  327. _currentMsg = _currentVceMessage.toString() + _newline ;
  328. _status = RState.Running;
  329. return _serialport.Write(_currentMsg);
  330. }
  331. public override bool Home(string axis)
  332. {
  333. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Home, Param = axis };
  334. _currentMsg = _currentVceMessage.toString() + _newline;
  335. _status = RState.Running;
  336. return _serialport.Write(_currentMsg);
  337. }
  338. public override bool CloseDoor()
  339. {
  340. if (!CheckVceStatus())
  341. return false;
  342. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.DoorClose };
  343. _currentMsg = _currentVceMessage.toString() + _newline;
  344. _status = RState.Running;
  345. return _serialport.Write(_currentMsg);
  346. }
  347. public override bool OpenDoor()
  348. {
  349. if (!CheckVceStatus())
  350. return false;
  351. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.DoorOpen };
  352. _currentMsg = _currentVceMessage.toString() + _newline;
  353. _status = RState.Running;
  354. return _serialport.Write(_currentMsg);
  355. }
  356. public override bool Load()
  357. {
  358. if (!CheckVceStatus())
  359. return false;
  360. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Load };
  361. _currentMsg = _currentVceMessage.toString() + _newline;
  362. _status = RState.Running;
  363. return _serialport.Write(_currentMsg);
  364. }
  365. public override bool UnLoad()
  366. {
  367. if (!CheckVceStatus())
  368. return false;
  369. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.UnLoad };
  370. _currentMsg = _currentVceMessage.toString() + _newline;
  371. _status = RState.Running;
  372. return _serialport.Write(_currentMsg);
  373. }
  374. public override bool Map()
  375. {
  376. if (!CheckVceStatus())
  377. return false;
  378. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Map };
  379. _currentMsg = _currentVceMessage.toString() + _newline;
  380. _status = RState.Running;
  381. return _serialport.Write(_currentMsg);
  382. }
  383. public override bool ReadMap()
  384. {
  385. if (!CheckVceStatus())
  386. return false;
  387. _currentVceMessage = new VceMessage { Head = VceMessageHead.Read, Command = VceCommand.ReadMap };
  388. _currentMsg = _currentVceMessage.toString() + _newline;
  389. _status = RState.Running;
  390. _HasReceiveMsg = false;
  391. return _serialport.Write(_currentMsg);
  392. }
  393. public override bool Goto(int Targetslot)
  394. {
  395. if (!CheckVceStatus())
  396. return false;
  397. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Goto, Param = Targetslot.ToString() };
  398. _currentMsg = _currentVceMessage.toString() + _newline;
  399. _status = RState.Running;
  400. return _serialport.Write(_currentMsg);
  401. }
  402. public override bool GotoLP()
  403. {
  404. if (!CheckVceStatus())
  405. return false;
  406. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.GotoLP };
  407. _currentMsg = _currentVceMessage.toString() + _newline;
  408. _status = RState.Running;
  409. return _serialport.Write(_currentMsg);
  410. }
  411. }
  412. }