123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.Util;
- using MECF.Framework.Common.Communications;
- using MECF.Framework.Common.Equipment;
- using MECF.Framework.Common.SubstrateTrackings;
- using MECF.Framework.RT.ModuleLibrary.VceModules;
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using Venus_Core;
- namespace Venus_RT.Devices.PreAligner
- {
- public class HongHuVPA : IPreAlign
- {
- private AsyncSerialPort _serialport;
- private string _newLine = "\r";
- private object _locker = new object();
- private bool _IsAsciiMode = true;
- private PeriodicJob _thread;
-
- private RState _state;
- private Regex _catchErrorCode = new Regex(@"(?<=_ERR )(.*)");
- private Regex _checkData = new Regex(@"(?<=DATA)(.*)");
- private LinkedList<string> _lstMessage = new LinkedList<string>();
- private ModuleName _module;
- private enum AlignerAction
- {
- Home,
- Align,
- AlignWithAngle,
- Reset,
- Scan,
- RsLT,
- ServeUp,
- }
- private Dictionary<int, string> _ErrorCode2Msg = new Dictionary<int, string>()
- {
- { 16 , "" },
- };
- private Dictionary<AlignerAction, string> _Command2Msg = new Dictionary<AlignerAction, string>()
- {
-
- { AlignerAction.Align , "ALGN" },
- { AlignerAction.AlignWithAngle, "MOVT REL" },
- { AlignerAction.Home , "HOME" },
- { AlignerAction.Reset , "RSET" },
- { AlignerAction.Scan , "SCAN" },
- { AlignerAction.RsLT , "RSLT" },
-
-
-
-
-
-
-
-
-
- };
- public ModuleName Module => _module;
- public bool IsConnect => _serialport.IsOpen();
- public RState Status => _state;
- public bool IsError => _state == RState.Failed || _state == RState.Timeout;
- private int _ROffset = 0;
- private int _TOffset = 0;
- private bool _IsOverRange = false;
- public int ROffset => _ROffset;
- public int TOffset => _TOffset;
- public bool IsOverRange => _IsOverRange;
- public HongHuVPA(ModuleName module)
- {
- _module = module;
- string port = SC.GetStringValue($"{module}.AlignerPort");
- _serialport = new AsyncSerialPort(port, 9600, 8, Parity.None, StopBits.One, _newLine, _IsAsciiMode);
- WaferManager.Instance.SubscribeLocation(ModuleName.Aligner1, 1);
- _serialport.Open();
- _serialport.OnDataChanged += OnReceiveData;
- _state = RState.Init;
- _thread = new PeriodicJob(50, OnTimer, "OnTimer->Aligner1");
- _thread.Start();
- }
-
- private void OnReceiveData(string obj)
- {
- lock (_locker)
- {
- if (string.IsNullOrEmpty(_newLine))
- {
- _lstMessage.AddLast(obj);
- return;
- }
- string[] array = obj.Split(_newLine.ToCharArray());
- foreach (string text in array)
- {
- if (!string.IsNullOrEmpty(text))
- {
- _lstMessage.AddLast(text + _newLine);
- }
- }
- }
- }
-
- private bool OnTimer()
- {
-
- lock (_locker)
- {
-
- if (_IsAsciiMode)
- {
-
- while (_lstMessage.Count > 0)
- {
-
- string handlemsg = _lstMessage.First.Value;
- Handlemessage(handlemsg);
- _lstMessage.RemoveFirst();
- }
- }
-
- else
- {
- }
- }
- return true;
- }
-
- private void Handlemessage(string handlemsg)
- {
-
- bool IsAction = true;
- handlemsg = handlemsg.Trim();
- if (IsAction)
- {
- switch (handlemsg)
- {
-
- case "_RDY":
- _state = RState.End;
- break;
-
- default:
- if (_checkData.IsMatch(handlemsg))
- {
- string[] data = handlemsg.Split(' ');
- _ROffset = Convert.ToInt32(data[2]);
- _TOffset = Convert.ToInt32(data[3]);
- if (data[5] == "Y")
- {
- _IsOverRange = true;
- LOG.Write(eEvent.WARN_DEVICE_INFO, Module, $"Wafer offset is over range");
- }
- if (data[5] == "N")
- {
- _IsOverRange = false;
- LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Wafer offset is in range");
- }
- LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"Ro{_ROffset} To{_TOffset}");
- }
- else
- {
- _state = RState.Failed;
-
- if (_catchErrorCode.IsMatch(handlemsg))
- {
- int errorcode = Convert.ToInt32(_catchErrorCode.Match(handlemsg).Value);
- if (_ErrorCode2Msg.ContainsKey(errorcode))
- {
- LOG.Write(eEvent.ERR_DEVICE_INFO, Module, _ErrorCode2Msg[errorcode]);
- }
- else
- {
- LOG.Write(eEvent.ERR_DEVICE_INFO, Module, $"未知错误码{errorcode}");
- }
- }
- else
- {
- LOG.Write(eEvent.ERR_DEVICE_INFO, Module, "未收录相关错误");
- }
- }
- break;
- }
- }
- }
-
- private bool SendMessage(string msg, params string[] args)
- {
- _state = RState.Running;
- if (args.Length > 0)
- {
- foreach (string arg in args)
- msg = msg + " " + arg;
- LOG.WriteSingeLine(eEvent.EV_DEVICE_INFO, _module, $"Send Command to HongHu Aligner: {msg}");
- return _serialport.Write(msg + _newLine);
- }
- else
- {
- LOG.WriteSingeLine(eEvent.EV_DEVICE_INFO, _module, $"Send Command to HongHu Aligner: {msg}");
- return _serialport.Write(msg + _newLine);
- }
- }
- public bool Home()
- {
- return SendMessage(_Command2Msg[AlignerAction.Home]);
- }
- public bool Align()
- {
- if (!CanSendCommand())
- return false;
- return SendMessage(_Command2Msg[AlignerAction.Align]);
- }
- public bool AlignWithAngle(float angle)
- {
- if (!CanSendCommand())
- return false;
- int ang = (int)Math.Floor(angle);
- return SendMessage(_Command2Msg[AlignerAction.AlignWithAngle], ang.ToString());
- }
- public bool ReSet()
- {
- if (!IsError)
- return false;
- return SendMessage(_Command2Msg[AlignerAction.Reset]);
- }
- public bool SCAN()
- {
- if (!CanSendCommand())
- return false;
- return SendMessage(_Command2Msg[AlignerAction.Scan]);
- }
- public bool QueryOffset()
- {
- if (!CanSendCommand())
- return false;
- return SendMessage(_Command2Msg[AlignerAction.RsLT]);
- }
- public bool ServeUp()
- {
- _state = RState.End;
- return true;
- }
- public bool CanSendCommand()
- {
- if (Status == RState.Init)
- {
- LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "Aligner is not homed, please home first.");
- return false;
- }
- else if (Status == RState.Running)
- {
- LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "Aligner is busy, please wait a minute");
- return false;
- }
- else if (Status == RState.Failed || Status == RState.Timeout)
- {
- LOG.Write(eEvent.ERR_DEVICE_INFO, _module, "Aligner has a error, please check and fix the hardware issue and home it");
- return false;
- }
- return true;
- }
- }
- }
|