PicoSmartApi_cshape.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. public PicoSmartApi_cshape()
  201. {
  202. if (File.Exists(ModelInfo.DLL_FIlENAME) == false)
  203. return;
  204. this.m_api = create();
  205. }
  206. public void set_port(int port)
  207. {
  208. set_port(m_api, port);
  209. }
  210. public int get_port()
  211. {
  212. return get_port(m_api);
  213. }
  214. public void set_ip(string ip)
  215. {
  216. set_ip(m_api, ip);
  217. }
  218. public int start()
  219. {
  220. return start(m_api);
  221. }
  222. public int stop()
  223. {
  224. return stop(m_api);
  225. }
  226. public void GET_COLUMN(uint dwRequest, uint dwInterval)
  227. {
  228. GET_COLUMN(m_api, dwRequest, dwInterval);
  229. }
  230. public int GET_COLUMN_MSGDATA(ref GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS, int iWaitMs)
  231. {
  232. return GET_COLUMN_MSGDATA(m_api, ref COLUMN_PARAMETERS, iWaitMs);
  233. }
  234. public void SET_COLUMN(uint dwSet, uint dwGunHV, uint dwMag, uint dwSpot, uint dwBrightness, uint dwContast)
  235. {
  236. SET_COLUMN(m_api, dwSet, dwGunHV, dwMag, dwSpot, dwBrightness, dwContast);
  237. }
  238. public void GET_STAGE(uint dwRequest, uint dwInterval)
  239. {
  240. GET_STAGE(m_api, dwRequest, dwInterval);
  241. }
  242. public int GET_STAGE_MSGDATA(ref GET_STAGE_PARAMETERS_STRUCT STAGE_PARAMETERS, int iWaitMs)//硬件最小发送间隔默认为1秒
  243. {
  244. return GET_STAGE_MSGDATA(m_api, ref STAGE_PARAMETERS, iWaitMs);
  245. }
  246. public int GET_STAGE_MOVE( ref GET_STAGE_MOVE_COMPLETED_STRUCT STAGE_MOVE, int iWaitMs)//硬件最小发送间隔默认为1秒
  247. {
  248. return GET_STAGE_MOVE( m_api,ref STAGE_MOVE, iWaitMs);//硬件最小发送间隔默认为1秒
  249. }
  250. public void SET_STAGE( uint dwSet, float dwXPos, float dwYPos, float dwZPos, float dwRPos, float dwTPos)
  251. {
  252. SET_STAGE(m_api, dwSet, dwXPos, dwYPos, dwZPos, dwRPos, dwTPos);
  253. }
  254. public void STOP_STAGE()
  255. {
  256. STOP_STAGE( m_api);
  257. }
  258. public void GET_IMAGE( uint dwDataType, int image_index, int chanel=3, int speed=0, int live=1)
  259. {
  260. GET_IMAGE( m_api, dwDataType, image_index, chanel, speed, live);
  261. }
  262. public int GET_IMAGE_MSGDATA(ref Mat IMAGE, int iWaitMs)
  263. {
  264. return GET_IMAGE_MSGDATA(m_api, IMAGE.CvPtr, iWaitMs);
  265. }
  266. public void GET_SCANMODE()
  267. {
  268. GET_SCANMODE( m_api);
  269. }
  270. public int GET_SCANMODE_MSGDATA(ref GET_SCANMODE_STRUCT SCANMODE, int iWaitMs)
  271. {
  272. return GET_SCANMODE_MSGDATA( m_api, ref SCANMODE, iWaitMs);
  273. }
  274. public void SET_SCANMODE( uint dwDataType)
  275. {
  276. SET_SCANMODE( m_api, dwDataType);
  277. }
  278. public void GET_SCANSPEED()
  279. {
  280. GET_SCANSPEED( m_api);
  281. }
  282. public int GET_SCANSPEED_MSGDATA( ref GET_SCANSPEED_STRUCT SCANSPEED, int iWaitMs)
  283. {
  284. return GET_SCANSPEED_MSGDATA( m_api, ref SCANSPEED, iWaitMs);
  285. }
  286. public void SET_SCANSPEED( uint dwDataType)
  287. {
  288. SET_SCANSPEED( m_api, dwDataType);
  289. }
  290. public int isStart()
  291. {
  292. return isStart(m_api);
  293. }
  294. #endregion
  295. #region custom thread
  296. private Thread recThread;
  297. private volatile bool rec_running; // 用于控制线程运行的布尔变量
  298. // 定义一个事件,用于通知主线程消费产品
  299. public event Action<GET_COLUMN_PARAMETERS_STRUCT_HUIHONG> COLUMN_MSGProduced;
  300. public event Action<Mat>IMAGE_MSGProduced;
  301. // 启动生产者线程
  302. public void StartProducing()
  303. {
  304. if (recThread == null || !recThread.IsAlive)
  305. {
  306. rec_running = true;
  307. recThread = new Thread(Produce);
  308. recThread.Start();
  309. }
  310. }
  311. // 停止生产者线程
  312. public void StopProducing()
  313. {
  314. rec_running = false;
  315. if (recThread != null && recThread.IsAlive)
  316. {
  317. recThread.Join(); // 等待线程终止
  318. }
  319. }
  320. private GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG();
  321. // 生产方法(线程入口点)
  322. private void Produce()
  323. {
  324. GET_COLUMN(0, 1);
  325. Mat IMAGE= new Mat();
  326. while (rec_running)
  327. {
  328. if(GET_COLUMN_MSGDATA(ref COLUMN,10)==1)
  329. {
  330. OnGET_COLUMN_MSG(COLUMN);
  331. }
  332. if(GET_IMAGE_MSGDATA(ref IMAGE,10)==1)
  333. {
  334. onGET_IMAGE_MSG(IMAGE);
  335. }
  336. Thread.Sleep(1);
  337. }
  338. IMAGE.Dispose();
  339. }
  340. // 触发产品生产事件
  341. public virtual void OnGET_COLUMN_MSG(GET_COLUMN_PARAMETERS_STRUCT_HUIHONG product)
  342. {
  343. COLUMN_MSGProduced?.Invoke(product);
  344. }
  345. public void onGET_IMAGE_MSG(Mat image)
  346. {
  347. IMAGE_MSGProduced?.Invoke(image);
  348. }
  349. #endregion
  350. }
  351. }