Esempio n. 1
0
        public Error WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value, string ip)
        {
            Error err = new Error();

            PlcDriver driver = new PlcDriver();
            bool      isFree = false;
            SpinWait  spin   = new SpinWait();

            while (isFree == false)
            {
                lock (connLock)
                {
                    for (int i = 0; i < connections.Count; i++)
                    {
                        if (connections[i].Ip == ip)
                        {
                            for (int ii = 0; ii < connections[i].Drivers.Count; ii++)
                            {
                                if (isFree == false)
                                {
                                    driver = connections[i].Drivers[ii];
                                    if (driver.GetIsActive() == false)
                                    {
                                        isFree = true;
                                        driver.SetIsActive(true);
                                        connections[i].Drivers[ii] = driver;
                                    }
                                }
                            }
                        }
                    }
                }

                if (isFree == false)
                {
                    spin.SpinOnce();
                }
            }

            err = driver.WriteBytes(dataType, db, startByteAdr, value);

            lock (connLock)
            {
                for (int i = 0; i < connections.Count; i++)
                {
                    if (connections[i].Ip == ip)
                    {
                        for (int ii = 0; ii < connections[i].Drivers.Count; ii++)
                        {
                            driver = connections[i].Drivers[ii];
                            driver.SetIsActive(false);
                            connections[i].Drivers[ii] = driver;
                        }
                    }
                }
            }

            return(err);
        }
Esempio n. 2
0
        private Error close(ref connection conn)
        {
            Error err = new Error();

            for (int i = 0; i < conn.Drivers.Count; i++)
            {
                PlcDriver driver = conn.Drivers[i];
                driver.SetIsActive(false);
                err = driver.Close();
                if (err.HasError())
                {
                    return(err);
                }
            }

            return(err);
        }
Esempio n. 3
0
        private Error open(ref connection conn)
        {
            Error err = new Error();

            for (int i = conn.Drivers.Count; i < conn.MaxCnt; i++)
            {
                PlcDriver driver = new PlcDriver();
                driver.SetIsActive(false);
                err = driver.Open(conn.Cpu, conn.Ip, conn.Port, conn.Rack, conn.Slot);
                if (err.HasError())
                {
                    return(err);
                }

                conn.Drivers.Add(driver);
            }

            return(err);
        }
Esempio n. 4
0
        public (byte[], Error) ReadBytes(DataType dataType, int db, int startByteAdr, int count, string ip)
        {
            Error err = new Error();

            byte[] ret = new byte[] { };

            PlcDriver driver = new PlcDriver();
            bool      isFree = false;
            SpinWait  spin   = new SpinWait();

            while (isFree == false)
            {
                lock (connLock)
                {
                    for (int i = 0; i < connections.Count; i++)
                    {
                        if (connections[i].Ip == ip)
                        {
                            for (int ii = 0; ii < connections[i].Drivers.Count; ii++)
                            {
                                if (isFree == false)
                                {
                                    driver = connections[i].Drivers[ii];
                                    if (driver.GetIsActive() == false)
                                    {
                                        isFree = true;
                                        driver.SetIsActive(true);
                                        connections[i].Drivers[ii] = driver;
                                    }
                                }
                            }
                        }
                    }
                }

                if (isFree == false)
                {
                    spin.SpinOnce();
                }
            }

            (ret, err) = driver.ReadBytes(dataType, db, startByteAdr, count);

            lock (connLock)
            {
                for (int i = 0; i < connections.Count; i++)
                {
                    if (connections[i].Ip == ip)
                    {
                        for (int ii = 0; ii < connections[i].Drivers.Count; ii++)
                        {
                            driver = connections[i].Drivers[ii];
                            driver.SetIsActive(false);
                            connections[i].Drivers[ii] = driver;
                        }
                    }
                }
            }

            return(ret, err);
        }