using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.IO; using OpenCvSharp; using System.Threading; //namespace picoapi_cshape namespace OTSMeasureApp.ServiceCenter { [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct PACKET_HEAD_STRUCT { public uint dwSize; public uint dwMsg; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct CHK_CERTIFICATION_CODE { public PACKET_HEAD_STRUCT Header; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public byte[] btCode; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct GET_COLUMN_PARAMETERS_COMMAND { public PACKET_HEAD_STRUCT Header; public uint dwRequest; public uint dwInterval; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SET_COLUMN_PARAMETERS_STRUCT { public PACKET_HEAD_STRUCT Header; public uint dwSet; public uint dwGunHV; public uint dwMag; public uint dwSpot; public uint dwWD; public uint dwBrightness; public uint dwContast; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct GET_COLUMN_PARAMETERS_STRUCT_HUIHONG { public PACKET_HEAD_STRUCT Header; public uint dwData; public uint dwState; public uint dwSignal; public uint dwGunHV; public uint dwScanSpeed; public uint dwMag; public uint dwSpot; public uint dwWD; public uint dwBrightness; public uint dwContast; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct GET_STAGE_PARAMETERS_STRUCT { public PACKET_HEAD_STRUCT Header; public uint dwData; public float dwXPos; public float dwYPos; public float dwZPos; public float dwRPos; public float dwTPos; public float dwLimit; public float dwXMax; public float dwYMax; public float dwZMax; public float dwRMax; public float dwTMax; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SET_STAGE_PARAMETERS_STRUCT { public PACKET_HEAD_STRUCT Header; public uint dwSet; public uint dwXPos; public uint dwYPos; public uint dwZPos; public uint dwRPos; public uint dwTPos; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct GET_STAGE_PARAMETERS_COMMAND { public PACKET_HEAD_STRUCT Header; public uint dwRequest; public uint dwInterval; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct GET_STAGE_MOVE_COMPLETED_STRUCT { public PACKET_HEAD_STRUCT Header; public uint dwAxis; public uint dwMotion; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct GET_SCANMODE_STRUCT { public PACKET_HEAD_STRUCT Header; public uint dwDataType; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SET_SCANMODE_STRUCT { public PACKET_HEAD_STRUCT Header; public uint dwDataType; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct GET_SCANSPEED_STRUCT { public PACKET_HEAD_STRUCT Header; public uint dwDataType; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SET_SCANSPEED_STRUCT { public PACKET_HEAD_STRUCT Header; public uint dwDataType; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct GET_IMAGE_STRUCT { public PACKET_HEAD_STRUCT Header; public int dwDataType; public uint LIVE; public uint SCANSPEED; public uint width; public uint height; public uint chanel; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SET_STAGE_AXIS_STOP_COMMAND { public PACKET_HEAD_STRUCT Header; public uint dwAxisStop; } public class ModelInfo { string currentPath = AppDomain.CurrentDomain.BaseDirectory; public const string DLL_FIlENAME = "D:\\proj\\opton\\SEM API\\SemApiTest\\bin\\Release\\SemApi.dll"; } public class PicoSmartApi_cshape { #region exten c api [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr create(); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void set_port(IntPtr m_api, int port); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern int get_port(IntPtr m_api); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void set_ip(IntPtr m_api, string server_ip); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern int start(IntPtr m_api); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern int stop(IntPtr m_api); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void GET_COLUMN(IntPtr m_api, uint dwRequest, uint dwInterval); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern int GET_COLUMN_MSGDATA(IntPtr m_api, ref GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS, int iWaitMs); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void SET_COLUMN(IntPtr m_api, uint dwSet, uint dwGunHV, uint dwMag, uint dwSpot, uint dwBrightness, uint dwContast); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void GET_STAGE(IntPtr m_api, uint dwRequest, uint dwInterval); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern int GET_STAGE_MSGDATA(IntPtr m_api, ref GET_STAGE_PARAMETERS_STRUCT STAGE_PARAMETERS, int iWaitMs);//硬件最小发送间隔默认为1秒 [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern int GET_STAGE_MOVE(IntPtr m_api, ref GET_STAGE_MOVE_COMPLETED_STRUCT STAGE_MOVE, int iWaitMs);//硬件最小发送间隔默认为1秒 [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void SET_STAGE(IntPtr m_api, uint dwSet, float dwXPos, float dwYPos, float dwZPos, float dwRPos, float dwTPos); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void STOP_STAGE(IntPtr m_api); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void GET_IMAGE(IntPtr m_api, uint dwDataType, int image_index, int chanel, int speed, int live); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern int GET_IMAGE_MSGDATA(IntPtr m_api, IntPtr IMAGE, int iWaitMs); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void GET_SCANMODE(IntPtr m_api); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern int GET_SCANMODE_MSGDATA(IntPtr m_api, ref GET_SCANMODE_STRUCT SCANMODE, int iWaitMs); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void SET_SCANMODE(IntPtr m_api, uint dwDataType); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void GET_SCANSPEED(IntPtr m_api); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern int GET_SCANSPEED_MSGDATA(IntPtr m_api, ref GET_SCANSPEED_STRUCT SCANSPEED, int iWaitMs); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern void SET_SCANSPEED(IntPtr m_api, uint dwDataType); [DllImport(ModelInfo.DLL_FIlENAME, CallingConvention = CallingConvention.Cdecl)] private static extern int isStart(IntPtr api); #endregion #region c# api private IntPtr m_api = IntPtr.Zero; public PicoSmartApi_cshape() { if (File.Exists(ModelInfo.DLL_FIlENAME) == false) return; this.m_api = create(); } public void set_port(int port) { set_port(m_api, port); } public int get_port() { return get_port(m_api); } public void set_ip(string ip) { set_ip(m_api, ip); } public int start() { return start(m_api); } public int stop() { return stop(m_api); } public void GET_COLUMN(uint dwRequest, uint dwInterval) { GET_COLUMN(m_api, dwRequest, dwInterval); } public int GET_COLUMN_MSGDATA(ref GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS, int iWaitMs) { return GET_COLUMN_MSGDATA(m_api, ref COLUMN_PARAMETERS, iWaitMs); } public void SET_COLUMN(uint dwSet, uint dwGunHV, uint dwMag, uint dwSpot, uint dwBrightness, uint dwContast) { SET_COLUMN(m_api, dwSet, dwGunHV, dwMag, dwSpot, dwBrightness, dwContast); } public void GET_STAGE(uint dwRequest, uint dwInterval) { GET_STAGE(m_api, dwRequest, dwInterval); } public int GET_STAGE_MSGDATA(ref GET_STAGE_PARAMETERS_STRUCT STAGE_PARAMETERS, int iWaitMs)//硬件最小发送间隔默认为1秒 { return GET_STAGE_MSGDATA(m_api, ref STAGE_PARAMETERS, iWaitMs); } public int GET_STAGE_MOVE( ref GET_STAGE_MOVE_COMPLETED_STRUCT STAGE_MOVE, int iWaitMs)//硬件最小发送间隔默认为1秒 { return GET_STAGE_MOVE( m_api,ref STAGE_MOVE, iWaitMs);//硬件最小发送间隔默认为1秒 } public void SET_STAGE( uint dwSet, float dwXPos, float dwYPos, float dwZPos, float dwRPos, float dwTPos) { SET_STAGE(m_api, dwSet, dwXPos, dwYPos, dwZPos, dwRPos, dwTPos); } public void STOP_STAGE() { STOP_STAGE( m_api); } public void GET_IMAGE( uint dwDataType, int image_index, int chanel=3, int speed=0, int live=1) { GET_IMAGE( m_api, dwDataType, image_index, chanel, speed, live); } public int GET_IMAGE_MSGDATA(ref Mat IMAGE, int iWaitMs) { return GET_IMAGE_MSGDATA(m_api, IMAGE.CvPtr, iWaitMs); } public void GET_SCANMODE() { GET_SCANMODE( m_api); } public int GET_SCANMODE_MSGDATA(ref GET_SCANMODE_STRUCT SCANMODE, int iWaitMs) { return GET_SCANMODE_MSGDATA( m_api, ref SCANMODE, iWaitMs); } public void SET_SCANMODE( uint dwDataType) { SET_SCANMODE( m_api, dwDataType); } public void GET_SCANSPEED() { GET_SCANSPEED( m_api); } public int GET_SCANSPEED_MSGDATA( ref GET_SCANSPEED_STRUCT SCANSPEED, int iWaitMs) { return GET_SCANSPEED_MSGDATA( m_api, ref SCANSPEED, iWaitMs); } public void SET_SCANSPEED( uint dwDataType) { SET_SCANSPEED( m_api, dwDataType); } public int isStart() { return isStart(m_api); } #endregion #region custom thread private Thread recThread; private volatile bool rec_running; // 用于控制线程运行的布尔变量 // 定义一个事件,用于通知主线程消费产品 public event Action COLUMN_MSGProduced; public event ActionIMAGE_MSGProduced; // 启动生产者线程 public void StartProducing() { if (recThread == null || !recThread.IsAlive) { rec_running = true; recThread = new Thread(Produce); recThread.Start(); } } // 停止生产者线程 public void StopProducing() { rec_running = false; if (recThread != null && recThread.IsAlive) { recThread.Join(); // 等待线程终止 } } private GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG(); // 生产方法(线程入口点) private void Produce() { GET_COLUMN(0, 1); Mat IMAGE= new Mat(); while (rec_running) { if(GET_COLUMN_MSGDATA(ref COLUMN,10)==1) { OnGET_COLUMN_MSG(COLUMN); } if(GET_IMAGE_MSGDATA(ref IMAGE,10)==1) { onGET_IMAGE_MSG(IMAGE); } Thread.Sleep(1); } IMAGE.Dispose(); } // 触发产品生产事件 public virtual void OnGET_COLUMN_MSG(GET_COLUMN_PARAMETERS_STRUCT_HUIHONG product) { COLUMN_MSGProduced?.Invoke(product); } public void onGET_IMAGE_MSG(Mat image) { IMAGE_MSGProduced?.Invoke(image); } #endregion } }