123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- using System;
- using System.Text.RegularExpressions;
- using Aitex.Core.Common.DeviceData;
- using Aitex.Core.RT.DataCenter;
- using Aitex.Core.RT.Device;
- using Aitex.Core.RT.Event;
- using Aitex.Core.Util;
- using MECF.Framework.Common.Communications;
- using MECF.Framework.Common.Equipment;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
- using DeviceState = Aitex.Sorter.Common.DeviceState;
- using TSC = Aitex.Sorter.Common;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.CarrierIdReaders.OmronRFID
- {
- public class OmronRfidReader : CarrierIdReader, IConnection
- {
-
- 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
- {
- get { return _foregroundHandler != null; }
- }
- public bool Error
- {
- get{return _commErr;}
- }
- public bool Communication
- {
- get
- {
- return _socket == null ? !_commErr : !_commErr && _socket.IsConnected;
- }
- }
- private string _foupId;
- public string FoupID
- {
- get
- {
- return _foupId;
- }
- set
- {
- //FAManager.Instance.ChangeFoupId(Name, value);
- _foupId = value;
- }
- }
- public string LoadPortName
- {
- get
- {
- return _loadPort;
- }
-
- }
- private static Object _locker = new Object();
- private AsyncSocket _socket;
- private IHandler _foregroundHandler = null; //current handler
- public const string delimiter = "\r";
- private bool _commErr = false;
- private string page;
- private string _addr;
- private string _loadPort ;
- public OmronRfidReader(string module, string name, string display, string deviceId, string address, string page, string loadport )
- : base(module, name, display, deviceId, address, page, loadport)
- {
- this.page = page;
- _addr = address;
- _loadPort = loadport ;
- _socket = new AsyncSocket(address);
- _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataChanged);
- _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHandler);
- Initalized = false;
- }
- public override bool Initialize()
- {
- ConnectionManager.Instance.Subscribe(Name, this);
- _socket.Connect(this._addr);
- DATA.Subscribe(Name, "RIDReaderBusy", () => Busy);
- DATA.Subscribe(Name, "FoupID", () => FoupID);
- DATA.Subscribe(Name, "RIDReaderState", () => State);
- DATA.Subscribe(string.Format("Device.{0}.{1}", Module , Name),
- () =>
- {
- AITRfidReaderData data = new AITRfidReaderData()
- {
- DeviceName = Name,
- DeviceSchematicId = DeviceID,
- DisplayName = Display,
- IsBusy = Busy,
- IsError = Error,
- IsInitalized = Initalized,
- State = State.ToString(),
- };
- return data;
- }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
- Initalized = true;
- return true;
- }
-
- public override void Reset()
- {
- base.Reset();
- lock (_locker)
- {
- _foregroundHandler = null;
- }
- if (_commErr)
- {
- Connect();
- }
- }
- public string Address
- {
- get { return _addr; }
- }
- public bool IsConnected
- {
- get { return _socket.IsConnected; }
- }
- public override bool Connect()
- {
- _commErr = false;
- _socket.Connect(this._addr);
- return true;
- }
- public bool Disconnect()
- {
- return true;
- }
- #region Command
- public override bool Read(out string reason)
- {
- reason = string.Empty;
- return execute(new handler<ReadHandler>(this.DeviceID, this.page), out reason);
- }
- public override bool Write(string id, out string reason)
- {
- reason = string.Empty;
- return execute(new handler<WriteHandler>(this.DeviceID, this.page,id), out reason);
- }
- #endregion
- private bool execute(IHandler handler, out string reason)
- {
- reason = string.Empty;
- lock (_locker)
- {
- if (_foregroundHandler != null)
- {
- reason = string.Format("Busy,can not execute");
- return false;
- }
- if(!handler.Execute(ref _socket))
- {
- reason = "Communication error, please re-connect";
- return false;
- }
- _foregroundHandler = handler;
- }
- return true;
- }
-
- private void OnDataChanged(string package)
- {
- try
- {
- package = package.ToUpper();
- string[] msgs = Regex.Split(package, delimiter);
- foreach (string msg in msgs)
- {
- if (msg.Length > 0)
- {
- bool completed = false;
- string resp = msg;
- lock (_locker)
- {
- if (_foregroundHandler != null && _foregroundHandler.OnMessage(ref _socket, resp, out completed))
- {
- _foregroundHandler = null;
- }
- }
- }
- }
- }
- catch (ExcuteFailedException ex)
- {
- EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
- _foregroundHandler = null;
- }
- catch (InvalidPackageException ex)
- {
- EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
- _foregroundHandler = null;
- }
- catch (System.Exception ex)
- {
- EV.PostMessage(Name, EventEnum.DefaultWarning, "【RFID】has exception:" + ex.ToString());
- }
- }
- private void OnErrorHandler(ErrorEventArgs args)
- {
- ReadFailed();
- _commErr = true;
- Initalized = false;
- EV.PostMessage(Module, EventEnum.CommunicationError, Display, args.Reason);
- }
- public void SetCarrierIdReadResult(string carrierId)
- {
- FoupID = carrierId;
- ReadOk( carrierId);
-
- }
- }
- }
|