프로젝트/Wafer 코팅 시뮬레이션 [반도체 장비 제어 시스템]

WaferLineCommLib - 클래스 라이브러리

언제나휴일 2021. 2. 27. 08:54
반응형

WaferLineCommLib는 클래스 라이브러리(.NET Framework)입니다.

 

WaferLineCommLib.zip
0.05MB
WaferLineCommLib 컴포넌트 다이어그램

1. MsgType.cs

namespace WaferLineCommLib
{
    public enum MsgType
    {
        MSG_CF_ADDSI,
        MSG_CF_ADDWF,
        MSG_CF_ADDPR,
        MSG_CF_SETSP,
        MSG_CF_SETDR,
        MSG_FC_ADDLN,
        MSG_FC_ADDWF,
        MSG_FC_ADDPR,
        MSG_FC_SETSP,
        MSG_FC_SETDR,
        MSG_FC_ENDPR,
        MSG_FC_ENDCO
    }
}

2. MyNetwork.cs

using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

namespace WaferLineCommLib
{
    public class MyNetwork
    {
        public static List<IPAddress> Addresses
        {
            get
            {
                List<IPAddress> addresses = new List<IPAddress>();
                string name = Dns.GetHostName();
                IPHostEntry iphe = Dns.GetHostEntry(name);
                foreach(IPAddress addr in iphe.AddressList)
                {
                    if(addr.AddressFamily == AddressFamily.InterNetwork)
                    {
                        addresses.Add(addr);
                    }    
                }
                return addresses;
            }
        }
    }
}

3. RecvStsEndPtEventArgs.cs

using System;
using System.Net;

namespace WaferLineCommLib
{
    public delegate void RecvStsEndPtEventHandler(object sender, RecvStsEndPtEventArgs e);
    public class RecvStsEndPtEventArgs:EventArgs
    {
        public IPAddress IPAddress { get; }
        public int Port { get; }
        public RecvStsEndPtEventArgs(IPAddress ipaddr, int port)
        {
            IPAddress = ipaddr;
            Port = port;
        }
    }
}

4. AddLineEventArgs.cs

using System;

namespace WaferLineCommLib
{
    public delegate void AddLineEventHandler(object sender, AddLineEventArgs e);
    public class AddLineEventArgs:EventArgs
    {
        public int No { get; }
        public AddLineEventArgs(int no)
        {
            No = no;
        }
    }
}

5. FactoryClient.cs

using System.IO;
using System.Net;
using System.Net.Sockets;

namespace WaferLineCommLib
{
    public class FactoryClient
    {
        string fip;
        int fport;
        public FactoryClient(string fip, int fport)
        {
            this.fip = fip;
            this.fport = fport;
        }
        public void SendMyInfo(string ip, int port)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_CF_ADDSI);
            bw.Write(ip);
            bw.Write(port);
            bw.Close();
            ms.Close();
            SendPacket(packet);
        }

        private void SendPacket(byte[] packet)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
            IPAddress addr = IPAddress.Parse(fip);
            IPEndPoint ep = new IPEndPoint(addr, fport);
            sock.Connect(ep);
            sock.Send(packet);
            sock.Close();
        }
        public void SendAddWafer(int no, int wcnt)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_CF_ADDWF);
            bw.Write(no);
            bw.Write(wcnt);
            bw.Close();
            ms.Close();
            SendPacket(packet);
        }
        public void SendAddPr(int no, int pcnt)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_CF_ADDPR);
            bw.Write(no);
            bw.Write(pcnt);
            bw.Close();
            ms.Close();
            SendPacket(packet);
        }
        public void SendSetSpin(int no, int spin)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_CF_SETSP);
            bw.Write(no);
            bw.Write(spin);
            bw.Close();
            ms.Close();
            SendPacket(packet);
        }
        public void SendSetDrop(int no, int drop)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_CF_SETDR);
            bw.Write(no);
            bw.Write(drop);
            bw.Close();
            ms.Close();
            SendPacket(packet);
        }
    }
}

6. FactoryServer.cs

using System.IO;
using System.Net;
using System.Net.Sockets;
using WaferLineLib;

namespace WaferLineCommLib
{
    public class FactoryServer
    {
        public event RecvStsEndPtEventHandler RecvStsEndPoint;
        public event AddWaferEventHandler AddedWafer;
        public event AddPREventHandler AddedPR;
        public event SetSpeedEventHandler SettedSpeed;
        public event SetDropEventHandler SettedDrop;
        public string IP { get; }
        public int Port { get; }
        public FactoryServer(string ip,int port)
        {
            IP = ip;
            Port = port;
        }

        delegate void StartDele();
        public void StartAsync()
        {
            StartDele dele = Start;
            dele.BeginInvoke(null, null);
        }
        Socket sock;
        public void Start()
        {
            sock = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
            IPAddress addr = IPAddress.Parse(IP);
            IPEndPoint endp = new IPEndPoint(addr, Port);
            sock.Bind(endp);
            sock.Listen(5);
            AcceptLoop();
        }

        private void AcceptLoop()
        {
            while(true)
            {
                Socket dosock = sock.Accept();
                DoIt(dosock);
            }
        }

        private void DoIt(Socket dosock)
        {
            byte[] packet = new byte[128];
            dosock.Receive(packet);
            MemoryStream ms = new MemoryStream(packet);
            BinaryReader br = new BinaryReader(ms);
            MsgType msgtype = (MsgType)br.ReadInt32();
            switch(msgtype)
            {
                case MsgType.MSG_CF_ADDSI: SetAddressProc(br); break;
                case MsgType.MSG_CF_ADDPR: AddPRProc(br); break;
                case MsgType.MSG_CF_ADDWF: AddWaferProc(br); break;
                case MsgType.MSG_CF_SETDR: SetDropProc(br); break;
                case MsgType.MSG_CF_SETSP: SetSpeedProc(br); break;
            }
            br.Close();
            ms.Close();
            dosock.Close();
        }
        private void SetSpeedProc(BinaryReader br)
        {
            int no = br.ReadInt32();
            int speed = br.ReadInt32();
            if (SettedSpeed != null)
            {
                SettedSpeed(this, new SetSpeedEventArgs(no, speed));
            }
        }
        private void SetDropProc(BinaryReader br)
        {
            int no = br.ReadInt32();
            int drop = br.ReadInt32();
            if (SettedDrop != null)
            {
                SettedDrop(this, new SetDropEventArgs(no, drop));
            }
        }
        private void AddPRProc(BinaryReader br)
        {
            int no = br.ReadInt32();
            int pcnt = br.ReadInt32();
            if (AddedPR != null)
            {
                AddedPR(this, new AddPREventArgs(no, pcnt));
            }
        }
        private void AddWaferProc(BinaryReader br)
        {
            int no = br.ReadInt32();
            int bwcnt = br.ReadInt32();
            if (AddedWafer != null)
            {
                AddedWafer(this, new AddWaferEventArgs(no, bwcnt));
            }
        }
        private void SetAddressProc(BinaryReader br)
        {
            IPAddress ipaddr = IPAddress.Parse(br.ReadString());
            int port = br.ReadInt32();
            if(RecvStsEndPoint != null)
            {
                RecvStsEndPoint(this, new RecvStsEndPtEventArgs(ipaddr, port));
            }
        }
    }
}

7. ControlClient.cs

using System.IO;
using System.Net;
using System.Net.Sockets;

namespace WaferLineCommLib
{
    public class ControlClient
    {
        IPAddress cip;
        int cport;
        public ControlClient(IPAddress cip,int cport)
        {
            this.cip = cip;
            this.cport = cport;
        }
        public bool SendAddWafer(int no,int bwcnt)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_FC_ADDWF);
            bw.Write(no);
            bw.Write(bwcnt);
            bw.Close();
            ms.Close();
            return SendPacket(packet);
        }
        public bool SendAddLine(int no)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_FC_ADDLN);
            bw.Write(no);            
            bw.Close();
            ms.Close();
            return SendPacket(packet);
        }
        bool SendPacket(byte[] packet)
        {
            try
            {
                Socket sock = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint ep = new IPEndPoint(cip, cport);
                sock.Connect(ep);
                sock.Send(packet);
                sock.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool SendAddPR(int no,int pcnt)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_FC_ADDPR);
            bw.Write(no);
            bw.Write(pcnt);
            bw.Close();
            ms.Close();
            return SendPacket(packet);
        }

        public bool SendSetSpeed(int no, int speed)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_FC_SETSP);
            bw.Write(no);
            bw.Write(speed);
            bw.Close();
            ms.Close();
            return SendPacket(packet);
        }

        public bool SendSetDrop(int no, int drop)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_FC_SETDR);
            bw.Write(no);
            bw.Write(drop);
            bw.Close();
            ms.Close();
            return SendPacket(packet);
        }

        public bool SendEndPR(int no)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_FC_ENDPR);
            bw.Write(no);
            bw.Close();
            ms.Close();
            return SendPacket(packet);
        }
        public bool SendEndedCoating(int no,int bwcnt,int awcnt)
        {
            byte[] packet = new byte[128];
            MemoryStream ms = new MemoryStream(packet);
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write((int)MsgType.MSG_FC_ENDCO);
            bw.Write(no);
            bw.Write(bwcnt);
            bw.Write(awcnt);
            bw.Close();
            ms.Close();
            return SendPacket(packet);
        }
    }
}

8. ControlServer.cs

using System.IO;
using System.Net;
using System.Net.Sockets;
using WaferLineLib;

namespace WaferLineCommLib
{
    public class ControlServer
    {
        public event AddLineEventHandler AddedLine;
        public event AddWaferEventHandler AddedWafer;
        public event AddPREventHandler AddedPR;
        public event SetSpeedEventHandler SettedSpeed;
        public event SetDropEventHandler SettedDrop;
        public event EndPREventHandler EndedPR;
        public event EndCoatingEventHandler EndedCoating;
        string ip;
        int port;
        public ControlServer(string ip,int port)
        {
            this.ip = ip;
            this.port = port;
        }

        delegate void StartDele();
        public void AsyncStart()
        {
            StartDele dele = Start;
            dele.BeginInvoke(null, null);
        }
        public void Start()
        {
            //소켓 생성
            Socket sock = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
            //소켓을 네트워크 인터페이스와 결합
            IPAddress addr = IPAddress.Parse(ip);
            IPEndPoint ep = new IPEndPoint(addr, port);
            sock.Bind(ep);
            //백로그 큐 크기 설정
            sock.Listen(5);
            //클라이언트 연결 요청 대기 및 수락 Loop
            while(true)
            {
                Socket dosock = sock.Accept();
                DoIt(dosock);
            }
        }

        private void DoIt(Socket dosock)
        {
            byte[] packet = new byte[128];
            dosock.Receive(packet);
            MemoryStream ms = new MemoryStream(packet);
            BinaryReader br = new BinaryReader(ms);
            MsgType msgtype = (MsgType)br.ReadInt32();
            switch(msgtype)
            {
                case MsgType.MSG_FC_ADDLN: AddLineProc(br); break;
                case MsgType.MSG_FC_ADDWF: AddWaferProc(br); break;
                case MsgType.MSG_FC_ADDPR: AddPrProc(br); break;
                case MsgType.MSG_FC_SETSP: SetSpeedProc(br); break;
                case MsgType.MSG_FC_SETDR: SetDropProc(br); break;
                case MsgType.MSG_FC_ENDPR: EndPrProc(br); break;
                case MsgType.MSG_FC_ENDCO: EndCoatingProc(br); break;
            }
            br.Close();
            ms.Close();
            dosock.Close();
        }

        private void EndCoatingProc(BinaryReader br)
        {
            int no = br.ReadInt32();
            int bwcnt = br.ReadInt32();
            int awcnt = br.ReadInt32();
            if (EndedCoating != null)
            {
                EndedCoating(this, new EndCoatingEventArgs(no, bwcnt,awcnt));
            }
        }

        private void EndPrProc(BinaryReader br)
        {
            int no = br.ReadInt32();            
            if (EndedPR != null)
            {
                EndedPR(this, new EndPREventArgs(no));
            }
        }

        private void SetDropProc(BinaryReader br)
        {
            int no = br.ReadInt32();
            int drop = br.ReadInt32();
            if (SettedDrop != null)
            {
                SettedDrop(this, new SetDropEventArgs(no,drop));
            }
        }

        private void SetSpeedProc(BinaryReader br)
        {
            int no = br.ReadInt32();
            int speed = br.ReadInt32();
            if (SettedSpeed != null)
            {
                SettedSpeed(this, new SetSpeedEventArgs(no, speed));
            }
        }

        private void AddPrProc(BinaryReader br)
        {
            int no = br.ReadInt32();
            int pcnt = br.ReadInt32();
            if (AddedPR != null)
            {
                AddedPR(this, new AddPREventArgs(no, pcnt));
            }
        }

        private void AddWaferProc(BinaryReader br)
        {
            int no = br.ReadInt32();
            int bwcnt = br.ReadInt32();
            if(AddedWafer != null)
            {
                AddedWafer(this, new AddWaferEventArgs(no, bwcnt));
            }
        }

        private void AddLineProc(BinaryReader br)
        {
            int no = br.ReadInt32();
            if(AddedLine != null)
            {
                AddedLine(this, new AddLineEventArgs(no));
            }
        }
    }
}
반응형