2026-06-05 13:44:39 +09:00

854 lines
28 KiB
C#

using Aurora.Api.Devices;
using Hermes;
using System;
using System.Text;
using System.Threading;
namespace AuroraLaser
{
class LaserNL202 : SerialConn, ILaser
{
private bool _IsSimul;
protected const int MAX_PORT_NUMBER = 16;
public const int MAX_SLAVES = 5;
protected string LOG_TAG = "NL202";
protected NL202Status _NL202Status;
public bool _isLaserOn;
public bool _checkLaserStandby;
public bool _checkLaserMotorMoving;
private static Mutex _mutexLaser = new Mutex(false, "MutexLaser");
#region NL202 Enumarator Definitions
public enum SendParam
{
NL202_Set_Run = 0, // -
NL202_Set_Stop, // -
NL202_Set_ResetFAULT, // -
NL202_Set_OutLev, // OFF, Adjustment, Maximum
NL202_Set_PumpLevel, // 0..100
NL202_Set_Wavelength, // 1064nm, 532nm
NL202_Set_BurstMode, // OFF, ON
NL202_Set_Burstlenth, // 1..65535
NL202_Set_TriggerBurst, // Burst Mode Shot
NL202_Set_AdjDelay, // 100..1000
NL202_Set_TTec1, // 15.00 .. 35.00
NL202_Set_SyncMode, // Internal, External
NL202_Set_SH1Temp, // 30.00 .. 70.0
NL202_Set_AttnTrans, // 0..100
NL202_Set_RepetitionRate, // Frq 1..1000
NL202_Set_MaxCnt
};
public enum ReadParam
{
NL202_Get_State = 0, // Idle, Running, Error, NotInitialized
NL202_Get_FaultCode, // 1..65535
NL202_Get_FaultMsg, // %s
NL202_Get_PumpLevel, // 0..100
NL202_Get_Wavelenth, // 1064nm, 532nm
NL202_Get_EnergyLevel, // OFF, Adjustment, Maximum
NL202_Get_TTec1, // 15.00 .. 35.00
NL202_Get_BurstMode, // OFF, ON
NL202_Get_QSWAdjDelay, // 100..1000
NL202_Get_BurstLenth, // 1..65535
NL202_Get_SyncMode, // Internal, External
NL202_Get_SH1Temp, // 30.00 .. 70.0
NL202_Get_AttnTrans, // 0..100
NL202_Get_RepetitionRate, // 확인필요 메뉴얼에는 없는내용임.
NL202_Get_ShotCnt, // 확인필요 메뉴얼에는 없는내용임.
NL202_Get_MaxCnt
};
#endregion
#region Status Class Definition
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; }
};
#endregion
public LaserNL202(string deviceid, bool isSimul) : base(deviceid, isSimul)
{
_IsSimul = isSimul;
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 int Open(LaserConfig Config)
{
if (_IsSimul) {
return E1.OK;
}
var connConfig = Config.ConnConfig as SerialConnConfig;
if (!IsConnected()) {
if (connConfig.PortNum >= 0 && connConfig.PortNum <= MAX_PORT_NUMBER) {
return Open(connConfig);
}
}
return Open(Config);
}
public void DisConnect()
{
if (_IsSimul) {
return;
}
Close();
}
public bool GetConnected()
{
return IsConnected();
}
public int SetData(int nTarget, int value)
{
return E1.OK;
}
public string GetData(int nTarget)
{
return "";
}
public int LaserOn()
{
RunLaser();
return 1;
}
public int LaserOff()
{
StopLaser();
return 1;
}
public int PilotLaserOn()
{
// guide beam 이나 pilot laser 에 대한 함수가 없다.
return 1;
}
public int PilotLaserOff()
{
// guide beam 이나 pilot laser 에 대한 함수가 없다.
return 1;
}
public bool GetInitCommunication()
{
// 업데이트 예정
return false;
}
public bool GetEmissionStatus()
{
// 업데이트 예정
return false;
}
public bool GetLaserReady()
{
// 업데이트 예정
return false;
}
public bool GetLaserError()
{
// 업데이트 예정
return false;
}
public bool GetWariningStatus()
{
// 업데이트 예정
return false;
}
// MAINS_STATUS, ERROR_STATUS 는 위에 있음
public bool GetPilotLaserStatus()
{
// 업데이트 예정
return false;
}
public string GetControlMode()
{
// 업데이트 예정
return "";
}
public string GetPowerCenter()
{
// 업데이트 예정
return "";
}
public string GetPowerRing()
{
// 업데이트 예정
return "";
}
#region Set 명령 나간 bit 를 모니터링
public bool GetStartLaserSetBitStatus()
{
// Laser Start 명령
// 업데이트 예정
return false;
}
public bool GetLaserStartSetBitStatus()
{
// Laser On 명령. Laser Emission Ready 를 위한 bit
// 업데이트 예정
return false;
}
public bool GetPilotLaserSetBitStatus()
{
// Pilot Laser On 명령.
// 업데이트 예정
return false;
}
public bool GetLaserResetSetBitStatus()
{
// Laser Reset 명령.
// 업데이트 예정
return false;
}
public bool GetControlModeSetStatus()
{
// Control mode Local(false) Remote(true)
// 업데이트 예정
return false;
}
#endregion
public int RS232_ReadBytes(ref byte[] readBuff, int size)
{
return Read(readBuff);
}
public const Int16 BUF_MAX = 32;
protected const Int16 FAIL = -1;
public int RS232_SendBuf(byte[] sendBuff, int length)
{
if (Write(sendBuff, length)) {
return E1.OK;
} else {
return FAIL;
}
}
public int RS232_SendByte(byte sendByte)
{
byte[] sendBuff = new byte[1];
sendBuff[0] = sendByte;
return RS232_SendBuf(sendBuff, 1);
}
public int RS232_SendString(SendParam index, double dValue = 0) // RS232_cputs -> string으로 변경
{
string sendStr = GetSendCommand(index, dValue);
if (sendStr.Length > 0) {
byte[] sendBuff = Encoding.UTF8.GetBytes(sendStr);
return RS232_SendBuf(sendBuff, sendBuff.Length);
}
return FAIL;
}
public int RS232_SendString(ReadParam index) // RS232_cputs -> string으로 변경
{
string sendStr = GetReadCommand(index);
if (sendStr.Length > 0) {
byte[] sendBuff = Encoding.UTF8.GetBytes(sendStr);
return RS232_SendBuf(sendBuff, sendBuff.Length);
}
return FAIL;
}
public string GetSendCommand(SendParam index, double dValue)
{
switch (index) {
case SendParam.NL202_Set_Run:
return "/?EXE/Run\r\n";
case SendParam.NL202_Set_Stop:
return "/?EXE/Stop\r\n";
case SendParam.NL202_Set_ResetFAULT:
return "/?EXE/ResetFAULT\r\n";
case SendParam.NL202_Set_OutLev:
if ((int)dValue == 0)
return "/?EXE/SetOutLev/OFF\r\n";
else if ((int)dValue == 1)
return "/?EXE/SetOutLev/Adjustment\r\n";
else if ((int)dValue == 2)
return "/?EXE/SetOutLev/Maximum\r\n";
break;
case SendParam.NL202_Set_PumpLevel:
return "/?EXE/SetPumpLevel/" + ((int)dValue).ToString() + "\r\n";
case SendParam.NL202_Set_Wavelength:
if ((int)dValue == 0)
return "/?EXE/SetWavelength/1064nm\r\n";
if ((int)dValue == 1)
return "/?EXE/SetWavelength/532nm\r\n";
break;
case SendParam.NL202_Set_BurstMode:
if ((int)dValue == 0)
return "/?EXE/SetBurstMode/OFF\r\n";
else if ((int)dValue == 1)
return "/?EXE/SetBurstMode/ON\r\n";
break;
case SendParam.NL202_Set_Burstlenth:
return "/?EXE/SetBurstLength/" + ((int)dValue + 1).ToString() + "\r\n";
case SendParam.NL202_Set_TriggerBurst:
return "/?EXE/TriggerBurst/Trigger\r\n";
case SendParam.NL202_Set_AdjDelay:
return "/?EXE/SetAdjDelay/" + ((int)dValue).ToString() + "\r\n";
case SendParam.NL202_Set_TTec1:
return "/?EXE/SetTTec1/" + ((int)dValue).ToString() + "\r\n";
case SendParam.NL202_Set_SyncMode:
//Internal, External
if ((int)dValue == 0)
return "/?EXE/SetSyncMode/Internal\r\n";
else if ((int)dValue == 1)
return "/?EXE/SetSyncMode/External\r\n";
break;
case SendParam.NL202_Set_SH1Temp:
return "/?EXE/SetSH1Temp/" + ((int)dValue).ToString() + "\r\n";
case SendParam.NL202_Set_AttnTrans:
return "/?EXE/SetAttnTrans/" + dValue.ToString("F2") + "\r\n";
case SendParam.NL202_Set_RepetitionRate:
return "/?EXE/SetRepetitionRate/" + ((int)dValue).ToString() + "\r\n";
default:
break;
}
return "";
}
public string GetReadCommand(ReadParam index)
{
switch (index) {
case ReadParam.NL202_Get_State:
return "/?RDVAR/State\r\n"; //prepare command
case ReadParam.NL202_Get_FaultCode:
return "/?RDVAR/FaultCode\r\n"; //prepare command
case ReadParam.NL202_Get_FaultMsg:
return "/?RDVAR/FaultMsg\r\n"; //prepare command
case ReadParam.NL202_Get_PumpLevel:
return "/?RDVAR/PumpLevel\r\n"; //prepare command
case ReadParam.NL202_Get_Wavelenth:
return "/?RDVAR/Wavelength\r\n"; //prepare command
case ReadParam.NL202_Get_EnergyLevel:
return "/?RDVAR/EnergyLevel\r\n"; //prepare command
case ReadParam.NL202_Get_TTec1:
return "/?RDVAR/Temperature_TTec1\r\n"; //prepare command
case ReadParam.NL202_Get_BurstMode:
return "/?RDVAR/BurstMode\r\n"; //prepare command
case ReadParam.NL202_Get_QSWAdjDelay:
return "/?RDVAR/QSWAdjDelay\r\n"; //prepare command
case ReadParam.NL202_Get_BurstLenth:
return "/?RDVAR/BurstLength\r\n"; //prepare command
case ReadParam.NL202_Get_SyncMode:
return "/?RDVAR/SyncMode\r\n"; //prepare command
case ReadParam.NL202_Get_SH1Temp:
return "/?RDVAR/SH1Temp\r\n"; //prepare command
case ReadParam.NL202_Get_AttnTrans:
return "/?RDVAR/AttenTrans\r\n"; //prepare command
case ReadParam.NL202_Get_RepetitionRate:
return "/?RDVAR/RepetitionRate\r\n"; //prepare command
case ReadParam.NL202_Get_ShotCnt:
///?RDVAR/TotalOpticalShots
return "/?RDVAR/TotalOpticalShots\r\n"; //prepare command
default:
break;
}
return "";
}
// Simulation Only
public void SetError(int errorCode, string errorMsg)
{
if (_IsSimul) {
_NL202Status.FaultCode = errorCode;
_NL202Status.FaultMsg = errorMsg;
}
}
public bool GetLastError(ref int errorCode, ref string errorMsg)
{
errorCode = _NL202Status.FaultCode;
errorMsg = _NL202Status.FaultMsg;
return true;
}
public bool RunLaser()
{
if (_IsSimul) {
_NL202Status.State = 1;
return true;
}
return SetLaserParam(SendParam.NL202_Set_Run, 0.0);
}
public bool StartBurstShot()
{
if (_IsSimul) {
return true;
}
return SetLaserParam(SendParam.NL202_Set_TriggerBurst, 0.0);
}
public bool StopLaser()
{
if (_IsSimul) {
_NL202Status.State = 0;
return true;
}
return SetLaserParam(SendParam.NL202_Set_Stop, 0.0); // laser 상태를 Stop
}
public bool StopShot()
{
if (_IsSimul) {
_NL202Status.State = 0;
return true;
}
// 쏘고있는 레이저를 Stop
bool bRet = SetLaserParam(SendParam.NL202_Set_Stop, 0.0);
Thread.Sleep(500);
bRet = SetLaserParam(SendParam.NL202_Set_SyncMode, 1);
Thread.Sleep(500);
bRet = SetLaserParam(SendParam.NL202_Set_Run, 0.0);
Thread.Sleep(700);
return bRet;
}
public bool ResetFault()
{
if (_IsSimul) {
SetError(0, "");
return true;
}
return SetLaserParam(SendParam.NL202_Set_ResetFAULT, 0.0);
}
public bool SetPumpLevel(double dValue)
{
if (_IsSimul) {
_NL202Status.PumpLevel = dValue;
return true;
}
return SetLaserParam(SendParam.NL202_Set_PumpLevel, dValue);
}
public bool SetBurstCount(int nBurst)
{
if (_IsSimul) {
_NL202Status.BurstCount = nBurst;
return true;
}
return SetLaserParam(SendParam.NL202_Set_Burstlenth, nBurst);
}
public bool SetBurstMode(int nFireMode)
{
if (_IsSimul) {
_NL202Status.BurstMode = nFireMode;
return true;
}
return SetLaserParam(SendParam.NL202_Set_BurstMode, nFireMode);
}
public bool SetRepetitionRate(int nRepetition)
{
if (_IsSimul) {
_NL202Status.RepetitionRate = nRepetition;
return true;
}
return SetLaserParam(SendParam.NL202_Set_RepetitionRate, nRepetition);
}
public bool SetEnergyLevel(int nMod)
{
if (_IsSimul) {
_NL202Status.EnergyLevel = nMod;
return true;
}
return SetLaserParam(SendParam.NL202_Set_OutLev, nMod);
}
public bool SetWaveLength(int nWaveLength)
{
if (_IsSimul) {
_NL202Status.WaveLength = nWaveLength;
return true;
}
return SetLaserParam(SendParam.NL202_Set_Wavelength, nWaveLength);
}
public bool SetAttenuator(double dValue)
{
if (_IsSimul) {
_NL202Status.AttenTrans = dValue;
return true;
}
return SetLaserParam(SendParam.NL202_Set_AttnTrans, dValue);
}
public bool SetSyncMode(int nMode)
{
if (_IsSimul) {
_NL202Status.SyncMode = nMode;
return true;
}
return SetLaserParam(SendParam.NL202_Set_SyncMode, nMode);
}
public 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 bool SetLaserParam(int nIndex, double dValue)
{
if (_IsSimul) {
return true;
}
if (nIndex >= 0 && nIndex < (int)SendParam.NL202_Set_MaxCnt) {
return SetLaserParam((SendParam)nIndex, dValue);
} else {
return false;
}
}
public bool SetLaserParam(SendParam nIndex, double dValue)
{
if (_IsSimul) {
return true;
}
_mutexLaser.WaitOne();
//clear buffers : Aurora의 Serialconn은 Write 전에 내부적으로 ClearSerialPortInOutBuffers 수행하므로 불필요
//_deviceNL202.RS232_flushRXTX();
RS232_SendString(nIndex, dValue);
_mutexLaser.ReleaseMutex();
return true;
}
public bool GetLaserParam(int nIndex)
{
if (nIndex >= 0 && nIndex < (int)ReadParam.NL202_Get_MaxCnt) {
return GetLaserParam((ReadParam)nIndex);
} else {
return false;
}
}
public bool GetLaserParam(ReadParam nIndex)
{
//USES_CONVERSION;
string strBuffer = "";
string str = "";
double dValue = 0.0;
int nValue = 0;
int n = 0;
byte[] readBuff = new byte[BUF_MAX];
_mutexLaser.WaitOne();
Thread.Sleep(100);
// 공통처리
RS232_SendString(nIndex); //send command
Thread.Sleep(50);
n = RS232_ReadBytes(ref readBuff, 4095); //read answer
if (n > 0) {
readBuff[n] = 0;
strBuffer = readBuff.ToString(); // byte to string
switch (nIndex) {
case 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 ReadParam.NL202_Get_FaultCode:
str = strBuffer.Substring(2, n - 4);
nValue = Convert.ToInt32(str);
_NL202Status.FaultCode = nValue;
break;
case ReadParam.NL202_Get_FaultMsg:
str = strBuffer.Substring(2, n - 4);
_NL202Status.FaultMsg = str;
break;
case ReadParam.NL202_Get_PumpLevel:
str = strBuffer.Substring(2, n - 4);
//nValue = Convert.ToDouble(str);
nValue = Convert.ToInt32(str);
_NL202Status.PumpLevel = nValue;
break;
case 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 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 ReadParam.NL202_Get_TTec1:
str = strBuffer.Substring(2, n - 4);
dValue = Convert.ToDouble(str);//인티저변경
_NL202Status.Temperature_TTec1 = dValue;
break;
case 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 ReadParam.NL202_Get_QSWAdjDelay:
str = strBuffer.Substring(2, n - 4);
nValue = Convert.ToInt32(str);
_NL202Status.QSWAdjustDelay = nValue;
break;
case ReadParam.NL202_Get_BurstLenth:
str = strBuffer.Substring(2, n - 4);
nValue = Convert.ToInt32(str);
_NL202Status.BurstCount = nValue - 1;
break;
case 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 ReadParam.NL202_Get_SH1Temp:
str = strBuffer.Substring(2, n - 4);
dValue = Convert.ToDouble(str);
_NL202Status.Temperature_SH1 = dValue;
break;
case ReadParam.NL202_Get_AttnTrans:
str = strBuffer.Substring(2, n - 4);
dValue = Convert.ToDouble(str);
_NL202Status.AttenTrans = dValue;
break;
case ReadParam.NL202_Get_RepetitionRate:
str = strBuffer.Substring(2, n - 4);
//nValue = Convert.ToDouble(str);
dValue = Convert.ToDouble(str);
_NL202Status.RepetitionRate = nValue;
break;
case 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 void CheckLaserParmeter(int nSynMode, int nBurstMode, int nBurstCount, int nWaveLength, int nRepetitionRate, double dAtt)
{
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;
}
}
}