PicoSmartApi_cshape.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.IO;
  8. using OpenCvSharp;
  9. using System.Threading;
  10. //namespace picoapi_cshape
  11. namespace OTSMeasureApp.ServiceCenter
  12. {
  13. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  14. public struct PACKET_HEAD_STRUCT
  15. {
  16. public uint dwSize;
  17. public uint dwMsg;
  18. }
  19. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  20. public struct CHK_CERTIFICATION_CODE
  21. {
  22. public PACKET_HEAD_STRUCT Header;
  23. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
  24. public byte[] btCode;
  25. }
  26. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  27. public struct GET_COLUMN_PARAMETERS_COMMAND
  28. {
  29. public PACKET_HEAD_STRUCT Header;
  30. public uint dwRequest;
  31. public uint dwInterval;
  32. }
  33. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  34. public struct SET_COLUMN_PARAMETERS_STRUCT
  35. {
  36. public PACKET_HEAD_STRUCT Header;
  37. public uint dwSet;
  38. public uint dwGunHV;
  39. public uint dwMag;
  40. public uint dwSpot;
  41. public uint dwWD;
  42. public uint dwBrightness;
  43. public uint dwContast;
  44. }
  45. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  46. public struct GET_COLUMN_PARAMETERS_STRUCT_HUIHONG
  47. {
  48. public PACKET_HEAD_STRUCT Header;
  49. public uint dwData;
  50. public uint dwState;
  51. public uint dwSignal;
  52. public uint dwGunHV;
  53. public uint dwScanSpeed;
  54. public uint dwMag;
  55. public uint dwSpot;
  56. public uint dwWD;
  57. public uint dwBrightness;
  58. public uint dwContast;
  59. }
  60. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  61. public struct GET_STAGE_PARAMETERS_STRUCT
  62. {
  63. public PACKET_HEAD_STRUCT Header;
  64. public uint dwData;
  65. public float dwXPos;
  66. public float dwYPos;
  67. public float dwZPos;
  68. public float dwRPos;
  69. public float dwTPos;
  70. public float dwLimit;
  71. public float dwXMax;
  72. public float dwYMax;
  73. public float dwZMax;
  74. public float dwRMax;
  75. public float dwTMax;
  76. }
  77. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  78. public struct SET_STAGE_PARAMETERS_STRUCT
  79. {
  80. public PACKET_HEAD_STRUCT Header;
  81. public uint dwSet;
  82. public uint dwXPos;
  83. public uint dwYPos;
  84. public uint dwZPos;
  85. public uint dwRPos;
  86. public uint dwTPos;
  87. }
  88. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  89. public struct GET_STAGE_PARAMETERS_COMMAND
  90. {
  91. public PACKET_HEAD_STRUCT Header;
  92. public uint dwRequest;
  93. public uint dwInterval;
  94. }
  95. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  96. public struct GET_STAGE_MOVE_COMPLETED_STRUCT
  97. {
  98. public PACKET_HEAD_STRUCT Header;
  99. public uint dwAxis;
  100. public uint dwMotion;
  101. }
  102. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  103. public struct GET_SCANMODE_STRUCT
  104. {
  105. public PACKET_HEAD_STRUCT Header;
  106. public uint dwDataType;
  107. }
  108. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  109. public struct SET_SCANMODE_STRUCT
  110. {
  111. public PACKET_HEAD_STRUCT Header;
  112. public uint dwDataType;
  113. }
  114. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  115. public struct GET_SCANSPEED_STRUCT
  116. {
  117. public PACKET_HEAD_STRUCT Header;
  118. public uint dwDataType;
  119. }
  120. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  121. public struct SET_SCANSPEED_STRUCT
  122. {
  123. public PACKET_HEAD_STRUCT Header;
  124. public uint dwDataType;
  125. }
  126. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  127. public struct GET_IMAGE_STRUCT
  128. {
  129. public PACKET_HEAD_STRUCT Header;
  130. public int dwDataType;
  131. public uint LIVE;
  132. public uint SCANSPEED;
  133. public uint width;
  134. public uint height;
  135. public uint chanel;
  136. }
  137. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  138. public struct SET_STAGE_AXIS_STOP_COMMAND
  139. {
  140. public PACKET_HEAD_STRUCT Header;
  141. public uint dwAxisStop;
  142. }
  143. public class ModelInfo
  144. {
  145. string currentPath = AppDomain.CurrentDomain.BaseDirectory;
  146. public const string DLL_FIlENAME = "D:\\proj\\opton\\SEM API\\SemApiTest\\bin\\Release\\SemApi.dll";
  147. }
  148. public class PicoSmartApi_cshape
  149. {
  150. #region exten c api
  151. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  152. private static extern IntPtr create();
  153. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  154. private static extern void set_port(IntPtr m_api, int port);
  155. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  156. private static extern int get_port(IntPtr m_api);
  157. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  158. private static extern void set_ip(IntPtr m_api, string server_ip);
  159. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  160. private static extern int start(IntPtr m_api);
  161. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  162. private static extern int stop(IntPtr m_api);
  163. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  164. private static extern void GET_COLUMN(IntPtr m_api, uint dwRequest, uint dwInterval);
  165. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  166. private static extern int GET_COLUMN_MSGDATA(IntPtr m_api, ref GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS, int iWaitMs);
  167. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  168. private static extern void SET_COLUMN(IntPtr m_api, uint dwSet, uint dwGunHV, uint dwMag, uint dwSpot, uint dwBrightness, uint dwContast);
  169. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  170. private static extern void GET_STAGE(IntPtr m_api, uint dwRequest, uint dwInterval);
  171. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  172. private static extern int GET_STAGE_MSGDATA(IntPtr m_api, ref GET_STAGE_PARAMETERS_STRUCT STAGE_PARAMETERS, int iWaitMs);//硬件最小发送间隔默认为1秒
  173. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  174. private static extern int GET_STAGE_MOVE(IntPtr m_api, ref GET_STAGE_MOVE_COMPLETED_STRUCT STAGE_MOVE, int iWaitMs);//硬件最小发送间隔默认为1秒
  175. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  176. private static extern void SET_STAGE(IntPtr m_api, uint dwSet, float dwXPos, float dwYPos, float dwZPos, float dwRPos, float dwTPos);
  177. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  178. private static extern void STOP_STAGE(IntPtr m_api);
  179. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  180. private static extern void GET_IMAGE(IntPtr m_api, uint dwDataType, int image_index, int chanel, int speed, int live);
  181. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  182. private static extern int GET_IMAGE_MSGDATA(IntPtr m_api, IntPtr IMAGE, int iWaitMs);
  183. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  184. private static extern void GET_SCANMODE(IntPtr m_api);
  185. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  186. private static extern int GET_SCANMODE_MSGDATA(IntPtr m_api, ref GET_SCANMODE_STRUCT SCANMODE, int iWaitMs);
  187. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  188. private static extern void SET_SCANMODE(IntPtr m_api, uint dwDataType);
  189. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  190. private static extern void GET_SCANSPEED(IntPtr m_api);
  191. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  192. private static extern int GET_SCANSPEED_MSGDATA(IntPtr m_api, ref GET_SCANSPEED_STRUCT SCANSPEED, int iWaitMs);
  193. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  194. private static extern void SET_SCANSPEED(IntPtr m_api, uint dwDataType);
  195. [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)]
  196. private static extern int isStart(IntPtr api);
  197. #endregion
  198. #region c# api
  199. private IntPtr m_api = IntPtr.Zero;
  200. int [] m_nGunHVTable = { 30000, 25000, 20000, 15000, 10000, 5000, 3000, 2000, 1500, 1000 };
  201. int[] m_nMagStepValue = { 30, 35, 40, 45, 50, 60, 70, 80, 90, 100,
  202. 110, 120, 130, 150, 180, 200, 220, 250, 300, 350,
  203. 400, 450, 500, 600, 700, 800, 900, 1000, 1100, 1200,
  204. 1300, 1500, 1800, 2000, 2200, 2500, 3000, 3500, 4000, 4500,
  205. 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 15000,
  206. 18000, 20000, 22000, 25000, 30000, 35000, 40000, 45000, 50000,
  207. 60000, 70000, 80000, 90000, 100000, 110000, 120000, 130000, 150000, 180000,
  208. 200000, 220000, 250000, 300000 };
  209. public PicoSmartApi_cshape()
  210. {
  211. if (File.Exists(ModelInfo.DLL_FIlENAME) == false)
  212. return;
  213. this.m_api = create();
  214. }
  215. public void set_port(int port)
  216. {
  217. set_port(m_api, port);
  218. }
  219. public int get_port()
  220. {
  221. return get_port(m_api);
  222. }
  223. public void set_ip(string ip)
  224. {
  225. set_ip(m_api, ip);
  226. }
  227. public int start()
  228. {
  229. return start(m_api);
  230. }
  231. public int stop()
  232. {
  233. return stop(m_api);
  234. }
  235. public void GET_COLUMN(uint dwRequest, uint dwInterval)
  236. {
  237. GET_COLUMN(m_api, dwRequest, dwInterval);
  238. }
  239. public GET_COLUMN_PARAMETERS_STRUCT_HUIHONG sem_COLUMN_PARAMETERS=new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG();
  240. public GET_STAGE_PARAMETERS_STRUCT sem_stage_struct = new GET_STAGE_PARAMETERS_STRUCT();
  241. public GET_SCANMODE_STRUCT sem_scanmode= new GET_SCANMODE_STRUCT();
  242. public GET_SCANSPEED_STRUCT sem_scanspeed=new GET_SCANSPEED_STRUCT();
  243. public void GET_STAGE_PARAMETERS_STRUCT_copy(GET_STAGE_PARAMETERS_STRUCT stage,ref GET_STAGE_PARAMETERS_STRUCT sem_stage_struct)
  244. {
  245. sem_stage_struct.dwTMax = stage.dwTMax;
  246. sem_stage_struct.dwZMax = stage.dwZMax;
  247. sem_stage_struct.dwYMax = stage.dwYMax;
  248. sem_stage_struct.dwXMax = stage.dwXMax;
  249. sem_stage_struct.dwData=stage.dwData;
  250. sem_stage_struct.dwRMax = stage.dwRMax;
  251. sem_stage_struct.dwLimit=stage.dwLimit;
  252. sem_stage_struct.dwTPos = stage.dwTPos;
  253. sem_stage_struct.dwXPos = stage.dwXPos;
  254. sem_stage_struct.dwZPos = stage.dwZPos;
  255. sem_stage_struct.dwXPos= stage.dwXPos;
  256. sem_stage_struct.dwRPos = stage.dwRPos;
  257. }
  258. public void GET_COLUMN_PARAMETERS_STRUCT_HUIHONG_copy(GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS,ref GET_COLUMN_PARAMETERS_STRUCT_HUIHONG sem_COLUMN_PARAMETERS)
  259. {
  260. sem_COLUMN_PARAMETERS.dwMag = COLUMN_PARAMETERS.dwMag;
  261. sem_COLUMN_PARAMETERS.dwBrightness = COLUMN_PARAMETERS.dwBrightness;
  262. sem_COLUMN_PARAMETERS.dwContast = COLUMN_PARAMETERS.dwContast;
  263. sem_COLUMN_PARAMETERS.dwData = COLUMN_PARAMETERS.dwData;
  264. sem_COLUMN_PARAMETERS.dwGunHV = COLUMN_PARAMETERS.dwGunHV;
  265. sem_COLUMN_PARAMETERS.dwScanSpeed = COLUMN_PARAMETERS.dwScanSpeed;
  266. sem_COLUMN_PARAMETERS.dwSignal = COLUMN_PARAMETERS.dwSignal;
  267. sem_COLUMN_PARAMETERS.dwSpot = COLUMN_PARAMETERS.dwSpot;
  268. sem_COLUMN_PARAMETERS.dwState = COLUMN_PARAMETERS.dwState;
  269. sem_COLUMN_PARAMETERS.dwWD= COLUMN_PARAMETERS.dwWD;
  270. }
  271. public int GET_COLUMN_MSGDATA(ref GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS, int iWaitMs)
  272. {
  273. GET_COLUMN(0, 0);
  274. int result = GET_COLUMN_MSGDATA(m_api, ref COLUMN_PARAMETERS, iWaitMs);
  275. if(result == 1)
  276. {
  277. GET_COLUMN_PARAMETERS_STRUCT_HUIHONG_copy(COLUMN_PARAMETERS,ref this.sem_COLUMN_PARAMETERS);
  278. }
  279. return result;
  280. }
  281. public bool GetMagnification(ref double a_dMagnification)
  282. {
  283. GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS=new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG();
  284. if(GET_COLUMN_MSGDATA( ref COLUMN_PARAMETERS, 50)==1)
  285. {
  286. a_dMagnification = COLUMN_PARAMETERS.dwMag;
  287. return true;
  288. }
  289. return false;
  290. }
  291. public bool GetWorkingDistance(ref double a_distance)
  292. {
  293. GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG();
  294. if (GET_COLUMN_MSGDATA(ref COLUMN_PARAMETERS, 150) == 1)
  295. {
  296. a_distance = COLUMN_PARAMETERS.dwWD;
  297. return true;
  298. }
  299. return false;
  300. }
  301. public bool GetSemBrightness(ref double a_dSemBrightness)
  302. {
  303. GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG();
  304. if (GET_COLUMN_MSGDATA(ref COLUMN_PARAMETERS, 50) == 1)
  305. {
  306. a_dSemBrightness = COLUMN_PARAMETERS.dwBrightness;
  307. return true;
  308. }
  309. return false;
  310. }
  311. public bool GetSemContrast(ref double a_dContrast)
  312. {
  313. GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG();
  314. if (GET_COLUMN_MSGDATA(ref COLUMN_PARAMETERS, 50) == 1)
  315. {
  316. a_dContrast = COLUMN_PARAMETERS.dwContast;
  317. return true;
  318. }
  319. return false;
  320. }
  321. public bool GetSemHighTension(ref double a_dKV)
  322. {
  323. GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG();
  324. if (GET_COLUMN_MSGDATA(ref COLUMN_PARAMETERS, 50) == 1)
  325. {
  326. a_dKV = COLUMN_PARAMETERS.dwGunHV;
  327. return true;
  328. }
  329. return false;
  330. }
  331. public void SET_COLUMN(uint dwSet, uint dwGunHV, uint dwMag, uint dwSpot, uint dwBrightness, uint dwContast)
  332. {
  333. SET_COLUMN(m_api, dwSet, dwGunHV, dwMag, dwSpot, dwBrightness, dwContast);
  334. }
  335. public bool SetMagnification(double a_dMagnification)
  336. {
  337. SET_COLUMN(m_api, 0, sem_COLUMN_PARAMETERS.dwGunHV,(uint) a_dMagnification, sem_COLUMN_PARAMETERS. dwSpot, sem_COLUMN_PARAMETERS. dwBrightness, sem_COLUMN_PARAMETERS. dwContast);
  338. return true;
  339. }
  340. public bool SetSemBrightness(double a_dBrightness)
  341. {
  342. SET_COLUMN(m_api, 0, sem_COLUMN_PARAMETERS.dwGunHV, sem_COLUMN_PARAMETERS.dwMag, sem_COLUMN_PARAMETERS.dwSpot,(uint) a_dBrightness, sem_COLUMN_PARAMETERS.dwContast);
  343. return true;
  344. }
  345. public bool SetSemContrast(double a_dContrast)
  346. {
  347. SET_COLUMN(m_api, 0, sem_COLUMN_PARAMETERS.dwGunHV, sem_COLUMN_PARAMETERS.dwMag, sem_COLUMN_PARAMETERS.dwSpot, sem_COLUMN_PARAMETERS.dwBrightness, (uint)a_dContrast);
  348. return true;
  349. }
  350. public bool SetSemHighTension(double a_dKV)
  351. {
  352. SET_COLUMN(m_api, 0, (uint)a_dKV, sem_COLUMN_PARAMETERS.dwMag, sem_COLUMN_PARAMETERS.dwSpot, sem_COLUMN_PARAMETERS.dwBrightness, sem_COLUMN_PARAMETERS.dwContast);
  353. return true;
  354. }
  355. public void GET_STAGE(uint dwRequest, uint dwInterval)
  356. {
  357. GET_STAGE(m_api, dwRequest, dwInterval);
  358. }
  359. public int GET_STAGE_MSGDATA(ref GET_STAGE_PARAMETERS_STRUCT STAGE_PARAMETERS, int iWaitMs)//硬件最小发送间隔默认为1秒
  360. {
  361. GET_STAGE(m_api, 0, 0);
  362. int result= GET_STAGE_MSGDATA(m_api, ref STAGE_PARAMETERS, iWaitMs);
  363. if(result == 1)
  364. {
  365. GET_STAGE_PARAMETERS_STRUCT_copy(STAGE_PARAMETERS,ref sem_stage_struct);
  366. }
  367. return result;
  368. }
  369. public int GET_STAGE_MOVE( ref GET_STAGE_MOVE_COMPLETED_STRUCT STAGE_MOVE, int iWaitMs)//硬件最小发送间隔默认为1秒
  370. {
  371. return GET_STAGE_MOVE( m_api,ref STAGE_MOVE, iWaitMs);//硬件最小发送间隔默认为1秒
  372. }
  373. public void SET_STAGE( uint dwSet, float dwXPos, float dwYPos, float dwZPos, float dwRPos, float dwTPos)
  374. {
  375. SET_STAGE(m_api, dwSet, dwXPos, dwYPos, dwZPos, dwRPos, dwTPos);
  376. }
  377. public bool MoveSEMToPoint(double a_dPositionX, double a_dPositionY)
  378. {
  379. SET_STAGE(m_api, 0,(float) a_dPositionX, (float)a_dPositionY, 1, 1, 1);
  380. return true;
  381. }
  382. public void STOP_STAGE()
  383. {
  384. STOP_STAGE( m_api);
  385. }
  386. public void GET_IMAGE( uint dwDataType, int image_index, int chanel=3, int speed=0, int live=1)
  387. {
  388. GET_IMAGE( m_api, dwDataType, image_index, chanel, speed, live);
  389. }
  390. public int GET_IMAGE_MSGDATA(ref Mat IMAGE, int iWaitMs)
  391. {
  392. return GET_IMAGE_MSGDATA(m_api, IMAGE.CvPtr, iWaitMs);
  393. }
  394. public void GET_SCANMODE()
  395. {
  396. GET_SCANMODE( m_api);
  397. }
  398. public int GET_SCANMODE_MSGDATA(ref GET_SCANMODE_STRUCT SCANMODE, int iWaitMs)
  399. {
  400. return GET_SCANMODE_MSGDATA( m_api, ref SCANMODE, iWaitMs);
  401. }
  402. public void SET_SCANMODE( uint dwDataType)
  403. {
  404. SET_SCANMODE( m_api, dwDataType);
  405. }
  406. public void GET_SCANSPEED()
  407. {
  408. GET_SCANSPEED( m_api);
  409. }
  410. public int GET_SCANSPEED_MSGDATA( ref GET_SCANSPEED_STRUCT SCANSPEED, int iWaitMs)
  411. {
  412. return GET_SCANSPEED_MSGDATA( m_api, ref SCANSPEED, iWaitMs);
  413. }
  414. public void SET_SCANSPEED( uint dwDataType)
  415. {
  416. SET_SCANSPEED( m_api, dwDataType);
  417. }
  418. public int isStart()
  419. {
  420. return isStart(m_api);
  421. }
  422. #endregion
  423. #region custom thread
  424. private Thread recThread;
  425. private volatile bool rec_running; // 用于控制线程运行的布尔变量
  426. // 定义一个事件,用于通知主线程消费产品
  427. public event Action<GET_COLUMN_PARAMETERS_STRUCT_HUIHONG> COLUMN_MSGProduced;
  428. public event Action<Mat>IMAGE_MSGProduced;
  429. // 启动生产者线程
  430. public void StartProducing()
  431. {
  432. if (recThread == null || !recThread.IsAlive)
  433. {
  434. rec_running = true;
  435. recThread = new Thread(Produce);
  436. recThread.Start();
  437. }
  438. }
  439. // 停止生产者线程
  440. public void StopProducing()
  441. {
  442. rec_running = false;
  443. if (recThread != null && recThread.IsAlive)
  444. {
  445. recThread.Join(); // 等待线程终止
  446. }
  447. }
  448. private GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG();
  449. // 生产方法(线程入口点)
  450. private void Produce()
  451. {
  452. GET_COLUMN(0, 1);
  453. Mat IMAGE= new Mat();
  454. while (rec_running)
  455. {
  456. if(GET_COLUMN_MSGDATA(ref COLUMN,10)==1)
  457. {
  458. OnGET_COLUMN_MSG(COLUMN);
  459. }
  460. if(GET_IMAGE_MSGDATA(ref IMAGE,10)==1)
  461. {
  462. onGET_IMAGE_MSG(IMAGE);
  463. }
  464. Thread.Sleep(1);
  465. }
  466. IMAGE.Dispose();
  467. }
  468. // 触发产品生产事件
  469. public virtual void OnGET_COLUMN_MSG(GET_COLUMN_PARAMETERS_STRUCT_HUIHONG product)
  470. {
  471. COLUMN_MSGProduced?.Invoke(product);
  472. }
  473. public void onGET_IMAGE_MSG(Mat image)
  474. {
  475. IMAGE_MSGProduced?.Invoke(image);
  476. }
  477. #endregion
  478. }
  479. }