CognexWaferIDReader.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. using Aitex.Common.Util;
  2. using Aitex.Core.Common;
  3. using Aitex.Core.RT.Device;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.Log;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.Communications;
  9. using MECF.Framework.Common.DBCore;
  10. using MECF.Framework.Common.Equipment;
  11. using MECF.Framework.Common.SubstrateTrackings;
  12. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Drawing;
  16. using System.Drawing.Imaging;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Text.RegularExpressions;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders.Cognex
  24. {
  25. public class CognexWaferIDReader:OCRReaderBaseDevice, IConnection
  26. {
  27. private CognexOCRConnection _connection;
  28. private static readonly object _lockerHandlerList = new object();
  29. private string _scRoot;
  30. private PeriodicJob _thread;
  31. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  32. private R_TRIG _trigError = new R_TRIG();
  33. private R_TRIG _trigRetryConnect = new R_TRIG();
  34. private R_TRIG _trigWarningMessage = new R_TRIG();
  35. private R_TRIG _trigActionDone = new R_TRIG();
  36. private bool _enableLog => SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  37. private string _imageStorePath;
  38. private DateTime _dtActionStart;
  39. private string _currentJob;
  40. private int m_TimelimitAck
  41. {
  42. get
  43. {
  44. if (SC.ContainsItem($"OCRReader.{Name}.TimeLimitACK"))
  45. return SC.GetValue<int>($"OCRReader.{Name}.TimeLimitACK");
  46. return 10;
  47. }
  48. }
  49. private int m_TimelimitCOM
  50. {
  51. get
  52. {
  53. if (SC.ContainsItem($"OCRReader.{Name}.TimeLimitCOM"))
  54. return SC.GetValue<int>($"OCRReader.{Name}.TimeLimitCOM");
  55. return 90;
  56. }
  57. }
  58. public override string CurrentImageFileName
  59. {
  60. get
  61. {
  62. if (SC.ContainsItem($"OCRReader.{Name}.TempImageFilePath"))
  63. {
  64. return SC.GetStringValue($"OCRReader.{Name}.TempImageFilePath");
  65. }
  66. return "";
  67. }
  68. }
  69. public bool IsLogined { get; set; }
  70. public bool IsOnline { get; private set; }
  71. public bool IsHstProtocal { get; set; }
  72. public CognexWaferIDReader(string module,string name,string scRoot):base(module,name)
  73. {
  74. _scRoot = scRoot;
  75. InitializeCognex();
  76. CheckToPostMessage((int)OCRReaderMsg.Reset, null);
  77. }
  78. public void InitializeCognex()
  79. {
  80. Address = SC.GetStringValue($"{_scRoot}.{Name}.Address");
  81. _lstHandler.Clear();
  82. _connection = new CognexOCRConnection(Address,this);
  83. _connection.EnableLog(_enableLog);
  84. if (CheckAddressPort(Address))
  85. {
  86. if (_connection.Connect())
  87. {
  88. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  89. }
  90. }
  91. else EV.PostWarningLog(Module, $"{Module}.{Name} Adress is illegal");
  92. ConnectionManager.Instance.Subscribe(Name, this);
  93. _thread = new PeriodicJob(50, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  94. var fileInfo = new FileInfo(PathManager.GetDirectory($"Logs\\{Name}"));
  95. if (fileInfo.Directory != null && !fileInfo.Directory.Exists)
  96. fileInfo.Directory.Create();
  97. if (fileInfo.Directory != null)
  98. _imageStorePath = fileInfo.FullName;
  99. if(SC.ContainsItem($"{_scRoot}.{Name}.IsHstProtocal"))
  100. {
  101. IsHstProtocal = SC.GetValue<bool>($"{_scRoot}.{Name}.IsHstProtocal");
  102. }
  103. ConnectionManager.Instance.Subscribe($"{Name}", this);
  104. }
  105. static bool CheckAddressPort(string s)
  106. {
  107. bool isLegal;
  108. 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
  109. Match match = regex.Match(s);
  110. if (match.Success)
  111. {
  112. isLegal = true;
  113. }
  114. else
  115. {
  116. isLegal = false;
  117. }
  118. return isLegal;
  119. }
  120. private HandlerBase handler = null;
  121. private int retryTime = 0;
  122. private bool OnTimer()
  123. {
  124. try
  125. {
  126. }
  127. catch (Exception ex)
  128. {
  129. LOG.Write(ex);
  130. }
  131. return true;
  132. }
  133. public void OnLaserMarkRead()
  134. {
  135. }
  136. public string Address { get; private set; }
  137. public bool IsConnected => _connection.IsConnected ;
  138. public bool Connect()
  139. {
  140. _connection.Connect();
  141. return true;
  142. }
  143. public bool Disconnect()
  144. {
  145. _connection.Disconnect();
  146. return true;
  147. }
  148. public override bool IsReady()
  149. {
  150. if (_lstHandler.Count != 0 || _connection.IsBusy)
  151. return false;
  152. return base.IsReady();
  153. }
  154. protected override bool fStartInit(object[] param)
  155. {
  156. return true;
  157. }
  158. private bool _isEnableSaveImageWithRead
  159. {
  160. get
  161. {
  162. if (SC.ContainsItem($"{_scRoot}.{Name}.EnableSaveImage"))
  163. return SC.GetValue<bool>($"{_scRoot}.{Name}.EnableSaveImage");
  164. return false;
  165. }
  166. }
  167. protected override bool fStartReadWaferID(object[] param)
  168. {
  169. _currentJob = param[0].ToString();
  170. int lasermarkindex = (int)param[1];
  171. IsReadLaserMark1 = lasermarkindex == 0;
  172. lock (_lockerHandlerList)
  173. {
  174. if(CurrentJobName != _currentJob)
  175. {
  176. _lstHandler.AddLast(new OnlineHandler(this, false, m_TimelimitAck, m_TimelimitCOM));
  177. _lstHandler.AddLast(new LoadJobHandler(this, _currentJob, m_TimelimitAck, m_TimelimitCOM));
  178. }
  179. _lstHandler.AddLast(new OnlineHandler(this, true, m_TimelimitAck, m_TimelimitCOM));
  180. _lstHandler.AddLast(new ReadWaferIDHandler(this, m_TimelimitAck, m_TimelimitCOM));
  181. //if (_isEnableSaveImageWithRead)
  182. // _lstHandler.AddLast(new SavePictureHandler(this, CurrentImageFileName, m_TimelimitAck, m_TimelimitCOM));
  183. }
  184. _dtActionStart = DateTime.Now;
  185. retryTime = 0;
  186. return true;
  187. }
  188. protected override bool fMonitorReading(object[] param)
  189. {
  190. IsBusy = false;
  191. ReadOK = false;
  192. Guid guid = Guid.NewGuid();
  193. var wafer = WaferManager.Instance.GetWafer(InstalledModule, 0);
  194. if (wafer == null) wafer = new WaferInfo();
  195. _lastWaferID = wafer.WaferID;
  196. if (!_connection.IsConnected)
  197. {
  198. Address = SC.GetStringValue($"{_scRoot}.{Name}.Address");
  199. _connection = new CognexOCRConnection(Address, this);
  200. _connection.EnableLog(_enableLog);
  201. if (CheckAddressPort(Address))
  202. {
  203. if (_connection.Connect())
  204. {
  205. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  206. }
  207. else
  208. {
  209. OnError($"{Name} Lost Communication");
  210. return false;
  211. }
  212. }
  213. else
  214. {
  215. EV.PostWarningLog(Module, $"{Module}.{Name} Adress is illegal");
  216. OnError($"{Name} Lost Communication");
  217. return false;
  218. }
  219. }
  220. if (DateTime.Now - _dtActionStart >TimeSpan.FromSeconds(TimeLimitForRead))
  221. {
  222. EV.Notify(AlarmWIDReadTimeout);
  223. CurrentLaserMark = "******";
  224. CurrentLaserMarkScore = 0;
  225. CurrentLaserMarkReadTime = (DateTime.Now - _dtActionStart).TotalSeconds.ToString();
  226. if (IsReadLaserMark1)
  227. {
  228. LaserMark1 = "******";
  229. LaserMark1Score = "0";
  230. LaserMark1ReadTime = (DateTime.Now - _dtActionStart).TotalSeconds.ToString();
  231. }
  232. else
  233. {
  234. LaserMark2 = "******";
  235. LaserMark2Score = "0";
  236. LaserMark2ReadTime = (DateTime.Now - _dtActionStart).TotalSeconds.ToString();
  237. }
  238. OCRDataRecorder.OcrReadComplete(guid.ToString(), wafer.WaferID ?? "", wafer.OriginStation.ToString() ?? "",
  239. wafer.OriginCarrierID ?? "", (wafer.OriginSlot+1).ToString() ?? "", Name, _currentJob, false, CurrentLaserMark,
  240. CurrentLaserMarkScore.ToString(), CurrentLaserMarkReadTime);
  241. _lstHandler.Clear();
  242. _connection.ForceClear();
  243. ReadOK = false;
  244. return true;
  245. }
  246. _connection.MonitorTimeout();
  247. if (_connection.IsCommunicationError && _connection.HandlerInError != null &&
  248. _connection.HandlerInError.IsAckTimeout && handler != null)
  249. {
  250. _connection.ForceClear();
  251. _lstHandler.Clear();
  252. retryTime++;
  253. EV.PostInfoLog(Module, $"{Name} start to retry read lasermark for the {retryTime} time");
  254. _lstHandler.AddLast(new OnlineHandler(this, false, m_TimelimitAck, m_TimelimitCOM));
  255. _lstHandler.AddLast(new LoadJobHandler(this, _currentJob, m_TimelimitAck, m_TimelimitCOM));
  256. _lstHandler.AddLast(new OnlineHandler(this, true, m_TimelimitAck, m_TimelimitCOM));
  257. _lstHandler.AddLast(new ReadWaferIDHandler(this, m_TimelimitAck, m_TimelimitCOM));
  258. //_dtActionStart = DateTime.Now;
  259. return false;
  260. }
  261. if(_connection.CurrentActiveHandler!= null && _connection.CurrentActiveHandler.CurrentState == EnumHandlerState.Nak)
  262. {
  263. _connection.ForceClear();
  264. _lstHandler.Clear();
  265. retryTime++;
  266. EV.PostInfoLog(Module, $"{Name} start to retry read lasermark for the {retryTime} time");
  267. _lstHandler.AddLast(new OnlineHandler(this, false, m_TimelimitAck, m_TimelimitCOM));
  268. _lstHandler.AddLast(new LoadJobHandler(this, _currentJob, m_TimelimitAck, m_TimelimitCOM));
  269. _lstHandler.AddLast(new OnlineHandler(this, true, m_TimelimitAck, m_TimelimitCOM));
  270. _lstHandler.AddLast(new ReadWaferIDHandler(this, m_TimelimitAck, m_TimelimitCOM));
  271. //_dtActionStart = DateTime.Now;
  272. return false;
  273. }
  274. if (_connection.IsBusy)
  275. return false;
  276. if (_lstHandler.Count > 0)
  277. {
  278. handler = _lstHandler.First.Value;
  279. if (handler != null)
  280. {
  281. if(handler.GetType() == typeof(SavePictureHandler))
  282. {
  283. _connection.EnableLog(false);
  284. }
  285. else
  286. _connection.EnableLog(_enableLog);
  287. _connection.Execute(handler);
  288. }
  289. _lstHandler.RemoveFirst();
  290. return false;
  291. }
  292. if (CurrentLaserMarkScore < ScoreThresholdToSaveImage)
  293. {
  294. CurrentLaserMark = "******";
  295. }
  296. else
  297. {
  298. ReadOK = true;
  299. }
  300. if (CurrentLaserMark.Length > KeepLaserMarkCharatersNumber)
  301. {
  302. CurrentLaserMark = CurrentLaserMark.Substring(0, KeepLaserMarkCharatersNumber);
  303. }
  304. OCRDataRecorder.OcrReadComplete(guid.ToString(), wafer.WaferID ?? "", wafer.OriginStation.ToString() ?? "",
  305. wafer.OriginCarrierID ?? "", (wafer.OriginSlot+1).ToString() ?? "", Name, _currentJob, !CurrentLaserMark.Contains("*"), CurrentLaserMark,
  306. CurrentLaserMarkScore.ToString(), CurrentLaserMarkReadTime);
  307. _connection.EnableLog(_enableLog);
  308. return true;
  309. }
  310. protected override bool fStartReadParameter(object[] param)
  311. {
  312. readparameter = param[0].ToString();
  313. switch (readparameter)
  314. {
  315. case "JobList":
  316. if (SC.ContainsItem($"{ _scRoot}.{ Name}.DirectoryForJob") && !string.IsNullOrEmpty(SC.GetStringValue($"{ _scRoot}.{ Name}.DirectoryForJob")))
  317. {
  318. string jobpath = SC.GetStringValue($"{ _scRoot}.{ Name}.DirectoryForJob");
  319. string[] files = Directory.GetFiles(jobpath);
  320. JobFileList = new List<string>();
  321. JobFileList.Clear();
  322. foreach(string strfile in files)
  323. {
  324. FileInfo f = new FileInfo(strfile);
  325. if(f.Extension == ".job")
  326. {
  327. JobFileList.Add(f.Name);
  328. }
  329. }
  330. }
  331. else
  332. {
  333. lock (_lockerHandlerList)
  334. {
  335. _lstHandler.AddLast(new GetJobListHandler(this, m_TimelimitAck, m_TimelimitCOM));
  336. }
  337. }
  338. break;
  339. default:
  340. break;
  341. }
  342. return true; ;
  343. }
  344. private string readparameter = "";
  345. protected override bool fMonitorReadParameter(object[] param)
  346. {
  347. IsBusy = false;
  348. if (readparameter == "JobList" && SC.ContainsItem($"{ _scRoot}.{ Name}.DirectoryForJob") && !string.IsNullOrEmpty(SC.GetStringValue($"{ _scRoot}.{ Name}.DirectoryForJob")))
  349. {
  350. return true;
  351. }
  352. if (_connection.IsBusy)
  353. return false;
  354. lock (_lockerHandlerList)
  355. {
  356. _connection.MonitorTimeout();
  357. if (_connection.IsCommunicationError && _connection.HandlerInError != null &&
  358. _connection.HandlerInError.IsAckTimeout && handler != null)
  359. {
  360. if (retryTime++ < 5)
  361. {
  362. _connection.ForceClear();
  363. _connection.Execute(handler);
  364. return false;
  365. }
  366. LaserMark1 = "******";
  367. LaserMark1Score = "0";
  368. LaserMark1ReadTime = (DateTime.Now - _dtActionStart).TotalSeconds.ToString();
  369. OnError("Retry Failed.");
  370. }
  371. if (_lstHandler.Count > 0)
  372. {
  373. handler = _lstHandler.First.Value;
  374. if (handler != null) _connection.Execute(handler);
  375. _lstHandler.RemoveFirst();
  376. retryTime = 0;
  377. return false;
  378. }
  379. }
  380. return true;
  381. }
  382. protected override bool fStartReset(object[] param)
  383. {
  384. _trigError.RST = true;
  385. _trigWarningMessage.RST = true;
  386. _lstHandler.Clear();
  387. _connection.ForceClear();
  388. if (_connection.IsCommunicationError || !_connection.IsConnected || Address != SC.GetStringValue($"{_scRoot}.{Name}.Address"))
  389. {
  390. Address = SC.GetStringValue($"{_scRoot}.{Name}.Address");
  391. _connection = new CognexOCRConnection(Address, this);
  392. _connection.Connect();
  393. }
  394. _connection.SetCommunicationError(false, "");
  395. //_trigCommunicationError.RST = true;
  396. //TimeLimitForRead = SC.GetValue<int>($"{_scRoot}.{Name}.TimeLimitForWID");
  397. _connection.EnableLog(_enableLog);
  398. IsOnline = false;
  399. CurrentJobName = "";
  400. return true;
  401. }
  402. protected override bool fMonitorReset(object[] param)
  403. {
  404. IsBusy = false;
  405. if (!_connection.IsConnected)
  406. OnError($"{Name} lost communication");
  407. return true;
  408. }
  409. protected override bool fStartSetParameter(object[] param)
  410. {
  411. string paraname = param[0].ToString();
  412. switch(paraname)
  413. {
  414. case "SetOnline":
  415. break;
  416. }
  417. return true; ;
  418. }
  419. protected override bool fSetParameterComplete(object[] param)
  420. {
  421. return true;
  422. }
  423. public override string[] GetJobFileList()
  424. {
  425. lock(_lockerHandlerList)
  426. {
  427. _lstHandler.AddLast(new GetJobListHandler(this, m_TimelimitAck, m_TimelimitCOM));
  428. }
  429. int delaytime = 0;
  430. while((_lstHandler.Count !=0 || _connection.IsBusy)&& delaytime<10)
  431. {
  432. Thread.Sleep(500);
  433. delaytime++;
  434. }
  435. return JobFileList.ToArray();
  436. }
  437. protected override bool fStartSavePicture(object[] param)
  438. {
  439. _dtActionStart = DateTime.Now;
  440. lock (_lockerHandlerList)
  441. {
  442. _lstHandler.AddLast(new SavePictureHandler(this, CurrentImageFileName, m_TimelimitAck, m_TimelimitCOM));
  443. }
  444. return true;
  445. }
  446. private bool _isSaveFtpImage
  447. {
  448. get
  449. {
  450. if (SC.ContainsItem($"{ _scRoot}.{ Name}.EnableHandleFtpImage"))
  451. return SC.GetValue<bool>($"{ _scRoot}.{ Name}.EnableHandleFtpImage");
  452. return true;
  453. }
  454. }
  455. private string _lastWaferID;
  456. protected override bool fMonitorSavingPicture(object[] param)
  457. {
  458. IsBusy = false;
  459. _connection.EnableLog(false);
  460. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(TimeLimitForRead))
  461. {
  462. EV.Notify(AlarmWIDSavePictureFail);
  463. return true;
  464. }
  465. _connection.MonitorTimeout();
  466. if (_connection.IsBusy)
  467. return false;
  468. if (_lstHandler.Count > 0)
  469. {
  470. handler = _lstHandler.First.Value;
  471. if (handler != null) _connection.Execute(handler);
  472. _lstHandler.RemoveFirst();
  473. retryTime = 0;
  474. return false;
  475. }
  476. if (_connection.HandlerInError != null && _connection.HandlerInError.IsAckTimeout && handler != null)
  477. {
  478. }
  479. _connection.EnableLog(_enableLog);
  480. LOG.Write($"Save image for {_lastWaferID} successfully, cost {(DateTime.Now - _dtActionStart).TotalSeconds} seconds.");
  481. return true;
  482. }
  483. private void DeleteEarlyImageFile(int fileCount)
  484. {
  485. try
  486. {
  487. string[] temps = CurrentImageFileName.Split('\\');
  488. string imagepath = CurrentImageFileName.Replace("\\" + temps.LastOrDefault(), "");
  489. if (string.IsNullOrEmpty(imagepath)) return;
  490. string[] fpath = Directory.GetFiles(imagepath, "*.bmp", SearchOption.AllDirectories);
  491. Dictionary<string, DateTime> fCreateDate = new Dictionary<string, DateTime>();
  492. for (int i = 0; i < fpath.Length; i++)
  493. {
  494. FileInfo fi = new FileInfo(fpath[i]);
  495. fCreateDate[fpath[i]] = fi.CreationTime;
  496. }
  497. fCreateDate = fCreateDate.OrderBy(f => f.Value).ToDictionary(f => f.Key, f => f.Value);
  498. int fCount = fCreateDate.Count;
  499. while (fCount > fileCount)
  500. {
  501. string strFile = fCreateDate.First().Key;
  502. if (strFile == CurrentImageFileName)
  503. {
  504. fCreateDate.Remove(strFile);
  505. fCount = fCreateDate.Count;
  506. continue;
  507. }
  508. File.Delete(strFile);
  509. fCreateDate.Remove(strFile);
  510. fCount = fCreateDate.Count;
  511. LOG.Write($"Success to delete file:{strFile}");
  512. }
  513. DeleteEmptyDirectory(imagepath);
  514. LOG.Write($"Complete to delete file");
  515. }
  516. catch(Exception ex)
  517. {
  518. }
  519. }
  520. public static void DeleteEmptyDirectory(String storagepath)
  521. {
  522. DirectoryInfo dir = new DirectoryInfo(storagepath);
  523. DirectoryInfo[] subdirs = dir.GetDirectories("*.*", SearchOption.AllDirectories);
  524. foreach (DirectoryInfo subdir in subdirs)
  525. {
  526. FileSystemInfo[] subFiles = subdir.GetFileSystemInfos();
  527. if (subFiles.Count() == 0)
  528. {
  529. subdir.Delete();
  530. }
  531. }
  532. }
  533. public void SaveImage(string filename,string imgString)
  534. {
  535. try
  536. {
  537. //DeleteEarlyImageFile();
  538. string foldername = Path.GetDirectoryName(filename);
  539. if(!Directory.Exists(foldername))
  540. {
  541. Directory.CreateDirectory(foldername);
  542. }
  543. var byBitmap = HexStringToBytes(imgString);
  544. var bmp = new Bitmap(new MemoryStream(byBitmap));
  545. if (File.Exists(filename))
  546. File.Delete(filename);
  547. bmp.Save($"{filename}", ImageFormat.Bmp);
  548. EV.PostInfoLog("WIDReader", $"Picture saved, file name is {filename}.");
  549. if (CurrentLaserMark.Contains("*"))
  550. {
  551. if (SavePictureOption == PictureOptionEnum.All || SavePictureOption == PictureOptionEnum.Failed)
  552. {
  553. string[] temps = CurrentImageFileName.Split('\\');
  554. string pathname = CurrentImageFileName.Replace(temps.LastOrDefault(), "") + "FailedPictures\\"
  555. + DateTime.Now.ToString("yyyy-MM-dd");
  556. if (!Directory.Exists(pathname))
  557. {
  558. Directory.CreateDirectory(pathname);
  559. }
  560. string failedImage = pathname + "\\" + "Failed_" + _lastWaferID + "_" + string.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + ".bmp";
  561. bmp.Save(failedImage, ImageFormat.Bmp);
  562. EV.PostInfoLog("WIDReader", $"Picture saved, file name is {failedImage}.");
  563. }
  564. }
  565. else
  566. {
  567. if (SavePictureOption == PictureOptionEnum.All || SavePictureOption == PictureOptionEnum.Success)
  568. {
  569. string[] temps = CurrentImageFileName.Split('\\');
  570. string pathname = CurrentImageFileName.Replace(temps.LastOrDefault(), "") + "SuccessPictures\\"
  571. + DateTime.Now.ToString("yyyy-MM-dd");
  572. if (!Directory.Exists(pathname))
  573. {
  574. Directory.CreateDirectory(pathname);
  575. }
  576. string sucessImage = pathname + "\\" + "Success_" +
  577. _lastWaferID + "_" + string.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + ".bmp";
  578. bmp.Save(sucessImage, ImageFormat.Bmp);
  579. EV.PostInfoLog("WIDReader", $"Picture saved, file name is {sucessImage}.");
  580. }
  581. }
  582. DeleteEarlyImageFile(PicturesQuanlityLimit);
  583. bmp.Dispose();
  584. }
  585. catch(Exception ex)
  586. {
  587. LOG.Write($"Save image for wafer:{_lastWaferID} to {filename} exception.");
  588. LOG.Write(ex);
  589. }
  590. }
  591. private static byte[] HexStringToBytes(string hex)
  592. {
  593. byte[] data = new byte[hex.Length / 2];
  594. int j = 0;
  595. for (int i = 0; i < hex.Length; i += 2)
  596. {
  597. data[j] = Convert.ToByte(hex.Substring(i, 2), 16);
  598. ++j;
  599. }
  600. return data;
  601. }
  602. protected override void OnWaferIDRead(string wid, string score, string readtime)
  603. {
  604. if(DeviceState == OCRReaderStateEnum.ReadWaferID)
  605. base.OnWaferIDRead(wid, score, readtime);
  606. }
  607. public void SetLogEnable(bool enable)
  608. {
  609. if(enable)
  610. _connection.EnableLog(_enableLog);
  611. else
  612. _connection.EnableLog(enable);
  613. }
  614. }
  615. }