HongHuVce.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. {VceCommand.DoorOpen, "DO" },
  53. {VceCommand.DoorClose, "DC" },
  54. //Read
  55. {VceCommand.ReadMap, "MI" },
  56. //Set
  57. };
  58. private Dictionary<VceMessageHead, string> _Type2Head = new Dictionary<VceMessageHead, string>()
  59. {
  60. {VceMessageHead.Action, "A"},
  61. {VceMessageHead.Read, "R"},
  62. {VceMessageHead.Set, "S"},
  63. {VceMessageHead.Petrify, "P"},
  64. };
  65. public VceMessageHead Head { get; set; }
  66. public VceCommand Command { get; set; }
  67. public string Param { get; set; }
  68. public string toString()
  69. {
  70. if (string.IsNullOrEmpty(Param))//含尾参
  71. return $"00,{_Type2Head[Head]},{_Command2Msg[Command]}";
  72. else//不含尾参 目前只允许一个
  73. return $"00,{_Type2Head[Head]},{_Command2Msg[Command]},{Param}";
  74. }
  75. }
  76. /// <summary>
  77. /// 泓浒Vce驱动 下发指令等
  78. /// </summary>
  79. public class HongHuVce : VceModuleBase
  80. {
  81. #region 私有变量
  82. private AsyncSerialPort _serialport;
  83. private string _portname;
  84. private string _newline = "\n\r";//终止符 0A 0D
  85. private object _locker = new object();
  86. private bool _IsAsciiMode;
  87. private LinkedList<string> _lstAsciiMsgs = new LinkedList<string>();
  88. private PeriodicJob _thread;
  89. private Regex _match_ReadMsg = new Regex(@"\d\d,X,.*");
  90. private Regex _matchErrorCode = new Regex(@"(?<=_BKGERR )(.*)");
  91. private ModuleName _moduleName;
  92. private RState _status;
  93. private string _currentMsg;
  94. private VceMessage _currentVceMessage;
  95. private bool _HasReceiveMsg;
  96. //待补充
  97. private Dictionary<string, string> _ErrorCode2Reason = new Dictionary<string, string>()
  98. {
  99. {"A1","Action Timeout" },
  100. {"A3","Hardware (CAN or VCN) or configuration failed" },
  101. {"A4","Open Door Prevented Motion" },
  102. {"A5","Platform Action Time-out" },
  103. {"A6","Door Action Timeout" },
  104. {"A7","The parameter value is out of range" },
  105. {"A8","Wafer Slideout" },
  106. {"A9","Door safety LED is blocked" },
  107. {"A11","No cassette present" },
  108. {"A12","Cassette present prior PICK" },
  109. {"A13","Cassette NOT present during PICK" },
  110. {"A14","Cassette NOT present prior PLACE" },
  111. {"A15","Cassette present after PLACE" },
  112. {"A16","Cassette Present on VCE platform (Servo Arm)" },
  113. {"A17","No new cassette at station after Load" },
  114. {"A18","Proximity sensor blocked but Cassette A not present" },
  115. {"A19","Cassette present at station A (Fixed Buffer)" },
  116. {"A20","Proximity sensor A is blocked (Fixed Buffer)" },
  117. {"A21","Cassette NOT at station A" },
  118. {"A34","Door Clamped sensor is not ON after clamp (only VCE4!)" },
  119. {"A36","Door Not Covered (sensor)" },
  120. {"C0","Illegal Slot Number" },
  121. {"C1","Illegal action command: wrong attachment, no arm, etc." },
  122. {"C2","Illegal pitch value (too big)" },
  123. {"C3","Illegal Cassette type offset" },
  124. {"C4","Illegal number of Slots" },
  125. {"C5","Illegal Partial step size" },
  126. {"C7","Illegal Find Bias" },
  127. {"C8","Unknown Configuration" },
  128. {"C9","Bad command: command cannot be executed with current HW configuration" },
  129. {"C10","VCE is busy" },
  130. {"C12","Bad Fixture Thickness" },
  131. {"C13","Command is NOT executable (file's operation exception)" },
  132. {"C19","VCEConfig.xml is corrupted" },
  133. {"C20","VCEDefaultSettting.xml is corrupted" },
  134. };
  135. //
  136. #endregion
  137. #region 暴露变量
  138. public override bool IsConnected => _serialport.IsOpen();
  139. public override RState Status => _status;
  140. public override bool IsReady => _status == RState.Init || _status == RState.End;
  141. public override bool IsError => _status == RState.Failed || _status == RState.Timeout;
  142. public override bool IsInit => _status == RState.Init;
  143. private string[] _slotMap = new string[25];
  144. public string SlotMap
  145. {
  146. get
  147. {
  148. WaferInfo[] wafers = WaferManager.Instance.GetWafers(ModuleHelper.Converter(Name));
  149. string slot = "";
  150. for (int i = 0; i < 25; i++)
  151. {
  152. slot += wafers[i].IsEmpty ? "0" : "1";
  153. }
  154. return slot;
  155. }
  156. }
  157. private bool _OutDoorIsOpen = false;
  158. public override bool OutDoorIsOpen => _OutDoorIsOpen;
  159. #endregion
  160. //传入slot数量
  161. public HongHuVce(int slot, ModuleName moduleName) : base(slot, moduleName)
  162. {
  163. _moduleName = moduleName;
  164. _IsAsciiMode = true;
  165. _portname = SC.GetStringValue($"{moduleName}.Port");
  166. //_portname = "COM162";
  167. _serialport = new AsyncSerialPort(_portname, 9600, 7, Parity.Even, StopBits.One, _newline, _IsAsciiMode);
  168. _serialport.Open();
  169. _status = RState.Init;
  170. _serialport.OnDataChanged += onDataChange;
  171. _thread = new PeriodicJob(50, fnTimer, "VCE", true);
  172. CarrierManager.Instance.DeleteCarrier(_moduleName.ToString());
  173. WaferManager.Instance.DeleteWafer(_moduleName, 0, 25);
  174. CarrierManager.Instance.SubscribeLocation(_moduleName.ToString(), 1);
  175. Action<ModuleName, int> _subscribeLoc = (ModuleName module, int waferCount) => {
  176. if (ModuleHelper.IsInstalled(module))
  177. {
  178. WaferManager.Instance.SubscribeLocation(module, waferCount);
  179. }
  180. };
  181. _subscribeLoc(_moduleName, slot);
  182. }
  183. /// <summary>
  184. /// 对处理过的数据list进行处理
  185. /// 将每条数据进行解析
  186. /// </summary>
  187. /// <returns></returns>
  188. private bool fnTimer()
  189. {
  190. lock (_locker)
  191. {
  192. //采用ascii传输
  193. if (_IsAsciiMode)
  194. {
  195. //有数据尚未处理
  196. while (_lstAsciiMsgs.Count > 0)
  197. {
  198. string _needHandle = _lstAsciiMsgs.First.Value;
  199. HandleSingleMsg(_needHandle);
  200. _lstAsciiMsgs.RemoveFirst();
  201. }
  202. }
  203. //采用binary
  204. else
  205. {
  206. }
  207. }
  208. return true;
  209. }
  210. /// <summary>
  211. /// 处理单条信息的函数
  212. /// 1、判断结束 2、判断错误
  213. /// </summary>
  214. /// <param name="msg">需要处理的单条回复</param>
  215. private void HandleSingleMsg(string msg)
  216. {
  217. //
  218. msg = msg.Trim();
  219. if (!string.IsNullOrEmpty(msg))
  220. {
  221. //action set petrify _BKGRDY结束
  222. switch (_currentVceMessage.Head)
  223. {
  224. case VceMessageHead.Action:
  225. case VceMessageHead.Set:
  226. case VceMessageHead.Petrify:
  227. switch (msg)
  228. {
  229. //设备收到 开始运行 目前状态在下发
  230. case "_RDY":
  231. LOG.Write(eEvent.EV_VCE_COMMON_INFO, _moduleName, $"vce start {_currentVceMessage.Head}");
  232. break;
  233. //设备执行完毕
  234. case "_BKGRDY":
  235. if (_currentVceMessage.Command == VceCommand.DoorOpen)
  236. {
  237. _OutDoorIsOpen = true;
  238. }
  239. if (_currentVceMessage.Command == VceCommand.DoorClose)
  240. {
  241. _OutDoorIsOpen = false;
  242. }
  243. LOG.Write(eEvent.EV_VCE_COMMON_INFO, _moduleName, $"vce {_currentVceMessage.Head} over");
  244. _status = RState.End;
  245. break;
  246. //异常处理
  247. default:
  248. _status = RState.Failed;
  249. string reason;
  250. Errorhandle(msg, out reason);
  251. LOG.Write(eEvent.ERR_VCE_COMMON_Failed, _moduleName, reason);
  252. break;
  253. }
  254. break;
  255. case VceMessageHead.Read:
  256. //如果收到的信息符合
  257. if (_match_ReadMsg.IsMatch(msg))
  258. {
  259. //收到消息 用于结束
  260. _HasReceiveMsg = true;
  261. switch (_currentVceMessage.Command)
  262. {
  263. //处理wafer 信息为map数据
  264. case VceCommand.ReadMap:
  265. ReadMapData(msg);
  266. break;
  267. }
  268. }
  269. //_RDY查询结束
  270. else
  271. {
  272. if (msg == "_RDY")
  273. {
  274. if (_HasReceiveMsg)
  275. {
  276. _status = RState.End;
  277. }
  278. else
  279. {
  280. LOG.Write(eEvent.ERR_VCE_COMMON_Failed, _moduleName, $"Read Message is over but not receive msg! raw message:{_currentMsg}");
  281. _status = RState.Failed;
  282. }
  283. }
  284. else
  285. {
  286. _status = RState.Failed;
  287. LOG.Write(eEvent.ERR_VCE_COMMON_Failed, _moduleName, $"Read Message is invalid: receive message {msg} and send message {_currentMsg}");
  288. }
  289. }
  290. break;
  291. }
  292. }
  293. }
  294. private void ReadMapData(string msg)
  295. {
  296. string waferinfo = "";
  297. string[] waferitems = msg.Split(',');
  298. //智能模式 可以识别叠片
  299. for (int i = 3; i < waferitems.Length - 1; ++i)
  300. {
  301. //如果包含只需要逐个检查
  302. if (waferitems[i].Contains('?'))
  303. {
  304. foreach (char j in waferitems[i])
  305. {
  306. if (j == '?')
  307. break;
  308. else
  309. waferinfo += j;
  310. }
  311. }
  312. else
  313. waferinfo += waferitems[i];
  314. }
  315. for (int i = 0; i < waferinfo.Length; ++i)
  316. {
  317. int slotnum = i;
  318. if (slotnum < 25)
  319. {
  320. switch (waferinfo[i])
  321. {
  322. case '0':
  323. WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Empty);
  324. break;
  325. case 'X':
  326. WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Normal);
  327. break;
  328. case 'C':
  329. WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Crossed);
  330. break;
  331. }
  332. }
  333. }
  334. //2进制模式
  335. //for (int i = 3; i < waferitems.Length - 1; i++)
  336. //{
  337. // //从16进制字符转义回二进制
  338. // string wafersingleinfo = Convert.ToString(Convert.ToInt32(waferitems[i], 16), 2);
  339. // if (wafersingleinfo.Length < 4)
  340. // wafersingleinfo = wafersingleinfo.PadLeft(4, '0');//补位
  341. // waferinfo = wafersingleinfo + waferinfo;//添加到数据中
  342. //}
  343. ////请将数据按照反向槽位进行解析存入到wafermanager中
  344. //for (int i = waferinfo.Length - 1; i > 0; i--)
  345. //{
  346. // int slotnum = waferinfo.Length - i - 1;
  347. // if (slotnum < 25)
  348. // {
  349. // if (waferinfo[i] == '1')
  350. // {
  351. // WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Normal);
  352. // }
  353. // else
  354. // {
  355. // WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Empty);
  356. // }
  357. // }
  358. //}
  359. }
  360. private void Errorhandle(string msg,out string reason)
  361. {
  362. if (_matchErrorCode.IsMatch(msg))
  363. {
  364. //若是匹配
  365. //包含原因
  366. string errorcode = _matchErrorCode.Match(msg).Value;
  367. if (_ErrorCode2Reason.ContainsKey(errorcode))
  368. {
  369. reason = _ErrorCode2Reason[errorcode];
  370. }
  371. else
  372. {
  373. reason = "未找到相关Error Code";
  374. }
  375. }
  376. else
  377. {
  378. //若不匹配
  379. reason = "回复消息不符合标准格式";
  380. }
  381. }
  382. /// <summary>
  383. /// 处理新到的数据
  384. /// 利用linkedlist处理拆包 粘包情况
  385. /// </summary>
  386. /// <param name="newline">新到数据</param>
  387. private void onDataChange(string oneLineMessage)
  388. {
  389. lock (_locker)
  390. {
  391. if (string.IsNullOrEmpty(_newline))//没有CR
  392. {
  393. _lstAsciiMsgs.AddLast(oneLineMessage);//将消息添加到最后
  394. return;
  395. }
  396. string[] array = oneLineMessage.Split(_newline.ToCharArray());//按照cr分开通讯数据
  397. foreach (string text in array)
  398. {
  399. if (!string.IsNullOrEmpty(text))
  400. {
  401. _lstAsciiMsgs.AddLast(text + _newline);//存进list中等待处理
  402. }
  403. }
  404. }
  405. }
  406. public override bool HomeALL()
  407. {
  408. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Home ,Param = "ALL" };
  409. _currentMsg = _currentVceMessage.toString() + _newline ;
  410. _status = RState.Running;
  411. return _serialport.Write(_currentMsg);
  412. }
  413. public override bool Home(string axis)
  414. {
  415. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Home, Param = axis };
  416. _currentMsg = _currentVceMessage.toString() + _newline;
  417. _status = RState.Running;
  418. return _serialport.Write(_currentMsg);
  419. }
  420. public override bool CloseDoor()
  421. {
  422. if (!CheckVceStatus())
  423. return false;
  424. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.DoorClose };
  425. _currentMsg = _currentVceMessage.toString() + _newline;
  426. _status = RState.Running;
  427. return _serialport.Write(_currentMsg);
  428. }
  429. public override bool OpenDoor()
  430. {
  431. if (!CheckVceStatus())
  432. return false;
  433. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.DoorOpen };
  434. _currentMsg = _currentVceMessage.toString() + _newline;
  435. _status = RState.Running;
  436. return _serialport.Write(_currentMsg);
  437. }
  438. public override bool Load()
  439. {
  440. if (!CheckVceStatus())
  441. return false;
  442. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Load };
  443. _currentMsg = _currentVceMessage.toString() + _newline;
  444. _status = RState.Running;
  445. return _serialport.Write(_currentMsg);
  446. }
  447. public override bool UnLoad()
  448. {
  449. if (!CheckVceStatus())
  450. return false;
  451. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.UnLoad };
  452. _currentMsg = _currentVceMessage.toString() + _newline;
  453. _status = RState.Running;
  454. return _serialport.Write(_currentMsg);
  455. }
  456. public override bool Map()
  457. {
  458. if (!CheckVceStatus())
  459. return false;
  460. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Map };
  461. _currentMsg = _currentVceMessage.toString() + _newline;
  462. _status = RState.Running;
  463. return _serialport.Write(_currentMsg);
  464. }
  465. public override bool ReadMap()
  466. {
  467. if (!CheckVceStatus())
  468. return false;
  469. _currentVceMessage = new VceMessage { Head = VceMessageHead.Read, Command = VceCommand.ReadMap, Param = "S" };
  470. _currentMsg = _currentVceMessage.toString() + _newline;
  471. _status = RState.Running;
  472. _HasReceiveMsg = false;
  473. return _serialport.Write(_currentMsg);
  474. }
  475. public override bool Goto(int Targetslot)
  476. {
  477. if (!CheckVceStatus())
  478. return false;
  479. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Goto, Param = Targetslot.ToString() };
  480. _currentMsg = _currentVceMessage.toString() + _newline;
  481. _status = RState.Running;
  482. return _serialport.Write(_currentMsg);
  483. }
  484. public override bool GotoLP()
  485. {
  486. if (!CheckVceStatus())
  487. return false;
  488. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.GotoLP };
  489. _currentMsg = _currentVceMessage.toString() + _newline;
  490. _status = RState.Running;
  491. return _serialport.Write(_currentMsg);
  492. }
  493. }
  494. }