using AuroraIO; using Hermes; using System; using System.Reflection; using System.Threading; using static AuroraIO.AJINEXTEK.AjinIO.IOModuleInfo; namespace AuroraLaser { class LaserIPG_IO : ILaser { public bool IsEmissionReady { get; set; } public bool HasAlarm { get; set; } private bool _IsSimul; private int _Status; private double _OperatingPower; private double _RepetitionRate = 0.0; //internal bool IsCommError => IsConnected(); private IODevice _IO; Thread m_threadUpdateLaserStatus = null; private bool bPilotLaserOnCmd; private bool bLaserEmissionReadyOnCmd; public LaserIPG_IO(string deviceid, bool isSimul, IODevice IO_Board) { _IsSimul = isSimul; bPilotLaserOnCmd = false; _IO = IO_Board; } public int Open(LaserConfig Config) { if (_IsSimul) { return E1.OK; } int nRet = 0; if (!_IO.IsActivated) nRet = _IO.Activate(); if (nRet == E1.OK) { Thread.Sleep(100); m_threadUpdateLaserStatus = new Thread(new ThreadStart(UpdateLaserStatus)); m_threadUpdateLaserStatus.Start(); return nRet; } else return E1.NETWORK; } public void DisConnect() { if (_IsSimul) { return; } if (_IO._iOUnit.IsConnected()) { // 동작중인것을 정지시키는 코드는 여기에 // update 하는 thread 정지 if (m_threadUpdateLaserStatus != null) { m_threadUpdateLaserStatus.Join(500); m_threadUpdateLaserStatus.Abort(); } _IO._iOUnit.Close(); } } public int LaserOn() { int nRet = SetLaserDOCommand(eADOAddress.AnalogControl, true) == true ? E1.OK : E1.NETWORK; if (nRet != 0) return nRet; Thread.Sleep(500); nRet = SetLaserDOCommand(eADOAddress.LaserOn, true) == true ? E1.OK : E1.NETWORK; if (nRet != 0) return nRet; Thread.Sleep(500); nRet = SetLaserDOCommand(eADOAddress.ProgramStart, true) == true ? E1.OK : E1.NETWORK; Thread.Sleep(500); bool bProgramActiveResult = false; nRet = GetLaserDIStatus(eADIAddress.ProgramActive, out bProgramActiveResult) == true ? E1.OK : E1.NETWORK; if (nRet != E1.OK || !bProgramActiveResult) LaserOff(); return nRet; } public int LaserOff() { int nRet = SetLaserDOCommand(eADOAddress.ProgramStart, false) == true ? E1.OK : E1.NETWORK; if (nRet != 0) return nRet; Thread.Sleep(500); nRet = SetLaserDOCommand(eADOAddress.LaserOn, false) == true ? E1.OK : E1.NETWORK; if (nRet != 0) return nRet; Thread.Sleep(500); nRet = SetLaserDOCommand(eADOAddress.AnalogControl, false) == true ? E1.OK : E1.NETWORK; return nRet; } public int PilotLaserOn() { int nRet = SetLaserDOCommand(eADOAddress.GuideLaserEnable, true) == true ? E1.OK : E1.NETWORK; return nRet; } public int PilotLaserOff() { int nRet = SetLaserDOCommand(eADOAddress.GuideLaserEnable, false) == true ? E1.OK : E1.NETWORK; return nRet; } public bool GetConnected() { return _IO._iOUnit.IsConnected(); } public int SetData(int nTarget, int value) { // 어떻게 쓸지 고민중 return E1.OK; } public string GetData(int nTarget) { // 어떻게 쓸지 고민중 return ""; } private void UpdateLaserStatus() { while (GetConnected()) { int UpdateTimeInterval = 300; // 300ms Thread.Sleep(UpdateTimeInterval); GetLaserAllStatus(); } } public bool GetInitCommunication() { // IPG 는 해당 함수 없음 return true; } public bool GetEmissionStatus() { if (_IsSimul) return false; bool bResult; GetLaserDIStatus(eADIAddress.EmissionStatus, out bResult); return bResult; } public bool GetLaserReady() { if (_IsSimul) return false; bool bResult; GetLaserDIStatus(eADIAddress.LaserReady, out bResult); return bResult; } public bool GetLaserError() { if (_IsSimul) return false; bool bResult; GetLaserDIStatus(eADIAddress.LaserError, out bResult); return bResult; } public bool GetWariningStatus() { if (_IsSimul) return false; bool bResult; GetLaserDIStatus(eADIAddress.Warning, out bResult); return bResult; } // MAINS_STATUS, ERROR_STATUS 는 위에 있음 public bool GetPilotLaserStatus() { // pilot 신호는 Pin map 배정이 안되어서 DI로 받아오는게 없다. DO 로 대신하자. 밑에 DO 검색이 있어서 보류. if (_IsSimul) return false; bool bResult; GetLaserDoStatus(eADOAddress.GuideLaserEnable, out bResult); return bResult; //return false; } public string GetControlMode() { // Control mode 신호는 Pin map 배정이 안되어서 DI로 받아오는게 없다. return ""; } public string GetPowerCenter() { // IO Board 제어는 파워를 직접 세팅하지 않는다. return ""; } public string GetPowerRing() { // IO Board 제어는 파워를 직접 세팅하지 않는다. return ""; } #region Set 명령 나간 bit 를 모니터링 public bool GetStartLaserSetBitStatus() { if (_IsSimul) return false; // Laser Start 명령. 비슷한 이름이 많은데 기능에 대한 설명이 없으니 유추가 어렵다. return false; } public bool GetLaserStartSetBitStatus() { if (_IsSimul) return false; bool bResult; GetLaserDoStatus(eADOAddress.LaserOn, out bResult); return bResult; } public bool GetPilotLaserSetBitStatus() { if (_IsSimul) return false; bool bResult; GetLaserDoStatus(eADOAddress.GuideLaserEnable, out bResult); return bResult; } public bool GetLaserResetSetBitStatus() { if (_IsSimul) return false; bool bResult; GetLaserDoStatus(eADOAddress.ResetAlarm, out bResult); return bResult; } public bool GetControlModeSetStatus() { // Control mode 는 Pin map 에 없다 // 업데이트 예정 return false; } #endregion #region IO Board Functions private bool GetLaserDIStatus(eADIAddress addr, out bool bValue) { if (_IsSimul) { bValue = false; return true; } bool[] arrDI = new bool[Convert.ToInt32(_IO._iOUnit.GetDICount())]; int nRet = _IO._iOUnit.GetDiArray(ref arrDI); if (nRet == E1.OK && arrDI.Length != 0) { bValue = arrDI[Convert.ToInt32(addr)]; return true; } else { bValue = false; return false; } } private bool GetLaserDoStatus(eADOAddress addr, out bool bValue) { if (_IsSimul) { bValue = false; return true; } bool[] arrDO = new bool[Convert.ToInt32(_IO._iOUnit.GetDOCount())]; int nRet = _IO._iOUnit.GetDoArray(ref arrDO); if (nRet == E1.OK && arrDO.Length != 0) { bValue = arrDO[Convert.ToInt32(addr)]; return true; } else { bValue = false; return false; } } private bool GetLaserAllStatus() { if (_IsSimul) { return true; } bool[] arrDI = new bool[Convert.ToInt32(_IO._iOUnit.GetDICount())]; int nRet = _IO._iOUnit.GetDoArray(ref arrDI); if (nRet == E1.OK && arrDI.Length != 0) { // 여기에 arrDI 를 저장하는 변수를 assign 하자. return true; } else { return false; } } private bool SetLaserDOCommand(eADOAddress addr, bool bSetValue) { if (_IsSimul) { return true; } int nRet = _IO._iOUnit.WriteDoBit(Convert.ToInt32(addr), bSetValue); if (nRet == E1.OK) { return true; } else { return false; } } #endregion public static object GetPropertyValue(object obj, string propertyName) { if (obj == null) throw new ArgumentNullException(nameof(obj)); if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException(nameof(propertyName)); Type type = obj.GetType(); PropertyInfo propertyInfo = type.GetProperty(propertyName); if (propertyInfo == null) throw new ArgumentException($"Property '{propertyName}' not found on type '{type.FullName}'"); return propertyInfo.GetValue(obj); } } #region IO Board Communication Format #endregion }