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; int [] m_nGunHVTable = { 30000, 25000, 20000, 15000, 10000, 5000, 3000, 2000, 1500, 1000 }; int[] m_nMagStepValue = { 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 110, 120, 130, 150, 180, 200, 220, 250, 300, 350, 400, 450, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1500, 1800, 2000, 2200, 2500, 3000, 3500, 4000, 4500, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 15000, 18000, 20000, 22000, 25000, 30000, 35000, 40000, 45000, 50000, 60000, 70000, 80000, 90000, 100000, 110000, 120000, 130000, 150000, 180000, 200000, 220000, 250000, 300000 }; 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 GET_COLUMN_PARAMETERS_STRUCT_HUIHONG sem_COLUMN_PARAMETERS=new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG(); public GET_STAGE_PARAMETERS_STRUCT sem_stage_struct = new GET_STAGE_PARAMETERS_STRUCT(); public GET_SCANMODE_STRUCT sem_scanmode= new GET_SCANMODE_STRUCT(); public GET_SCANSPEED_STRUCT sem_scanspeed=new GET_SCANSPEED_STRUCT(); public void GET_STAGE_PARAMETERS_STRUCT_copy(GET_STAGE_PARAMETERS_STRUCT stage,ref GET_STAGE_PARAMETERS_STRUCT sem_stage_struct) { sem_stage_struct.dwTMax = stage.dwTMax; sem_stage_struct.dwZMax = stage.dwZMax; sem_stage_struct.dwYMax = stage.dwYMax; sem_stage_struct.dwXMax = stage.dwXMax; sem_stage_struct.dwData=stage.dwData; sem_stage_struct.dwRMax = stage.dwRMax; sem_stage_struct.dwLimit=stage.dwLimit; sem_stage_struct.dwTPos = stage.dwTPos; sem_stage_struct.dwXPos = stage.dwXPos; sem_stage_struct.dwZPos = stage.dwZPos; sem_stage_struct.dwXPos= stage.dwXPos; sem_stage_struct.dwRPos = stage.dwRPos; } public void GET_COLUMN_PARAMETERS_STRUCT_HUIHONG_copy(GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS,ref GET_COLUMN_PARAMETERS_STRUCT_HUIHONG sem_COLUMN_PARAMETERS) { sem_COLUMN_PARAMETERS.dwMag = COLUMN_PARAMETERS.dwMag; sem_COLUMN_PARAMETERS.dwBrightness = COLUMN_PARAMETERS.dwBrightness; sem_COLUMN_PARAMETERS.dwContast = COLUMN_PARAMETERS.dwContast; sem_COLUMN_PARAMETERS.dwData = COLUMN_PARAMETERS.dwData; sem_COLUMN_PARAMETERS.dwGunHV = COLUMN_PARAMETERS.dwGunHV; sem_COLUMN_PARAMETERS.dwScanSpeed = COLUMN_PARAMETERS.dwScanSpeed; sem_COLUMN_PARAMETERS.dwSignal = COLUMN_PARAMETERS.dwSignal; sem_COLUMN_PARAMETERS.dwSpot = COLUMN_PARAMETERS.dwSpot; sem_COLUMN_PARAMETERS.dwState = COLUMN_PARAMETERS.dwState; sem_COLUMN_PARAMETERS.dwWD= COLUMN_PARAMETERS.dwWD; } public int GET_COLUMN_MSGDATA(ref GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS, int iWaitMs) { GET_COLUMN(0, 0); int result = GET_COLUMN_MSGDATA(m_api, ref COLUMN_PARAMETERS, iWaitMs); if(result == 1) { GET_COLUMN_PARAMETERS_STRUCT_HUIHONG_copy(COLUMN_PARAMETERS,ref this.sem_COLUMN_PARAMETERS); } return result; } public bool GetMagnification(ref double a_dMagnification) { GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS=new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG(); if(GET_COLUMN_MSGDATA( ref COLUMN_PARAMETERS, 50)==1) { a_dMagnification = COLUMN_PARAMETERS.dwMag; return true; } return false; } public bool GetWorkingDistance(ref double a_distance) { GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG(); if (GET_COLUMN_MSGDATA(ref COLUMN_PARAMETERS, 150) == 1) { a_distance = COLUMN_PARAMETERS.dwWD; return true; } return false; } public bool GetSemBrightness(ref double a_dSemBrightness) { GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG(); if (GET_COLUMN_MSGDATA(ref COLUMN_PARAMETERS, 50) == 1) { a_dSemBrightness = COLUMN_PARAMETERS.dwBrightness; return true; } return false; } public bool GetSemContrast(ref double a_dContrast) { GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG(); if (GET_COLUMN_MSGDATA(ref COLUMN_PARAMETERS, 50) == 1) { a_dContrast = COLUMN_PARAMETERS.dwContast; return true; } return false; } public bool GetSemHighTension(ref double a_dKV) { GET_COLUMN_PARAMETERS_STRUCT_HUIHONG COLUMN_PARAMETERS = new GET_COLUMN_PARAMETERS_STRUCT_HUIHONG(); if (GET_COLUMN_MSGDATA(ref COLUMN_PARAMETERS, 50) == 1) { a_dKV = COLUMN_PARAMETERS.dwGunHV; return true; } return false; } 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 bool SetMagnification(double a_dMagnification) { SET_COLUMN(m_api, 0, sem_COLUMN_PARAMETERS.dwGunHV,(uint) a_dMagnification, sem_COLUMN_PARAMETERS. dwSpot, sem_COLUMN_PARAMETERS. dwBrightness, sem_COLUMN_PARAMETERS. dwContast); return true; } public bool SetSemBrightness(double a_dBrightness) { SET_COLUMN(m_api, 0, sem_COLUMN_PARAMETERS.dwGunHV, sem_COLUMN_PARAMETERS.dwMag, sem_COLUMN_PARAMETERS.dwSpot,(uint) a_dBrightness, sem_COLUMN_PARAMETERS.dwContast); return true; } public bool SetSemContrast(double a_dContrast) { SET_COLUMN(m_api, 0, sem_COLUMN_PARAMETERS.dwGunHV, sem_COLUMN_PARAMETERS.dwMag, sem_COLUMN_PARAMETERS.dwSpot, sem_COLUMN_PARAMETERS.dwBrightness, (uint)a_dContrast); return true; } public bool SetSemHighTension(double a_dKV) { SET_COLUMN(m_api, 0, (uint)a_dKV, sem_COLUMN_PARAMETERS.dwMag, sem_COLUMN_PARAMETERS.dwSpot, sem_COLUMN_PARAMETERS.dwBrightness, sem_COLUMN_PARAMETERS.dwContast); return true; } 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秒 { GET_STAGE(m_api, 0, 0); int result= GET_STAGE_MSGDATA(m_api, ref STAGE_PARAMETERS, iWaitMs); if(result == 1) { GET_STAGE_PARAMETERS_STRUCT_copy(STAGE_PARAMETERS,ref sem_stage_struct); } return result; } 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 bool MoveSEMToPoint(double a_dPositionX, double a_dPositionY) { SET_STAGE(m_api, 0,(float) a_dPositionX, (float)a_dPositionY, 1, 1, 1); return true; } 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 } }