using log4net; using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DIT.Framework.Module._01_SerialModules { /// /// Model : IVC 3000 , 3100 Module /// Home : www.isvt.co.kr /// class IVC3000 : SerialModule { private enum Read_Cmd { SystemPressure, ThrottleValvePos, MFCFlow, MFCOpen, MFCSetFlow, MFCRange, State, } /// /// Exception Log /// private ILog ExceptionLog = LogManager.GetLogger("IVC3000_Exception"); /// /// Exception Log를 기록하기 위한 메서드 /// /// Error Message private void WriteExceptionLog(string msg) { if (UseExceptionLog) ExceptionLog.Debug(msg); } /// /// System Pressure (atm) /// public string Pressure { get { return pressure; } private set { pressure = value; } } private string pressure; /// /// Throttle Vavle Current Point /// public string VlvPos { get { return vlvPos; } private set { vlvPos = value; } } private string vlvPos; /// /// MFC Flow Range /// public string MFCRange { get { return mfcRange; } private set { mfcRange = value; } } private string mfcRange; /// /// MFC Set Flow /// public string MFCSetFlow { get { return mfcSetFlow; } private set { mfcSetFlow = value; } } private string mfcSetFlow; /// /// MFC Current Flow /// public string MFCFlow { get { return mfcFlow; } private set { mfcFlow = value; } } private string mfcFlow; /// /// MFC Open State /// public bool MFCOpen { get { return mfcOpen; } private set { mfcOpen = value; } } private bool mfcOpen; /// /// Autonics_PMC_2HS의 생성자. /// /// Port이름 ex)COM6, COM13 /// 전송속도 ex)4800, 9600, 19200 /// 바이트 당 데이터 비트의 표준길이 ex)5, 6, 7, 8 /// 패리티 검사 프로토콜 ex)Ports.Parity.None /// 비트당 정지비트의 표준 개수 ex)Ports.StopBits.One /// 직렬 전송을 위한 핸드셰이킹 ex)Ports.Handshake.None public IVC3000(string PortName, int BaudRate, int DataBits = 8, Parity Parity = Parity.None, StopBits StopBits = StopBits.One, Handshake Handshake = Handshake.None) : base(PortName, BaudRate, DataBits, Parity, StopBits, Handshake) { Pressure = "0"; VlvPos = "0"; MFCRange = "0"; MFCSetFlow = "0"; MFCFlow = "0"; MFCOpen = false; } /// /// Serial에서 받은 명령어 처리 메서드 /// /// Recv받은 Data private void OnRecvData(Read_Cmd ReadCmd, byte[] recvData) { try { string str = Encoding.ASCII.GetString(recvData); switch (ReadCmd) { case Read_Cmd.SystemPressure: string[] _sSplitPressue = str.Replace("+", "\r").Split('P'); Pressure = (Convert.ToDouble(_sSplitPressue[1]) * 10).ToString(); break; case Read_Cmd.ThrottleValvePos: string[] _sSplitVlvPos = str.Replace("+", "\r").Split('V'); VlvPos = (Convert.ToDouble(_sSplitVlvPos[1])).ToString(); break; case Read_Cmd.MFCFlow: string[] sSplitMFCFlow = str.Split('+'); MFCFlow = (int.Parse(MFCRange) * Convert.ToDouble(sSplitMFCFlow[1]) / 100).ToString(); break; case Read_Cmd.MFCOpen: MFCOpen = str.Substring(2, 1) == "1" ? true : false; break; case Read_Cmd.MFCSetFlow: MFCSetFlow = (int.Parse(MFCRange) * double.Parse(str.Split('+')[1]) / 100).ToString(); break; case Read_Cmd.MFCRange: MFCRange = str.Split('+')[1]; break; default: break; } } catch(Exception e) { WriteExceptionLog(e.Message); } } /// /// System Pressure(atm) Request /// public void Request_SystemPressure() { try { byte[] cmd = Encoding.ASCII.GetBytes($"R5{CR}"); byte[] recvData = SendWaitData(cmd); if (recvData == null) return; OnRecvData(Read_Cmd.SystemPressure, recvData); } catch (Exception e) { WriteExceptionLog(e.Message); } } /// /// Throttle Valve Position Request /// public void Request_ThrottleValvePos() { try { byte[] cmd = Encoding.ASCII.GetBytes($"R6{CR}"); byte[] recvData = SendWaitData(cmd); if (recvData == null) return; OnRecvData(Read_Cmd.ThrottleValvePos, recvData); } catch (Exception e) { WriteExceptionLog(e.Message); } } /// /// MFC Current Flow Request /// public void Request_MFCFlow() { try { byte[] cmd = Encoding.ASCII.GetBytes($"R61{CR}"); byte[] recvData = SendWaitData(cmd); if (recvData == null) return; OnRecvData(Read_Cmd.MFCFlow, recvData); } catch (Exception e) { WriteExceptionLog(e.Message); } } /// /// MFC Open State Request /// public void Request_MFCOpen() { try { byte[] cmd = Encoding.ASCII.GetBytes($"R69{CR}"); byte[] recvData = SendWaitData(cmd); if (recvData == null) return; OnRecvData(Read_Cmd.MFCOpen, recvData); } catch (Exception e) { WriteExceptionLog(e.Message); } } /// /// MFC Set Flow Request /// public void Request_MFCSet() { try { byte[] cmd = Encoding.ASCII.GetBytes($"R65{CR}"); byte[] recvData = SendWaitData(cmd); if (recvData == null) return; OnRecvData(Read_Cmd.MFCSetFlow, recvData); } catch (Exception e) { WriteExceptionLog(e.Message); } } /// /// MFC Flow Range Request /// public void Request_MFCRange() { try { byte[] cmd = Encoding.ASCII.GetBytes($"R70{CR}"); byte[] recvData = SendWaitData(cmd); if (recvData == null) return; OnRecvData(Read_Cmd.MFCRange, recvData); } catch (Exception e) { WriteExceptionLog(e.Message); } } /// /// Throttle Valve Set Pos 명령어 /// /// Point public void SetValvePos(string Pos) { try { int pos = 0; int.TryParse(Pos, out pos); SET_ThrottleVlvPointType(1, 0); SET_ThrottleVlvPoint(1, pos); RUN_ThrottleVlvPoint(1); } catch (Exception e) { WriteExceptionLog(e.Message); } } /// /// Throttle Valve Set - Point에 대한 Type /// /// Chanel 1 ~ 5 /// 0 : Position, 1 : Pressure public void SET_ThrottleVlvPointType(int iCh, int Type) { try { SendData($"T{iCh}{Type}{CR}"); } catch (Exception e) { WriteExceptionLog(e.Message); } } /// /// Throttle Valve Set Point iCh /// /// Chanel 1 ~ 5 /// Set Point 1 ~ 100 public void SET_ThrottleVlvPoint(int iCh, int Point) { SendData($"S{iCh}{Point}{CR}"); } /// /// Throttle Vavle Run iCh /// /// Chanel 1 ~ 5 public void RUN_ThrottleVlvPoint(int iCh) { SendData($"D{iCh}{CR}"); } /// /// MFC Open 명령어 /// /// public void RUN_MFCOpen(bool open) { SET_MFCType(5, 2); SET_MFCValveOpen(1, open); } /// /// MFC Type Set /// /// Chanel 5 ~ 8 /// 1 : SCCM, 2 : SLM public void SET_MFCType(int iCh, int Type) { SendData($"W{iCh} {Type}{CR}"); } /// /// SET MFC Valve Open /// /// Chanel 1 ~ 4 /// true : Oepn, False : Close public void SET_MFCValveOpen(int iCh, bool open) { string sopen = open ? "1" : "0"; SendData($"L{iCh}{sopen}{CR}"); } } }