HstOcrReader.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Device;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.Util;
  10. using Aitex.Sorter.Common;
  11. using MECF.Framework.Common.Communications;
  12. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders.HST
  13. {
  14. public class HstOcrReader : BaseDevice, IDevice
  15. {
  16. #region properties
  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 Guid OCRGuid { get; set; }
  36. public bool Busy
  37. {
  38. get { return _lstHandler.Count > 0 || _connection.IsBusy; }
  39. }
  40. public bool Error
  41. {
  42. get
  43. {
  44. return _commErr || ExeError|| _connection.IsCommunicationError||!_connection.IsConnected;
  45. }
  46. }
  47. public bool ExeError { get; set; } = false;
  48. public bool ReadLaserMaker { get; set; }
  49. public string JobName { get; set; }
  50. public string LaserMaker { get; set; }
  51. public string T7Code { get; set; }
  52. public string LaserMark1 { get; set; }
  53. public string LaserMark1Score { get; set; }
  54. public string LaserMark1ReadTime { get; set; }
  55. public string LaserMark2 { get; set; }
  56. public string LaserMark2Score { get; set; }
  57. public string LaserMark2ReadTime { get; set; }
  58. public bool ReadOK { get; set; }
  59. public string ImageFileName { get; set; }
  60. public string ImageStorePath { get; set; }
  61. #endregion
  62. #region fields
  63. private HstConnection _connection;
  64. private string _address;
  65. private PeriodicJob _thread;
  66. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  67. private object _lockerHandlerList = new object();
  68. private R_TRIG _trigError = new R_TRIG();
  69. private R_TRIG _trigWarningMessage = new R_TRIG();
  70. private bool _enableLog;
  71. #endregion
  72. public const string delimiter = "\r\n";
  73. private bool _commErr = false;
  74. public HstOcrReader(string module, string name, string display, string deviceId, string address)
  75. : base(module, name, display, deviceId)
  76. {
  77. _address = address;
  78. Initalized = false;
  79. }
  80. public bool Initialize()
  81. {
  82. _address = SC.GetStringValue($"{Module}.{Name}.Address");
  83. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  84. _lstHandler.Clear();
  85. _connection = new HstConnection(_address);
  86. if (CheckAddressPort(_address))
  87. {
  88. if (_connection.Connect())
  89. {
  90. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  91. }
  92. }
  93. else EV.PostWarningLog(Module, $"{Module}.{Name} Adress is illegal");
  94. _thread = new PeriodicJob(50, OnTimer, $"{Module} MonitorHandler", true);
  95. DEVICE.Register(String.Format("{0}.{1}", Name, "ReadWaferID"), (out string reason, int time, object[] param) =>
  96. {
  97. bool bLaser = bool.Parse((string)param[0]);
  98. string jobName = (string)param[1];
  99. bool ret = Read(bLaser, jobName, out reason);
  100. if (ret)
  101. {
  102. reason = string.Format("{0}", Name, "读WaferID");
  103. return true;
  104. }
  105. return false;
  106. });
  107. DATA.Subscribe(Name, "WRIDReaderState", () => State);
  108. DATA.Subscribe(Name, "WRIDReaderBusy", () => Busy);
  109. DATA.Subscribe(Name, "LaserMaker1", () => LaserMark1);
  110. DATA.Subscribe(Name, "LaserMaker2", () => LaserMark2);
  111. Initalized = true;
  112. _lstHandler.AddLast(new OnlineHandler(this, true));
  113. return true;
  114. }
  115. //int queryintervalcount = 0;
  116. private bool OnTimer()
  117. {
  118. try
  119. {
  120. _connection.MonitorTimeout();
  121. //if (!_connection.IsConnected || _connection.IsCommunicationError)
  122. if (_connection.IsCommunicationError)
  123. {
  124. _lstHandler.Clear();
  125. return true;
  126. }
  127. if (_connection.IsBusy)
  128. return true;
  129. HandlerBase handler = null;
  130. lock (_lockerHandlerList)
  131. {
  132. if (_lstHandler.Count > 0)
  133. {
  134. handler = _lstHandler.First.Value;
  135. if (handler != null) _connection.Execute(handler);
  136. _lstHandler.RemoveFirst();
  137. }
  138. //else
  139. //{
  140. // if (queryintervalcount > 100)
  141. // {
  142. // _lstHandler.AddLast(new OnlineHandler(this, true));
  143. // _lstHandler.AddLast(new OnlineHandler(this, false));
  144. // queryintervalcount = 0;
  145. // }
  146. // else
  147. // queryintervalcount++;
  148. //}
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. LOG.Write(ex);
  154. }
  155. return true;
  156. }
  157. static bool CheckAddressPort(string s)
  158. {
  159. bool isLegal;
  160. Regex regex = new Regex(@"^((2[0-4]\d|25[0-5]|[1]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[1]?\d\d?)\:([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-6][0-5][0-5][0-3][0-5])$");//CheckAddressPort
  161. Match match = regex.Match(s);
  162. if (match.Success)
  163. {
  164. isLegal = true;
  165. }
  166. else
  167. {
  168. isLegal = false;
  169. }
  170. return isLegal;
  171. }
  172. public void Terminate()
  173. {
  174. }
  175. public void Monitor()
  176. {
  177. //if (!_onlineInited && _socket.IsConnected)
  178. //{
  179. // _onlineInited = true;
  180. // string error = string.Empty;
  181. // Online(true, out error);
  182. //}
  183. }
  184. public void Reset()
  185. {
  186. _trigError.RST = true;
  187. _trigWarningMessage.RST = true;
  188. _lstHandler.Clear();
  189. if(_connection.IsCommunicationError ||!_connection.IsConnected)
  190. {
  191. _connection.Connect();
  192. }
  193. _connection.SetCommunicationError(false, "");
  194. //_trigCommunicationError.RST = true;
  195. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  196. //_trigRetryConnect.RST = true;
  197. ExeError = false;
  198. }
  199. #region Command
  200. public bool Read(bool bLaserMaker, string jobName, out string reason)
  201. {
  202. lock(_lockerHandlerList)
  203. {
  204. _lstHandler.AddLast(new OnlineHandler(this, false));
  205. _lstHandler.AddLast(new LoadJobHandler(this, ConvetJobName(jobName)));
  206. _lstHandler.AddLast(new OnlineHandler(this, true));
  207. _lstHandler.AddLast(new ReadLMHandler(this));
  208. }
  209. ReadOK = false;
  210. ReadLaserMaker = bLaserMaker;
  211. reason = "";
  212. return true;
  213. }
  214. public bool ReadOnHostMode(bool bLaserMaker, string[] jobnames, out string reason)
  215. {
  216. lock (_lockerHandlerList)
  217. {
  218. _lstHandler.AddLast(new OnlineHandler(this, false));
  219. _lstHandler.AddLast(new LoadJobHandler(this, ConvetJobName(jobnames[0])));
  220. _lstHandler.AddLast(new OnlineHandler(this, true));
  221. _lstHandler.AddLast(new ReadLMHandler(this));
  222. }
  223. ReadOK = false;
  224. ReadLaserMaker = bLaserMaker;
  225. reason = "";
  226. return true;
  227. }
  228. #endregion
  229. private string[] GetJobName(bool bLaserMark1)
  230. {
  231. string jobs = string.Empty;
  232. if (bLaserMark1)
  233. {
  234. jobs = SC.GetStringValue("OcrReader.ReaderJob1Main");
  235. }
  236. else
  237. {
  238. jobs = SC.GetStringValue("OcrReader.ReaderJob2Main");
  239. }
  240. return jobs.Split(';');
  241. }
  242. public void OnLaskerMarkerRead()
  243. {
  244. }
  245. private string ConvetJobName(string name)
  246. {
  247. name = name.Substring(name.LastIndexOf("\\") + 1); //remove dir
  248. name = name.Substring(0, name.LastIndexOf(".")); //remove expand
  249. return name;
  250. }
  251. }
  252. }