public void SyncCircle(object source, ElapsedEventArgs e)
 {
     if (connection != null)
     {
         IsPlcConnected = connection.Connected;
     }
     DisconnectTimer.Stop();
     DisconnectTimer.Start();
     periodTimer.Enabled = false;
     if (ReferenceEquals(ConnList, null))
     {
         periodTimer.Start();
         return;
     }
     if (ConnList.Count == 0)
     {
         periodTimer.Start();
         return;
     }
     for (int i = 0; i <= ConnList.Count - 1; i++)
     {
         general.PLCLink temp_ConnListi = ConnList[i];
         SendRequest(ref temp_ConnListi);
         ConnList[i] = temp_ConnListi;
     }
     periodTimer.Start();
 }
 public void AddLink(general.PLCLink link)
 {
     if (ReferenceEquals(ConnList, null))
     {
         ConnList = new List <general.PLCLink>();
     }
     ConnList.Add(link);
 }
 public override void CalcCommand(bool Write = false, string value = "")
 {
     if (ReferenceEquals(link, null))
     {
         link = new general.PLCLink();
     }
     if (Write)
     {
         link.cmd = "M" + value;
     }
     else
     {
         link.cmd = "?M";
     }
 }
 public virtual void CalcCommand(bool Write = false, string value = "")
 {
     if (ReferenceEquals(link, null))
     {
         link = new general.PLCLink();
     }
     link.add.MemType = MemoryType;
     link.add.MemAdd  = PLCAddress;
     link.add.format  = DataFormat;
     if (Write)
     {
         link.cmd = "WR " + link.add.MemType + System.Convert.ToString(link.add.MemAdd) + link.add.format + " " + value;
     }
     else
     {
         link.cmd = "RD " + link.add.MemType + System.Convert.ToString(link.add.MemAdd) + link.add.format;
     }
 }
        public void SendRequest(ref general.PLCLink link)
        {
            if (link.enable == false)
            {
                return;
            }
            string returnStr = "";

            if (ReferenceEquals(connection, null))
            {
                Debug.Print("Creat connection");
                connection = new TcpClient();
            }
            if (!connection.Connected)
            {
                try
                {
                    //connection.ConnectAsync(_IP, _port)
                    connection.Connect(_IP, _port);
                    Debug.Print("Start connecting ...");
                    int timeout = Environment.TickCount;
                    while (((timeout + 5000) > Environment.TickCount) && (connection.Connected == false))
                    {
                    }
                    if (connection.Connected == true)
                    {
                        Debug.Print("Connected!");
                    }
                }
                catch (Exception)
                {
                }
            }

            if (connection.Connected)
            {
                returnStr = ReadCommand(link);
                link.usingmethod.useResult(returnStr);
            }
            else
            {
            }
        }
        public string ReadCommand(general.PLCLink link)
        {
            if (!connection.Connected)
            {
                return("Disconnect");
            }
            string str = link.cmd;

            byte[] dataByte  = System.Text.Encoding.ASCII.GetBytes(str);
            byte[] dataByte2 = new byte[1] {
                0xD
            };
            NetworkStream stream = connection.GetStream();

            Debug.Print("Start sending command ...");

            //if (PingToAddress(IP))
            {
                stream.Write(dataByte, 0, dataByte.Length);
                stream.Write(dataByte2, 0, dataByte2.Length);
            }

            Debug.Print("Command was send!");
            int timeout = Environment.TickCount;

            while (connection.Available == 0 && (timeout + 5000) > Environment.TickCount)
            {
            }

            if (connection.Available != 0)
            {
                Debug.Print("Reading data ...");
                byte[] receiveByte = new byte[connection.ReceiveBufferSize + 1];
                stream.Read(receiveByte, 0, receiveByte.Length);
                Debug.Print("Data was read!");
                string retStr = System.Text.Encoding.ASCII.GetString(receiveByte, 0, receiveByte.Length);
                return(retStr);
            }
            Debug.Print("Time out!");
            return("");
        }