refactor: 将Framework库和RT、UI放在同一个解决方案中。
This commit is contained in:
+504
@@ -0,0 +1,504 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Aitex.Core.RT.Event;
|
||||
using Aitex.Core.RT.Log;
|
||||
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
|
||||
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders
|
||||
{
|
||||
public enum CongnexHandlerType
|
||||
{
|
||||
None,
|
||||
LoginUsername,
|
||||
LoginPassword,
|
||||
ReadLM,
|
||||
Online,
|
||||
ReadImage,
|
||||
GetJob,
|
||||
GetJobList,
|
||||
LoadJob,
|
||||
}
|
||||
public enum CongnexHandlerState
|
||||
{
|
||||
None,
|
||||
CMDSend,
|
||||
CMDSuccess,
|
||||
CMDFail,
|
||||
ExecuteComplete,
|
||||
Error,
|
||||
Timeout,
|
||||
}
|
||||
|
||||
public class CongexHandler : IHandler
|
||||
{
|
||||
private static readonly int retry_time = 1;
|
||||
|
||||
private readonly IReaderMsg _imp;
|
||||
|
||||
private readonly object[] _objs;
|
||||
private int retry_count = retry_time;
|
||||
|
||||
public CongexHandler(IReaderMsg imp, params object[] objs)
|
||||
{
|
||||
_imp = imp;
|
||||
_objs = objs;
|
||||
}
|
||||
|
||||
public int ID { get; set; }
|
||||
public int Unit { get; set; }
|
||||
|
||||
public bool IsBackground => _imp.background;
|
||||
|
||||
public bool Execute<TPort>(ref TPort port) where TPort : ICommunication
|
||||
{
|
||||
retry_count = retry_time;
|
||||
//return port.Write(string.Format("{0}\r", _imp.package(this._objs)));
|
||||
return port.Write(string.Format("{0}\r\n", _imp.package(_objs)));
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// return value: bhandle
|
||||
/// </summary>
|
||||
/// <typeparam name="TPort"></typeparam>
|
||||
/// <param name="port"></param>
|
||||
/// <param name="msg"></param>
|
||||
/// <param name="completed"></param>
|
||||
/// <returns></returns>
|
||||
public bool OnMessage<TPort>(ref TPort port, string message, out bool completed) where TPort : ICommunication
|
||||
{
|
||||
completed = false;
|
||||
try
|
||||
{
|
||||
var msg = message.Trim();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(msg)) return true;
|
||||
|
||||
if(msg.Equals("-1"))
|
||||
{
|
||||
if (retry_count-- <= 0)
|
||||
{
|
||||
var warning = string.Format("retry over {0} times", retry_time);
|
||||
LOG.Warning(warning);
|
||||
if (!IsBackground)
|
||||
{
|
||||
throw new ExcuteFailedException(warning);
|
||||
}
|
||||
completed = true;
|
||||
return true;
|
||||
}
|
||||
port.Write(string.Format("{0}\r\n", _imp.package(_objs)));
|
||||
}
|
||||
|
||||
//msg = message.Trim().Trim("[]".ToCharArray());
|
||||
var data = Regex.Split(msg, "\r\n");
|
||||
completed = _imp.unpackage("", data);
|
||||
return true;
|
||||
}
|
||||
catch (ExcuteFailedException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
|
||||
throw new InvalidPackageException(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class CognexUserNameHandler : IReaderMsg //common move
|
||||
{
|
||||
private readonly CognexOcrReader _device;
|
||||
|
||||
public CognexUserNameHandler(CognexOcrReader device)
|
||||
{
|
||||
_device = device;
|
||||
background = true;
|
||||
|
||||
}
|
||||
|
||||
public bool background { get; }
|
||||
|
||||
public string package(params object[] args)
|
||||
{
|
||||
_device.CurrentHandlerType = CongnexHandlerType.LoginUsername;
|
||||
return "admin";
|
||||
}
|
||||
|
||||
public void SetExeError()
|
||||
{
|
||||
_device.Error = true; ;
|
||||
}
|
||||
|
||||
public bool unpackage(string type, string[] items)
|
||||
{
|
||||
if (items.Length != 1) return false;
|
||||
|
||||
if (items[0].Contains("Password"))
|
||||
{
|
||||
_device.CurrentHandlerState = CongnexHandlerState.ExecuteComplete;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_device.CurrentHandlerState = CongnexHandlerState.CMDFail;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class CognexPasswordHandler : IReaderMsg //common move
|
||||
{
|
||||
private readonly CognexOcrReader _device;
|
||||
|
||||
public CognexPasswordHandler(CognexOcrReader device)
|
||||
{
|
||||
_device = device;
|
||||
background = true;
|
||||
}
|
||||
|
||||
public bool background { get; }
|
||||
|
||||
public string package(params object[] args)
|
||||
{
|
||||
_device.CurrentHandlerType = CongnexHandlerType.LoginPassword;
|
||||
return "";
|
||||
}
|
||||
public void SetExeError()
|
||||
{
|
||||
_device.Error = true; ;
|
||||
}
|
||||
|
||||
public bool unpackage(string type, string[] items)
|
||||
{
|
||||
if (items.Length != 1) return false;
|
||||
if(items[0].Contains("User Logged In"))
|
||||
{
|
||||
_device.CurrentHandlerState = CongnexHandlerState.ExecuteComplete;
|
||||
_device.IsLogined = true;
|
||||
return true;
|
||||
}
|
||||
if(items[0].Contains("Invalid Password"))
|
||||
{
|
||||
_device.CurrentHandlerState = CongnexHandlerState.CMDFail;
|
||||
_device.IsLogined = false;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class CognexReadHandler : IReaderMsg //common move
|
||||
{
|
||||
private readonly CognexOcrReader _device;
|
||||
|
||||
public CognexReadHandler(CognexOcrReader device)
|
||||
{
|
||||
_device = device;
|
||||
background = true;
|
||||
|
||||
}
|
||||
public void SetExeError()
|
||||
{
|
||||
_device.Error = true; ;
|
||||
}
|
||||
public bool background { get; }
|
||||
|
||||
public string package(params object[] args)
|
||||
{
|
||||
_device.CurrentHandlerType = CongnexHandlerType.ReadLM;
|
||||
return "SM\"READ\"0 ";
|
||||
}
|
||||
|
||||
|
||||
public bool unpackage(string type, string[] items)
|
||||
{
|
||||
string[] stritems;
|
||||
if(items[0] == "1" && items.Length == 1)
|
||||
{
|
||||
_device.CurrentHandlerState = CongnexHandlerState.CMDSuccess;
|
||||
return true;
|
||||
}
|
||||
if(items[0] == "1" && items.Length == 2)
|
||||
{
|
||||
stritems = items[1].Trim("[]".ToCharArray()).Split(',');
|
||||
|
||||
}
|
||||
else stritems = items[0].Trim("[]".ToCharArray()).Split(',');
|
||||
if (_device.ReadLaserMaker) {
|
||||
|
||||
_device.LaserMark1ReadResult = "";
|
||||
foreach(string item in stritems)
|
||||
{
|
||||
_device.LaserMark1ReadResult = _device.LaserMark1ReadResult + item + ",";
|
||||
}
|
||||
|
||||
_device.LaserMark1 = stritems[0].Trim("[]".ToCharArray());
|
||||
_device.CurrentLaserMark = _device.LaserMark1;
|
||||
_device.LaserMark1Score = 0;
|
||||
if (stritems.Length > 1 && double.TryParse(stritems[1], out double score))
|
||||
{
|
||||
_device.LaserMark1Score = score;
|
||||
|
||||
}
|
||||
else _device.LaserMark1ReadResult = _device.LaserMark1ReadResult + "Failed!";
|
||||
if (stritems.Length > 2) _device.LaserMark1ReadTime = stritems[2];
|
||||
|
||||
EV.PostInfoLog("WIDReader",$"Wafer laser mark1 read : {stritems[0]}, score:{_device.LaserMark1Score},time:{_device.LaserMark1ReadTime}.");
|
||||
}
|
||||
else
|
||||
{
|
||||
_device.LaserMark2ReadResult = "";
|
||||
foreach (string item in stritems)
|
||||
{
|
||||
_device.LaserMark2ReadResult = _device.LaserMark2ReadResult + item + ",";
|
||||
}
|
||||
_device.LaserMark2 = stritems[0].Trim("[]".ToCharArray());
|
||||
_device.CurrentLaserMark = _device.LaserMark2;
|
||||
_device.LaserMark2Score = 0;
|
||||
if (stritems.Length > 1 && double.TryParse(stritems[1], out double score))
|
||||
_device.LaserMark2Score = score;
|
||||
else _device.LaserMark2ReadResult = _device.LaserMark2ReadResult + "Failed!";
|
||||
if (stritems.Length > 2) _device.LaserMark2ReadTime = stritems[2];
|
||||
EV.PostInfoLog("WIDReader",$"Wafer laser mark2 read : {stritems[0]}, score:{_device.LaserMark2Score},time:{_device.LaserMark2ReadTime}.");
|
||||
}
|
||||
_device.ReadOK = true;
|
||||
_device.CurrentHandlerState = CongnexHandlerState.ExecuteComplete;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class CognexOnlineHandler : IReaderMsg //common move
|
||||
{
|
||||
private CognexOcrReader _device;
|
||||
|
||||
private bool _online;
|
||||
|
||||
public CognexOnlineHandler(CognexOcrReader device)
|
||||
{
|
||||
_device = device;
|
||||
background = false;
|
||||
|
||||
}
|
||||
public void SetExeError()
|
||||
{
|
||||
_device.Error = true; ;
|
||||
}
|
||||
public bool background { get; }
|
||||
|
||||
public string package(params object[] args)
|
||||
{
|
||||
_device.CurrentHandlerType = CongnexHandlerType.Online;
|
||||
_online = (bool) args[0];
|
||||
if (_online)
|
||||
return "SO1";
|
||||
|
||||
return "SO0";
|
||||
}
|
||||
|
||||
|
||||
public bool unpackage(string type, string[] items)
|
||||
{
|
||||
if(items.Length ==1 && items[0].Trim() =="1")
|
||||
{
|
||||
_device.CurrentHandlerState = CongnexHandlerState.ExecuteComplete;
|
||||
_device.IsOnline = _online;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class CognexReadImageHandler : IReaderMsg
|
||||
{
|
||||
private readonly CognexOcrReader _device;
|
||||
//private bool _readStart;
|
||||
private int _imageLength;
|
||||
private StringBuilder _stringBuilder;
|
||||
|
||||
public CognexReadImageHandler(CognexOcrReader device)
|
||||
{
|
||||
_device = device;
|
||||
background = true;
|
||||
//_readStart = false;
|
||||
_imageLength = 0;
|
||||
_stringBuilder = new StringBuilder();
|
||||
|
||||
}
|
||||
public void SetExeError()
|
||||
{
|
||||
_device.Error = true; ;
|
||||
}
|
||||
public bool background { get; }
|
||||
|
||||
public string package(params object[] args)
|
||||
{
|
||||
_device.CurrentHandlerType = CongnexHandlerType.ReadImage;
|
||||
_device.NeedSocketLog = false;
|
||||
return "RI";
|
||||
}
|
||||
|
||||
|
||||
public bool unpackage(string type, string[] items)
|
||||
{
|
||||
if(_device.CurrentHandlerState == CongnexHandlerState.CMDSend)
|
||||
{
|
||||
if (items.Length>=2 && items[0].Trim() == "1" && int.TryParse(items[1], out _imageLength))
|
||||
{
|
||||
_device.CurrentHandlerState = CongnexHandlerState.CMDSuccess;
|
||||
|
||||
}
|
||||
else _device.CurrentHandlerState = CongnexHandlerState.CMDFail;
|
||||
return true;
|
||||
}
|
||||
if(_device.CurrentHandlerState == CongnexHandlerState.CMDSuccess)
|
||||
{
|
||||
foreach(string str in items)
|
||||
{
|
||||
_stringBuilder?.Append(str.Trim());
|
||||
}
|
||||
if (_stringBuilder.Length >= _imageLength)
|
||||
{
|
||||
string strimage = _stringBuilder?.ToString().Substring(0, _imageLength);
|
||||
_device.SaveImage(strimage);
|
||||
_device.CurrentHandlerState = CongnexHandlerState.ExecuteComplete;
|
||||
_device.NeedSocketLog = true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class CognexGetJobHandler : IReaderMsg
|
||||
{
|
||||
private readonly CognexOcrReader _device;
|
||||
|
||||
public void SetExeError()
|
||||
{
|
||||
_device.Error = true; ;
|
||||
}
|
||||
|
||||
public CognexGetJobHandler(CognexOcrReader device)
|
||||
{
|
||||
_device = device;
|
||||
background = true;
|
||||
|
||||
}
|
||||
|
||||
public bool background { get; }
|
||||
|
||||
public string package(params object[] args)
|
||||
{
|
||||
_device.CurrentHandlerType = CongnexHandlerType.GetJob;
|
||||
return "GF";
|
||||
}
|
||||
|
||||
|
||||
public bool unpackage(string type, string[] items)
|
||||
{
|
||||
if (items[0].Trim() == "1")
|
||||
{
|
||||
if (items.Length > 1) _device.CurrentJobName = items[1].Trim();
|
||||
else _device.CurrentJobName = "";
|
||||
_device.CurrentHandlerState = CongnexHandlerState.ExecuteComplete;
|
||||
}
|
||||
else
|
||||
{
|
||||
_device.CurrentHandlerState = CongnexHandlerState.CMDFail;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class CognexGetJobListHandler : IReaderMsg
|
||||
{
|
||||
private readonly CognexOcrReader _device;
|
||||
public void SetExeError()
|
||||
{
|
||||
_device.Error = true; ;
|
||||
}
|
||||
|
||||
public CognexGetJobListHandler(CognexOcrReader device)
|
||||
{
|
||||
_device = device;
|
||||
background = true;
|
||||
|
||||
}
|
||||
|
||||
public bool background { get; }
|
||||
|
||||
public string package(params object[] args)
|
||||
{
|
||||
_device.CurrentHandlerType = CongnexHandlerType.GetJobList;
|
||||
return "Get Filelist";
|
||||
}
|
||||
|
||||
|
||||
public bool unpackage(string type, string[] items)
|
||||
{
|
||||
if (items[0].Trim() == "1" && items.Length >=2)
|
||||
{
|
||||
int filecount;
|
||||
int.TryParse(items[1].Trim(), out filecount);
|
||||
var job = items.Where(i => i.Contains(".job")).ToList();
|
||||
_device.JobFileList = job;
|
||||
_device.CurrentHandlerState = CongnexHandlerState.ExecuteComplete;
|
||||
}
|
||||
else
|
||||
{
|
||||
_device.CurrentHandlerState = CongnexHandlerState.CMDFail;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class CognexLoadJobHandler : IReaderMsg //common move
|
||||
{
|
||||
private readonly CognexOcrReader _device;
|
||||
|
||||
private string _job;
|
||||
|
||||
public CognexLoadJobHandler(CognexOcrReader device)
|
||||
{
|
||||
_device = device;
|
||||
background = false;
|
||||
|
||||
}
|
||||
public void SetExeError()
|
||||
{
|
||||
_device.Error = true; ;
|
||||
}
|
||||
public bool background { get; }
|
||||
|
||||
public string package(params object[] args)
|
||||
{
|
||||
_device.CurrentHandlerType = CongnexHandlerType.LoadJob;
|
||||
_job = (string) args[0]; //full path
|
||||
// _job = _job.Substring(_job.LastIndexOf("\\") + 1); //remove dir
|
||||
// _job = _job.Substring(0, _job.LastIndexOf(".")); //remove expand
|
||||
return $"LF{_job}.job";
|
||||
}
|
||||
|
||||
|
||||
public bool unpackage(string type, string[] items)
|
||||
{
|
||||
if (items[0].Trim() == "1")
|
||||
{
|
||||
_device.CurrentJobName = _job;
|
||||
_device.CurrentHandlerState = CongnexHandlerState.ExecuteComplete;
|
||||
}
|
||||
else
|
||||
{
|
||||
_device.CurrentHandlerState = CongnexHandlerState.CMDFail;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
+627
@@ -0,0 +1,627 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Aitex.Common.Util;
|
||||
using Aitex.Core.RT.DataCenter;
|
||||
using Aitex.Core.RT.Device;
|
||||
using Aitex.Core.RT.Event;
|
||||
using Aitex.Core.RT.SCCore;
|
||||
using Aitex.Core.Util;
|
||||
using Aitex.Sorter.Common;
|
||||
using MECF.Framework.Common.Communications;
|
||||
using MECF.Framework.Common.Equipment;
|
||||
using MECF.Framework.Common.SubstrateTrackings;
|
||||
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
|
||||
using ErrorEventArgs = MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.ErrorEventArgs;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders
|
||||
{
|
||||
public class CognexOcrReader : BaseDevice, IDevice, IConnection
|
||||
{
|
||||
public const string delimiter = "\r\n";
|
||||
|
||||
private static readonly object _locker = new object();
|
||||
private readonly string _addr;
|
||||
|
||||
private IHandler _foregroundHandler; //current handler
|
||||
|
||||
private readonly Queue<IHandler> _handlers = new Queue<IHandler>();
|
||||
|
||||
public bool IsLogined { get; set; } = false;
|
||||
public bool IsOnline { get; set; } = false;
|
||||
|
||||
public string ImageFileName => _imageFileName;
|
||||
public string ImageStorePath => _imageStorePath;
|
||||
|
||||
private AsyncSocket _socket;
|
||||
private string _imageStorePath;
|
||||
private string _imageFileName;
|
||||
private string _imageString; //Read image in BMP format
|
||||
//private int _imageLength; //Read image in BMP format
|
||||
//private StringBuilder _stringBuilder;
|
||||
//private bool _readStart;
|
||||
|
||||
public CongnexHandlerType CurrentHandlerType { get; set; } = CongnexHandlerType.None;
|
||||
public CongnexHandlerState CurrentHandlerState { get; set; } = CongnexHandlerState.None;
|
||||
|
||||
public Guid OCRGuid { get; set; }
|
||||
public bool CurrentHandlerExcuteResult { get; set; } = true;
|
||||
public bool CurrentHandlerExcuteComplete { get; set; } = true;
|
||||
public string CurrentWaferID { get; set; }
|
||||
private const string EventCMDFail = "CongnexCommandFailed";
|
||||
private DeviceTimer _timer = new DeviceTimer();
|
||||
private int _ts;
|
||||
|
||||
public bool NeedSocketLog
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
public string Address => _addr;
|
||||
|
||||
public bool IsConnected => _socket.IsConnected;
|
||||
|
||||
|
||||
public CognexOcrReader(string module, string name, string display, string deviceId, string address)
|
||||
: base(module, name, display, deviceId)
|
||||
{
|
||||
_addr = address;
|
||||
_socket = new AsyncSocket(address,524288, "");
|
||||
_socket.OnDataChanged += OnDataChanged;
|
||||
_socket.OnErrorHappened += OnErrorHandler;
|
||||
//IsReadImage = false;
|
||||
Initalized = false;
|
||||
|
||||
_imageString = "";
|
||||
}
|
||||
|
||||
public DeviceState State
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!Initalized) return DeviceState.Unknown;
|
||||
if (Error) return DeviceState.Error;
|
||||
|
||||
if (Busy)
|
||||
return DeviceState.Busy;
|
||||
|
||||
return DeviceState.Idle;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Initalized { get; set; }
|
||||
|
||||
public bool Busy => _handlers.Count > 0 || _foregroundHandler != null;
|
||||
|
||||
public bool Error { get; set; }
|
||||
|
||||
public bool ReadLaserMaker { get; set; }
|
||||
|
||||
public string CurrentJobName { get; set; }
|
||||
public string LaserMark1 { get; set; }
|
||||
public double LaserMark1Score { get; set; }
|
||||
public string LaserMark1ReadTime { get; set; } = string.Empty;
|
||||
public string LaserMark2 { get; set; }
|
||||
public double LaserMark2Score { get; set; }
|
||||
public string LaserMark2ReadTime { get; set; } = string.Empty;
|
||||
public string CurrentLaserMark { get;set; }
|
||||
|
||||
public List<string> JobFileList { get; set; }
|
||||
|
||||
public bool ReadOK { get; set; }
|
||||
|
||||
public string LaserMark1ReadResult { get; set; }
|
||||
public string LaserMark2ReadResult { get; set; }
|
||||
|
||||
public string ImageString => _imageString;
|
||||
|
||||
public bool Connect()
|
||||
{
|
||||
_socket.Connect(_addr);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Disconnect()
|
||||
{
|
||||
_socket.Dispose();
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Initialize()
|
||||
{
|
||||
ConnectionManager.Instance.Subscribe(Name, this);
|
||||
JobFileList = new List<string>();
|
||||
int retry = 0;
|
||||
while (!_socket.IsConnected && retry<5)
|
||||
{
|
||||
_socket.Connect(_addr);
|
||||
Thread.Sleep(1000);
|
||||
retry++;
|
||||
}
|
||||
|
||||
DEVICE.Register(string.Format("{0}.{1}", Name, "ReadWaferID"),//DeviceOperationName.ReadWaferID),
|
||||
(out string reason, int time, object[] param) =>
|
||||
{
|
||||
var bLaser = bool.Parse((string) param[0]);
|
||||
var jobName = (string) param[1];
|
||||
var ret = Read(bLaser, jobName, out reason);
|
||||
if (ret)
|
||||
{
|
||||
reason = string.Format("{0}", Name, "Read Laser Mark.");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
DEVICE.Register(string.Format("{0}.{1}", Name, "ReadLM1"),//DeviceOperationName.ReadWaferID),
|
||||
(out string reason, int time, object[] param) =>
|
||||
{
|
||||
var bLaser =true;
|
||||
var jobName = (string)param[0];
|
||||
var ret = Read(bLaser, jobName, out reason);
|
||||
if (ret)
|
||||
{
|
||||
reason = string.Format("{0}", Name, "Read Laser Mark.");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
DEVICE.Register(string.Format("{0}.{1}", Name, "ReadLM2"),//DeviceOperationName.ReadWaferID),
|
||||
(out string reason, int time, object[] param) =>
|
||||
{
|
||||
var bLaser = false;
|
||||
var jobName = (string)param[0];
|
||||
var ret = Read(bLaser, jobName, out reason);
|
||||
if (ret)
|
||||
{
|
||||
reason = string.Format("{0}", Name, "Read Laser Mark.");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
DEVICE.Register($"{Name}.RefreshJobList", (out string reason, int time, object[] param) =>
|
||||
{
|
||||
GetJobList(out reason);
|
||||
return true;
|
||||
});
|
||||
|
||||
DATA.Subscribe(Name, "WRIDReaderState", () => State);
|
||||
DATA.Subscribe(Name, "WRIDReaderBusy", () => Busy);
|
||||
DATA.Subscribe(Name, "LaserMaker1", () => LaserMark1);
|
||||
DATA.Subscribe(Name, "LaserMaker2", () => LaserMark2);
|
||||
DATA.Subscribe(Name, "LaserMark1Result", () => LaserMark1ReadResult);
|
||||
DATA.Subscribe(Name, "LaserMark2Result", () => LaserMark2ReadResult);
|
||||
DATA.Subscribe(Name, "JobFileList", () => JobFileList);
|
||||
DATA.Subscribe(Name, "CurrentLaserMark", () => CurrentLaserMark);
|
||||
|
||||
EV.Subscribe(new EventItem("Event", EventCMDFail, "Congnex command execution failed.", EventLevel.Alarm, Aitex.Core.RT.Event.EventType.HostNotification));
|
||||
|
||||
|
||||
_ts = SC.ContainsItem("OcrReader.TimeLimitForWID") ? SC.GetValue<int>("OcrReader.TimeLimitForWID") : 5;
|
||||
|
||||
var fileInfo = new FileInfo(PathManager.GetDirectory($"Logs\\{DeviceID}"));
|
||||
|
||||
if (fileInfo.Directory != null && !fileInfo.Directory.Exists)
|
||||
fileInfo.Directory.Create();
|
||||
|
||||
if (fileInfo.Directory != null)
|
||||
_imageStorePath = fileInfo.FullName;
|
||||
|
||||
_handlers.Clear();
|
||||
|
||||
Initalized = true;
|
||||
if(_socket.IsConnected)
|
||||
Login(out _);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Terminate()
|
||||
{
|
||||
_socket.Dispose();
|
||||
}
|
||||
|
||||
public void Monitor()
|
||||
{
|
||||
var wafer = WaferManager.Instance.GetWafers(ModuleName.Aligner)[0];
|
||||
if(wafer.IsWaferEmpty) CurrentLaserMark = string.Empty;
|
||||
//if (IO.DI["DI_PreAlignerWaferOn"].Value) CurrentLaserMark = string.Empty;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
lock (_locker)
|
||||
{
|
||||
_foregroundHandler = null;
|
||||
_handlers.Clear();
|
||||
}
|
||||
|
||||
if (Error)
|
||||
{
|
||||
Error = false;
|
||||
_socket.Connect(_addr);
|
||||
}
|
||||
}
|
||||
|
||||
private string[] GetJobName(bool bLaserMark1)
|
||||
{
|
||||
var jobs = string.Empty;
|
||||
|
||||
if (bLaserMark1)
|
||||
jobs = SC.GetStringValue("OcrReader.ReaderJob1Main");
|
||||
else
|
||||
jobs = SC.GetStringValue("OcrReader.ReaderJob2Main");
|
||||
|
||||
return jobs.Split(';');
|
||||
}
|
||||
|
||||
private bool execute(IHandler handler, out string reason)
|
||||
{
|
||||
reason = string.Empty;
|
||||
NeedSocketLog = true;
|
||||
lock (_locker)
|
||||
{
|
||||
CurrentHandlerExcuteComplete = false;
|
||||
CurrentHandlerExcuteResult = false;
|
||||
CurrentHandlerState = CongnexHandlerState.CMDSend;
|
||||
_foregroundHandler = handler;
|
||||
|
||||
if (!handler.Execute(ref _socket))
|
||||
{
|
||||
EV.Notify(EventCMDFail);
|
||||
reason = "Communication failed, please recovery it.";
|
||||
CurrentHandlerState = CongnexHandlerState.CMDFail;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool execute(out string reason)
|
||||
{
|
||||
reason = string.Empty;
|
||||
NeedSocketLog = true;
|
||||
lock (_locker)
|
||||
{
|
||||
if (_handlers.Count > 0)
|
||||
{
|
||||
_foregroundHandler = _handlers.Dequeue();
|
||||
CurrentHandlerExcuteComplete = false;
|
||||
CurrentHandlerExcuteResult = false;
|
||||
CurrentHandlerState = CongnexHandlerState.CMDSend;
|
||||
|
||||
|
||||
if (!_foregroundHandler.Execute(ref _socket))
|
||||
{
|
||||
EV.Notify(EventCMDFail);
|
||||
reason = " communication failed, please recovery it.";
|
||||
//LOG.Error(reason);
|
||||
EV.PostMessage(Name, EventEnum.DefaultWarning, "【Reader】" + reason);
|
||||
CurrentHandlerState = CongnexHandlerState.CMDFail;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnDataChanged(string package)
|
||||
{
|
||||
try
|
||||
{
|
||||
if(package.Contains("User:"))
|
||||
{
|
||||
_foregroundHandler = null;
|
||||
_handlers.Clear();
|
||||
Login(out _);
|
||||
return;
|
||||
}
|
||||
if (_foregroundHandler == null) return;
|
||||
|
||||
if (package.Trim() == "") return;
|
||||
|
||||
bool completed = false;
|
||||
|
||||
lock (_locker)
|
||||
{
|
||||
if (_foregroundHandler != null && _foregroundHandler.OnMessage(ref _socket, package.Trim(), out completed))
|
||||
{
|
||||
if (CurrentHandlerState == CongnexHandlerState.ExecuteComplete)
|
||||
{
|
||||
_foregroundHandler = null;
|
||||
var reason = string.Empty;
|
||||
execute(out reason);
|
||||
}
|
||||
if (CurrentHandlerState == CongnexHandlerState.CMDFail)
|
||||
{
|
||||
EV.Notify(EventCMDFail);
|
||||
_foregroundHandler = null;
|
||||
_handlers.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (ExcuteFailedException ex)
|
||||
{
|
||||
EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
|
||||
}
|
||||
catch (InvalidPackageException ex)
|
||||
{
|
||||
EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
EV.PostMessage(Name, EventEnum.DefaultWarning, "【Reader】has exception:" + ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnErrorHandler(ErrorEventArgs args)
|
||||
{
|
||||
Error = true;
|
||||
Initalized = false;
|
||||
EV.PostMessage(Module, EventEnum.CommunicationError, Display, args.Reason);
|
||||
|
||||
//EV.PostMessage(Name, EventEnum.DefaultWarning, string.Format("{0} Communication failed,please check it.{1}", Name, args.Reason));
|
||||
}
|
||||
|
||||
private string ConvertJobName(string name)
|
||||
{
|
||||
name = name.Substring(name.LastIndexOf("\\") + 1); //remove dir
|
||||
name = name.Substring(0, name.LastIndexOf(".")); //remove expand
|
||||
return name;
|
||||
}
|
||||
|
||||
public void SaveImage(string imgString)
|
||||
{
|
||||
DeleteEarlyImageFile();
|
||||
var byBitmap = HexStringToBytes(imgString);
|
||||
var bmp = new Bitmap(new MemoryStream(byBitmap));
|
||||
_imageFileName = CurrentWaferID + DateTime.Now.ToString("G").Replace('/','_').Replace(':','_');
|
||||
|
||||
bmp.Save($"{_imageStorePath}{_imageFileName}.bmp",ImageFormat.Bmp);
|
||||
EV.PostInfoLog("WIDReader",$"Picture saved, file name is {_imageFileName}.bmp");
|
||||
}
|
||||
|
||||
private void DeleteEarlyImageFile()
|
||||
{
|
||||
string[] fpath = Directory.GetFiles(_imageStorePath, "*.bmp", SearchOption.TopDirectoryOnly);
|
||||
Dictionary<string, DateTime> fCreateDate = new Dictionary<string, DateTime>();
|
||||
for (int i = 0; i < fpath.Length; i++)
|
||||
{
|
||||
FileInfo fi = new FileInfo(fpath[i]);
|
||||
fCreateDate[fpath[i]] = fi.CreationTime;
|
||||
}
|
||||
fCreateDate = fCreateDate.OrderBy(f => f.Value).ToDictionary(f => f.Key, f => f.Value);
|
||||
int fCount = fCreateDate.Count;
|
||||
while (fCount > 99)
|
||||
{
|
||||
string strFile = fCreateDate.First().Key;
|
||||
File.Delete(strFile);
|
||||
fCreateDate.Remove(strFile);
|
||||
fCount = fCreateDate.Count;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static byte[] HexStringToBytes(string hex)
|
||||
{
|
||||
byte[] data = new byte[hex.Length /2];
|
||||
int j = 0;
|
||||
for (int i = 0; i < hex.Length; i+=2)
|
||||
{
|
||||
data[ j ] = Convert.ToByte(hex.Substring(i, 2), 16);
|
||||
++j;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private string ConvetJobName(string name)
|
||||
{
|
||||
name = name.Substring(name.LastIndexOf("\\") + 1); //remove dir
|
||||
name = name.Substring(0, name.LastIndexOf(".")); //remove expand
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
#region Command
|
||||
|
||||
public bool Login(out string reason)
|
||||
{
|
||||
_handlers.Clear();
|
||||
_handlers.Enqueue(new CongexHandler(new CognexUserNameHandler(this))); //UserName
|
||||
_handlers.Enqueue(new CongexHandler(new CognexPasswordHandler(this))); //Password
|
||||
return execute(out reason);
|
||||
}
|
||||
|
||||
public bool ReadOnHostMode(bool bLaserMaker, string[] jobnames, out string reason)
|
||||
{
|
||||
_handlers.Clear();
|
||||
|
||||
string[] jobs = jobnames;
|
||||
|
||||
if (jobs.Length == 0 || string.IsNullOrEmpty(jobs[0]))
|
||||
{
|
||||
reason = "Job is undefine";
|
||||
return false;
|
||||
}
|
||||
|
||||
string jobName = ConvetJobName(jobs[0]).Replace(".job","");
|
||||
|
||||
if (CurrentJobName != jobName)
|
||||
{
|
||||
_handlers.Enqueue(new CongexHandler(new CognexOnlineHandler(this), false)); //offline
|
||||
_handlers.Enqueue(new CongexHandler(new CognexLoadJobHandler(this), jobName)); //LoadJob
|
||||
|
||||
}
|
||||
_handlers.Enqueue(new CongexHandler(new CognexOnlineHandler(this), true)); //online
|
||||
_handlers.Enqueue(new CongexHandler(new CognexReadHandler(this))); //Read
|
||||
|
||||
//if (jobs.Length > 1)
|
||||
//{
|
||||
// for (int i = 1; i < jobs.Length; i++)
|
||||
// {
|
||||
// jobName = ConvetJobName(jobs[i]);
|
||||
// _handlers.Enqueue(new handler(new CognexOnlineHandler(this), false)); //offline
|
||||
// _handlers.Enqueue(new handler(new CognexLoadJobHandler(this), jobName)); //LoadJob
|
||||
// _handlers.Enqueue(new handler(new CognexOnlineHandler(this), true)); //online
|
||||
|
||||
// _handlers.Enqueue(new handler(new CognexReadHandler(this))); //Read
|
||||
// }
|
||||
//}
|
||||
|
||||
ReadOK = false;
|
||||
ReadLaserMaker = bLaserMaker;
|
||||
return execute(out reason);
|
||||
}
|
||||
|
||||
public bool Read(out string reason)
|
||||
{
|
||||
_handlers.Clear();
|
||||
|
||||
string[] jobs = GetJobName(true);
|
||||
|
||||
if (jobs.Length == 0 || string.IsNullOrEmpty(jobs[0]))
|
||||
{
|
||||
reason = "Job is undefine";
|
||||
return false;
|
||||
}
|
||||
|
||||
string jobName = ConvetJobName(jobs[0]);
|
||||
if (CurrentJobName != jobName)
|
||||
{
|
||||
_handlers.Enqueue(new CongexHandler(new CognexOnlineHandler(this), false)); //offline
|
||||
_handlers.Enqueue(new CongexHandler(new CognexLoadJobHandler(this), jobName)); //LoadJob
|
||||
|
||||
}
|
||||
_handlers.Enqueue(new CongexHandler(new CognexOnlineHandler(this), true)); //online
|
||||
_handlers.Enqueue(new CongexHandler(new CognexReadHandler(this))); //Read
|
||||
|
||||
//if (jobs.Length > 1)
|
||||
//{
|
||||
// for (int i = 1; i < jobs.Length; i++)
|
||||
// {
|
||||
// jobName = ConvetJobName(jobs[i]);
|
||||
// _handlers.Enqueue(new handler(new CognexOnlineHandler(this), false)); //offline
|
||||
// _handlers.Enqueue(new handler(new CognexLoadJobHandler(this), jobName)); //LoadJob
|
||||
// _handlers.Enqueue(new handler(new CognexOnlineHandler(this), true)); //online
|
||||
|
||||
// _handlers.Enqueue(new handler(new CognexReadHandler(this))); //Read
|
||||
// }
|
||||
//}
|
||||
ReadOK = false;
|
||||
ReadLaserMaker = true;
|
||||
return execute(out reason);
|
||||
}
|
||||
|
||||
public bool Read(bool bLaserMaker, out string reason)
|
||||
{
|
||||
_handlers.Clear();
|
||||
|
||||
string[] jobs = GetJobName(bLaserMaker);
|
||||
|
||||
if (jobs.Length == 0 || string.IsNullOrEmpty(jobs[0]))
|
||||
{
|
||||
reason = "Job is undefine";
|
||||
return false;
|
||||
}
|
||||
|
||||
string jobName = ConvetJobName(jobs[0]);
|
||||
if (CurrentJobName != jobName)
|
||||
{
|
||||
_handlers.Enqueue(new CongexHandler(new CognexOnlineHandler(this), false)); //offline
|
||||
_handlers.Enqueue(new CongexHandler(new CognexLoadJobHandler(this), jobName)); //LoadJob
|
||||
|
||||
}
|
||||
_handlers.Enqueue(new CongexHandler(new CognexOnlineHandler(this), true)); //online
|
||||
_handlers.Enqueue(new CongexHandler(new CognexReadHandler(this))); //Read
|
||||
|
||||
//if (jobs.Length > 1)
|
||||
//{
|
||||
// for (int i = 1; i < jobs.Length; i++)
|
||||
// {
|
||||
// jobName = ConvetJobName(jobs[i]);
|
||||
// _handlers.Enqueue(new handler(new CognexOnlineHandler(this), false)); //offline
|
||||
// _handlers.Enqueue(new handler(new CognexLoadJobHandler(this), jobName)); //LoadJob
|
||||
// _handlers.Enqueue(new handler(new CognexOnlineHandler(this), true)); //online
|
||||
|
||||
// _handlers.Enqueue(new handler(new CognexReadHandler(this))); //Read
|
||||
// }
|
||||
//}
|
||||
ReadOK = false;
|
||||
ReadLaserMaker = bLaserMaker;
|
||||
return execute(out reason);
|
||||
}
|
||||
|
||||
public bool Read(bool bLaserMaker, string jobName, out string reason)
|
||||
{
|
||||
_handlers.Clear();
|
||||
if (string.IsNullOrEmpty(jobName))
|
||||
{
|
||||
reason = "Job is undefine.";
|
||||
return false;
|
||||
}
|
||||
|
||||
jobName = ConvertJobName(jobName);
|
||||
if (CurrentJobName != jobName)
|
||||
{
|
||||
_handlers.Enqueue(new CongexHandler(new CognexOnlineHandler(this), false)); //offline
|
||||
_handlers.Enqueue(new CongexHandler(new CognexLoadJobHandler(this), jobName)); //LoadJob
|
||||
|
||||
}
|
||||
|
||||
|
||||
ReadOK = false;
|
||||
ReadLaserMaker = bLaserMaker;
|
||||
_handlers.Enqueue(new CongexHandler(new CognexOnlineHandler(this), true)); //online
|
||||
_handlers.Enqueue(new CongexHandler(new CognexReadHandler(this))); //Read
|
||||
return execute(out reason);
|
||||
}
|
||||
|
||||
public bool ReadImage(string waferid,out string reason)
|
||||
{
|
||||
_handlers.Clear();
|
||||
this.CurrentWaferID = waferid;
|
||||
reason = string.Empty;
|
||||
_handlers.Enqueue(new CongexHandler(new CognexReadImageHandler(this))); //Read
|
||||
return execute(out reason);
|
||||
}
|
||||
|
||||
public bool ReadJobName(out string reason)
|
||||
{
|
||||
reason = string.Empty;
|
||||
return execute(new CongexHandler(new CognexGetJobHandler(this)), out reason);
|
||||
}
|
||||
|
||||
public bool GetJobList(out string reason)
|
||||
{
|
||||
reason = string.Empty;
|
||||
return execute(new CongexHandler(new CognexGetJobListHandler(this)), out reason);
|
||||
}
|
||||
|
||||
public bool Online(bool online, out string reason)
|
||||
{
|
||||
reason = string.Empty;
|
||||
return execute(new CongexHandler(new CognexOnlineHandler(this), online), out reason);
|
||||
}
|
||||
|
||||
public bool LoadJob(string jobName, out string reason)
|
||||
{
|
||||
reason = string.Empty;
|
||||
return execute(new CongexHandler(new CognexLoadJobHandler(this), jobName), out reason);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user