Exemplo n.º 1
0
        public string ReadCommand(general.PLCLink link)
        {
            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 ...");
            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("");
        }
Exemplo n.º 2
0
 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";
     }
 }
Exemplo n.º 4
0
 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;
     }
 }
Exemplo n.º 5
0
        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
            {
            }
        }
Exemplo n.º 6
0
 public void CalcRequestCommand(bool write = false, int value = 0)
 {
     if (ReferenceEquals(link, null))
     {
         link = new general.PLCLink();
     }
     if (write)
     {
         if (value == 0)
         {
             link.cmd = "RS " + link.add.MemType + System.Convert.ToString(link.add.MemAdd);
         }
         else
         {
             link.cmd = "ST " + link.add.MemType + System.Convert.ToString(link.add.MemAdd);
         }
     }
     else
     {
         link.cmd = "RD " + link.add.MemType + System.Convert.ToString(link.add.MemAdd) + link.add.format;
     }
 }