123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- using System;
- using System.Text.RegularExpressions;
- using Aitex.Core.RT.Log;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders
- {
- public interface IReaderMsg
- {
- string package(params object[] args);
- /// </summary>
- /// return value, completed
- /// <param name="type"></param>
- /// <param name="cmd"></param>
- /// <returns></returns>
- bool unpackage(string type, string[] items);
- bool background { get; }
- }
- public class handler : IHandler
- {
- public int ID { get; set; }
- public int Unit { get; set; }
- public bool IsBackground { get { return _imp.background; } }
-
- private static int retry_time = 1;
- private int retry_count = retry_time;
- private IReaderMsg _imp ;
-
- private object[] _objs = null;
- public handler(IReaderMsg imp, params object[] objs)
- {
- _imp = imp;
- this._objs = objs;
- }
- 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(this._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
- {
- string msg = message.Trim();
- if (msg.IndexOf("Welcome") >= 0)
- {
- return true;
- }
- msg = msg.Replace("User:","");
- if (string.IsNullOrWhiteSpace(msg))
- {
- return true;
- }
- if (msg.Length <= 2)
- {
- if (!msg.Equals("1")) //0: command failed
- {
- if (retry_count-- <= 0)
- {
- string warning = string.Format("retry over {0} times", retry_time);
- //LOG.Warning(warning);
- if (!IsBackground)
- throw (new ExcuteFailedException(warning));
- else
- {
- completed = true;
- return true;
- }
-
- }
- port.Write(string.Format("{0}\r\n", _imp.package(this._objs)));
- }
- if (!IsBackground)
- {
- _imp.unpackage("", null);
- completed = true;
- }
-
- return true;
- }
- if (IsBackground)
- {
- msg = message.TrimStart('[');
- msg = msg.TrimEnd(']');
- string[] data = Regex.Split(msg, ",");
- completed = _imp.unpackage("", data);
- // if (!completed)
- // {
- // port.Write(string.Format("{0}\r", _imp.package(this._objs))); //read failed. retry
- // }
- }
- return true;
- }
- catch (ExcuteFailedException e)
- {
- throw (e);
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- throw (new InvalidPackageException(message));
- }
- }
- }
- public class ReadHandler : IReaderMsg //common move
- {
- public bool background { get; private set; }
- private OcrReader _device ;
- public ReadHandler(OcrReader device)
- {
- _device = device;
- background = true;
- }
- public string package(params object[] args)
- {
- return string.Format("SM\"READ\"0 ");
- }
- public bool unpackage(string type, string[] items)
- {
- if (_device.ReadLaserMaker)
- {
- _device.LaserMaker = items[0];
- }
- else
- {
- _device.T7Code = items[0];
- }
- _device.ReadOK = double.Parse(items[2]) > 0;
- return true;
- }
- }
- public class OnlineHandler : IReaderMsg //common move
- {
- public bool background { get; private set; }
- private OcrReader _device ;
- private bool _online = false;
- public OnlineHandler(OcrReader device)
- {
- _device = device;
- background = false;
- }
- public string package(params object[] args)
- {
- _online = (bool)args[0];
- if(_online)
- return string.Format("SO1");
- return string.Format("SO0");
- }
- public bool unpackage(string type, string[] items)
- {
- return true;
- }
- }
- public class GetJobHandler : IReaderMsg
- {
- public bool background { get; private set; }
- private OcrReader _device ;
- public GetJobHandler(OcrReader device)
- {
- _device = device;
- background = true;
- }
- public string package(params object[] args)
- {
- return string.Format("GF");
- }
- public bool unpackage(string type, string[] items)
- {
- _device.JobName = (string)items[0];
- return true;
- }
- }
- public class LoadJobHandler : IReaderMsg //common move
- {
- public bool background { get; private set; }
- private OcrReader _device ;
- private string _job;
- public LoadJobHandler(OcrReader device)
- {
- _device = device;
- background = false;
- }
- public string package(params object[] args)
- {
- _job = (string)args[0]; //full path
- // _job = _job.Substring(_job.LastIndexOf("\\") + 1); //remove dir
- // _job = _job.Substring(0, _job.LastIndexOf(".")); //remove expand
- return string.Format("LF{0}.job",_job);
- }
- public bool unpackage(string type, string[] items)
- {
- _device.JobName = _job;
- return true;
- }
- }
- }
|