| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139 | using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;using CyberX8_Core;using CyberX8_RT.Modules;using MECF.Framework.Common.CommonData;using MECF.Framework.Common.Equipment;using MECF.Framework.Common.SubstrateTrackings;using Aitex.Sorter.Common;using Aitex.Core.Common;using Aitex.Core.RT.SCCore;using Aitex.Core.RT.Log;using Aitex.Core.Util;using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot;using CyberX8_RT.Devices.YASKAWA;using System.Collections.Concurrent;using System.Diagnostics;using System.Data.SqlTypes;using System.Windows.Media.Media3D;using System.ComponentModel;using MECF.Framework.Common.Beckhoff.Station;using System.Runtime.Remoting.Contexts;using System.Windows.Markup;using CyberX8_RT.Modules.LPs;using MECF.Framework.Common.ToolLayout;namespace CyberX8_RT.Devices.EFEM{    public class SunWayRobot : EfemBase    {        #region 常量        private const string NONE = "None";        #endregion        private RState _status;        private bool _IsHomed;        private bool _bIsUnloadClamp;        private RobotMoveInfo _robotMoveInfo = new RobotMoveInfo();        private readonly SignalTower _signalT = new SignalTower();        private readonly AsyncSocket _socket;        private EfemMessage _currentMessage;        private string _error;        private bool _LiftIsUp = false;        private bool _LiftIsDown = false;        private R_TRIG _busyTrig = new R_TRIG();        private Stopwatch _busyWatch = new Stopwatch();        private Dictionary<string, int> _moduleStationNumberDictionary = new Dictionary<string, int>();        private Dictionary<Hand, string> _armString = new Dictionary<Hand, string>        {            [Hand.Blade1] = "A",            [Hand.Blade2] = "B"        };        public override RState Status        {            get            {                _busyTrig.CLK = _status == RState.Running;                if (_busyTrig.Q)                {                    _busyWatch.Restart();                }                else if (_busyTrig.M)                {                    int timeOut = 5000;                    if (                        _currentMessage.Operation == EfemOperation.Home ||                        _currentMessage.Operation == EfemOperation.Map ||                        _currentMessage.Operation == EfemOperation.Pick ||                         _currentMessage.Operation == EfemOperation.Place)                    {                        timeOut = 30000;                    }                    if (_busyWatch.ElapsedMilliseconds > timeOut)                    {                        _busyWatch.Stop();                        _status = RState.Timeout;                    }                }                return _status;            }        }        public override bool IsHomed { get { return _IsHomed; } }        public override RobotMoveInfo TMRobotMoveInfo { get { return _robotMoveInfo; } }        public override bool LiftIsUp { get { return _LiftIsUp; } }        public override bool LiftIsDown { get { return _LiftIsDown; } }        private BlockingCollection<RobotAnimationData> blockingCollection = new BlockingCollection<RobotAnimationData>();        private string _address = "";        public SunWayRobot()        {            _socket = new AsyncSocket("","",DataType.Ascii,true);            try            {                _address = SC.GetStringValue($"EFEM.IPAddress");                _socket.Connect(_address);                _socket.OnConnect += Socket_OnConnect;                _socket.OnDataChanged += OnReceiveMessage;                _socket.OnErrorHappened += OnErrorHappen;            }            catch(Exception ex)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, "EFEM", ex.Message);            }            _status = RState.Init;            _IsHomed = false;            _busyTrig.RST = true;            InitializeModuleStation(ModuleName.LP1);            InitializeModuleStation(ModuleName.LP2);            InitializeModuleStation(ModuleName.LP3);            InitializeModuleStation(ModuleName.Dummy1);            InitializeModuleStation(ModuleName.Dummy2);            InitializeModuleStation(ModuleName.Aligner1);            InitializeModuleStation(ModuleName.SRD1);            InitializeModuleStation(ModuleName.SRD2);            InitializeModuleStation(ModuleName.PUF1);            CarrierManager.Instance.SubscribeLocation(ModuleName.LP1.ToString(), 1);            CarrierManager.Instance.SubscribeLocation(ModuleName.LP2.ToString(), 1);            CarrierManager.Instance.SubscribeLocation(ModuleName.LP3.ToString(), 1);            WaferManager.Instance.SubscribeLocation(ModuleName.EfemRobot, 2);            WaferManager.Instance.SubscribeLocation(ModuleName.Aligner1, 1);            WaferManager.Instance.SubscribeLocation(ModuleName.LP1, SC.GetValue<int>("EFEM.LoadPort.SlotNumber"));            WaferManager.Instance.SubscribeLocation(ModuleName.LP2, SC.GetValue<int>("EFEM.LoadPort.SlotNumber"));            WaferManager.Instance.SubscribeLocation(ModuleName.LP3, SC.GetValue<int>("EFEM.LoadPort.SlotNumber"));            Task.Run(() =>            {                foreach (var data in blockingCollection.GetConsumingEnumerable())                {                    _robotMoveInfo.Action = data.Action;                    _robotMoveInfo.ArmTarget = data.Hand == Hand.Blade1 ? RobotArm.ArmA : (data.Hand == Hand.Both ? RobotArm.Both : RobotArm.ArmB);                    _robotMoveInfo.BladeTarget = $"{_robotMoveInfo.ArmTarget}.{data.Target}";                    System.Threading.Thread.Sleep(600);                }            });        }        /// <summary>        /// 初始化Module Station        /// </summary>        /// <param name="module"></param>        private void InitializeModuleStation(ModuleName module)        {            int cassete200Station = SC.GetValue<int>($"EFEM.{module}.Cassete200Station");            _moduleStationNumberDictionary[$"{module}_200"] = cassete200Station;            int cassete150Station = SC.GetValue<int>($"EFEM.{module}.Cassete150Station");            _moduleStationNumberDictionary[$"{module}_150"] = cassete150Station;            int cassete100Station = SC.GetValue<int>($"EFEM.{module}.Cassete100Station");            _moduleStationNumberDictionary[$"{module}_100"]=cassete100Station;        }        private void Socket_OnConnect()        {            Singleton<RouteManager>.Instance.EFEM.PostMsg(EfemEntity.MSG.CommReady);            LOG.WriteLog(eEvent.EV_EFEM_COMMON_INFO, Module.ToString(), $"connect {_address} success");        }        private void OnErrorHappen(ErrorEventArgs args)        {            _status = RState.Failed;            if (_error != args.Reason)            {                _error = args.Reason;                LOG.Write(eEvent.ERR_EFEM_COMMON_FAILED, Module, $"{Module} {_error}");            }            Singleton<RouteManager>.Instance.EFEM.PostMsg(EfemEntity.MSG.Error);        }        public override void Monitor()        {        }        public override void Terminate()        {        }        public override void Reset()        {            _status = RState.End;            _currentMessage = null;        }        public override void SetOnline(bool online)        {        }        public override void SetOnline(ModuleName mod, bool online)        {        }        public override void SetBusy(ModuleName mod, bool online)        {            _status = RState.Running;        }        public override bool HomeAll()        {            if (_status == RState.Running&&_currentMessage!=null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot home All");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)             {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false ;            }            _status = RState.Running;            string data = "HOME ALL\r";            _currentMessage =new EfemMessage(){                Operation= EfemOperation.Home,                Module=ModuleName.EFEM            };            //SetRobotMovingInfo(RobotAction.Homing, Hand.Both, ModuleName.EFEM);            return WriteCommand(data);        }        public override bool Home(ModuleName mod)        {            if (_status == RState.Running && _currentMessage!= null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot home {mod}");                return false;            }            if (mod != ModuleName.Aligner1&&mod!=ModuleName.EfemRobot)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{mod} can not support home method");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            if(mod == ModuleName.EfemRobot) SetRobotMovingInfo(RobotAction.Homing, Hand.Blade1, ModuleName.EfemRobot);            _status = RState.Running;            string cmd = "Home ALL\r";            if (mod == ModuleName.Aligner1)            {                cmd = "ALIGNER HOME\r";            }            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.Home,                Module = ModuleName.Aligner1            };            return WriteCommand(cmd);        }        public override bool OriginalSearch(ModuleName mod)        {            return true;        }        public override bool CheckWaferPresence()        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot check wafer");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            if (!CheckEfemStatus())                return false;            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.StateTrack,                Module = ModuleName.EfemRobot            };            _status = RState.Running;            string cmd = $"RQ LOAD {_armString[Hand.Blade1]}\r";            return WriteCommand(cmd);        }        public override string GetWaferPresence()        {            return "";        }        public override bool Halt()        {            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.Abort,                Module = ModuleName.EfemRobot            };            SetRobotMovingInfo(RobotAction.None, Hand.Both, ModuleName.EFEM);            string cmd = "HALT\r";            return WriteCommand(cmd);        }        public override bool ClearError()        {            return true;        }        public override bool CloseBuzzer()        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support {EfemConstant.OperationString[EfemOperation.TurnOffBuzzer]}");            return false;        }        public override bool PickExtend(ModuleName chamber, int slot, Hand hand)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support {EfemConstant.OperationString[EfemOperation.Extend]}");            return false;        }        public override bool PickRetract(ModuleName chamber, int slot, Hand hand)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support {EfemConstant.OperationString[EfemOperation.Extend]}");            return false;        }        public override bool PlaceExtend(ModuleName chamber, int slot, Hand hand)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support {EfemConstant.OperationString[EfemOperation.Extend]}");            return false;        }        public override bool PlaceRetract(ModuleName chamber, int slot, Hand hand)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support {EfemConstant.OperationString[EfemOperation.Extend]}");            return false;        }        public override bool Pick(ModuleName station, int slot, Hand hand)        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot pick");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }                        //判断Loadport的门是否打开            //if (ModuleHelper.IsLoadPort(station) && !GetLoadPort(station).IsDoorOpened)            //{            //    LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{station}'s Door is Closed, Cannot Execute Pick Action");            //    return false;            //}            if (!CheckEfemStatus())                return false;            int waferSize = GetModuleSlotWaferSize(station,slot);            if (waferSize == 0)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{station} slot {slot} wafer size is 0, Cannot Execute Pick Action");                return false;            }            string strModuleWaferSize = $"{station}_{waferSize}";            if (!_moduleStationNumberDictionary.ContainsKey(strModuleWaferSize))            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{station}'s number is not exist, Cannot Execute Pick Action");                return false;            }            int stationNumber= _moduleStationNumberDictionary[strModuleWaferSize];            string cmd = $"PICK {stationNumber} SLOT {slot+1} ARM {_armString[hand]}\r";            if(ModuleHelper.IsPUF(station)||ModuleHelper.IsAligner(station)||ModuleHelper.IsSRD(station))            {                cmd = $"PICK {stationNumber} SLOT 1 ARM {_armString[hand]}\r";            }            _currentMessage = new EfemMessage()            {                Operation=EfemOperation.Pick            };            _status = RState.Running;            SetRobotMovingInfo(RobotAction.Picking, hand, station);            return WriteCommand(cmd);        }        private Loadport GetLoadPort(ModuleName station)        {            LoadPortModule loadPortModule = Singleton<RouteManager>.Instance.EFEM.GetLoadportModule(station - ModuleName.LP1);            return loadPortModule.LPDevice;        }        public override bool Place(ModuleName station, int slot, Hand hand)        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot place");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            //判断Loadport的门是否打开            //if (ModuleHelper.IsLoadPort(station) && !GetLoadPort(station).IsDoorOpened)            //{            //    LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{station}'s Door is Closed, Cannot Execute Pick Action");            //    return false;            //}            if (!CheckEfemStatus())                return false;            int waferSize = GetModuleSlotWaferSize(ModuleName.EfemRobot, (int)hand);            if (waferSize == 0)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{station} slot {slot} wafer size is 0, Cannot Execute Place Action");                return false;            }            string strModuleWaferSize = $"{station}_{waferSize}";            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.Place            };            _status = RState.Running;            int stationNumber = _moduleStationNumberDictionary[strModuleWaferSize];            string cmd = $"PLACE {stationNumber} SLOT {slot+1} ARM {_armString[hand]}\r";            if (ModuleHelper.IsPUF(station)||ModuleHelper.IsSRD(station))            {                cmd = $"PLACE {stationNumber} SLOT 1 ARM {_armString[hand]}\r";            }            if (ModuleHelper.IsAligner(station))            {                cmd = $"PLACE {stationNumber} PHOME\r";            }            SetRobotMovingInfo(RobotAction.Placing, hand, station);            return WriteCommand(cmd);        }        public override bool Goto(ModuleName station, Hand hand, string updown = "UP")        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot goto");                return false;            }            if (!CheckEfemStatus())                return false;            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.Goto            };             int waferSize = GetModuleNameWaferSize(station);            if (waferSize == 0)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"wafersize is 0");                return false;            }            string strModuleWaferSize = $"{station}_{waferSize}";            int stationNumber = _moduleStationNumberDictionary[strModuleWaferSize];            string cmd = $"GOTO N {stationNumber} R EX Z {updown} ARM SLOT 1 {_armString[hand]}\r";            _status = RState.Running;            return WriteCommand(cmd);        }        public override bool Grip(Hand blade, bool isGrip)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support {EfemConstant.OperationString[EfemOperation.Grip]}");            return false;        }        public override bool GotoMap(ModuleName mod,Hand hand,string extend="EX")        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot goto map");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            if (!CheckEfemStatus())                return false;             int waferSize = GetModuleNameWaferSize(mod);            if (waferSize == 0)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"wafersize is 0");                return false;            }            string strModuleWaferSize = $"{mod}_{waferSize}";            int stationNumber = _moduleStationNumberDictionary[strModuleWaferSize];            string cmd = $"GOTO N {stationNumber} MAP {extend} ARM {_armString[hand]}\r";            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.Goto,                Module= mod            };            _status = RState.Running;            return WriteCommand(cmd);        }        public override bool Map(ModuleName mod)        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot map");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            if (!CheckEfemStatus())                return false;            int waferSize = GetModuleNameWaferSize(mod);            if (waferSize == 0)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"wafersize is 0");                return false;            }            string strModuleWaferSize = $"{mod}_{waferSize}";            int stationNumber = _moduleStationNumberDictionary[strModuleWaferSize];            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.Map,                Module = mod            };            string cmd = $"MAP {stationNumber} ARM {_armString[Hand.Blade1]}\r";            _status = RState.Running;            return WriteCommand(cmd);        }        public override bool RequestMapResult(ModuleName mod)        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot request map");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            if (!CheckEfemStatus())                return false;            int waferSize = GetModuleNameWaferSize(mod);            if (waferSize == 0)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"wafersize is 0");                return false;            }            string strModuleWaferSize = $"{mod}_{waferSize}";            int stationNumber = _moduleStationNumberDictionary[strModuleWaferSize];            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.RequestMapResult,                Module = mod            };            string cmd = $"RSR {stationNumber} ARM {_armString[Hand.Blade1]}\r";            _status = RState.Running;            return WriteCommand(cmd);        }        public override bool Vacuum(ModuleName mod,bool VacuumState)        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot vacuum");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.Vacuum,                Module = mod            };            _status = RState.Running;            string strVacuum = VacuumState ? "ON" : "OFF";            string cmd = "";            if (ModuleName.Aligner1 == mod)            {                 cmd = $"ALIGNER VAC {strVacuum}\r";            }            else            {                 cmd = $"VAC {strVacuum} ARM {_armString[Hand.Blade1]}\r";            }            return WriteCommand(cmd);        }        public override bool GetWaferSize(ModuleName mod)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support GetWaferSize");            return false;        }        public override bool SetWaferSize(ModuleName mod, int WaferSize)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support SetWaferSize");            return false;        }        public override bool SetPinUp(ModuleName mod)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support {EfemConstant.OperationString[EfemOperation.Lift]}");            return false;        }        public override bool SetPinDown(ModuleName mod)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support {EfemConstant.OperationString[EfemOperation.Lift]}");            return false;        }        public override bool SetAlignWaferSize()        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot Align Wafer Size");                return false;            }            if (!CheckEfemStatus())                return false;            if (!WaferManager.Instance.CheckHasWafer(ModuleName.EfemRobot, 0))            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{ModuleName.EfemRobot} has no wafer, Cannot Execute Set aligner wafersize");                return false;            }            int waferSize = GetModuleSlotWaferSize(ModuleName.EfemRobot, 0);            if (waferSize == 0)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{ModuleName.EfemRobot} wafer size is 0, Cannot Execute Set aligner wafersize");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.AlignWaferSize            };            string cmd = $"SET ALIGNER WAFERSIZE {waferSize}\r";            _status = RState.Running;            return WriteCommand(cmd);        }        public override bool SetAlignFlatType(int flatType)        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot Align flat type");                return false;            }            if (!CheckEfemStatus())                return false;            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.AlignFlatType            };            string cmd = $"SET ALIGNER WAFERTYPE {flatType}\r";            _status = RState.Running;            return WriteCommand(cmd);        }        public override bool SetAlignDistance()        {            int distance200 = SC.GetValue<int>("EFEM.Aligner1.Aligner200Distance");            int distance150 = SC.GetValue<int>("EFEM.Aligner1.Aligner150Distance");            int distance100 = SC.GetValue<int>("EFEM.Aligner1.Aligner100Distance");            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot Align flat type");                return false;            }            if (!CheckEfemStatus())                return false;            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            if (!WaferManager.Instance.CheckHasWafer(ModuleName.EfemRobot, 0))            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{ModuleName.EfemRobot} has no wafer, Cannot Execute Set aligner wafersize");                return false;            }            int waferSize = GetModuleSlotWaferSize(ModuleName.EfemRobot, 0);            if (waferSize == 0)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{ModuleName.EfemRobot} wafer size is 0, Cannot Execute Set aligner wafersize");                return false;            }            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.AlignDistance            };            int distance = distance200;            if (waferSize == (int)WaferSize.WS6)            {                distance = distance150;            }            else if (waferSize == (int)WaferSize.WS4)            {                distance = distance100;            }            string cmd = $"SET ALIGNER WAFERDISTANCE {distance}\r";            _status = RState.Running;            return WriteCommand(cmd);        }        public override bool SetAlignAngle(ModuleName mod, double angle)        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot Align Angle");                return false;            }            if (!CheckEfemStatus())                return false;            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.Align,            };            string cmd = $"SET ALIGNER POSTPOS 1 POS {angle*1000}\r";            _status = RState.Running;            return WriteCommand(cmd);        }        public override bool SetRobotSpeed(ModuleName mod, int speed)        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot Speed");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.Speed            };            string cmd = $"SET ACTION SPEEDS {speed}\r";            _status = RState.Running;            return WriteCommand(cmd);        }        public override bool RobotPowerOn(ModuleName mod, bool Status)        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot PowerOn");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            string status = Status? "ON":"OFF";            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.PowerOn            };            string cmd = $"SET SERVOS {status}\r";            _status = RState.Running;            return WriteCommand(cmd);        }        public override bool Align(ModuleName mod, double angle, float delayTime, WaferSize size)        {            if (_status == RState.Running && _currentMessage != null)            {                LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"Current Msg {_currentMessage.Operation} is Running,cannot Align");                return false;            }            //判断socket是否链接            if (!_socket.IsConnected)            {                LOG.WriteLog(eEvent.ERROR_EFEM_COMMUNICATION, Module.ToString(), $"Socket is not Conntected");                return false;            }            if (!CheckEfemStatus())                return false;            _currentMessage = new EfemMessage()            {                Operation = EfemOperation.Align            };            _status = RState.Running;            string cmd = "ALIGNER ALGN\r";            return WriteCommand(cmd);        }        public override bool SetLamp(LightType light, LightStatus status)        {            return false;        }        public override bool Load(ModuleName mod)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support Load");            return false;        }        public override bool Unload(ModuleName mod)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support Unload");            return false;        }        public override bool ReadCarrierId(ModuleName mod)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support ReadCarrierId");            return false;        }        public override bool WriteCarrierId(ModuleName mod, string id)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support WriteCarrierId");            return false;        }        public override bool ReadTagData(ModuleName mod)        {            return ReadCarrierId(mod);        }        public override bool WriteTagData(ModuleName mod, string tagData)        {            return WriteCarrierId(mod, tagData);        }        public override bool Dock(ModuleName mod)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support Dock");            return false;        }        public override bool Undock(ModuleName mod)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support Undock");            return false;        }        public override bool Clamp(ModuleName mod, bool isUnloadClamp)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support Clamp");            return false;        }        public override bool Unclamp(ModuleName mod)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support Unclamp");            return false;        }        public override bool SetThickness(ModuleName mod, string thickness)        {            LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"System cannot support {EfemConstant.OperationString[EfemOperation.SetThickness]}");            return false;        }        public override void SetRobotMovingInfo(RobotAction action, Hand hand, ModuleName target)        {            blockingCollection.Add(new RobotAnimationData(action, hand, target));        }        private void OnReceiveMessage(string msg)        {            LOG.WriteLog(eEvent.EV_EFEM_COMMON_INFO, Module.ToString(), msg.Replace("\r",""));            if (_currentMessage == null)            {                return;            }            if(msg.StartsWith("_ERR"))            {                string error = msg.Replace("_ERR", "").Trim();                OnErrorOccurred(error);                _currentMessage = null;                SetRobotMovingInfo(RobotAction.None, Hand.Both, ModuleName.EFEM);                return;            }            else if(msg.StartsWith("_RDY"))            {                _status = RState.End;                switch (_currentMessage.Operation)                {                    case EfemOperation.Home:                        SetRobotMovingInfo(RobotAction.Homing, Hand.Both, ModuleName.EFEM);                        break;                    case EfemOperation.Map:                    case EfemOperation.GotoMap:                        if (ModuleHelper.IsLoadPort(_currentMessage.Module))                        {                            GetLoadPort(_currentMessage.Module).OnMaped();                        }                        break;                    case EfemOperation.Pick:                    case EfemOperation.Place:                        SetRobotMovingInfo(RobotAction.None, Hand.Both, ModuleName.EfemRobot);                        break;                }            }            else            {                switch (_currentMessage.Operation)                {                    case EfemOperation.StateTrack:                        DealStateTrack(msg);                        break;                    case EfemOperation.RequestMapResult:                        DealMapResult(msg);                        break;                }                _status = RState.End;                _currentMessage = null;            }        }        /// <summary>        /// 处理StateTrack结果        /// </summary>        /// <param name="msg"></param>        private void DealStateTrack(string msg)        {            if (msg.StartsWith("LOAD"))            {                string[] strAry = msg.Trim().Split(' ');                if (strAry.Length >= 2)                {                    bool hasWafer = strAry[2] == "ON" ? true : false;                    if (strAry[1] == _armString[Hand.Blade1])                    {                        if (WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, 0)&& hasWafer)                        {                            WaferManager.Instance.CreateWafer(ModuleName.EfemRobot, 0, WaferStatus.Normal);                        }                    }                    else if (strAry[1] == _armString[Hand.Blade2])                    {                        if (WaferManager.Instance.CheckNoWafer(ModuleName.EfemRobot, 1) && hasWafer)                        {                            WaferManager.Instance.CreateWafer(ModuleName.EfemRobot, 1, WaferStatus.Normal);                        }                    }                }            }        }        /// <summary>        /// 处理Map结果        /// </summary>        /// <param name="msg"></param>        private void DealMapResult(string msg)        {            if (_currentMessage.Operation != EfemOperation.RequestMapResult)            {                return;            }            if(!msg.StartsWith("MAP"))            {                return;            }            string[] sWaferInfo = msg.Split(new char[] { ' ', '\r', '\n' });            if (sWaferInfo.Length <= 2)            {                return;            }            int slotMap = SC.GetValue<int>("EFEM.LoadPort.SlotNumber");            WaferType waferType = WaferType.Production;            if (ModuleHelper.IsDummy(_currentMessage.Module))            {                DummyCassetteItem item = DummyCasseteItemManager.Instance.GetDummyCassetteItem(_currentMessage.Module.ToString());                if (item != null)                {                    slotMap = item.MaxNumberOfSlots;                }                waferType = WaferType.Assit;            }            int waferSize = GetModuleNameWaferSize(_currentMessage.Module);            //Map 结果(1 1 1 1 1 1 1 ...0)            int startIndex = 1;            int count = slotMap >= sWaferInfo.Length-startIndex? sWaferInfo.Length-startIndex : slotMap;            bool result = true;            for (int index = startIndex; index <= count; index++)            {                string strState = sWaferInfo[index];                int waferState = 0;                //合理的映射到内部支持的叠片/交叉片                if (strState == "1") waferState = (int)WaferStatus.Normal;                else if (strState == "2") waferState = (int)WaferStatus.Double;                else if (strState == "3") waferState = (int)WaferStatus.Crossed;                else if (strState == "5") waferState = (int)WaferStatus.Thin;                else if(strState=="?") waferState=(int)WaferStatus.Unknown;                WaferStatus st = (WaferStatus)waferState;                if (st != WaferStatus.Empty)                {                    WaferManager.Instance.CreateWafer(_currentMessage.Module, index-startIndex, st,(WaferSize)waferSize,waferType);                    if (st != WaferStatus.Normal)                    {                        result = false;                    }                }                else                {                    WaferManager.Instance.DeleteWafer(_currentMessage.Module, index-startIndex);                }            }            if (!result)            {                LOG.WriteLog(eEvent.ERROR_EFEM_ERROR_WAFER, Module.ToString(), $"{_currentMessage.Module} slot map failed {msg}");            }            if (ModuleHelper.IsLoadPort(_currentMessage.Module))            {                GetLoadPort(_currentMessage.Module).UpdateMapResult(result);            }        }        private void OnErrorOccurred(string errorCode)        {            _status = RState.Failed;            string description = errorCode;            if (SumWayRobotErrorCode.ErrorCodeDescription.ContainsKey(errorCode))            {                description=SumWayRobotErrorCode.ErrorCodeDescription[errorCode];            }            Singleton<RouteManager>.Instance.EFEM.PostMsg(EfemEntity.MSG.Error, description);            if (_currentMessage != null)            {                LOG.Write(eEvent.ERR_EFEM_COMMON_FAILED, ModuleName.EFEM, $"current operation {_currentMessage.Operation} failed,{description}");            }            _currentMessage=null;        }        private bool WriteCommand(string cmd)        {            LOG.WriteLog(eEvent.EV_EFEM_COMMON_INFO, Module.ToString(), cmd.Replace("\r", ""));            return _socket.Write(cmd);        }        /// <summary>        /// 获取模块Wafer尺寸        /// </summary>        /// <param name="moduleName"></param>        /// <returns></returns>        private int GetModuleNameWaferSize(ModuleName moduleName)        {            if (ModuleHelper.IsLoadPort(moduleName))            {                Loadport loadport = GetLoadPort(moduleName);                if (!loadport.HasCassette)                {                    LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{moduleName} does not have cassete.");                    return 0;                }                return (int)loadport.WaferSize;            }            else if (ModuleHelper.IsDummy(moduleName))            {                DummyDevice dummyDevice = Singleton<RouteManager>.Instance.EFEM.GetDummyDevice(moduleName - ModuleName.Dummy1);                if (!dummyDevice.HasCassette)                {                    LOG.WriteLog(eEvent.ERR_EFEM_COMMON_FAILED, Module.ToString(), $"{moduleName} does not have cassete.");                    return 0;                }                return (int)dummyDevice.WaferSize;            }            return 0;        }        /// <summary>        /// 获取模块尺寸        /// </summary>        /// <param name="moduleName"></param>        /// <returns></returns>        private int GetModuleSlotWaferSize(ModuleName moduleName,int slotNumber)        {            if(WaferManager.Instance.CheckHasWafer(moduleName,slotNumber))            {                WaferInfo waferInfo=WaferManager.Instance.GetWafer(moduleName, slotNumber);                return (int)waferInfo.Size;            }            else            {                return 0;            }        }    }}
 |