HongHuVce.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. #endregion
  158. //传入slot数量
  159. public HongHuVce(int slot, ModuleName moduleName) : base(slot, moduleName)
  160. {
  161. _moduleName = moduleName;
  162. _IsAsciiMode = true;
  163. _portname = SC.GetStringValue($"{moduleName}.Port");
  164. //_portname = "COM162";
  165. _serialport = new AsyncSerialPort(_portname, 9600, 7, Parity.Even, StopBits.One, _newline, _IsAsciiMode);
  166. _serialport.Open();
  167. _status = RState.Init;
  168. _serialport.OnDataChanged += onDataChange;
  169. _thread = new PeriodicJob(50, fnTimer, "VCE", true);
  170. CarrierManager.Instance.DeleteCarrier(_moduleName.ToString());
  171. WaferManager.Instance.DeleteWafer(_moduleName, 0, 25);
  172. CarrierManager.Instance.SubscribeLocation(_moduleName.ToString(), 1);
  173. Action<ModuleName, int> _subscribeLoc = (ModuleName module, int waferCount) => {
  174. if (ModuleHelper.IsInstalled(module))
  175. {
  176. WaferManager.Instance.SubscribeLocation(module, waferCount);
  177. }
  178. };
  179. _subscribeLoc(_moduleName, slot);
  180. }
  181. /// <summary>
  182. /// 对处理过的数据list进行处理
  183. /// 将每条数据进行解析
  184. /// </summary>
  185. /// <returns></returns>
  186. private bool fnTimer()
  187. {
  188. lock (_locker)
  189. {
  190. //采用ascii传输
  191. if (_IsAsciiMode)
  192. {
  193. //有数据尚未处理
  194. while (_lstAsciiMsgs.Count > 0)
  195. {
  196. string _needHandle = _lstAsciiMsgs.First.Value;
  197. HandleSingleMsg(_needHandle);
  198. _lstAsciiMsgs.RemoveFirst();
  199. }
  200. }
  201. //采用binary
  202. else
  203. {
  204. }
  205. }
  206. return true;
  207. }
  208. /// <summary>
  209. /// 处理单条信息的函数
  210. /// 1、判断结束 2、判断错误
  211. /// </summary>
  212. /// <param name="msg">需要处理的单条回复</param>
  213. private void HandleSingleMsg(string msg)
  214. {
  215. //
  216. msg = msg.Trim();
  217. if (!string.IsNullOrEmpty(msg))
  218. {
  219. //action set petrify _BKGRDY结束
  220. switch (_currentVceMessage.Head)
  221. {
  222. case VceMessageHead.Action:
  223. case VceMessageHead.Set:
  224. case VceMessageHead.Petrify:
  225. switch (msg)
  226. {
  227. //设备收到 开始运行 目前状态在下发
  228. case "_RDY":
  229. LOG.Write(eEvent.EV_VCE_COMMON_INFO, _moduleName, $"vce start {_currentVceMessage.Head}");
  230. break;
  231. //设备执行完毕
  232. case "_BKGRDY":
  233. LOG.Write(eEvent.EV_VCE_COMMON_INFO, _moduleName, $"vce {_currentVceMessage.Head} over");
  234. _status = RState.End;
  235. break;
  236. //异常处理
  237. default:
  238. _status = RState.Failed;
  239. string reason;
  240. Errorhandle(msg, out reason);
  241. LOG.Write(eEvent.ERR_VCE_COMMON_Failed, _moduleName, reason);
  242. break;
  243. }
  244. break;
  245. case VceMessageHead.Read:
  246. //如果收到的信息符合
  247. if (_match_ReadMsg.IsMatch(msg))
  248. {
  249. //收到消息 用于结束
  250. _HasReceiveMsg = true;
  251. switch (_currentVceMessage.Command)
  252. {
  253. //处理wafer 信息为map数据
  254. case VceCommand.ReadMap:
  255. ReadMapData(msg);
  256. break;
  257. }
  258. }
  259. //_RDY查询结束
  260. else
  261. {
  262. if (msg == "_RDY")
  263. {
  264. if (_HasReceiveMsg)
  265. {
  266. _status = RState.End;
  267. }
  268. else
  269. {
  270. LOG.Write(eEvent.ERR_VCE_COMMON_Failed, _moduleName, $"Read Message is over but not receive msg! raw message:{_currentMsg}");
  271. _status = RState.Failed;
  272. }
  273. }
  274. else
  275. {
  276. _status = RState.Failed;
  277. LOG.Write(eEvent.ERR_VCE_COMMON_Failed, _moduleName, $"Read Message is invalid: receive message {msg} and send message {_currentMsg}");
  278. }
  279. }
  280. break;
  281. }
  282. }
  283. }
  284. private void ReadMapData(string msg)
  285. {
  286. string waferinfo = "";
  287. string[] waferitems = msg.Split(',');
  288. //智能模式 可以识别叠片
  289. for (int i = 3; i < waferitems.Length - 1; ++i)
  290. {
  291. //如果包含只需要逐个检查
  292. if (waferitems[i].Contains('?'))
  293. {
  294. foreach (char j in waferitems[i])
  295. {
  296. if (j == '?')
  297. break;
  298. else
  299. waferinfo += j;
  300. }
  301. }
  302. else
  303. waferinfo += waferitems[i];
  304. }
  305. for (int i = 0; i < waferinfo.Length; ++i)
  306. {
  307. int slotnum = i;
  308. if (slotnum < 25)
  309. {
  310. switch (waferinfo[i])
  311. {
  312. case '0':
  313. WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Empty);
  314. break;
  315. case 'X':
  316. WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Normal);
  317. break;
  318. case 'C':
  319. WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Crossed);
  320. break;
  321. }
  322. }
  323. }
  324. //2进制模式
  325. //for (int i = 3; i < waferitems.Length - 1; i++)
  326. //{
  327. // //从16进制字符转义回二进制
  328. // string wafersingleinfo = Convert.ToString(Convert.ToInt32(waferitems[i], 16), 2);
  329. // if (wafersingleinfo.Length < 4)
  330. // wafersingleinfo = wafersingleinfo.PadLeft(4, '0');//补位
  331. // waferinfo = wafersingleinfo + waferinfo;//添加到数据中
  332. //}
  333. ////请将数据按照反向槽位进行解析存入到wafermanager中
  334. //for (int i = waferinfo.Length - 1; i > 0; i--)
  335. //{
  336. // int slotnum = waferinfo.Length - i - 1;
  337. // if (slotnum < 25)
  338. // {
  339. // if (waferinfo[i] == '1')
  340. // {
  341. // WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Normal);
  342. // }
  343. // else
  344. // {
  345. // WaferManager.Instance.CreateWafer(_moduleName, slotnum, WaferStatus.Empty);
  346. // }
  347. // }
  348. //}
  349. }
  350. private void Errorhandle(string msg,out string reason)
  351. {
  352. if (_matchErrorCode.IsMatch(msg))
  353. {
  354. //若是匹配
  355. //包含原因
  356. string errorcode = _matchErrorCode.Match(msg).Value;
  357. if (_ErrorCode2Reason.ContainsKey(errorcode))
  358. {
  359. reason = _ErrorCode2Reason[errorcode];
  360. }
  361. else
  362. {
  363. reason = "未找到相关Error Code";
  364. }
  365. }
  366. else
  367. {
  368. //若不匹配
  369. reason = "回复消息不符合标准格式";
  370. }
  371. }
  372. /// <summary>
  373. /// 处理新到的数据
  374. /// 利用linkedlist处理拆包 粘包情况
  375. /// </summary>
  376. /// <param name="newline">新到数据</param>
  377. private void onDataChange(string oneLineMessage)
  378. {
  379. lock (_locker)
  380. {
  381. if (string.IsNullOrEmpty(_newline))//没有CR
  382. {
  383. _lstAsciiMsgs.AddLast(oneLineMessage);//将消息添加到最后
  384. return;
  385. }
  386. string[] array = oneLineMessage.Split(_newline.ToCharArray());//按照cr分开通讯数据
  387. foreach (string text in array)
  388. {
  389. if (!string.IsNullOrEmpty(text))
  390. {
  391. _lstAsciiMsgs.AddLast(text + _newline);//存进list中等待处理
  392. }
  393. }
  394. }
  395. }
  396. public override bool HomeALL()
  397. {
  398. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Home ,Param = "ALL" };
  399. _currentMsg = _currentVceMessage.toString() + _newline ;
  400. _status = RState.Running;
  401. return _serialport.Write(_currentMsg);
  402. }
  403. public override bool Home(string axis)
  404. {
  405. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Home, Param = axis };
  406. _currentMsg = _currentVceMessage.toString() + _newline;
  407. _status = RState.Running;
  408. return _serialport.Write(_currentMsg);
  409. }
  410. public override bool CloseDoor()
  411. {
  412. if (!CheckVceStatus())
  413. return false;
  414. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.DoorClose };
  415. _currentMsg = _currentVceMessage.toString() + _newline;
  416. _status = RState.Running;
  417. return _serialport.Write(_currentMsg);
  418. }
  419. public override bool OpenDoor()
  420. {
  421. if (!CheckVceStatus())
  422. return false;
  423. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.DoorOpen };
  424. _currentMsg = _currentVceMessage.toString() + _newline;
  425. _status = RState.Running;
  426. return _serialport.Write(_currentMsg);
  427. }
  428. public override bool Load()
  429. {
  430. if (!CheckVceStatus())
  431. return false;
  432. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Load };
  433. _currentMsg = _currentVceMessage.toString() + _newline;
  434. _status = RState.Running;
  435. return _serialport.Write(_currentMsg);
  436. }
  437. public override bool UnLoad()
  438. {
  439. if (!CheckVceStatus())
  440. return false;
  441. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.UnLoad };
  442. _currentMsg = _currentVceMessage.toString() + _newline;
  443. _status = RState.Running;
  444. return _serialport.Write(_currentMsg);
  445. }
  446. public override bool Map()
  447. {
  448. if (!CheckVceStatus())
  449. return false;
  450. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Map };
  451. _currentMsg = _currentVceMessage.toString() + _newline;
  452. _status = RState.Running;
  453. return _serialport.Write(_currentMsg);
  454. }
  455. public override bool ReadMap()
  456. {
  457. if (!CheckVceStatus())
  458. return false;
  459. _currentVceMessage = new VceMessage { Head = VceMessageHead.Read, Command = VceCommand.ReadMap, Param = "S" };
  460. _currentMsg = _currentVceMessage.toString() + _newline;
  461. _status = RState.Running;
  462. _HasReceiveMsg = false;
  463. return _serialport.Write(_currentMsg);
  464. }
  465. public override bool Goto(int Targetslot)
  466. {
  467. if (!CheckVceStatus())
  468. return false;
  469. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.Goto, Param = Targetslot.ToString() };
  470. _currentMsg = _currentVceMessage.toString() + _newline;
  471. _status = RState.Running;
  472. return _serialport.Write(_currentMsg);
  473. }
  474. public override bool GotoLP()
  475. {
  476. if (!CheckVceStatus())
  477. return false;
  478. _currentVceMessage = new VceMessage { Head = VceMessageHead.Action, Command = VceCommand.GotoLP };
  479. _currentMsg = _currentVceMessage.toString() + _newline;
  480. _status = RState.Running;
  481. return _serialport.Write(_currentMsg);
  482. }
  483. }
  484. }