//时间:20200610 //作者:王琦 //功能:SmartSEM remote API控制 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using APILib; using System.Threading; namespace SmartSEMControl { //可能遇到的错误提示部分??在log加入之后添加 class SmartSEM : ISEMControl { #region 模拟参数 //放大缩小 private String AP_MAG = "AP_MAG"; //焦距 private String AP_WD = "AP_WD"; //亮度 private String AP_BRIGHTNESS = "AP_BRIGHTNESS"; //对比度 private String AP_CONTRAST = "AP_CONTRAST"; //亮度目标 private String AP_AUTO_BRIGHT = "AP_AUTO_BRIGHT"; //对比度目标 private String AP_AUTO_CONTRAST = "AP_AUTO_CONTRAST"; //消像散X轴 private String AP_STIG_X = "AP_STIG_X"; //消像散Y轴 private String AP_STIG_Y = "AP_STIG_Y"; //角度补偿 private String AP_TILT_ANGLE = "AP_TILT_ANGLE"; //SEM移动X private String AP_BEAMSHIFT_X = "AP_BEAMSHIFT_X"; //SEM移动Y private String AP_BEAMSHIFT_Y = "AP_BEAMSHIFT_Y"; //X轴移动到 private String AP_STAGE_GOTO_X = "AP_STAGE_GOTO_X"; //Y轴移动到 private String AP_STAGE_GOTO_Y = "AP_STAGE_GOTO_Y"; //Z轴移动到 private String AP_STAGE_GOTO_Z = "AP_STAGE_GOTO_Z"; //T轴移动到 private String AP_STAGE_GOTO_T = "AP_STAGE_GOTO_T"; //R轴移动到 private String AP_STAGE_GOTO_R = "AP_STAGE_GOTO_R"; //M轴移动到 private String AP_STAGE_GOTO_M = "AP_STAGE_GOTO_M"; //Scan Rotate角度 private String AP_SCANROTATION = "AP_SCANROTATION"; //像素大小 private String AP_PIXEL_SIZE = "AP_PIXEL_SIZE"; //X轴坐标 private String AP_STAGE_AT_X = "AP_STAGE_AT_X"; //Y轴坐标 private String AP_STAGE_AT_Y = "AP_STAGE_AT_Y"; //Z轴坐标 private String AP_STAGE_AT_Z = "AP_STAGE_AT_Z"; //T轴坐标 private String AP_STAGE_AT_T = "AP_STAGE_AT_T"; //R轴坐标 private String AP_STAGE_AT_R = "AP_STAGE_AT_R"; //M轴坐标 private String AP_STAGE_AT_M = "AP_STAGE_AT_M"; #endregion #region 数字参数 //Scan Rotate开关 private String DP_SCAN_ROT = "DP_SCAN_ROT"; //取消电子束校正 private String DP_TILT_CORRECTION = "DP_TILT_CORRECTION"; //自动亮度对比度 private String DP_AUTO_VIDEO = "DP_AUTO_VIDEO"; //图像解冻 private String DP_FROZEN = "DP_FROZEN"; //图像类型切换 private String DP_DETECTOR_TYPE = "DP_DETECTOR_TYPE"; //图像大小 private String DP_IMAGE_STORE = "DP_IMAGE_STORE"; //样品台状态 private String DP_STAGE_IS = "DP_STAGE_IS"; //自动函数状态 private String DP_AUTO_FUNCTION = "DP_AUTO_FUNCTION"; #endregion #region 命令 //开启电压 private String CMD_BEAM_ON = "CMD_BEAM_ON"; //关闭电压 private String CMD_EHT_OFF = "CMD_EHT_OFF"; //自动对焦1 private String CMD_AUTO_FOCUS_COARSE = "CMD_AUTO_FOCUS_COARSE"; //自动对焦2 private String CMD_AUTO_FOCUS_FINE = "CMD_AUTO_FOCUS_FINE"; //自动消像散 private String CMD_AUTO_STIG = "CMD_AUTO_STIG"; #endregion #region 宏文件命令 private String MCF_FILENAME = "MCF_"; #endregion #region APILib private Api CZEMApi = new Api(); private Boolean m_bInitialised = false; #endregion #region 构造函数 public SmartSEM() { Init(); } #endregion #region 设备初始化 /// /// 设备初始化 /// /// return:true or false public Boolean Init() { long lrt = 0; #if ROMOTE lrt = CZEMApi.InitialiseRemoting(); #else lrt = CZEMApi.Initialise(""); #endif if (lrt != 0) { m_bInitialised = false; return false; } else { m_bInitialised = true; return true; } } #endregion #region 设备远程初始化 /// /// 设备远程初始化 /// /// return:true or false public Boolean InitRemoting() { long lrt = CZEMApi.InitialiseRemoting(); if (lrt != 0) { m_bInitialised = false; return false; } else { m_bInitialised = true; return true; } } #endregion #region 连接状态 public Boolean ConnectStatus() { return m_bInitialised; } #endregion #region 关闭设备 /// /// 关闭设备 /// /// true or false public Boolean Dispose() { if (m_bInitialised) { long lrt = CZEMApi.ClosingControl(); if (lrt != 0) { return false; } else { m_bInitialised = false; return true; } } else { return false; } } #endregion #region 读参数函数 private Boolean ReadParams(String str_param, ref float value) { if (m_bInitialised) { object varfloat = new VariantWrapper((float)0.0f); long lrt = CZEMApi.Get(str_param, ref varfloat); if (lrt != 0) { return false; } else { value = Convert.ToSingle(varfloat); return true; } } else { return false; } } #endregion #region 写模拟参数函数 private Boolean WriteParams(String str_param, float value) { if (m_bInitialised) { //先取范围 object varMin = new VariantWrapper((float)0.0f); object varMax = new VariantWrapper((float)0.0f); long lrt = CZEMApi.GetLimits(str_param, ref varMin, ref varMax); if (lrt != 0) { return false; } if (value <= Convert.ToSingle(varMax) && value >= Convert.ToSingle(varMin)) { object varFloat = new VariantWrapper(value); lrt = CZEMApi.Set(str_param, ref varFloat);// set value if (lrt != 0) { return false; } return true; } else { return false; } } else { return false; } } #endregion #region 写数字参数函数 private Boolean WriteDataParams(String str_param, float value) { if (m_bInitialised) { object varFloat = new VariantWrapper(value); long lrt = CZEMApi.Set(str_param, ref varFloat);// set value if (lrt != 0) { return false; } return true; } else { return false; } } #endregion #region 执行命令 private Boolean ExecuteCmd(String cmd) { if (m_bInitialised) { long lrt = CZEMApi.Execute(cmd);// execute command if (lrt != 0) { return false; } else { return true; } } else { return false; } } #endregion #region 放大缩小 /// /// 读取缩放倍数 /// /// true:float or false:NaN public float GetMagnification() { float ret = 0; if(ReadParams(AP_MAG, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置缩放倍数 /// /// 倍数 /// true or false public Boolean SetMagnification(float set) { return WriteParams(AP_MAG, set); } #endregion #region 焦距 /// /// 读取工作距离 /// /// true:float or false:NaN public float GetWorkingDistance() { float ret = 0; if (ReadParams(AP_WD, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置焦距 /// /// 焦距 /// true or false public Boolean SetWorkingDistance(float set) { return WriteParams(AP_WD, set); } #endregion #region 亮度 /// /// 读取亮度 /// /// true:float or false:NaN public float GetBrightness() { float ret = 0; if (ReadParams(AP_BRIGHTNESS, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置亮度 /// /// 亮度 /// true or false public Boolean SetBrightness(float set) { return WriteParams(AP_BRIGHTNESS, set); } #endregion #region 对比度 /// /// 读取对比度 /// /// true:float or false:NaN public float GetContrast() { float ret = 0; if (ReadParams(AP_CONTRAST, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置对比度 /// /// 对比度 /// true or false public Boolean SetContrast(float set) { return WriteParams(AP_CONTRAST, set); } #endregion #region 自动亮度对比度 /// /// 读取自动亮度对比度状态 /// /// true:float or false:NaN public float GetAutoVideo() { float ret = 0; if (ReadParams(DP_AUTO_VIDEO, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置自动亮度对比度状态 /// /// 0:off;1:B(亮度);2:C(对比度);3:BC /// true or false public Boolean SetAutoVideoOff() { return WriteDataParams(DP_AUTO_VIDEO, (float)0); } public Boolean SetAutoVideoBrightness() { return WriteDataParams(DP_AUTO_VIDEO, (float)1); } public Boolean SetAutoVideoContrast() { return WriteDataParams(DP_AUTO_VIDEO, (float)2); } public Boolean SetAutoVideoBrightnessAndContrast() { return WriteDataParams(DP_AUTO_VIDEO, (float)3); } #endregion #region 亮度目标 /// /// 读取亮度目标 /// /// true:float or false:NaN public float GetAutoBright() { float ret = 0; if (ReadParams(AP_AUTO_BRIGHT, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置亮度目标 /// /// 亮度 /// true or false public Boolean SetAutoBright(float set) { return WriteParams(AP_AUTO_BRIGHT, set); } #endregion #region 对比度目标 /// /// 读取对比度目标 /// /// true:float or false:NaN public float GetAutoContrast() { float ret = 0; if (ReadParams(AP_AUTO_CONTRAST, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置对比度目标 /// /// 对比度目标 /// true or false public Boolean SetAutoContrast(float set) { return WriteParams(AP_AUTO_CONTRAST, set); } #endregion #region 消像散 /// /// 读取消像散X /// /// true:float or false:NaN public float GetAstigmatismX() { float ret = 0; if (ReadParams(AP_STIG_X, ref ret)) { return ret; } else { return float.NaN; } } /// /// 读取消像散Y /// /// true:float or false:NaN public float GetAstigmatismY() { float ret = 0; if (ReadParams(AP_STIG_Y, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置消像散X /// /// 对比度 /// true or false public Boolean SetAstigmatismX(float set) { return WriteParams(AP_STIG_X, set); } /// /// 设置消像散Y /// /// 对比度 /// true or false public Boolean SetAstigmatismY(float set) { return WriteParams(AP_STIG_Y, set); } #endregion #region 角度补偿 /// /// 读取角度补偿 /// /// true:float or false:NaN public float GetTiltAngle() { float ret = 0; if (ReadParams(AP_TILT_ANGLE, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置角度补偿 /// /// 角度补偿 /// true or false public Boolean SetTiltAngle(float set) { Boolean ret = false; ret = WriteDataParams(DP_TILT_CORRECTION, 1); ret = WriteParams(AP_TILT_ANGLE, set); ret = WriteDataParams(DP_TILT_CORRECTION, 0); return ret; } #endregion #region 图像大小 public int[] GetImageStore() { int[] i_ret = new int[2]; float ret = 0; if (ReadParams(DP_IMAGE_STORE, ref ret)) { switch(ret) { case 0: i_ret[0] = 1024; i_ret[1] = 768; break; case 1: i_ret[0] = 512; i_ret[1] = 384; break; case 2: i_ret[0] = 2048; i_ret[1] = 1536; break; case 3: i_ret[0] = 3072; i_ret[1] = 2304; break; case 4: i_ret[0] = 4096; i_ret[1] = 3072; break; case 5: i_ret[0] = 5120; i_ret[1] = 3840; break; case 6: i_ret[0] = 6144; i_ret[1] = 4608; break; case 7: i_ret[0] = 8192; i_ret[1] = 6144; break; case 8: i_ret[0] = 12288; i_ret[1] = 9216; break; case 9: i_ret[0] = 16384; i_ret[1] = 12288; break; case 10: i_ret[0] = 24576; i_ret[1] = 18432; break; case 11: i_ret[0] = 32768; i_ret[1] = 24576; break; default: i_ret[0] = 0; i_ret[1] = 0; break; } } else { i_ret[0] = 0; i_ret[1] = 0; } return i_ret; } public Boolean SetImageStore(float set) { return WriteDataParams("DP_IMAGE_STORE", set); } #endregion #region 抓取图像 /// /// 抓取图像 /// /// true:float or false:NaN public Boolean GrabImage(String filename, short xoff,short yoff, short width,short height,short type) { if (m_bInitialised) { long lrt = CZEMApi.Grab(xoff, yoff, width, height, type, filename); if (lrt != 0) { return false; } else { ImageFrozen(); return true; } } else { return false; } } #endregion #region 图像解冻 public Boolean ImageFrozen() { return WriteDataParams(DP_FROZEN, (float)1); } public Boolean ImageLive() { return WriteDataParams(DP_FROZEN, (float)0); } #endregion #region 图像类型切换 public Boolean DetectorType(float set) { return WriteDataParams(DP_DETECTOR_TYPE, set); } #endregion #region 电子束移动接口 /// /// 获取电子束移动接口X /// /// true:float or false:NaN public float GetBeamShiftX() { float ret = 0; if (ReadParams(AP_BEAMSHIFT_X, ref ret)) { return ret; } else { return float.NaN; } } /// /// 获取电子束移动接口Y /// /// true:float or false:NaN public float GetBeamShiftY() { float ret = 0; if (ReadParams(AP_BEAMSHIFT_Y, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置电子束移动接口X /// /// X坐标 /// true or false public Boolean SetBeamShiftX(float set) { return WriteParams(AP_BEAMSHIFT_X, set); } /// /// 设置电子束移动接口Y /// /// X坐标 /// true or false public Boolean SetBeamShiftY(float set) { return WriteParams(AP_BEAMSHIFT_Y, set); } #endregion #region 样品台移动接口 /// /// 返回样品台坐标数组 /// /// Lenght=6 public float[] GetStagePosition() { object retx = new VariantWrapper((float)0.0f); object rety = new VariantWrapper((float)0.0f); object retz = new VariantWrapper((float)0.0f); object rett = new VariantWrapper((float)0.0f); object retr = new VariantWrapper((float)0.0f); object retm = new VariantWrapper((float)0.0f); float[] ret = new float[6]; if (m_bInitialised) { CZEMApi.GetStagePosition(ref retx, ref rety, ref retz, ref rett, ref retr, ref retm); ret[0] = Convert.ToSingle(retx); ret[1] = Convert.ToSingle(rety); ret[2] = Convert.ToSingle(retz); ret[3] = Convert.ToSingle(rett); ret[4] = Convert.ToSingle(retr); ret[5] = Convert.ToSingle(retm); } else { ret[0] = float.NaN; ret[1] = float.NaN; ret[2] = float.NaN; ret[3] = float.NaN; ret[4] = float.NaN; ret[5] = float.NaN; } return ret; } /// /// 样品台位置 X轴 /// /// true:float or false:NaN public float GetStageAtX() { float ret = 0; if (ReadParams(AP_STAGE_AT_X, ref ret)) { return ret; } else { return float.NaN; } } /// /// 样品台位置 Y轴 /// /// true:float or false:NaN public float GetStageAtY() { float ret = 0; if (ReadParams(AP_STAGE_AT_Y, ref ret)) { return ret; } else { return float.NaN; } } /// /// 样品台位置 Z轴 /// /// true:float or false:NaN public float GetStageAtZ() { float ret = 0; if (ReadParams(AP_STAGE_AT_Z, ref ret)) { return ret; } else { return float.NaN; } } /// /// 样品台位置 T轴 /// /// true:float or false:NaN public float GetStageAtT() { float ret = 0; if (ReadParams(AP_STAGE_AT_T, ref ret)) { return ret; } else { return float.NaN; } } /// /// 样品台位置 R轴 /// /// true:float or false:NaN public float GetStageAtR() { float ret = 0; if (ReadParams(AP_STAGE_AT_R, ref ret)) { return ret; } else { return float.NaN; } } /// /// 样品台位置 M轴 /// /// true:float or false:NaN public float GetStageAtM() { float ret = 0; if (ReadParams(AP_STAGE_AT_M, ref ret)) { return ret; } else { return float.NaN; } } /// /// 样品台位置 X轴 /// /// X轴移动坐标 /// public Boolean SetStageGotoX(float set) { return WriteParams(AP_STAGE_GOTO_X, set); } /// /// 样品台位置 Y轴 /// /// Y轴移动坐标 /// public Boolean SetStageGotoY(float set) { return WriteParams(AP_STAGE_GOTO_Y, set); } /// /// 样品台位置 Z轴 /// /// Y轴移动坐标 /// public Boolean SetStageGotoZ(float set) { return WriteParams(AP_STAGE_GOTO_Z, set); } /// /// 样品台位置 T轴 /// /// Y轴移动坐标 /// public Boolean SetStageGotoT(float set) { return WriteParams(AP_STAGE_GOTO_T, set); } /// /// 样品台位置 R轴 /// /// Y轴移动坐标 /// public Boolean SetStageGotoR(float set) { return WriteParams(AP_STAGE_GOTO_R, set); } /// /// 样品台位置 M轴 /// /// M轴移动坐标 /// public Boolean SetStageGotoM(float set) { return WriteParams(AP_STAGE_GOTO_M, set); } #endregion #region 移动样品台XY轴 public Boolean MoveStageXY(float x,float y) { Boolean ret = false; ret = SetStageGotoX(x); ret = SetStageGotoY(y); return ret; } #endregion #region Scan Rotate角度接口 /// /// 获取Scan Rotate角度接口 /// /// true:float or false:NaN public float GetScanRotation() { float ret = 0; if (ReadParams(AP_SCANROTATION, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置Scan Rotate角度接口 /// /// 角度补偿 /// true or false public Boolean SetScanRotation(float set) { Boolean ret = false; ret = WriteDataParams(DP_SCAN_ROT, 1); ret = WriteParams(AP_SCANROTATION, set); ret = WriteDataParams(DP_SCAN_ROT, 0); return ret; } #endregion #region 像素读取PixelSize /// /// 获取Pixel Size 接口,只读 /// /// true:float or false:NaN public float GetPixelSize() { float ret = 0; if (ReadParams(AP_PIXEL_SIZE, ref ret)) { return ret; } else { return float.NaN; } } #endregion #region 电子束校正设置 /// /// 读取电子束校正状态 /// /// public float GetTiltCorrection() { float ret = 0; if (ReadParams(DP_TILT_CORRECTION, ref ret)) { return ret; } else { return float.NaN; } } /// /// 设置电子束校正状态 /// /// /// public Boolean SetTiltCorrectionOff() { return WriteDataParams(DP_TILT_CORRECTION, (float)0); } public Boolean SetTiltCorrectionOn() { return WriteDataParams(DP_TILT_CORRECTION, (float)1); } #endregion #region 开启电压 public Boolean CmdOpenVoltage() { return ExecuteCmd(CMD_BEAM_ON); } #endregion #region 关闭电压 public Boolean CmdCloseVoltage() { return ExecuteCmd(CMD_EHT_OFF); } #endregion #region 自动对焦1 public Boolean CmdAutoFocusCoarse() { return ExecuteCmd(CMD_AUTO_FOCUS_COARSE); } #endregion #region 自动对焦2 public Boolean CmdAutoFocusFine() { return ExecuteCmd(CMD_AUTO_FOCUS_FINE); } #endregion #region 自动消像散 public Boolean CmdAutoStig() { return ExecuteCmd(CMD_AUTO_STIG); } #endregion #region 调用宏文件命令 public Boolean CMDMCFFilename(String filename) { String CMDMCF = MCF_FILENAME + filename; return ExecuteCmd(CMDMCF); } #endregion #region 读取样品台状态 public float GetStageIs() { float ret = 0; if (ReadParams(DP_STAGE_IS, ref ret)) { return ret; } else { return float.NaN; } } #endregion #region 读取自动函数的状态 public float GetAutoFunction() { float ret = 0; if (ReadParams(DP_AUTO_FUNCTION, ref ret)) { return ret; } else { return float.NaN; } } #endregion public void InsertPT() { } //PT针退出 public void OutputPT() { } } }