Wid110Lib.cs 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. // ============================================================================
  2. // Copyright (c) 2010 IOSS GmbH
  3. // All Rights Reserved.
  4. // ============================================================================
  5. // ============================================================================
  6. //
  7. // Wid110Lib - WID110 C# based library
  8. //
  9. // ============================================================================
  10. //
  11. // File: Wid110Lib.cs Type: Implementation
  12. //
  13. // Date: 05.02.2010 Last Change: 09.11.2011
  14. //
  15. // Author: Thomas M. Schlageter
  16. // Silvio Robel
  17. //
  18. // Methods: Wid110Lib - constructor
  19. // ~Wid110Lib - destructor
  20. //
  21. // FCreateDll - create WID110Lib instance
  22. // FDestroyDll - destroy WID110Lib instance
  23. // FExit - terminate connection and exit
  24. // FGetCodeQualityBCR - get code qualitiy for BCR codes
  25. // FGetCodeQualityDMR - get code qualitiy for DMR codes
  26. // FGetCodeQualityLast - get code qualitiy for LAST code
  27. // FGetCodeQualityOCR - get code qualitiy for OCR codes
  28. // FGetErrorDescription - get error description
  29. // FGetLastError - get the last error number
  30. // FGetVersionParam - return sensor/interface version
  31. // FGetVersion - return library version
  32. // FGetWaferId - get the last BCR/OCR decode result
  33. // FInit - initialize library and connect
  34. // FIsInitialized - check for initialized state
  35. // FLiveGetImage - take single image with parameters
  36. // FLiveRead - perform a live read
  37. // FLoadRecipes - load parameters by sending a file
  38. // FLoadRecipesToSlot - load parameters by sending a file
  39. // FProcessGetImage - get image from process trigger
  40. // FProcessRead - perform a process read
  41. // FSwitchOverlay - switch overlay on/off
  42. // FGetCodeTime - get certain time parameter
  43. //
  44. //
  45. // Auxiliary methods:
  46. //
  47. // getErrno - get error number
  48. // getLastExcp - get the last exception message
  49. // getReadOK - get result read state
  50. // getTmpImage - return temporary image name
  51. // isException - return exception state
  52. //
  53. // ============================================================================
  54. using System;
  55. using System.IO;
  56. using System.Collections;
  57. using System.Collections.Generic;
  58. using System.Data;
  59. using System.Drawing;
  60. using System.Reflection;
  61. using System.Text;
  62. using System.Runtime.InteropServices;
  63. using System.Net.NetworkInformation;
  64. using System.Drawing.Imaging;
  65. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders.IOSS
  66. {
  67. public class Wid110Lib : IDisposable
  68. {
  69. #region [ Private Fields ]
  70. // ====================================================================
  71. /// <summary>
  72. /// Private member variables.
  73. /// </summary>
  74. // ====================================================================
  75. private string version;
  76. private string versParam;
  77. private string tmpImage = Wid110LibConst.tmpImage;
  78. private string lastExcp;
  79. private int errno = Wid110LibConst.ecNone;
  80. private int readOK = Wid110LibConst.rcError;
  81. private IntPtr dll;
  82. #endregion
  83. #region [ DLL Method Prototypes ]
  84. // ====================================================================
  85. /// <summary>
  86. /// Imported method prototypes.
  87. /// </summary>
  88. // ====================================================================
  89. [DllImport("wid110Lib.dll")]
  90. private static extern IntPtr FuncCreateDll();
  91. [DllImport("wid110Lib.dll")]
  92. private static extern int FuncDestroyDll(IntPtr objptr);
  93. [DllImport("wid110Lib.dll")]
  94. private static extern int FuncInit(IntPtr objptr,
  95. string cpIPAddress);
  96. [DllImport("wid110Lib.dll")]
  97. private static extern int FuncIsInitialized(IntPtr objptr);
  98. [DllImport("wid110Lib.dll")]
  99. private static extern int FuncCheckConnection(IntPtr objptr,
  100. string cpIPAddress,
  101. int timeout);
  102. [DllImport("wid110Lib.dll")]
  103. private static extern int FuncGetVersionParam(IntPtr objptr,
  104. StringBuilder cVersion,
  105. int nMaxLen);
  106. [DllImport("wid110Lib.dll")]
  107. private static extern int FuncGetVersion(IntPtr objptr,
  108. StringBuilder cVersion,
  109. int nMaxLen);
  110. [DllImport("wid110Lib.dll")]
  111. private static extern int FuncSwitchOverlay(IntPtr objptr,
  112. int bOnOff);
  113. [DllImport("wid110Lib.dll")]
  114. private static extern int FuncExit(IntPtr objptr);
  115. // exchange parameter functions
  116. [DllImport("wid110Lib.dll")]
  117. private static extern int FuncSetImageCapture(IntPtr objptr, IntPtr pCapture);
  118. [DllImport("wid110Lib.dll")]
  119. private static extern int FuncGetImageCapture(IntPtr objptr, IntPtr pCapture);
  120. [DllImport("wid110Lib.dll")]
  121. private static extern int FuncSetParamOCR(IntPtr objptr, IntPtr pOcr, IntPtr capture);
  122. [DllImport("wid110Lib.dll")]
  123. private static extern int FuncGetParamOCR(IntPtr objptr, IntPtr pOcr, IntPtr capture);
  124. [DllImport("wid110Lib.dll")]
  125. private static extern int FuncSetParamBCR(IntPtr objptr, IntPtr pBcr, IntPtr capture);
  126. [DllImport("wid110Lib.dll")]
  127. private static extern int FuncGetParamBCR(IntPtr objptr, IntPtr pBcr, IntPtr capture);
  128. [DllImport("wid110Lib.dll")]
  129. private static extern int FuncSetParamDMR(IntPtr objptr, IntPtr pDmr, IntPtr capture);
  130. [DllImport("wid110Lib.dll")]
  131. private static extern int FuncGetParamDMR(IntPtr objptr, IntPtr pDmr, IntPtr capture);
  132. [DllImport("wid110Lib.dll")]
  133. private static extern int FuncLiveGetImage(IntPtr objptr,
  134. string cpFileName,
  135. int nChannel,
  136. int nIntensity,
  137. int nColor);
  138. [DllImport("wid110Lib.dll")]
  139. private static extern int FuncLiveRead(IntPtr objptr);
  140. [DllImport("wid110Lib.dll")]
  141. private static extern int FuncLiveGetImageRead(IntPtr objptr,
  142. string cpFileName,
  143. int nChannel,
  144. int nIntensity,
  145. int nColor);
  146. [DllImport("wid110Lib.dll")]
  147. private static extern int FuncProcessRead(IntPtr objptr);
  148. [DllImport("wid110Lib.dll")]
  149. private static extern int FuncProcessGetImage(IntPtr objptr,
  150. string cpFileName,
  151. int nTypeImage);
  152. [DllImport("wid110Lib.dll")]
  153. private static extern int FuncGetWaferId(IntPtr objptr,
  154. StringBuilder cReadId,
  155. int nMaxLen,
  156. IntPtr bReadSuccessful);
  157. [DllImport("wid110Lib.dll")]
  158. private static extern int FuncGetCodeQualityOCR(IntPtr objptr,
  159. IntPtr pnQuality);
  160. [DllImport("wid110Lib.dll")]
  161. private static extern int FuncGetCodeQualityBCR(IntPtr objptr,
  162. IntPtr pnQuality);
  163. [DllImport("wid110Lib.dll")]
  164. private static extern int FuncGetCodeQualityDMR(IntPtr objptr,
  165. IntPtr pnQuality);
  166. [DllImport("wid110Lib.dll")]
  167. private static extern int FuncGetCodeQualityLast(IntPtr objptr,
  168. IntPtr pnQuality);
  169. [DllImport("wid110Lib.dll")]
  170. private static extern int FuncGetCodeTime(IntPtr objptr,
  171. IntPtr pnTime);
  172. [DllImport("wid110Lib.dll")]
  173. private static extern int FuncLoadRecipes(IntPtr objptr,
  174. string cpFilePath,
  175. string cpFilename);
  176. [DllImport("wid110Lib.dll")]
  177. private static extern int FuncLoadRecipesToSlot(IntPtr objptr,
  178. string cpFilePath,
  179. string cpFilename,
  180. int nSlot);
  181. [DllImport("wid110Lib.dll")]
  182. private static extern int FuncSaveJob(IntPtr objptr,
  183. string cpFilePath,
  184. string cpFilename);
  185. [DllImport("wid110Lib.dll")]
  186. private static extern int FuncGetLastError(IntPtr objptr);
  187. [DllImport("wid110Lib.dll")]
  188. private static extern int FuncGetErrorDescription(IntPtr objptr,
  189. int nError,
  190. StringBuilder strText,
  191. int nTextLength);
  192. // teachin functions
  193. [DllImport("wid110Lib.dll")]
  194. private static extern int FuncTeachingBCR(IntPtr objptr);
  195. [DllImport("wid110Lib.dll")]
  196. private static extern int FuncTeachingOCR(IntPtr objptr);
  197. [DllImport("wid110Lib.dll")]
  198. private static extern int FuncTeachingDMR(IntPtr objptr);
  199. [DllImport("wid110Lib.dll")]
  200. private static extern int FuncConfigureBCR(IntPtr objptr, string cName);
  201. [DllImport("wid110Lib.dll")]
  202. private static extern int FuncConfigureOCR(IntPtr objptr, string cName);
  203. [DllImport("wid110Lib.dll")]
  204. private static extern int FuncConfigureDMR(IntPtr objptr, string cName);
  205. [DllImport("wid110Lib.dll")]
  206. private static extern int FuncConfigureDelete(IntPtr objptr, string cName);
  207. [DllImport("wid110Lib.dll")]
  208. private static extern int FuncConfigurePreset(IntPtr objptr, string cName);
  209. // image handling functions
  210. [DllImport("wid110Lib.dll")]
  211. private static extern int FuncGetImageRawData(IntPtr objptr, IntPtr pImageData);
  212. [DllImport("wid110Lib.dll")]
  213. private static extern int FuncGetImageXSize(IntPtr objptr, IntPtr pXSize);
  214. [DllImport("wid110Lib.dll")]
  215. private static extern int FuncGetImageYSize(IntPtr objptr, IntPtr pYSize);
  216. // further function for change configuration
  217. [DllImport("wid110Lib.dll")]
  218. private static extern int FuncSetFineScan(IntPtr objptr, IntPtr pMode);
  219. [DllImport("wid110Lib.dll")]
  220. private static extern int FuncGetFineScan(IntPtr objptr, IntPtr pMode);
  221. [DllImport("wid110Lib.dll")]
  222. private static extern int FuncGetFontName(IntPtr objptr, StringBuilder fName, int index);
  223. [DllImport("wid110Lib.dll")]
  224. private static extern int FuncGetConfigurationName(IntPtr objptr, StringBuilder cName, int index);
  225. [DllImport("wid110Lib.dll")]
  226. private static extern int FuncSaveJobToReader(IntPtr objptr);
  227. [DllImport("wid110Lib.dll")]
  228. private static extern int FuncGetROI(IntPtr objptr, string cName, IntPtr pWnd, IntPtr pCapture);
  229. [DllImport("wid110Lib.dll")]
  230. private static extern int FuncSetROI(IntPtr objptr, string cName, IntPtr pWnd, IntPtr pCapture);
  231. #endregion
  232. // ====================================================================
  233. /// <summary>
  234. /// Constructor: Initialize WID C# Library.
  235. /// </summary>
  236. // ====================================================================
  237. public Wid110Lib()
  238. {
  239. dll = FCreateDll();
  240. }
  241. #region IDisposable Support
  242. private bool disposedValue = false; // To detect redundant calls
  243. // ====================================================================
  244. /// <summary>
  245. /// Dispose: Free WID C# Library.
  246. /// </summary>
  247. // ====================================================================
  248. protected virtual void Dispose(bool disposing)
  249. {
  250. if (!disposedValue)
  251. {
  252. if (disposing)
  253. {
  254. // Dispose managed state (managed objects).
  255. }
  256. FDestroyDll(dll);
  257. dll = IntPtr.Zero;
  258. disposedValue = true;
  259. }
  260. }
  261. // ====================================================================
  262. /// <summary>
  263. /// Finalizer: Free WID C# Library (Internal Implementation)
  264. /// </summary>
  265. // ====================================================================
  266. ~Wid110Lib()
  267. {
  268. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  269. Dispose(false);
  270. }
  271. // This code added to correctly implement the disposable pattern.
  272. public void Dispose()
  273. {
  274. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  275. Dispose(true);
  276. GC.SuppressFinalize(this);
  277. }
  278. #endregion
  279. // ====================================================================
  280. /// <summary>
  281. /// Create WID C# Library instance.
  282. /// </summary>
  283. /// <return> DLL if created, NULL upon error.</return>
  284. // ====================================================================
  285. private IntPtr FCreateDll()
  286. {
  287. IntPtr d = IntPtr.Zero;
  288. lastExcp = "";
  289. errno = Wid110LibConst.ecNone;
  290. try
  291. {
  292. d = FuncCreateDll();
  293. }
  294. catch (Exception e)
  295. {
  296. lastExcp = e.ToString();
  297. }
  298. return d;
  299. }
  300. // ====================================================================
  301. /// <summary>
  302. /// Destroy WID C# Library instance.
  303. /// </summary>
  304. /// <param name="dll"> DLL to destroy.</param>
  305. /// <return> true if destroyed.</return>
  306. // ====================================================================
  307. private bool FDestroyDll(IntPtr dll)
  308. {
  309. try
  310. {
  311. if (HandleDLLStatus(FuncDestroyDll(dll))) return true;
  312. }
  313. catch (Exception e) { lastExcp = e.ToString(); }
  314. return false;
  315. }
  316. // ====================================================================
  317. /// <summary>
  318. /// Initialize library and connect to IP.
  319. /// </summary>
  320. /// <param name="ip"> IP to connect.</param>
  321. /// <return> true if connected.</return>
  322. // ====================================================================
  323. public bool FInit(string ip)
  324. {
  325. try
  326. {
  327. Ping PingHandler = new Ping();
  328. PingReply reply = PingHandler.Send(ip, 1000);
  329. if (reply.Status == IPStatus.Success)
  330. {
  331. if (HandleDLLStatus(FuncInit(dll, ip))) return true;
  332. }
  333. else throw new IOException("IP Address " + ip + " not reachable!");
  334. }
  335. catch (Exception e) { lastExcp = e.ToString(); }
  336. return false;
  337. }
  338. // ====================================================================
  339. /// <summary>
  340. /// Check library state.
  341. /// </summary>
  342. /// <return> true if initialized.</return>
  343. // ====================================================================
  344. public bool FIsInitialized()
  345. {
  346. try
  347. {
  348. if (HandleDLLStatus(FuncIsInitialized(dll)))
  349. return true;
  350. }
  351. catch (Exception e) { lastExcp = e.ToString(); }
  352. return false;
  353. }
  354. // ====================================================================
  355. /// <summary>
  356. /// Check reader connection
  357. /// </summary>
  358. /// <param name="ip">Reader IP Address</param>
  359. /// <param name="timeout_s">Timeout in seconds</param>
  360. /// <return> true if connected.</return>
  361. // ====================================================================
  362. public bool FCheckConnection(string ip, int timeout_s)
  363. {
  364. try
  365. {
  366. if (HandleDLLStatus(FuncCheckConnection(dll, ip, timeout_s))) return true;
  367. }
  368. catch (Exception e) { lastExcp = e.ToString(); }
  369. return false;
  370. }
  371. // ====================================================================
  372. /// <summary>
  373. /// Terminate connection.
  374. /// </summary>
  375. /// <return> true if disconnected.</return>
  376. // ====================================================================
  377. public bool FExit()
  378. {
  379. try
  380. {
  381. if (HandleDLLStatus(FuncExit(dll)))
  382. return true;
  383. }
  384. catch (Exception e) { lastExcp = e.ToString(); }
  385. return false;
  386. }
  387. // ====================================================================
  388. /// <summary>
  389. /// Set the image acquisition parameter to the reader.
  390. /// </summary>
  391. /// <param name="capture">Image capturing parameters. <see cref="WID_CAPTURE"/></param>
  392. /// <returns>true if no error.</returns>
  393. // ====================================================================
  394. public bool FSetImageCapture(WID_CAPTURE capture)
  395. {
  396. IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
  397. try
  398. {
  399. Marshal.StructureToPtr(capture, ptr, false);
  400. if (HandleDLLStatus(FuncSetImageCapture(dll, ptr))) return true;
  401. }
  402. catch (Exception e) { lastExcp = e.ToString(); }
  403. finally
  404. {
  405. Marshal.FreeHGlobal(ptr);
  406. }
  407. return false;
  408. }
  409. // ====================================================================
  410. /// <summary>
  411. /// Get the image acquisition parameter from reader.
  412. /// </summary>
  413. /// <returns>Image capturing parameters. <see cref="WID_CAPTURE"/></returns>
  414. // ====================================================================
  415. public WID_CAPTURE FGetImageCapture()
  416. {
  417. WID_CAPTURE capture = new WID_CAPTURE();
  418. IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
  419. try
  420. {
  421. Marshal.StructureToPtr(capture, ptr, false);
  422. if (HandleDLLStatus(FuncGetImageCapture(dll, ptr)))
  423. {
  424. capture = (WID_CAPTURE)Marshal.PtrToStructure(ptr, typeof(WID_CAPTURE));
  425. return capture;
  426. }
  427. }
  428. catch (Exception e) { lastExcp = e.ToString(); }
  429. finally
  430. {
  431. Marshal.FreeHGlobal(ptr);
  432. }
  433. return capture;
  434. }
  435. // ====================================================================
  436. /// <summary>
  437. /// Get library version.
  438. /// </summary>
  439. /// <param name="v"> WID110 library version.</param>
  440. // ====================================================================
  441. public string FGetVersion()
  442. {
  443. try
  444. {
  445. version = "";
  446. StringBuilder sb = new StringBuilder("", Wid110LibConst.versLenCS);
  447. if (HandleDLLStatus(FuncGetVersion(dll, sb, Wid110LibConst.versLenC)))
  448. {
  449. version = sb.ToString();
  450. }
  451. }
  452. catch (Exception e) { lastExcp = e.ToString(); }
  453. return version;
  454. }
  455. // ====================================================================
  456. /// <summary>
  457. /// Get sensor interface version.
  458. /// </summary>
  459. /// <return> WID110 interface version.</return>
  460. // ====================================================================
  461. public string FGetVersionParam()
  462. {
  463. try
  464. {
  465. versParam = "";
  466. StringBuilder sb = new StringBuilder("", Wid110LibConst.versLenCS);
  467. if (HandleDLLStatus(FuncGetVersionParam(dll, sb, Wid110LibConst.versLenC)))
  468. {
  469. versParam = sb.ToString();
  470. }
  471. }
  472. catch (Exception e) { lastExcp = e.ToString(); }
  473. return versParam;
  474. }
  475. // ====================================================================
  476. /// <summary>
  477. /// Change overlay flag.
  478. /// </summary>
  479. /// <param name="o"> overlay flag.</param>
  480. /// <return> true if no error.</return>
  481. // ====================================================================
  482. public bool FSwitchOverlay(bool o)
  483. {
  484. try
  485. {
  486. if (HandleDLLStatus(FuncSwitchOverlay(dll, Convert.ToInt32(o)))) return true;
  487. }
  488. catch (Exception e) { lastExcp = e.ToString(); }
  489. return false;
  490. }
  491. // ====================================================================
  492. /// <summary>
  493. /// Perform a live read using the temporary settings from an
  494. /// earlier 'FLiveGetImage()' call.
  495. /// </summary>
  496. /// <return> true if done.</return>
  497. // ====================================================================
  498. public bool FLiveRead()
  499. {
  500. try
  501. {
  502. if (HandleDLLStatus(FuncLiveRead(dll))) return true;
  503. }
  504. catch (Exception e) { lastExcp = e.ToString(); }
  505. return false;
  506. }
  507. // ====================================================================
  508. /// <summary>
  509. /// Perform a live read using the given parameters, and decode.
  510. /// </summary>
  511. /// <param name="name"> name to save image to.</param>
  512. /// <param name="channel"> illumination channel.</param>
  513. /// <param name="intensity"> illumination intensity.</param>
  514. /// <param name="color"> illumination color.</param>
  515. /// <return> true if done.</return>
  516. // ====================================================================
  517. public bool FLiveGetImageRead(string name,
  518. int channel,
  519. int intensity,
  520. int color)
  521. {
  522. try
  523. {
  524. if (HandleDLLStatus(FuncLiveGetImageRead(dll, name, channel, intensity, color))) return true;
  525. }
  526. catch (Exception e) { lastExcp = e.ToString(); }
  527. return false;
  528. }
  529. // ====================================================================
  530. /// <summary>
  531. /// Perform a process read..
  532. /// </summary>
  533. /// <return> true if done.</return>
  534. // ====================================================================
  535. public bool FProcessRead()
  536. {
  537. try
  538. {
  539. if (HandleDLLStatus(FuncProcessRead(dll))) return true;
  540. }
  541. catch (Exception e) { lastExcp = e.ToString(); }
  542. return false;
  543. }
  544. // ====================================================================
  545. /// <summary>
  546. /// Get code quality for OCR codes.
  547. /// </summary>
  548. /// <return> code quality, or rsltNoCodeQuality upon failure.</return>
  549. // ====================================================================
  550. public int FGetCodeQualityOCR()
  551. {
  552. IntPtr q = Marshal.AllocHGlobal(sizeof(int));
  553. int rc = Wid110LibConst.rsltNoCodeQuality;
  554. try
  555. {
  556. if (HandleDLLStatus(FuncGetCodeQualityOCR(dll, q)))
  557. {
  558. rc = Marshal.ReadInt32(q);
  559. }
  560. }
  561. catch (Exception e) { lastExcp = e.ToString(); }
  562. finally { Marshal.FreeHGlobal(q); }
  563. return rc;
  564. }
  565. // ====================================================================
  566. /// <summary>
  567. /// Get code quality for BCR codes.
  568. /// </summary>
  569. /// <return> code quality, or rsltNoCodeQuality upon failure.</return>
  570. // ====================================================================
  571. public int FGetCodeQualityBCR()
  572. {
  573. IntPtr q = Marshal.AllocHGlobal(sizeof(int));
  574. int rc = Wid110LibConst.rsltNoCodeQuality;
  575. try
  576. {
  577. if (HandleDLLStatus(FuncGetCodeQualityBCR(dll, q)))
  578. {
  579. rc = Marshal.ReadInt32(q);
  580. }
  581. }
  582. catch (Exception e) { lastExcp = e.ToString(); }
  583. finally { Marshal.FreeHGlobal(q); }
  584. return rc;
  585. }
  586. // ====================================================================
  587. /// <summary>
  588. /// Get code quality for DMR codes.
  589. /// </summary>
  590. /// <return> code quality, or rsltNoCodeQuality upon failure.</return>
  591. // ====================================================================
  592. public int FGetCodeQualityDMR()
  593. {
  594. IntPtr q = Marshal.AllocHGlobal(sizeof(int));
  595. int rc = Wid110LibConst.rsltNoCodeQuality;
  596. try
  597. {
  598. if (HandleDLLStatus(FuncGetCodeQualityDMR(dll, q)))
  599. {
  600. rc = Marshal.ReadInt32(q);
  601. }
  602. }
  603. catch (Exception e) { lastExcp = e.ToString(); }
  604. finally { Marshal.FreeHGlobal(q); }
  605. return rc;
  606. }
  607. // ====================================================================
  608. /// <summary>
  609. /// Get code quality for LAST code.
  610. /// </summary>
  611. /// <return> code quality, or rsltNoCodeQuality upon failure.</return>
  612. // ====================================================================
  613. public int FGetCodeQualityLast()
  614. {
  615. IntPtr q = Marshal.AllocHGlobal(sizeof(int));
  616. int rc = Wid110LibConst.rsltNoCodeQuality;
  617. try
  618. {
  619. if (HandleDLLStatus(FuncGetCodeQualityLast(dll, q)))
  620. {
  621. rc = Marshal.ReadInt32(q);
  622. }
  623. }
  624. catch (Exception e) { lastExcp = e.ToString(); }
  625. finally { Marshal.FreeHGlobal(q); }
  626. return rc;
  627. }
  628. // ====================================================================
  629. /// <summary>
  630. /// Retrieve image from last process trigger.
  631. /// </summary>
  632. /// <param name="name"> file name to save image.</param>
  633. /// <param name="type"> 'pvImgBest' or 'pvImgAll'.</param>
  634. /// <return> true if OK.</return>
  635. // ====================================================================
  636. public bool FProcessGetImage(string name,
  637. int type)
  638. {
  639. try
  640. {
  641. if (HandleDLLStatus(FuncProcessGetImage(dll, name, type))) return true;
  642. }
  643. catch (Exception e) { lastExcp = e.ToString(); }
  644. return false;
  645. }
  646. // ====================================================================
  647. /// <summary>
  648. /// Get the last BCR/OCR/DMR decode result.
  649. /// </summary>
  650. /// <return> the latest read result.</return>
  651. // ====================================================================
  652. public string FGetWaferId()
  653. {
  654. StringBuilder sb = new StringBuilder(Wid110LibConst.rsltERROR, Wid110LibConst.rsltLenCS);
  655. string res = Wid110LibConst.rsltERROR;
  656. IntPtr pok = Marshal.AllocHGlobal(1);
  657. try
  658. {
  659. if (HandleDLLStatus(FuncGetWaferId(dll, sb, Wid110LibConst.rsltLenC, pok)))
  660. {
  661. readOK = Marshal.ReadByte(pok, 0);
  662. if (1 == readOK)
  663. res = Wid110LibConst.rsltREAD + sb.ToString();
  664. else
  665. res = Wid110LibConst.rsltNOREAD + sb.ToString();
  666. }
  667. }
  668. catch (Exception e) { lastExcp = e.ToString(); }
  669. finally
  670. {
  671. Marshal.FreeHGlobal(pok);
  672. }
  673. return res;
  674. }
  675. // ====================================================================
  676. /// <summary>
  677. /// Take a single image using the given parameters.
  678. /// </summary>
  679. /// <param name="name"> name to save image to.</param>
  680. /// <param name="channel"> illumination channel.</param>
  681. /// <param name="intensity"> illumination intensity.</param>
  682. /// <param name="color"> illumination color.</param>
  683. /// <return> true if OK.</return>
  684. // ====================================================================
  685. public bool FLiveGetImage(string name,
  686. int channel,
  687. int intensity,
  688. int color)
  689. {
  690. try
  691. {
  692. if (HandleDLLStatus(FuncLiveGetImage(dll, name, channel, intensity, color))) return true;
  693. }
  694. catch (Exception e) { lastExcp = e.ToString(); }
  695. return false;
  696. }
  697. // ====================================================================
  698. /// <summary>
  699. /// Saves reader configuration into a *.wid file on the pc
  700. /// </summary>
  701. /// <param name="jobFilePath"> Full path to file.</param>
  702. /// <return> true if OK.</return>
  703. // ====================================================================
  704. public bool FSaveJob(string jobFilePath)
  705. {
  706. string path = Path.GetDirectoryName(jobFilePath);
  707. string file = Path.GetFileName(jobFilePath);
  708. try
  709. {
  710. if (HandleDLLStatus(FuncSaveJob(dll, path, file))) return true;
  711. }
  712. catch (Exception e) { lastExcp = e.ToString(); }
  713. return false;
  714. }
  715. // ====================================================================
  716. /// <summary>
  717. /// Load process parameter file
  718. /// </summary>
  719. /// <param name="jobFilePath"> Full path of job file</param>
  720. /// <return> true if OK.</return>
  721. // ====================================================================
  722. public bool FLoadRecipes(string jobFilePath)
  723. {
  724. try
  725. {
  726. if (!File.Exists(jobFilePath)) throw new FileNotFoundException(jobFilePath);
  727. string path = Path.GetDirectoryName(jobFilePath);
  728. string file = Path.GetFileName(jobFilePath);
  729. if (HandleDLLStatus(FuncLoadRecipes(dll, path, file))) return true;
  730. }
  731. catch (Exception e) { lastExcp = e.ToString(); }
  732. return false;
  733. }
  734. // ====================================================================
  735. /// <summary>
  736. /// Load parameters to a certain parameter slot (if applicable)
  737. /// </summary>
  738. /// <param name="jobFilePathpath"> Full path to job file.</param>
  739. /// <param name="slot"> slot (only for ocf and led files).</param>
  740. /// <return> true if valid.</return>
  741. // ====================================================================
  742. public bool FLoadRecipesToSlot(string jobFilePath,
  743. int slot)
  744. {
  745. try
  746. {
  747. if (!File.Exists(jobFilePath)) throw new FileNotFoundException(jobFilePath);
  748. string path = Path.GetDirectoryName(jobFilePath);
  749. string file = Path.GetFileName(jobFilePath);
  750. if (HandleDLLStatus(FuncLoadRecipesToSlot(dll, path, file, slot))) return true;
  751. }
  752. catch (Exception e) { lastExcp = e.ToString(); }
  753. return false;
  754. }
  755. // ====================================================================
  756. /// <summary>
  757. /// Save Job to Reader
  758. /// </summary>
  759. /// <return>true if valid.</return>
  760. // ====================================================================
  761. public bool FSaveJobToReader()
  762. {
  763. try
  764. {
  765. if (HandleDLLStatus(FuncSaveJobToReader(dll))) return true;
  766. }
  767. catch (Exception e) { lastExcp = e.ToString(); }
  768. return false;
  769. }
  770. // ====================================================================
  771. /// <summary>
  772. /// Get code time parameter
  773. /// </summary>
  774. /// <return> overall process time or rsltNoCodeTime upon failure.</return>
  775. // ====================================================================
  776. public int FGetCodeTime()
  777. {
  778. IntPtr q = Marshal.AllocHGlobal(sizeof(int));
  779. int rc = Wid110LibConst.rsltNoCodeTime;
  780. try
  781. {
  782. if (HandleDLLStatus(FuncGetCodeTime(dll, q)))
  783. {
  784. rc = Marshal.ReadInt32(q);
  785. }
  786. }
  787. catch (Exception e) { lastExcp = e.ToString(); }
  788. finally { Marshal.FreeHGlobal(q); }
  789. return rc;
  790. }
  791. // ====================================================================
  792. /// <summary>
  793. /// Get error description.
  794. /// </summary>
  795. /// <param name="eno"> error number to translate.</param>
  796. /// <return> error description.</return>
  797. // ====================================================================
  798. public string FGetErrorDescription(int eno)
  799. {
  800. StringBuilder sb = new StringBuilder(Wid110LibConst.errDESC, Wid110LibConst.errLen);
  801. int len = Wid110LibConst.errLen;
  802. try // if lib handle is invalid, error text is generated as well
  803. {
  804. if (FuncGetErrorDescription(dll, eno, sb, len) == Wid110LibConst.rcNoError)
  805. return sb.ToString();
  806. }
  807. catch (Exception e)
  808. {
  809. lastExcp = e.ToString();
  810. }
  811. return Wid110LibConst.errDESC;
  812. }
  813. // ====================================================================
  814. /// <summary>
  815. /// Get last error number.
  816. /// </summary>
  817. /// <return> last error number.</return>
  818. // ====================================================================
  819. public int FGetLastError()
  820. {
  821. try
  822. {
  823. return FuncGetLastError(dll); // get last internal lib error
  824. }
  825. catch (Exception e)
  826. {
  827. lastExcp = e.ToString();
  828. }
  829. return errno;
  830. }
  831. // ====================================================================
  832. /// <summary>
  833. /// Handle error from Wid110.dll (Internal use only)
  834. /// </summary>
  835. /// <param name="status">return status from DLL Function</param>
  836. /// <returns></returns>
  837. // ====================================================================
  838. private bool HandleDLLStatus(int status)
  839. {
  840. lastExcp = "";
  841. switch (status)
  842. {
  843. case Wid110LibConst.rcError:
  844. errno = FuncGetLastError(dll);
  845. return false;
  846. case Wid110LibConst.rcInvObj:
  847. errno = Wid110LibConst.ecInvObj;
  848. return false;
  849. case Wid110LibConst.rcNoError:
  850. errno = Wid110LibConst.ecNone;
  851. return true;
  852. }
  853. return false;
  854. }
  855. // ====================================================================
  856. /// <summary>
  857. /// Get the last error number.
  858. /// </summary>
  859. /// <return> last error number.</return>
  860. // ====================================================================
  861. public int getErrno()
  862. {
  863. return errno;
  864. }
  865. // ====================================================================
  866. /// <summary>
  867. /// Get the result read state.
  868. /// </summary>
  869. /// <return> result read state.</return>
  870. // ====================================================================
  871. public int getReadOK()
  872. {
  873. return readOK;
  874. }
  875. // ====================================================================
  876. /// <summary>
  877. /// Get the last exception message.
  878. /// </summary>
  879. /// <return> last exception message.</return>
  880. // ====================================================================
  881. public string getLastExcp()
  882. {
  883. return lastExcp;
  884. }
  885. // ====================================================================
  886. /// <summary>
  887. /// Get temporary image name.
  888. /// </summary>
  889. /// <return> temporary image name.</return>
  890. // ====================================================================
  891. public string getTmpImage()
  892. {
  893. return tmpImage;
  894. }
  895. // ====================================================================
  896. /// <summary>
  897. /// Get exception state.
  898. /// </summary>
  899. /// <return> true if exception.</return>
  900. // ====================================================================
  901. public bool isException()
  902. {
  903. return lastExcp.Length != 0;
  904. }
  905. // ====================================================================
  906. /// <summary>
  907. /// Check error for last function call
  908. /// </summary>
  909. /// <param name="errorMesage">Return error message from Wid110Lib.dll</param>
  910. /// <returns>True if error raised.</returns>
  911. // ====================================================================
  912. public bool CheckError(out string errorMesage)
  913. {
  914. errorMesage = string.Empty;
  915. if (isException())
  916. {
  917. errorMesage = getLastExcp();
  918. return true;
  919. }
  920. if (errno == Wid110LibConst.ecNone) return false;
  921. errorMesage = FGetErrorDescription(errno);
  922. return true;
  923. }
  924. // ====================================================================
  925. /// <summary>
  926. /// Return font name on selected index
  927. /// </summary>
  928. /// <param name="index">Font index. [0 - 7]</param>
  929. /// <returns>Font name</returns>
  930. // ====================================================================
  931. public string FGetFontName(int index)
  932. {
  933. try
  934. {
  935. StringBuilder sb = new StringBuilder("", 255);
  936. if (HandleDLLStatus(FuncGetFontName(dll, sb, index)))
  937. {
  938. return sb.ToString();
  939. }
  940. }
  941. catch (Exception e) { lastExcp = e.ToString(); }
  942. return string.Empty;
  943. }
  944. // ====================================================================
  945. /// <summary>
  946. /// Set fine scanning mode flag
  947. /// </summary>
  948. /// <param name="value"></param>
  949. /// <returns></returns>
  950. // ====================================================================
  951. public bool FSetFineScan(bool value)
  952. {
  953. IntPtr q = Marshal.AllocHGlobal(sizeof(Int32));
  954. try
  955. {
  956. Marshal.WriteInt32(q, Convert.ToInt32(value));
  957. if (HandleDLLStatus(FuncSetFineScan(dll, q))) return true;
  958. }
  959. catch (Exception e) { lastExcp = e.ToString(); }
  960. finally { Marshal.FreeHGlobal(q); }
  961. return false;
  962. }
  963. // ====================================================================
  964. /// <summary>
  965. /// Get fine scanning mode flag
  966. /// </summary>
  967. /// <returns>True if fine scanning enabled, return false if error</returns>
  968. // ====================================================================
  969. public bool FGetFineScan()
  970. {
  971. IntPtr q = Marshal.AllocHGlobal(sizeof(Int32));
  972. try
  973. {
  974. if (HandleDLLStatus(FuncGetFineScan(dll, q)))
  975. {
  976. return Convert.ToBoolean(Marshal.ReadInt32(q));
  977. }
  978. }
  979. catch (Exception e) { lastExcp = e.ToString(); }
  980. finally { Marshal.FreeHGlobal(q); }
  981. return false;
  982. }
  983. // ====================================================================
  984. /// <summary>
  985. /// Retrieve name of saved configuration
  986. /// </summary>
  987. /// <param name="index">Slot index [1 -99]</param>
  988. /// <returns>Configuration Name</returns>
  989. // ====================================================================
  990. public string FGetConfigurationName(int index)
  991. {
  992. try
  993. {
  994. StringBuilder sb = new StringBuilder("", 255);
  995. if (HandleDLLStatus(FuncGetConfigurationName(dll, sb, index)))
  996. {
  997. return sb.ToString();
  998. }
  999. }
  1000. catch (Exception e) { lastExcp = e.ToString(); }
  1001. return string.Empty;
  1002. }
  1003. // ====================================================================
  1004. /// <summary>
  1005. /// Set OCR Parameter
  1006. /// </summary>
  1007. /// <param name="ocr">OCR Parameters</param>
  1008. /// <param name="capture">Image Capture Parameters</param>
  1009. /// <returns>true if no error</returns>
  1010. // ====================================================================
  1011. public bool FSetParamOCR(WID_OCR ocr, WID_CAPTURE capture)
  1012. {
  1013. WID_OCR_Unsafe unsafeOcr = ocr.ToUnsafeStruct();
  1014. IntPtr ptrOcr = Marshal.AllocHGlobal(Marshal.SizeOf(unsafeOcr) + 1);
  1015. IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
  1016. try
  1017. {
  1018. Marshal.StructureToPtr(capture, ptrCapture, false);
  1019. Marshal.StructureToPtr(unsafeOcr, ptrOcr, false);
  1020. if (HandleDLLStatus(FuncSetParamOCR(dll, ptrOcr, ptrCapture))) return true;
  1021. }
  1022. catch (Exception e) { lastExcp = e.ToString(); }
  1023. finally
  1024. {
  1025. Marshal.FreeHGlobal(ptrCapture);
  1026. Marshal.FreeHGlobal(ptrOcr);
  1027. }
  1028. return false;
  1029. }
  1030. // ====================================================================
  1031. /// <summary>
  1032. /// Get OCR Parameter
  1033. /// </summary>
  1034. /// <param name="ocr">OCR Parameters</param>
  1035. /// <param name="capture">Image Capture Parameters</param>
  1036. /// <returns>true if no error</returns>
  1037. // ====================================================================
  1038. public bool FGetParamOCR(out WID_OCR ocr, out WID_CAPTURE capture)
  1039. {
  1040. WID_OCR_Unsafe unsafeOcr = new WID_OCR_Unsafe();
  1041. IntPtr ptrOcr = Marshal.AllocHGlobal(Marshal.SizeOf(unsafeOcr) + 1);
  1042. capture = new WID_CAPTURE();
  1043. IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
  1044. try
  1045. {
  1046. Marshal.StructureToPtr(unsafeOcr, ptrOcr, false);
  1047. Marshal.StructureToPtr(capture, ptrCapture, false);
  1048. if (HandleDLLStatus(FuncGetParamOCR(dll, ptrOcr, ptrCapture)))
  1049. {
  1050. unsafeOcr = (WID_OCR_Unsafe)Marshal.PtrToStructure(ptrOcr, typeof(WID_OCR_Unsafe));
  1051. ocr = new WID_OCR(unsafeOcr);
  1052. capture = (WID_CAPTURE)Marshal.PtrToStructure(ptrCapture, typeof(WID_CAPTURE));
  1053. return true;
  1054. }
  1055. }
  1056. catch (Exception e) { lastExcp = e.ToString(); }
  1057. finally
  1058. {
  1059. Marshal.FreeHGlobal(ptrOcr);
  1060. Marshal.FreeHGlobal(ptrCapture);
  1061. }
  1062. ocr = new WID_OCR();
  1063. return false;
  1064. }
  1065. // ====================================================================
  1066. /// <summary>
  1067. /// Set BCR Parameter
  1068. /// </summary>
  1069. /// <param name="bcr">BCR Parameters</param>
  1070. /// <param name="capture">Image Capture Parameters</param>
  1071. /// <returns>true if no error</returns>
  1072. // ====================================================================
  1073. public bool FSetParamBCR(WID_BCR bcr, WID_CAPTURE capture)
  1074. {
  1075. WID_BCR_Unsafe unsafeBcr = bcr.ToUnsafeStruct();
  1076. IntPtr ptrBcr = Marshal.AllocHGlobal(Marshal.SizeOf(unsafeBcr) + 1);
  1077. IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
  1078. try
  1079. {
  1080. Marshal.StructureToPtr(capture, ptrCapture, false);
  1081. Marshal.StructureToPtr(unsafeBcr, ptrBcr, false);
  1082. if (HandleDLLStatus(FuncSetParamBCR(dll, ptrBcr, ptrCapture))) return true;
  1083. }
  1084. catch (Exception e) { lastExcp = e.ToString(); }
  1085. finally
  1086. {
  1087. Marshal.FreeHGlobal(ptrCapture);
  1088. Marshal.FreeHGlobal(ptrBcr);
  1089. }
  1090. return false;
  1091. }
  1092. // ====================================================================
  1093. /// <summary>
  1094. /// Get BCR Parameter
  1095. /// </summary>
  1096. /// <param name="bcr">BCR Parameters</param>
  1097. /// <param name="capture">Image Capture Parameters</param>
  1098. /// <returns>true if no error</returns>
  1099. // ====================================================================
  1100. public bool FGetParamBCR(out WID_BCR bcr, out WID_CAPTURE capture)
  1101. {
  1102. WID_BCR_Unsafe unsafebcr = new WID_BCR_Unsafe();
  1103. IntPtr ptrBcr = Marshal.AllocHGlobal(Marshal.SizeOf(unsafebcr) + 1);
  1104. capture = new WID_CAPTURE();
  1105. IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
  1106. try
  1107. {
  1108. Marshal.StructureToPtr(unsafebcr, ptrBcr, false);
  1109. Marshal.StructureToPtr(capture, ptrCapture, false);
  1110. if (HandleDLLStatus(FuncGetParamBCR(dll, ptrBcr, ptrCapture)))
  1111. {
  1112. unsafebcr = (WID_BCR_Unsafe)Marshal.PtrToStructure(ptrBcr, typeof(WID_BCR_Unsafe));
  1113. bcr = new WID_BCR(unsafebcr);
  1114. capture = (WID_CAPTURE)Marshal.PtrToStructure(ptrCapture, typeof(WID_CAPTURE));
  1115. return true;
  1116. }
  1117. }
  1118. catch (Exception e) { lastExcp = e.ToString(); }
  1119. finally
  1120. {
  1121. Marshal.FreeHGlobal(ptrBcr);
  1122. Marshal.FreeHGlobal(ptrCapture);
  1123. }
  1124. bcr = new WID_BCR();
  1125. return false;
  1126. }
  1127. // ====================================================================
  1128. /// <summary>
  1129. /// Set DMR Parameter
  1130. /// </summary>
  1131. /// <param name="dmr">DMR Parameters</param>
  1132. /// <param name="capture">Image Capture Parameters</param>
  1133. /// <returns>true if no error</returns>
  1134. // ====================================================================
  1135. public bool FSetParamDMR(WID_DMR dmr, WID_CAPTURE capture)
  1136. {
  1137. WID_DMR_Unsafe usafeDmr = dmr.ToUnsafeStruct();
  1138. IntPtr ptrDmr = Marshal.AllocHGlobal(Marshal.SizeOf(usafeDmr) + 1);
  1139. IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
  1140. try
  1141. {
  1142. Marshal.StructureToPtr(capture, ptrCapture, false);
  1143. Marshal.StructureToPtr(usafeDmr, ptrDmr, false);
  1144. if (HandleDLLStatus(FuncSetParamDMR(dll, ptrDmr, ptrCapture))) return true;
  1145. }
  1146. catch (Exception e) { lastExcp = e.ToString(); }
  1147. finally
  1148. {
  1149. Marshal.FreeHGlobal(ptrCapture);
  1150. Marshal.FreeHGlobal(ptrDmr);
  1151. }
  1152. return false;
  1153. }
  1154. // ====================================================================
  1155. /// <summary>
  1156. /// Get DMR Parameter
  1157. /// </summary>
  1158. /// <param name="dmr">DMR Parameters</param>
  1159. /// <param name="capture">Image Capture Parameters</param>
  1160. /// <returns>true if no error</returns>
  1161. // ====================================================================
  1162. public bool FGetParamDMR(out WID_DMR dmr, out WID_CAPTURE capture)
  1163. {
  1164. WID_DMR_Unsafe unsafedmr = new WID_DMR_Unsafe();
  1165. IntPtr ptrDmr = Marshal.AllocHGlobal(Marshal.SizeOf(unsafedmr) + 1);
  1166. capture = new WID_CAPTURE();
  1167. IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
  1168. try
  1169. {
  1170. Marshal.StructureToPtr(unsafedmr, ptrDmr, false);
  1171. Marshal.StructureToPtr(capture, ptrCapture, false);
  1172. if (HandleDLLStatus(FuncGetParamDMR(dll, ptrDmr, ptrCapture)))
  1173. {
  1174. unsafedmr = (WID_DMR_Unsafe)Marshal.PtrToStructure(ptrDmr, typeof(WID_DMR_Unsafe));
  1175. dmr = new WID_DMR(unsafedmr);
  1176. capture = (WID_CAPTURE)Marshal.PtrToStructure(ptrCapture, typeof(WID_CAPTURE));
  1177. return true;
  1178. }
  1179. }
  1180. catch (Exception e) { lastExcp = e.ToString(); }
  1181. finally
  1182. {
  1183. Marshal.FreeHGlobal(ptrDmr);
  1184. Marshal.FreeHGlobal(ptrCapture);
  1185. }
  1186. dmr = new WID_DMR();
  1187. return false;
  1188. }
  1189. // ====================================================================
  1190. /// <summary>
  1191. /// Set ROI of named configuration
  1192. /// </summary>
  1193. /// <param name="configName">Configuration name</param>
  1194. /// <param name="roi">ROI Parameters</param>
  1195. /// <param name="capture">Image Capture Parameters</param>
  1196. /// <returns>true if no error.</returns>
  1197. // ====================================================================
  1198. public bool FSetROI(string configName, WID_ROI roi, WID_CAPTURE capture)
  1199. {
  1200. IntPtr ptrRoi = Marshal.AllocHGlobal(Marshal.SizeOf(roi) + 1);
  1201. IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
  1202. try
  1203. {
  1204. Marshal.StructureToPtr(roi, ptrRoi, false);
  1205. Marshal.StructureToPtr(capture, ptrCapture, false);
  1206. if (HandleDLLStatus(FuncSetROI(dll, configName, ptrRoi, ptrCapture))) return true;
  1207. }
  1208. catch (Exception e) { lastExcp = e.ToString(); }
  1209. finally
  1210. {
  1211. Marshal.FreeHGlobal(ptrCapture);
  1212. Marshal.FreeHGlobal(ptrRoi);
  1213. }
  1214. return false;
  1215. }
  1216. // ====================================================================
  1217. /// <summary>
  1218. /// Get ROI of named configuration
  1219. /// </summary>
  1220. /// <param name="configName">Configuration name</param>
  1221. /// <param name="roi">ROI Parameters</param>
  1222. /// <param name="capture">Image Capture Parameters</param>
  1223. /// <returns>true if no error.</returns>
  1224. // ====================================================================
  1225. public bool FGetROI(string configName, out WID_ROI roi, out WID_CAPTURE capture)
  1226. {
  1227. roi = new WID_ROI();
  1228. IntPtr ptrRoi = Marshal.AllocHGlobal(Marshal.SizeOf(roi) + 1);
  1229. capture = new WID_CAPTURE();
  1230. IntPtr ptrCapture = Marshal.AllocHGlobal(Marshal.SizeOf(capture) + 1);
  1231. try
  1232. {
  1233. Marshal.StructureToPtr(roi, ptrRoi, false);
  1234. Marshal.StructureToPtr(capture, ptrCapture, false);
  1235. if (HandleDLLStatus(FuncGetROI(dll, configName, ptrRoi, ptrCapture)))
  1236. {
  1237. roi = (WID_ROI)Marshal.PtrToStructure(ptrRoi, typeof(WID_ROI));
  1238. capture = (WID_CAPTURE)Marshal.PtrToStructure(ptrCapture, typeof(WID_CAPTURE));
  1239. return true;
  1240. }
  1241. }
  1242. catch (Exception e) { lastExcp = e.ToString(); }
  1243. finally
  1244. {
  1245. Marshal.FreeHGlobal(ptrRoi);
  1246. Marshal.FreeHGlobal(ptrCapture);
  1247. }
  1248. return false;
  1249. }
  1250. // ====================================================================
  1251. /// <summary>
  1252. /// Get Image Width
  1253. /// </summary>
  1254. /// <returns>Image Width in pixels</returns>
  1255. // ====================================================================
  1256. public int FGetImageXSize()
  1257. {
  1258. IntPtr q = Marshal.AllocHGlobal(sizeof(Int32));
  1259. try
  1260. {
  1261. if (HandleDLLStatus(FuncGetImageXSize(dll, q)))
  1262. {
  1263. return Convert.ToInt32(Marshal.ReadInt32(q));
  1264. }
  1265. }
  1266. catch (Exception e) { lastExcp = e.ToString(); }
  1267. finally { Marshal.FreeHGlobal(q); }
  1268. return -1;
  1269. }
  1270. // ====================================================================
  1271. /// <summary>
  1272. /// Get Image Height
  1273. /// </summary>
  1274. /// <returns>Image height in pixels</returns>
  1275. // ====================================================================
  1276. public int FGetImageYSize()
  1277. {
  1278. IntPtr q = Marshal.AllocHGlobal(sizeof(Int32));
  1279. try
  1280. {
  1281. if (HandleDLLStatus(FuncGetImageYSize(dll, q)))
  1282. {
  1283. return Convert.ToInt32(Marshal.ReadInt32(q));
  1284. }
  1285. }
  1286. catch (Exception e) { lastExcp = e.ToString(); }
  1287. finally { Marshal.FreeHGlobal(q); }
  1288. return -1;
  1289. }
  1290. // ====================================================================
  1291. /// <summary>
  1292. /// Delete all configuration with the name.
  1293. /// </summary>
  1294. /// <param name="configName">Configuration Name</param>
  1295. /// <returns>true if no error</returns>
  1296. // ====================================================================
  1297. public bool FConfigureDelete(string configName)
  1298. {
  1299. try
  1300. {
  1301. if (HandleDLLStatus(FuncConfigureDelete(dll, configName))) return true;
  1302. }
  1303. catch (Exception e) { lastExcp = e.ToString(); }
  1304. return false;
  1305. }
  1306. // ====================================================================
  1307. /// <summary>
  1308. /// Preset Configuration with matching name.
  1309. /// </summary>
  1310. /// <param name="configName">Configuration name, leave empty to reset cycle</param>
  1311. /// <returns>true if no error</returns>
  1312. // ====================================================================
  1313. public bool FConfigurePreset(string configName = "")
  1314. {
  1315. try
  1316. {
  1317. if (HandleDLLStatus(FuncConfigurePreset(dll, configName))) return true;
  1318. }
  1319. catch (Exception e) { lastExcp = e.ToString(); }
  1320. return false;
  1321. }
  1322. // ====================================================================
  1323. /// <summary>
  1324. /// Tune OCR
  1325. /// </summary>
  1326. /// <returns>true if no error</returns>
  1327. // ====================================================================
  1328. public bool FTeachingOCR()
  1329. {
  1330. try
  1331. {
  1332. if (HandleDLLStatus(FuncTeachingOCR(dll))) return true;
  1333. }
  1334. catch (Exception e) { lastExcp = e.ToString(); }
  1335. return false;
  1336. }
  1337. // ====================================================================
  1338. /// <summary>
  1339. /// Tune Barcode (NOT IMPLEMENTED YET)
  1340. /// </summary>
  1341. /// <returns>true if no error</returns>
  1342. // ====================================================================
  1343. public bool FTeachingBCR()
  1344. {
  1345. try
  1346. {
  1347. if (HandleDLLStatus(FuncTeachingBCR(dll))) return true;
  1348. }
  1349. catch (Exception e) { lastExcp = e.ToString(); }
  1350. return false;
  1351. }
  1352. // ====================================================================
  1353. /// <summary>
  1354. /// Tune 2D Barcode (NOT IMPLEMENTED YET)
  1355. /// </summary>
  1356. /// <returns>true if no error</returns>
  1357. // ====================================================================
  1358. public bool FTeachingDMR()
  1359. {
  1360. try
  1361. {
  1362. if (HandleDLLStatus(FuncTeachingDMR(dll))) return true;
  1363. }
  1364. catch (Exception e) { lastExcp = e.ToString(); }
  1365. return false;
  1366. }
  1367. // ====================================================================
  1368. /// <summary>
  1369. /// OCR Configuration steps
  1370. /// </summary>
  1371. /// <param name="configName">null or configuration name</param>
  1372. /// <returns>true if no error</returns>
  1373. // ====================================================================
  1374. public bool FConfigureOCR(string configName = "\0")
  1375. {
  1376. try
  1377. {
  1378. if (HandleDLLStatus(FuncConfigureOCR(dll, configName))) return true;
  1379. }
  1380. catch (Exception e) { lastExcp = e.ToString(); }
  1381. return false;
  1382. }
  1383. // ====================================================================
  1384. /// <summary>
  1385. /// Barcode Configuration steps. (NOT IMPLEMENTED YET)
  1386. /// </summary>
  1387. /// <param name="configName">null or configuration name</param>
  1388. /// <returns>true if no error</returns>
  1389. // ====================================================================
  1390. public bool FConfigureBCR(string configName = "")
  1391. {
  1392. try
  1393. {
  1394. if (HandleDLLStatus(FuncConfigureBCR(dll, configName))) return true;
  1395. }
  1396. catch (Exception e) { lastExcp = e.ToString(); }
  1397. return false;
  1398. }
  1399. // ====================================================================
  1400. /// <summary>
  1401. /// 2D Barcode Configuration steps. (NOT IMPLEMENTED YET)
  1402. /// </summary>
  1403. /// <param name="configName">null or configuration name</param>
  1404. /// <returns>true if no error</returns>
  1405. // ====================================================================
  1406. public bool FConfigureDMR(string configName = "")
  1407. {
  1408. try
  1409. {
  1410. if (HandleDLLStatus(FuncConfigureDMR(dll, configName))) return true;
  1411. }
  1412. catch (Exception e) { lastExcp = e.ToString(); }
  1413. return false;
  1414. }
  1415. public bool FGetImageRawData(out Image image)
  1416. {
  1417. image = null;
  1418. int width = FGetImageXSize();
  1419. int height = FGetImageYSize();
  1420. int imageSize = width * height;
  1421. byte[] imageData = new byte[imageSize];
  1422. IntPtr ptrImage = Marshal.AllocHGlobal(imageData.Length);
  1423. try
  1424. {
  1425. if (HandleDLLStatus(FuncGetImageRawData(dll, ptrImage)))
  1426. {
  1427. Marshal.Copy(ptrImage, imageData, 0, imageSize);
  1428. byte[] flippedData = ImageDataFlipHorizontal(imageData, width, height);
  1429. image = CreateBMP(width, height, flippedData);
  1430. image.Save("BMPDump.bmp");
  1431. image.Dispose();
  1432. return true;
  1433. }
  1434. }
  1435. catch (Exception e) { lastExcp = e.ToString(); }
  1436. finally { Marshal.FreeHGlobal(ptrImage); }
  1437. return false;
  1438. }
  1439. private byte[] ImageDataFlipHorizontal(byte[] i, int w, int h)
  1440. {
  1441. byte[] d = new byte[i.Length];
  1442. for (int r = 0; r < h; r++)
  1443. {
  1444. Array.Copy(i, r * w, d, (h - 1 - r) * w, w);
  1445. }
  1446. return d;
  1447. }
  1448. private Bitmap CreateBMP(int width, int height, byte[] values)
  1449. {
  1450. Bitmap b = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
  1451. ColorPalette ncp = b.Palette;
  1452. for (int i = 0; i < 256; i++)
  1453. ncp.Entries[i] = Color.FromArgb(255, i, i, i);
  1454. b.Palette = ncp;
  1455. var BoundsRect = new Rectangle(0, 0, width, height);
  1456. BitmapData bmpData = b.LockBits(BoundsRect,
  1457. ImageLockMode.WriteOnly,
  1458. b.PixelFormat);
  1459. IntPtr ptr = bmpData.Scan0;
  1460. int bytes = bmpData.Stride * b.Height;
  1461. Marshal.Copy(values, 0, ptr, bytes);
  1462. b.UnlockBits(bmpData);
  1463. return b;
  1464. }
  1465. }
  1466. }