Exemple #1
0
        public bool BindToPLC()
        {
            opcServer = new OPCServerAsyn();
            if (!opcServer.Connect())
            {
                return(false);
            }
            if (!opcServer.AddGroup(this))
            {
                return(false);
            }
            int client = 1;

            if (statusDB != null)
            {
                OpcRcw.Da.OPCITEMDEF[] statusitem = new OPCITEMDEF[statusDB.Length];
                for (int i = 0; i < statusDB.Length; i++)
                {
                    statusitem[i].szAccessPath        = "";
                    statusitem[i].bActive             = 1;
                    statusitem[i].hClient             = client;
                    statusitem[i].dwBlobSize          = 1;
                    statusitem[i].pBlob               = IntPtr.Zero;
                    statusitem[i].vtRequestedDataType = (int)System.Runtime.InteropServices.VarEnum.VT_BSTR;
                    statusitem[i].szItemID            = string.Format("S7:[S7 connection_1]{0}", statusDB[i]); //状态DB
                    client++;
                }

                if (!opcServer.AddItems(statusitem, statusHandle))
                {
                    return(false);
                }
            }
            //异步读
            opcServer.ReadPlc(statusHandle);
            //订阅
            if (!opcServer.DataChange())
            {
                return(false);
            }

            isBindToPLC = true;
            return(isBindToPLC);
        }
Exemple #2
0
        public bool BindToPLCAsyn()
        {
            this.opcServerAsyn = new OPCServerAsyn();
            if (!this.opcServerAsyn.Connect())
            {
                return(false);
            }
            if (!this.opcServerAsyn.AddGroup(this))
            {
                return(false);
            }
            int num = 1;

            if (base.statusDB != null)
            {
                OPCITEMDEF[] items = new OPCITEMDEF[base.statusDB.Length];
                for (int i = 0; i < base.statusDB.Length; i++)
                {
                    items[i].szAccessPath        = "";
                    items[i].bActive             = 1;
                    items[i].hClient             = num;
                    items[i].dwBlobSize          = 1;
                    items[i].pBlob               = IntPtr.Zero;
                    items[i].vtRequestedDataType = 8;
                    items[i].szItemID            = $"S7:[S7_Connection_1]{base.statusDB[i]}";
                    //items[0].szItemID = $"Logical.test3";
                    num++;
                }


                if (!this.opcServerAsyn.AddItems(items, base.statusHandle))
                {
                    return(false);
                }
            }
            this.opcServerAsyn.ReadPlc(base.statusHandle);
            if (!this.opcServerAsyn.DataChange())
            {
                return(false);
            }
            return(true);
        }
Exemple #3
0
        public bool BindToPLCAsyn(int dwRequestedUpdateRate)
        {
            this.opcServerAsyn       = new OPCServerAsyn();
            this.opcServerAsyn.plcip = this.mainFrm.plc_ip;
            if (!this.opcServerAsyn.Connect())
            {
                return(false);
            }
            if (!this.opcServerAsyn.AddGroup(this, dwRequestedUpdateRate))
            {
                return(false);
            }
            int num = 1;

            if (this.R_CarDownTaskDB != null)
            {
                OPCITEMDEF[] items = new OPCITEMDEF[this.R_CarDownTaskDB.Length];
                for (int i = 0; i < this.R_CarDownTaskDB.Length; i++)
                {
                    items[i].szAccessPath        = "";
                    items[i].bActive             = 1;
                    items[i].hClient             = num;
                    items[i].dwBlobSize          = 1;
                    items[i].pBlob               = IntPtr.Zero;
                    items[i].vtRequestedDataType = 8;
                    items[i].szItemID            = $"S7:[S7_Connection_1]{this.R_CarDownTaskDB[i]}";
                    num++;
                }

                //for (int i = 0; i < 4; i++)
                //{
                //    items[i].szAccessPath = "";
                //    items[i].bActive = 1;
                //    items[i].hClient = num;
                //    items[i].dwBlobSize = 1;
                //    items[i].pBlob = IntPtr.Zero;
                //    items[i].vtRequestedDataType = 8;
                //    items[i].szItemID = $"Logical.test{num}";
                //    if (i == 3)
                //    {
                //        items[i].hClient = num + 10;
                //        items[i].szItemID = "Numeric.NewDataItem1";
                //    }
                //    num++;
                //}

                if (!this.opcServerAsyn.AddItems(items, this.returnHandle))
                {
                    return(false);
                }
            }
            //if (this.R_CarDownTaskDB1 != null)
            //{
            //    OPCITEMDEF[] items = new OPCITEMDEF[this.R_CarDownTaskDB1.Length];
            //    for (int i = 0; i < this.R_CarDownTaskDB1.Length; i++)
            //    {
            //        items[i].szAccessPath = "";
            //        items[i].bActive = 1;
            //        items[i].hClient = num;
            //        items[i].dwBlobSize = 1;
            //        items[i].pBlob = IntPtr.Zero;
            //        items[i].vtRequestedDataType = 8;
            //        items[i].szItemID = $"S7:[S7_Connection_1]{this.R_CarDownTaskDB1[i]}";
            //        num++;
            //    }

            //    //for (int i = 0; i < 4; i++)
            //    //{
            //    //    items[i].szAccessPath = "";
            //    //    items[i].bActive = 1;
            //    //    items[i].hClient = num;
            //    //    items[i].dwBlobSize = 1;
            //    //    items[i].pBlob = IntPtr.Zero;
            //    //    items[i].vtRequestedDataType = 8;
            //    //    items[i].szItemID = $"Logical.test{num}";
            //    //    if (i == 3)
            //    //    {
            //    //        items[i].hClient = num + 10;
            //    //        items[i].szItemID = "Numeric.NewDataItem1";
            //    //    }
            //    //    num++;
            //    //}

            //    if (!this.opcServerAsyn.AddItems(items, this.returnHandle1))
            //    {
            //        return false;
            //    }
            // }


            this.opcServerAsyn.ReadPlc(this.returnHandle);
            this.opcServerAsyn.ReadPlc(this.returnHandle1);
            if (!this.opcServerAsyn.DataChange())
            {
                return(false);
            }
            return(true);
        }