using System; using System.Threading; namespace AuroraLaser.Ekspla { public class LaserDeviceNL202 : LaserObj { public enum State { LASER_IDLE = 0, LASER_RUNNING, LASER_ERROR, LASER_NOTINITIALIZED }; public enum ShotDevice { SHOT_DEVICE_SCANNER = 0, SHOT_DEVICE_SLIT, }; public class NL202Status { public int State { get; set; } public int EnergyLevel { get; set; } public double PumpLevel { get; set; } public int QSWAdjustDelay { get; set; } public int SyncMode { get; set; } public int BurstMode { get; set; } //0 == Off / 1 == ON public int BurstCount { get; set; } public int RepetitionRate { get; set; } public double Temperature_TTec1 { get; set; } public double Temperature_SH1 { get; set; } public int WaveLength { get; set; } public double AttenTrans { get; set; } public int FaultCode { get; set; } public string FaultMsg { get; set; } public int ShotCount { get; set; } }; protected string LOG_TAG = "LaserDeviceNL202"; protected Aurora.Api.Logger LOG = new Aurora.Api.Logger("LaserDeviceNL202"); protected string _deviceId; // _device protected NL202 _deviceNL202; // _device //protected LaserConfig _config; private static Mutex _mutexLaser = new Mutex(false, "MutexLaser"); protected bool _exitDevice; // _exitLaserObj (LaserObj 소멸여부) protected int _laserState; //protected int m_nPort; protected bool _isConnected; protected NL202Status _NL202Status; protected bool _isSimul; public bool _isLaserOn; public bool _checkLaserStandby; public bool _checkLaserMotorMoving; public LaserDeviceNL202(string deviceId, LaserConfig config, bool isSimulation) { _deviceId = deviceId; //_config = config; _deviceNL202 = new NL202(_deviceId, config); _NL202Status = new NL202Status(); _isSimul = isSimulation; if (_isSimul) { _NL202Status.State = -1; _NL202Status.AttenTrans = new Random().NextDouble(); _NL202Status.BurstCount = new Random().Next(0, 2); _NL202Status.BurstMode = new Random().Next(0, 2); _NL202Status.EnergyLevel = new Random().Next(0, 10); _NL202Status.FaultCode = new Random().Next(0, 100); _NL202Status.FaultMsg = "Random Fault Message : " + _NL202Status.FaultCode; _NL202Status.PumpLevel = new Random().NextDouble(); _NL202Status.QSWAdjustDelay = new Random().Next(0, 10); _NL202Status.RepetitionRate = new Random().Next(0, 10); _NL202Status.ShotCount = new Random().Next(0, 10); _NL202Status.SyncMode = new Random().Next(0, 2); _NL202Status.Temperature_SH1 = new Random().NextDouble() * 20; _NL202Status.Temperature_TTec1 = new Random().NextDouble() * 20; _NL202Status.WaveLength = new Random().Next(0, 10); } } public override void Connect() { if (_isSimul) { _isConnected = true; _NL202Status.State = 0; return; } //송인복 - 확인필요 Init하는곳임 0=sucess 1=fail //_lastError = _deviceNL202.RS232_OpenComport(m_nPort, 115200, "8N1"); //init com port int result = _deviceNL202.RS232_OpenComport(); //init com port if (result == 0) { _isConnected = true; LOG.I(LOG_TAG + " :: Laser Connect OK"); } else { _isConnected = false; LOG.E(LOG_TAG + " :: Laser Connect Fail"); } } public override void Disconnect() { if (_isSimul) { _isConnected = false; _NL202Status.State = -1; return; } _deviceNL202.RS232_CloseComport(); _isConnected = false; } // Simulation Only public override void SetError(int errorCode, string errorMsg) { LOG.V(LOG_TAG + " :: SetError " + errorCode + ", " + errorMsg); if (_isSimul) { _NL202Status.FaultCode = errorCode; _NL202Status.FaultMsg = errorMsg; } } public override bool GetLastError(ref int errorCode, ref string errorMsg) { //if (_isSimul) { // errorCode = 0; // errorMsg = "no error"; // return true; //} errorCode = _NL202Status.FaultCode; errorMsg = _NL202Status.FaultMsg; return true; } public override bool RunLaser() { LOG.V("RunLaser"); if (_isSimul) { _NL202Status.State = 1; return true; } return SetLaserParam(NL202.SendParam.NL202_Set_Run, 0.0); } public override bool StartBurstShot() { LOG.V("StartBurstShot"); if (_isSimul) { return true; } return SetLaserParam(NL202.SendParam.NL202_Set_TriggerBurst, 0.0); } public override bool StopLaser() { LOG.V("StopLaser"); if (_isSimul) { _NL202Status.State = 0; return true; } return SetLaserParam(NL202.SendParam.NL202_Set_Stop, 0.0); // laser 상태를 Stop } public override bool StopShot() { LOG.V("StopShot"); if (_isSimul) { _NL202Status.State = 0; return true; } // 쏘고있는 레이저를 Stop bool bRet = SetLaserParam(NL202.SendParam.NL202_Set_Stop, 0.0); Thread.Sleep(500); bRet = SetLaserParam(NL202.SendParam.NL202_Set_SyncMode, 1); Thread.Sleep(500); bRet = SetLaserParam(NL202.SendParam.NL202_Set_Run, 0.0); Thread.Sleep(700); return bRet; } public override bool ResetFault() { LOG.V("ResetFault"); if (_isSimul) { SetError(0, ""); return true; } return SetLaserParam(NL202.SendParam.NL202_Set_ResetFAULT, 0.0); } public override bool SetPumpLevel(double dValue) { LOG.V("SetPumpLevel"); if (_isSimul) { _NL202Status.PumpLevel = dValue; return true; } return SetLaserParam(NL202.SendParam.NL202_Set_PumpLevel, dValue); } public override bool SetBurstCount(int nBurst) { LOG.V("SetBurstCount"); if (_isSimul) { _NL202Status.BurstCount = nBurst; return true; } return SetLaserParam(NL202.SendParam.NL202_Set_Burstlenth, nBurst); } public override bool SetBurstMode(int nFireMode) { LOG.V("SetBurstMode"); if (_isSimul) { _NL202Status.BurstMode = nFireMode; return true; } return SetLaserParam(NL202.SendParam.NL202_Set_BurstMode, nFireMode); } public override bool SetRepetitionRate(int nRepetition) { LOG.V("SetRepetitionRate"); if (_isSimul) { _NL202Status.RepetitionRate = nRepetition; return true; } return SetLaserParam(NL202.SendParam.NL202_Set_RepetitionRate, nRepetition); } public override bool SetEnergyLevel(int nMod) { LOG.V("SetEnergyLevel"); if (_isSimul) { _NL202Status.EnergyLevel = nMod; return true; } return SetLaserParam(NL202.SendParam.NL202_Set_OutLev, nMod); } public override bool SetWaveLength(int nWaveLength) { LOG.V("SetWaveLength"); if (_isSimul) { _NL202Status.WaveLength = nWaveLength; return true; } return SetLaserParam(NL202.SendParam.NL202_Set_Wavelength, nWaveLength); } public override bool SetAttenuator(double dValue) { LOG.V("SetAttenuator"); if (_isSimul) { _NL202Status.AttenTrans = dValue; return true; } return SetLaserParam(NL202.SendParam.NL202_Set_AttnTrans, dValue); } public override bool SetSyncMode(int nMode) { LOG.V("SetSyncMode"); if (_isSimul) { _NL202Status.SyncMode = nMode; return true; } return SetLaserParam(NL202.SendParam.NL202_Set_SyncMode, nMode); } public override bool GetConnected() { return _isConnected; } public override double GetTemperature(int diode) { //if (_isSimul) return new Random().NextDouble() * 20; if (diode == 0) return _NL202Status.Temperature_TTec1; else if (diode == 1) return _NL202Status.Temperature_SH1; else return 0.0; } //public override bool SetOutputControl(int nValue) // SetEnergyLevel과 중복. 삭제 //{ // if (_isSimul) { // //_NL202Status.OutputControl = nMode; // EnergyLevel ?? // return true; // } // return SetLaserParam(NL202.SendParam.NL202_Set_OutLev, nValue); //} public override bool SetLaserParam(int nIndex, double dValue) { LOG.V("SetLaserParam"); if (_isSimul) { return true; } if (nIndex >= 0 && nIndex < (int)NL202.SendParam.NL202_Set_MaxCnt) { return SetLaserParam((NL202.SendParam)nIndex, dValue); } else { return false; } } public bool SetLaserParam(NL202.SendParam nIndex, double dValue) { LOG.V("SetLaserParam"); if (_isSimul) { return true; } _mutexLaser.WaitOne(); //clear buffers : Aurora의 Serialconn은 Write 전에 내부적으로 ClearSerialPortInOutBuffers 수행하므로 불필요 //_deviceNL202.RS232_flushRXTX(); _deviceNL202.RS232_SendString(nIndex, dValue); _mutexLaser.ReleaseMutex(); return true; } public override bool GetLaserParam(int nIndex) { if (nIndex >= 0 && nIndex < (int)NL202.ReadParam.NL202_Get_MaxCnt) { return GetLaserParam((NL202.ReadParam)nIndex); } else { return false; } } public bool GetLaserParam(NL202.ReadParam nIndex) { //USES_CONVERSION; string strBuffer = ""; string str = ""; double dValue = 0.0; int nValue = 0; int n = 0; byte[] readBuff = new byte[NL202.BUF_MAX]; _mutexLaser.WaitOne(); Thread.Sleep(100); // 공통처리 _deviceNL202.RS232_SendString(nIndex); //send command Thread.Sleep(50); n = _deviceNL202.RS232_ReadBytes(ref readBuff, 4095); //read answer if (n > 0) { readBuff[n] = 0; strBuffer = readBuff.ToString(); // byte to string switch (nIndex) { case NL202.ReadParam.NL202_Get_State: str = strBuffer.Substring(4, n - 7); if (str == "Idle") _NL202Status.State = 0; else if (str == "Running") _NL202Status.State = 1; else if (str == "Error") _NL202Status.State = 2; else if (str == "NotInitialized") _NL202Status.State = 3; else break; //return false; break; case NL202.ReadParam.NL202_Get_FaultCode: str = strBuffer.Substring(2, n - 4); nValue = Convert.ToInt32(str); _NL202Status.FaultCode = nValue; break; case NL202.ReadParam.NL202_Get_FaultMsg: str = strBuffer.Substring(2, n - 4); _NL202Status.FaultMsg = str; break; case NL202.ReadParam.NL202_Get_PumpLevel: str = strBuffer.Substring(2, n - 4); //nValue = Convert.ToDouble(str); nValue = Convert.ToInt32(str); _NL202Status.PumpLevel = nValue; break; case NL202.ReadParam.NL202_Get_Wavelenth: str = strBuffer.Substring(2, n - 6); int ntmp = Convert.ToInt32(str); if (ntmp == 532) { nValue = 1; } else if (ntmp == 1064) { nValue = 0; } _NL202Status.WaveLength = nValue; break; case NL202.ReadParam.NL202_Get_EnergyLevel: str = strBuffer.Substring(3, n - 5); if (str == "OFF") _NL202Status.EnergyLevel = 0; else if (str == "Adjustment") _NL202Status.EnergyLevel = 1; else if (str == "Maximum") _NL202Status.EnergyLevel = 2; else break; //return false; break; case NL202.ReadParam.NL202_Get_TTec1: str = strBuffer.Substring(2, n - 4); dValue = Convert.ToDouble(str);//인티저변경 _NL202Status.Temperature_TTec1 = dValue; break; case NL202.ReadParam.NL202_Get_BurstMode: str = strBuffer.Substring(3, n - 5); if (str == "OFF") _NL202Status.BurstMode = 0; else if (str == "ON") _NL202Status.BurstMode = 1; else break; //return false; break; case NL202.ReadParam.NL202_Get_QSWAdjDelay: str = strBuffer.Substring(2, n - 4); nValue = Convert.ToInt32(str); _NL202Status.QSWAdjustDelay = nValue; break; case NL202.ReadParam.NL202_Get_BurstLenth: str = strBuffer.Substring(2, n - 4); nValue = Convert.ToInt32(str); _NL202Status.BurstCount = nValue - 1; break; case NL202.ReadParam.NL202_Get_SyncMode: str = strBuffer.Substring(3, n - 5); if (str == "Internal") _NL202Status.SyncMode = 0; else if (str == "External") _NL202Status.SyncMode = 1; else break; //return false; break; case NL202.ReadParam.NL202_Get_SH1Temp: str = strBuffer.Substring(2, n - 4); dValue = Convert.ToDouble(str); _NL202Status.Temperature_SH1 = dValue; break; case NL202.ReadParam.NL202_Get_AttnTrans: str = strBuffer.Substring(2, n - 4); dValue = Convert.ToDouble(str); _NL202Status.AttenTrans = dValue; break; case NL202.ReadParam.NL202_Get_RepetitionRate: str = strBuffer.Substring(2, n - 4); //nValue = Convert.ToDouble(str); dValue = Convert.ToDouble(str); _NL202Status.RepetitionRate = nValue; break; case NL202.ReadParam.NL202_Get_ShotCnt: str = strBuffer.Substring(2, n - 4); nValue = Convert.ToInt32(str); _NL202Status.ShotCount = nValue; break; default: break; } } _mutexLaser.ReleaseMutex(); return true; } public override void CheckLaserParmeter(int nSynMode, int nBurstMode, int nBurstCount, int nWaveLength, int nRepetitionRate, double dAtt) { LOG.V("CheckLaserParmeter"); if (_isSimul) return; if (nSynMode != GetSyncMode()) { SetSyncMode(nSynMode); } if (nBurstMode != GetBurstMode()) { SetBurstMode(nBurstMode); } if (nBurstCount != GetBurstCount()) { SetBurstCount(nBurstCount); } if (nWaveLength != GetWaveLength()) { SetWaveLength(nWaveLength); } if (nRepetitionRate != GetRepetitionRate()) { SetRepetitionRate(nRepetitionRate); } if (dAtt != GetAttTrans()) { SetAttenuator(dAtt); } if (GetState() == 0) { RunLaser(); } } public int GetState() { return _NL202Status.State; } public double GetPumpLevel() { //if (_isSimul) return new Random().Next(0, 2); return _NL202Status.PumpLevel; } public int GetBurstCount() { //if (_isSimul) return new Random().Next(0, 2); return _NL202Status.BurstCount; } public int GetBurstMode() { //if (_isSimul) return new Random().Next(0, 2); return _NL202Status.BurstMode; } public double GetAttTrans() { //if (_isSimul) return new Random().NextDouble(); return _NL202Status.AttenTrans; } public int GetRepetitionRate() { //if (_isSimul) return new Random().Next(0, 2); return _NL202Status.RepetitionRate; } public int GetWaveLength() { //if (_isSimul) return new Random().Next(0, 2); return _NL202Status.WaveLength; } public int GetEnergyLevel() { //if (_isSimul) return new Random().Next(0, 2); return _NL202Status.EnergyLevel; } public int GetSyncMode() { //if (_isSimul) return new Random().Next(0, 2); return _NL202Status.SyncMode; } public int GetQSWadjDelay() { //if (_isSimul) return new Random().Next(0, 2); return _NL202Status.QSWAdjustDelay; } public int GetShotCnt() { //if (_isSimul) return new Random().Next(0, 2); return _NL202Status.ShotCount; } } }