/// <summary>
        /// when Client Try to connect server
        /// </summary>
        /// <param name="o"></param>
        private void Acceptor(IAsyncResult o)
        {
            TcpListener server = o.AsyncState as TcpListener;

            try
            {
                PLCClient newClient = new PLCClient();
                newClient.NetWork = server.EndAcceptTcpClient(o);
                lstClient.Add(newClient);
                LogerHelper2.ToLog("Robot PLC " + newClient.Name + " is connected now.", 0);
                RobotPLCStatus.UpdateRobotPLCStatus("Connected");
                isPLConLine = true;
                newClient.NetWork.GetStream().BeginRead(newClient.buffer, 0, newClient.buffer.Length, new AsyncCallback(TCPCallBack), newClient);
                server.BeginAcceptTcpClient(new AsyncCallback(Acceptor), server);//continue listening

                //send F999 to active PLC QueryMessageListAndRunFirstMessage("DUTSTATUS");
                byte[] tempdata = Encoding.ASCII.GetBytes("F999");
                MessageReceived.BeginInvoke("", MainPLCMessage.ConvertByteToMessage(tempdata), null, null);//async data output
                //Server. QueryMessageListAndRunFirstMessage("DUTSTATUS");
            }
            catch (Exception ex)
            {
                LogerHelper2.ToLog("test method PLCServers.Acceptor catch exception" + ex.Message, 3);
            }
        }
Example #2
0
        static public MainPLCMessage ConvertByteToMessage(byte[] data)
        {
            MainPLCMessage plcmessage = new MainPLCMessage();

            try
            {
                plcmessage = new MainPLCMessage(data);
            }
            catch (Exception e)
            {
                plcmessage.InvalidMessage = true;
                plcmessage.Info           = new ASCIIEncoding().GetString(data);
            }
            return(plcmessage);
        }
        /// <summary>
        /// PLC Client send message to server
        /// </summary>
        /// <param name="ar"></param>
        private void TCPCallBack(IAsyncResult ar)
        {
            PLCClient client = (PLCClient)ar.AsyncState;

            try
            {
                if (client.NetWork.Connected)
                {
                    NetworkStream ns      = client.NetWork.GetStream();
                    byte[]        recdata = new byte[ns.EndRead(ar)];
                    if (recdata.Length > 0)
                    {
                        Array.Copy(client.buffer, recdata, recdata.Length);
                        if (MessageReceived != null)
                        {
                            MessageReceived.BeginInvoke(client.Name, MainPLCMessage.ConvertByteToMessage(recdata), null, null);//async data output
                        }
                        ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client);
                    }
                    else
                    {
                        client.DisConnect();
                        LogerHelper2.ToLog("plc is disconnected now", 2);
                        plcHeartBeatTimer.Stop();
                        RobotPLCStatus.UpdateRobotPLCStatus("Disconnected");
                        isPLConLine = false;
                        lstClient.Remove(client);
                    }
                }
            }
            catch (Exception ex)
            {
                LogerHelper2.ToLog("testmothod TCPCallBack catch exception " + ex.Message, 3);
                RobotPLCStatus.UpdateRobotPLCStatus("Disconnected");
                plcHeartBeatTimer.Stop();
                client.DisConnect();
                isPLConLine = false;
                lstClient.Remove(client);
            }
        }