OmronRfidReader.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using Aitex.Core.Common.DeviceData;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Device;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.Communications;
  9. using MECF.Framework.Common.Equipment;
  10. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
  11. using DeviceState = Aitex.Sorter.Common.DeviceState;
  12. using TSC = Aitex.Sorter.Common;
  13. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.CarrierIdReaders.OmronRFID
  14. {
  15. public class OmronRfidReader : CarrierIdReader, IConnection
  16. {
  17. public DeviceState State
  18. {
  19. get
  20. {
  21. if (!Initalized)
  22. {
  23. return DeviceState.Unknown;
  24. }
  25. if (Error)
  26. {
  27. return DeviceState.Error;
  28. }
  29. if (Busy)
  30. return DeviceState.Busy;
  31. return DeviceState.Idle;
  32. }
  33. }
  34. public bool Initalized { get; set; }
  35. public bool Busy
  36. {
  37. get { return _foregroundHandler != null; }
  38. }
  39. public bool Error
  40. {
  41. get{return _commErr;}
  42. }
  43. public bool Communication
  44. {
  45. get
  46. {
  47. return _socket == null ? !_commErr : !_commErr && _socket.IsConnected;
  48. }
  49. }
  50. private string _foupId;
  51. public string FoupID
  52. {
  53. get
  54. {
  55. return _foupId;
  56. }
  57. set
  58. {
  59. //FAManager.Instance.ChangeFoupId(Name, value);
  60. _foupId = value;
  61. }
  62. }
  63. public string LoadPortName
  64. {
  65. get
  66. {
  67. return _loadPort;
  68. }
  69. }
  70. private static Object _locker = new Object();
  71. private AsyncSocket _socket;
  72. private IHandler _foregroundHandler = null; //current handler
  73. public const string delimiter = "\r";
  74. private bool _commErr = false;
  75. private string page;
  76. private string _addr;
  77. private string _loadPort ;
  78. public OmronRfidReader(string module, string name, string display, string deviceId, string address, string page, string loadport )
  79. : base(module, name, display, deviceId, address, page, loadport)
  80. {
  81. this.page = page;
  82. _addr = address;
  83. _loadPort = loadport ;
  84. _socket = new AsyncSocket(address);
  85. _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataChanged);
  86. _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHandler);
  87. Initalized = false;
  88. }
  89. public override bool Initialize()
  90. {
  91. ConnectionManager.Instance.Subscribe(Name, this);
  92. _socket.Connect(this._addr);
  93. DATA.Subscribe(Name, "RIDReaderBusy", () => Busy);
  94. DATA.Subscribe(Name, "FoupID", () => FoupID);
  95. DATA.Subscribe(Name, "RIDReaderState", () => State);
  96. DATA.Subscribe(string.Format("Device.{0}.{1}", Module , Name),
  97. () =>
  98. {
  99. AITRfidReaderData data = new AITRfidReaderData()
  100. {
  101. DeviceName = Name,
  102. DeviceSchematicId = DeviceID,
  103. DisplayName = Display,
  104. IsBusy = Busy,
  105. IsError = Error,
  106. IsInitalized = Initalized,
  107. State = State.ToString(),
  108. };
  109. return data;
  110. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  111. Initalized = true;
  112. return true;
  113. }
  114. public override void Reset()
  115. {
  116. base.Reset();
  117. lock (_locker)
  118. {
  119. _foregroundHandler = null;
  120. }
  121. if (_commErr)
  122. {
  123. Connect();
  124. }
  125. }
  126. public string Address
  127. {
  128. get { return _addr; }
  129. }
  130. public bool IsConnected
  131. {
  132. get { return _socket.IsConnected; }
  133. }
  134. public override bool Connect()
  135. {
  136. _commErr = false;
  137. _socket.Connect(this._addr);
  138. return true;
  139. }
  140. public bool Disconnect()
  141. {
  142. return true;
  143. }
  144. #region Command
  145. public override bool Read(out string reason)
  146. {
  147. reason = string.Empty;
  148. return execute(new handler<ReadHandler>(this.DeviceID, this.page), out reason);
  149. }
  150. public override bool Write(string id, out string reason)
  151. {
  152. reason = string.Empty;
  153. return execute(new handler<WriteHandler>(this.DeviceID, this.page,id), out reason);
  154. }
  155. #endregion
  156. private bool execute(IHandler handler, out string reason)
  157. {
  158. reason = string.Empty;
  159. lock (_locker)
  160. {
  161. if (_foregroundHandler != null)
  162. {
  163. reason = string.Format("Busy,can not execute");
  164. return false;
  165. }
  166. if(!handler.Execute(ref _socket))
  167. {
  168. reason = "Communication error, please re-connect";
  169. return false;
  170. }
  171. _foregroundHandler = handler;
  172. }
  173. return true;
  174. }
  175. private void OnDataChanged(string package)
  176. {
  177. try
  178. {
  179. package = package.ToUpper();
  180. string[] msgs = Regex.Split(package, delimiter);
  181. foreach (string msg in msgs)
  182. {
  183. if (msg.Length > 0)
  184. {
  185. bool completed = false;
  186. string resp = msg;
  187. lock (_locker)
  188. {
  189. if (_foregroundHandler != null && _foregroundHandler.OnMessage(ref _socket, resp, out completed))
  190. {
  191. _foregroundHandler = null;
  192. }
  193. }
  194. }
  195. }
  196. }
  197. catch (ExcuteFailedException ex)
  198. {
  199. EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
  200. _foregroundHandler = null;
  201. }
  202. catch (InvalidPackageException ex)
  203. {
  204. EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
  205. _foregroundHandler = null;
  206. }
  207. catch (System.Exception ex)
  208. {
  209. EV.PostMessage(Name, EventEnum.DefaultWarning, "【RFID】has exception:" + ex.ToString());
  210. }
  211. }
  212. private void OnErrorHandler(ErrorEventArgs args)
  213. {
  214. ReadFailed();
  215. _commErr = true;
  216. Initalized = false;
  217. EV.PostMessage(Module, EventEnum.CommunicationError, Display, args.Reason);
  218. }
  219. public void SetCarrierIdReadResult(string carrierId)
  220. {
  221. FoupID = carrierId;
  222. ReadOk( carrierId);
  223. }
  224. }
  225. }