BrooksVCE.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using Aitex.Core.Common.DeviceData;
  6. using Aitex.Core.RT.Device;
  7. using Aitex.Core.RT.Device.Unit;
  8. using Aitex.Core.RT.Event;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.RT.OperationCenter;
  11. using Aitex.Core.RT.SCCore;
  12. using Aitex.Core.Util;
  13. using MECF.Framework.Common.Communications;
  14. using MECF.Framework.Common.Device.Bases;
  15. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
  16. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDK;
  17. using Newtonsoft.Json;
  18. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.VCE.BrooksVCE
  19. {
  20. public class BrooksVCE : SerialPortDevice, IConnection
  21. {
  22. public string Address { get { return _address; } }
  23. public bool IsConnected { get; }
  24. public bool Connect()
  25. {
  26. return true;
  27. }
  28. public bool Disconnect()
  29. {
  30. return true;
  31. }
  32. public string PortStatus { get; set; } = "Closed";
  33. private BrooksVCEConnection _connection;
  34. public BrooksVCEConnection Connection
  35. {
  36. get { return _connection; }
  37. }
  38. private R_TRIG _trigError = new R_TRIG();
  39. private R_TRIG _trigCommunicationError = new R_TRIG();
  40. private R_TRIG _trigRetryConnect = new R_TRIG();
  41. private PeriodicJob _thread;
  42. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  43. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  44. public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
  45. private object _locker = new object();
  46. private bool _enableLog;
  47. private string _scRoot;
  48. public BrooksVCE(string module, string name, string scRoot, string portName) : base(module, name)
  49. {
  50. _scRoot = scRoot;
  51. PortName = portName;
  52. }
  53. private void ResetPropertiesAndResponses()
  54. {
  55. DoorClosed = null;
  56. DoorOpened = null;
  57. IsHalted = null;
  58. foreach (var ioResponse in IOResponseList)
  59. {
  60. ioResponse.ResonseContent = null;
  61. ioResponse.ResonseRecievedTime = DateTime.Now;
  62. }
  63. }
  64. private string _address;
  65. public override bool Initialize(string portName)
  66. {
  67. base.Initialize(portName);
  68. ResetPropertiesAndResponses();
  69. if (_connection != null && _connection.IsConnected && PortName == portName)
  70. return true;
  71. if (_connection != null && _connection.IsConnected)
  72. _connection.Disconnect();
  73. PortName = portName;
  74. _address = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.DeviceAddress");
  75. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
  76. _connection = new BrooksVCEConnection(PortName);
  77. _connection.EnableLog(_enableLog);
  78. if (_connection.Connect())
  79. {
  80. PortStatus = "Open";
  81. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  82. }
  83. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  84. return true;
  85. }
  86. private bool OnTimer()
  87. {
  88. try
  89. {
  90. //return true;
  91. //_connection.MonitorTimeout();
  92. if (!_connection.IsConnected || _connection.IsCommunicationError)
  93. {
  94. lock (_locker)
  95. {
  96. _lstHandler.Clear();
  97. }
  98. _trigRetryConnect.CLK = !_connection.IsConnected;
  99. if (_trigRetryConnect.Q)
  100. {
  101. _connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
  102. if (!_connection.Connect())
  103. {
  104. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  105. }
  106. else
  107. {
  108. //_lstHandler.AddLast(new BrooksVCEQueryPinHandler(this, _deviceAddress));
  109. //_lstHandler.AddLast(new BrooksVCESetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  110. }
  111. }
  112. return true;
  113. }
  114. HandlerBase handler = null;
  115. if (!_connection.IsBusy)
  116. {
  117. lock (_locker)
  118. {
  119. if (_lstHandler.Count == 0)
  120. {
  121. foreach (var monitorHandler in _lstMonitorHandler)
  122. {
  123. _lstHandler.AddLast(monitorHandler);
  124. }
  125. }
  126. if (_lstHandler.Count > 0)
  127. {
  128. handler = _lstHandler.First.Value;
  129. _lstHandler.RemoveFirst();
  130. }
  131. }
  132. if (handler != null)
  133. {
  134. _connection.Execute(handler);
  135. }
  136. }
  137. }
  138. catch (Exception ex)
  139. {
  140. LOG.Write(ex);
  141. }
  142. return true;
  143. }
  144. internal void NoteCommonActionResult(string command)
  145. {
  146. if(command == "JD")
  147. {
  148. }
  149. else if (command == "JU")
  150. {
  151. }
  152. else if (command == "LOAD")
  153. {
  154. LoadCompleted = true;
  155. }
  156. else if (command == "LP")
  157. {
  158. LoadPosition = true;
  159. }
  160. else if (command == "PI")
  161. {
  162. RetractPosition = true;
  163. }
  164. else if (command == "PICK")
  165. {
  166. Picked = true;
  167. }
  168. else if (command == "PLACE")
  169. {
  170. Placed = true;
  171. }
  172. else if (command == "PO")
  173. {
  174. ExtendPosition = true;
  175. }
  176. else if (command == "UNLOAD")
  177. {
  178. Unloaded = true;
  179. }
  180. }
  181. internal void NoteZAxisPosition(string pos)
  182. {
  183. ZAxisPosition = pos;
  184. }
  185. internal void NoteHomed(bool homed)
  186. {
  187. Homed = homed;
  188. }
  189. internal void NoteMoveResult(string position)
  190. {
  191. MovedPosition = position;
  192. }
  193. public override void Monitor()
  194. {
  195. try
  196. {
  197. //_connection.EnableLog(_enableLog);
  198. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  199. if (_trigCommunicationError.Q)
  200. {
  201. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  202. }
  203. }
  204. catch (Exception ex)
  205. {
  206. LOG.Write(ex);
  207. }
  208. }
  209. public override void Reset()
  210. {
  211. _trigError.RST = true;
  212. _connection.SetCommunicationError(false, "");
  213. _trigCommunicationError.RST = true;
  214. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  215. _trigRetryConnect.RST = true;
  216. base.Reset();
  217. }
  218. public override bool Home(out string reason)
  219. {
  220. return base.Home(out reason);
  221. }
  222. #region Command Functions
  223. public void PerformRawCommand(string commandType)
  224. {
  225. lock (_locker)
  226. {
  227. _lstHandler.AddLast(new BrooksVCERawCommandHandler(this, commandType, ""));
  228. }
  229. }
  230. public void PerformRawCommand(string commandType, string command)
  231. {
  232. lock (_locker)
  233. {
  234. _lstHandler.AddLast(new BrooksVCERawCommandHandler(this, commandType, command));
  235. }
  236. }
  237. internal void NoteCurrentCommunicationMode(string data)
  238. {
  239. CurrentCommunicationMode = data;
  240. }
  241. public void PerformRawCommand(string commandType, string command, string comandArgument)
  242. {
  243. lock (_locker)
  244. {
  245. _lstHandler.AddLast(new BrooksVCERawCommandHandler(this, commandType,command, comandArgument));
  246. }
  247. }
  248. public void MonitorRawCommand(bool isSelected, string commandType, string command, string comandArgument)
  249. {
  250. lock (_locker)
  251. {
  252. string msg = comandArgument == null ? $"{command}\r" : $"{command} {comandArgument}\r";//??
  253. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksVCERawCommandHandler) && ((BrooksVCERawCommandHandler)handler).IsSendText(commandType, command, comandArgument));
  254. if (isSelected)
  255. {
  256. if (!existHandlers.Any())
  257. _lstMonitorHandler.AddFirst(new BrooksVCERawCommandHandler(this, commandType,command, comandArgument));
  258. }
  259. else
  260. {
  261. if (existHandlers.Any())
  262. {
  263. _lstMonitorHandler.Remove(existHandlers.First());
  264. }
  265. }
  266. }
  267. }
  268. internal void NoteCurrentACEStatus(string data)
  269. {
  270. throw new NotImplementedException();
  271. }
  272. internal void NoteCurrentErrorStatus(string data)
  273. {
  274. CurrentErrorStatus = data;
  275. }
  276. internal void NoteIsLoadPositon(string data)
  277. {
  278. InLoadPositon = data.Last() == 'Y';
  279. }
  280. public void RequestArmRPositon(bool isSelected, string axisPosition)
  281. {
  282. lock (_locker)
  283. {
  284. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestArmRPositonHandler) && handler.SendText.Contains(axisPosition));
  285. if (isSelected)
  286. {
  287. if (!existHandlers.Any())
  288. _lstMonitorHandler.AddFirst(new BrooksACERequestArmRPositonHandler(this, axisPosition));
  289. }
  290. else
  291. {
  292. if (existHandlers.Any())
  293. {
  294. _lstMonitorHandler.Remove(existHandlers.First());
  295. }
  296. }
  297. }
  298. }
  299. internal void NoteCurrentMappingInfo(string data)
  300. {
  301. CurrentMappingInfo = data;
  302. }
  303. internal void NoteIsPlatformPosition(string data)
  304. {
  305. InPlatformPosition = data.Last() == 'Y';
  306. }
  307. public void RequestArmZPositon(bool isSelected, string axisPosition)
  308. {
  309. lock (_locker)
  310. {
  311. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestArmZPositonHandler) && handler.SendText.Contains(axisPosition));
  312. if (isSelected)
  313. {
  314. if (!existHandlers.Any())
  315. _lstMonitorHandler.AddFirst(new BrooksACERequestArmZPositonHandler(this, axisPosition));
  316. }
  317. else
  318. {
  319. if (existHandlers.Any())
  320. {
  321. _lstMonitorHandler.Remove(existHandlers.First());
  322. }
  323. }
  324. }
  325. }
  326. public void RequestCommunicationMode(bool isSelected)
  327. {
  328. lock (_locker)
  329. {
  330. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestCommModeHandler));
  331. if (isSelected)
  332. {
  333. if (!existHandlers.Any())
  334. _lstMonitorHandler.AddFirst(new BrooksACERequestCommModeHandler(this));
  335. }
  336. else
  337. {
  338. if (existHandlers.Any())
  339. {
  340. _lstMonitorHandler.Remove(existHandlers.First());
  341. }
  342. }
  343. }
  344. }
  345. public void RequestErrorStatus(bool isSelected)
  346. {
  347. lock (_locker)
  348. {
  349. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestErrorStatusHandler));
  350. if (isSelected)
  351. {
  352. if (!existHandlers.Any())
  353. _lstMonitorHandler.AddFirst(new BrooksACERequestErrorStatusHandler(this));
  354. }
  355. else
  356. {
  357. if (existHandlers.Any())
  358. {
  359. _lstMonitorHandler.Remove(existHandlers.First());
  360. }
  361. }
  362. }
  363. }
  364. public void RequestVCEStatus(bool isSelected)
  365. {
  366. lock (_locker)
  367. {
  368. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestVCEStatusHandler));
  369. if (isSelected)
  370. {
  371. if (!existHandlers.Any())
  372. _lstMonitorHandler.AddFirst(new BrooksACERequestVCEStatusHandler(this));
  373. }
  374. else
  375. {
  376. if (existHandlers.Any())
  377. {
  378. _lstMonitorHandler.Remove(existHandlers.First());
  379. }
  380. }
  381. }
  382. }
  383. public void RequestLoadPositon(bool isSelected)
  384. {
  385. lock (_locker)
  386. {
  387. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestLoadPositionHandler));
  388. if (isSelected)
  389. {
  390. if (!existHandlers.Any())
  391. _lstMonitorHandler.AddFirst(new BrooksACERequestLoadPositionHandler(this));
  392. }
  393. else
  394. {
  395. if (existHandlers.Any())
  396. {
  397. _lstMonitorHandler.Remove(existHandlers.First());
  398. }
  399. }
  400. }
  401. }
  402. public void RequestMappingInfo(bool isSelected)
  403. {
  404. lock (_locker)
  405. {
  406. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestMappingInfoHandler));
  407. if (isSelected)
  408. {
  409. if (!existHandlers.Any())
  410. _lstMonitorHandler.AddFirst(new BrooksACERequestMappingInfoHandler(this));
  411. }
  412. else
  413. {
  414. if (existHandlers.Any())
  415. {
  416. _lstMonitorHandler.Remove(existHandlers.First());
  417. }
  418. }
  419. }
  420. }
  421. public void RequestPlatformPosition(bool isSelected)
  422. {
  423. lock (_locker)
  424. {
  425. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestPlatformPositionHandler));
  426. if (isSelected)
  427. {
  428. if (!existHandlers.Any())
  429. _lstMonitorHandler.AddFirst(new BrooksACERequestPlatformPositionHandler(this));
  430. }
  431. else
  432. {
  433. if (existHandlers.Any())
  434. {
  435. _lstMonitorHandler.Remove(existHandlers.First());
  436. }
  437. }
  438. }
  439. }
  440. public void Abort()
  441. {
  442. lock (_locker)
  443. {
  444. _lstHandler.AddLast(new BrooksVCEAbortHandler(this));
  445. }
  446. }
  447. public void CloseDoor()
  448. {
  449. lock (_locker)
  450. {
  451. _lstHandler.AddLast(new BrooksVCECloseDoorHandler(this));
  452. }
  453. }
  454. public void OpenDoor()
  455. {
  456. lock (_locker)
  457. {
  458. _lstHandler.AddLast(new BrooksVCEOpenDoorHandler(this));
  459. }
  460. }
  461. public void JogDown()
  462. {
  463. lock (_locker)
  464. {
  465. _lstHandler.AddLast(new BrooksVCECommonActionHandler(this, "JD"));
  466. }
  467. }
  468. public void JogUp()
  469. {
  470. lock (_locker)
  471. {
  472. _lstHandler.AddLast(new BrooksVCECommonActionHandler(this, "JU"));
  473. }
  474. }
  475. public void Load()
  476. {
  477. lock (_locker)
  478. {
  479. _lstHandler.AddLast(new BrooksVCECommonActionHandler(this, "LOAD"));
  480. }
  481. }
  482. public void MoveToLoadPositon()
  483. {
  484. lock (_locker)
  485. {
  486. _lstHandler.AddLast(new BrooksVCECommonActionHandler(this, "LP"));
  487. }
  488. }
  489. public void MapCassette()
  490. {
  491. lock (_locker)
  492. {
  493. _lstHandler.AddLast(new BrooksVCECommonActionHandler(this, "LP"));
  494. }
  495. }
  496. public void Move(string axis, string type, string value)
  497. {
  498. lock (_locker)
  499. {
  500. _lstHandler.AddLast(new BrooksVCEMoveHandler(this, axis, type, value));
  501. }
  502. }
  503. public void RoateRAxisToRetractPosition()
  504. {
  505. lock (_locker)
  506. {
  507. _lstHandler.AddLast(new BrooksVCECommonActionHandler(this, "PI"));
  508. }
  509. }
  510. public void RoateRAxisToExtendPosition()
  511. {
  512. lock (_locker)
  513. {
  514. _lstHandler.AddLast(new BrooksVCECommonActionHandler(this, "PO"));
  515. }
  516. }
  517. public void Pick()
  518. {
  519. lock (_locker)
  520. {
  521. _lstHandler.AddLast(new BrooksVCECommonActionHandler(this, "PICK"));
  522. }
  523. }
  524. public void Place()
  525. {
  526. lock (_locker)
  527. {
  528. _lstHandler.AddLast(new BrooksVCECommonActionHandler(this, "PLACE"));
  529. }
  530. }
  531. public void Unload()
  532. {
  533. lock (_locker)
  534. {
  535. _lstHandler.AddLast(new BrooksVCECommonActionHandler(this, "UNLOAD"));
  536. }
  537. }
  538. public void GotoZ(string direction)
  539. {
  540. lock (_locker)
  541. {
  542. _lstHandler.AddLast(new BrooksVCEGotoZHandler(this, direction));
  543. }
  544. }
  545. public void Home(string axis)
  546. {
  547. lock (_locker)
  548. {
  549. _lstHandler.AddLast(new BrooksVCEHomeHandler(this,axis));
  550. }
  551. }
  552. public void SetCommunicationMode(string commMode)
  553. {
  554. lock (_locker)
  555. {
  556. _lstHandler.AddLast(new BrooksVCESetCommunicationModeHandler(this, commMode));
  557. }
  558. }
  559. #endregion
  560. #region Properties
  561. public string DoorClosed { get; private set; }
  562. public string DoorOpened { get; private set; }
  563. public string IsHalted { get; private set; }
  564. public string Error { get; private set; }
  565. public string ArmRPosition { get; private set; }
  566. public string CommunicationMode { get; private set; }
  567. public string ZAxisPosition { get; private set; }
  568. public bool Homed { get; private set; }
  569. public bool LoadCompleted { get; private set; }
  570. public bool LoadPosition { get; private set; }
  571. public bool RetractPosition { get; private set; }
  572. public bool Picked { get; private set; }
  573. public bool Placed { get; private set; }
  574. public bool ExtendPosition { get; private set; }
  575. public bool Unloaded { get; private set; }
  576. public string MovedPosition { get; private set; }
  577. public string ArmZPosition { get; private set; }
  578. public string CurrentCommunicationMode { get; private set; }
  579. public string CurrentErrorStatus { get; private set; }
  580. public bool InLoadPositon { get; private set; }
  581. public string CurrentMappingInfo { get; private set; }
  582. public bool InPlatformPosition { get; private set; }
  583. #endregion
  584. #region Note Functions
  585. private R_TRIG _trigWarningMessage = new R_TRIG();
  586. public void NoteError(string reason)
  587. {
  588. if (reason != null)
  589. {
  590. _trigWarningMessage.CLK = true;
  591. if (_trigWarningMessage.Q)
  592. {
  593. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  594. }
  595. Error = reason;
  596. }
  597. else
  598. {
  599. Error = null;
  600. }
  601. }
  602. internal void NoteDoorClosed(bool isClosed)
  603. {
  604. DoorClosed = isClosed ? "true" : "false";
  605. DoorOpened = isClosed ? "false" : "true";
  606. }
  607. internal void NoteIsHalted(bool isHalted)
  608. {
  609. IsHalted = isHalted ? "true" : "false";
  610. }
  611. internal void NoteRawCommandInfo(string commandType, string command, string data)
  612. {
  613. var curIOResponse = IOResponseList.Find(res => res.SourceCommandName == command);
  614. if (curIOResponse != null)
  615. {
  616. IOResponseList.Remove(curIOResponse);
  617. }
  618. IOResponseList.Add(new IOResponse() { SourceCommandType = commandType,SourceCommand = command, ResonseContent = data, ResonseRecievedTime = DateTime.Now });
  619. }
  620. internal void NoteArmRPositon(string armRPosition)
  621. {
  622. ArmRPosition = armRPosition;
  623. }
  624. internal void NoteArmZPositon(string armPosition)
  625. {
  626. ArmZPosition = armPosition;
  627. }
  628. internal void NoteCommunicationMode(string mode)
  629. {
  630. CommunicationMode = mode;
  631. }
  632. #endregion
  633. }
  634. }