Exemple #1
0
        /// <summary>
        /// 移除项,注意:此接口目前未能实现,需要继续调试
        /// </summary>
        /// <param name="groupName"></param>
        /// <param name="itemName"></param>
        /// <param name="reStr"></param>
        /// <returns></returns>
        public bool RemoveItem(string groupName, string itemName, out string reStr)
        {
            reStr = "";
            try
            {
                OPCGroup myGroup = GetGroup(groupName);
                if (myGroup == null)
                {
                    reStr = "组:" + groupName + "不存在 ";
                    return(false);
                }
                OPCItems items       = myGroup.OPCItems;
                OPCItem  item        = items.Item(itemName);
                Array    itemHandles = Array.CreateInstance(typeof(int), 1);
                itemHandles.SetValue(item.ServerHandle, 0);
                Array errors = null;// Array.CreateInstance(typeof(long), 10);

                items.Remove((int)1, ref itemHandles, out errors);

                reStr = "移除项:" + itemName + "成功!";
                return(true);
            }
            catch (System.Exception ex)
            {
                reStr = "移除项:" + itemName + "失败,返回异常:" + ex.Message;
                return(false);
            }
        }
Exemple #2
0
        public bool AddItem(Tag bi)
        {
            try
            {
                #region *********集合已包含此Tag,则退出*************************
                foreach (Tag item in this.Tags)
                {
                    if (item.TagName == bi.TagName)
                    {
                        MessageBox.Show("Tag重复:在在重复的Tag点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return(false);
                    }
                }
                #endregion

                OPCItems        items = this.OPCGroup.OPCItems;//根据指定的组类别,取出组下的标签集合
                Func <Tag, int> f     = p => { if (p != null)
                                               {
                                                   return(p.ExtraAs <DaExtra>().ItmHandleClient + 1);
                                               }
                                               return(0); };
                int itmHandleBegin = f(Tags.LastOrDefault());//itmHandleBegin的逻辑为最后一个标签itmHandle值+1
                //关键,此步骤将标签添加入OPCclient的监听列表中
                OPCItem ki = items.AddItem(bi.OpcTagName, itmHandleBegin);
                bi.ExtraAs <DaExtra>().ItmHandleClient = itmHandleBegin;
                bi.DataType = Enum.GetName(typeof(OpcTypes.CanonicalDataTypesForDa), ki.CanonicalDataType);
                bi.ExtraAs <DaExtra>().ItmHandleServer = ki.ServerHandle;
                Tags.Add((Tag)(Object)bi);
                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemple #3
0
        public void initOPC()
        {
            KepServer = new OPCServer();
            try
            {
                KepServer.Connect("KEPware.KEPServerEx.V6", "");
            }
            catch (Exception err1)
            {
                KepServer.Connect("KEPware.KEPServerEx.V4", "");
            }

            connected = true;
            try
            {
                KepGroups = KepServer.OPCGroups;
                KepGroup  = KepGroups.Add("OPCDOTNETGROUP");
                SetGroupProperty();
                KepItems = KepGroup.OPCItems;
            }
            catch (Exception err)
            {
                string errtxt = "创建组出现错误:" + err.Message;
                Logger.logwrite(errtxt);
                Console.ReadKey();
                Environment.Exit(0);
            }

            InitOPCItem();
        }
Exemple #4
0
        public bool GetItems(string groupName, out string[] itemNames, out string reStr)
        {
            reStr = "";

            itemNames = null;
            try
            {
                OPCGroups myGroups = myServer.OPCGroups;
                if (myGroups == null)
                {
                    reStr = "得到项列表失败,组名:" + groupName + "不存在";
                    return(false);
                }
                OPCGroup myGroup = myGroups.GetOPCGroup(groupName);
                if (myGroup == null)
                {
                    reStr = "得到项列表失败";
                    return(false);
                }
                OPCItems items = myGroup.OPCItems;
                itemNames = new string[items.Count];
                for (int i = 0; i < itemNames.Count(); i++)
                {
                    itemNames[i] = items.Item(i + 1).ItemID;
                }
                reStr = "得到项列成功!";
                return(true);
            }
            catch (System.Exception ex)
            {
                reStr = "得到项列表失败,返回异常:" + ex.Message;
                return(false);
            }
        }
Exemple #5
0
        //结束并断开opc server
        private void button4_Click(object sender, EventArgs e)
        {
            //释放所有组资源


            if (null != objGroup)
            {
                objGroup.DataChange         -= new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
                objGroup.AsyncWriteComplete -= new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(AsyncWriteComplete);
                objGroup = null;
                objServer.OPCGroups.RemoveAll();
            }
            if (null != objServer)
            {
                objServer.Disconnect();
                objServer       = null;
                objItems        = null;
                this.Data1.Text = "0";
                this.Data2.Text = "0";
                this.Data3.Text = State.ToString();
                GC.Collect();
            }
            //关闭kepserver进程,这个跟OPC操作无关

            /*
             * foreach ( Process oneProcess in Process.GetProcesses())
             * {
             * if (oneProcess.ProcessName == "ServerMain")
             * oneProcess.Kill();
             * }
             */
        }
Exemple #6
0
 protected override void OnStop()
 {
     try
     {
         //释放所有组资源
         if (null != objGroup)
         {
             objGroup.DataChange         -= new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
             objGroup.AsyncWriteComplete -= new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(AsyncWriteComplete);
             objGroup = null;
             objServer.OPCGroups.RemoveAll();
             SystemLogs.InsertPLCLog("Order2PLC", "释放所有组资源!");
         }
         if (null != objServer)
         {
             objServer.Disconnect();
             objServer = null;
             objItems  = null;
             GC.Collect();
             SystemLogs.InsertPLCLog("Order2PLC", "断开连接OPC释放所有组资源!");
         }
     }
     catch (Exception ex)
     {
         SystemLogs.InsertPLCLog("Order2PLC", ex.Message);
     }
 }
Exemple #7
0
 public void Init()
 {
     MyServer = new OPCServer();
     //初始化item数组
     MyItem = new OPCItem[4];
     MyServer.Connect("KEPware.KEPServerEx.V4", "127.0.0.1");//连接本地服务器:服务器名+主机名或IP
     if (MyServer.ServerState == (int)OPCServerState.OPCRunning)
     {
         Console.WriteLine("已连接到:{0}", MyServer.ServerName);
         Console.WriteLine("几个组:{0}", MyServer.OPCGroups.Count);
     }
     else
     {
         //这里你可以根据返回的状态来自定义显示信息,请查看自动化接口API文档
         string errMessage = MyServer.ServerState.ToString();
         throw new Exception(errMessage);
     }
     MyGroups             = MyServer.OPCGroups;
     MyGroup              = MyServer.OPCGroups.Add("GE.GE330.GEPLC");
     MyGroup.UpdateRate   = 250;
     MyGroup.IsActive     = true;
     MyGroup.IsSubscribed = true;
     //MyGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(GroupDataChange);
     MyItems = MyGroup.OPCItems;
     //添加item
     MyItem[0] = MyItems.AddItem("GE.GE330.GEPLC.M8001", 0);    //bool-buildable
     MyItem[1] = MyItems.AddItem("GE.GE330.GEPLC.M801", 1);     //bool-
     MyItem[2] = MyItems.AddItem("GE.GE330.GEPLC.M802", 2);     //bool-
     MyItem[3] = MyItems.AddItem("GE.GE330.GEPLC.PLC_PC_S", 3); //string
 }
Exemple #8
0
        private void AddGroupItems()//添加组
        {
            try
            {
                //IList<Model.PZYS_Storage.StorageTag> StorTagList = BusinessLogicLayer.PZYS_Storage.StorageTag.GetAllStorageTagList();
                //itmHandleServer;
                KepItems = KepGroup.OPCItems;
                //MyItems2 = MyGroup2.OPCItems;
                OPCItem[] KepItem = new OPCItem[3];
                //添加item
                KepItem[0] = KepItems.AddItem("Channel_0_User_Defined.Random.Random1", 0); //byte.Sine.Sine1
                KepItem[1] = KepItems.AddItem("Channel_0_User_Defined.Random.Random2", 0);
                KepItem[2] = KepItems.AddItem("Channel_0_User_Defined.Random.Random3", 0); //byte.Sine.Sine1
                // MyItem[1] = MyItems.AddItem("BPJ.Db1.dbw10", 1);//short
                //MyItem[2] = MyItems.AddItem("BPJ.Db16.dbx0", 2);//bool
                //MyItem[3] = MyItems.AddItem("BPJ.Db11.S0", 3);//string
                //移除组内item
                //Array Errors;
                //int[] temp = new int[] { 0, MyItem[3].ServerHandle };
                //Array serverHandle = (Array)temp;
                //MyItems.Remove(1, ref serverHandle, out Errors);
                // MyItem[3] = MyItems.AddItem("BPJ.Db11.S0", 3);//string


                //MyItem2[0] = MyItems2.AddItem("BPJ.Db1.dbb96", 0);//byte
                //MyItem2[1] = MyItems2.AddItem("BPJ.Db1.dbw10", 1);//short
                //MyItem2[2] = MyItems2.AddItem("BPJ.Db16.dbx0", 2);//bool
                //MyItem2[3] = MyItems2.AddItem("BPJ.Db11.S0", 3);//string
            }
            catch (Exception err)
            {
                MessageBox.Show("创建项出现错误:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Exemple #9
0
        public static OPCGroup CreateGroup(OPCServer opcServer, OPCItems opcItems, string opcGroupName, GroupProperty groupProperty)
        {
            try {
                OPCGroup opcGroup = opcServer.OPCGroups.Add(opcGroupName);



                opcServer.OPCGroups.DefaultGroupIsActive = groupProperty.DefaultGroupIsActive;

                opcServer.OPCGroups.DefaultGroupDeadband = groupProperty.DefaultGroupDeadband;

                opcGroup.UpdateRate = groupProperty.UpdateRate;

                opcGroup.IsActive = groupProperty.IsActive;

                opcGroup.IsSubscribed = groupProperty.IsSubscribed;



                //opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(OpcGroupDataChange);

                //opcGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);

                //opcItems = opcGroup.OPCItems;

                return(opcGroup);
            }

            catch (Exception err) {
                throw err;
            }
        }
        /// <summary>
        /// 创建组
        /// </summary>
        /// <returns></returns>
        public bool CreateGroups()
        {
            if (OpcGroup != null)
            {
                return(true);
            }

            bool isCreate = false;

            try
            {
                OpcGroups = OpcServer.OPCGroups;
                OpcGroup  = OpcGroups.Add("ZZHGROUP" + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
                //设置组属性
                OpcServer.OPCGroups.DefaultGroupIsActive = true;
                OpcServer.OPCGroups.DefaultGroupDeadband = 0;

                OpcItems = OpcGroup.OPCItems;

                isCreate = true;
            }
            catch (Exception ex)
            {
                Log.Info(ex.ToString());
                isCreate = false;
            }
            return(isCreate);
        }
Exemple #11
0
        public void RemoveItemsAll()
        {
            if (Tags.Count == 0)
            {
                return;                                      //监控的集合为空时,直接返回
            }
            OPCItems   items       = this.OPCGroup.OPCItems; //根据组名,取出分组下的标签集合
            List <int> tempHandles = new List <int>();       //临时标签句柄集合,保存需要删除的每个标签的服务端句柄
            Array      ItemServerHandles;                    //保存需要删除的标签句柄集合,由临时句柄集合转换而来
            Array      Errors;

            #region *********将需要删除的标签句柄压入数组ItemServerHandles******************
            tempHandles.Add(0);                        //因为opc的传入参数特殊性,数组第一位置留空
            tempHandles.AddRange(Tags.Where(p => p.Enabled == true).Select(p => p.ExtraAs <DaExtra>().ItmHandleServer));
            ItemServerHandles = tempHandles.ToArray(); //由临时标签集合转换
            Tags.Clear();                              //监控集合删除所有位置的标签
            #endregion
            try
            {
                //移除
                items.Remove(ItemServerHandles.Length - 1, ref ItemServerHandles, out Errors);//OPC集合删除指定位置的元素
            }
            catch (System.AccessViolationException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void AddGroupItems()//添加组
        {
            //itmHandleServer;
            MyItems  = MyGroup.OPCItems;
            MyItems2 = MyGroup2.OPCItems;

            //添加item
            MyItem[0] = MyItems.AddItem("BPJ.Db1.dbb96", 0); //byte
            MyItem[1] = MyItems.AddItem("BPJ.Db1.dbw10", 1); //short
            MyItem[2] = MyItems.AddItem("BPJ.Db16.dbx0", 2); //bool
            MyItem[3] = MyItems.AddItem("BPJ.Db11.S0", 3);   //string
            //移除组内item
            Array Errors;

            int [] temp         = new int[] { 0, MyItem[3].ServerHandle };
            Array  serverHandle = (Array)temp;

            MyItems.Remove(1, ref serverHandle, out Errors);
            MyItem[3] = MyItems.AddItem("BPJ.Db11.S0", 3);     //string

            MyItem2[0] = MyItems2.AddItem("BPJ.Db1.dbb96", 0); //byte
            MyItem2[1] = MyItems2.AddItem("BPJ.Db1.dbw10", 1); //short
            MyItem2[2] = MyItems2.AddItem("BPJ.Db16.dbx0", 2); //bool
            MyItem2[3] = MyItems2.AddItem("BPJ.Db11.S0", 3);   //string
        }
Exemple #13
0
        /// <summary>
        /// 设置服务器和客户端的句柄对应的点位名
        /// 将点位集合存入对应的服务器和客户端的句柄
        /// </summary>
        /// <param name="bindingData">点位集合</param>
        /// <param name="opcItems"></param>
        /// <param name="opcGroup"></param>
        /// <param name="dic">客户端句柄点位字典</param>
        /// <param name="serviceDic">服务器句柄点位字典</param>
        public void SetServerHandle(List <OpcData> bindingData, OPCItems opcItems,
                                    OPCGroup opcGroup, Dictionary <int, string> dic, Dictionary <int, string> serviceDic)
        {
            for (int i = 0; i < bindingData.Count; i++)
            {
                try
                {
                    OPCItem item = opcItems.AddItem(bindingData[i].OpcName, i);
                    if (item != null)
                    {
                        dic.Add(item.ClientHandle, bindingData[i].OpcName);
                        serviceDic.Add(item.ServerHandle, bindingData[i].OpcName);
                    }
                }
                catch (Exception e)
                {
                    throw;
                }

                foreach (KeyValuePair <int, string> keyValuePair in serviceDic)
                {
                    try
                    {
                        ReadOpcValue(keyValuePair.Key, opcItems, opcGroup, bindingData, serviceDic);
                    }
                    catch (Exception e)
                    {
                        // ignored
                    }
                }
            }
        }
Exemple #14
0
        private void myOPCItems_Fill(List <VarItem> OPCVariables, OPCItems items)
        {
            for (int i = 0; i < OPCVariables.Count; i++)
            {
                VarItem myVarItem = OPCVariables[i];
                OPCItem myItem    = items.AddItem(myVarItem.name, myVarItem.array_index);

                // Check if the OPC variable consist in OPC server's configuration !!!
                if (myItem == null)
                {
                    this.myLog.LogAlert(AlertType.Error, this.id.ToString(), this.GetType().ToString(), "myOPCItems_Fill()",
                                        "Exception: unknown variable '" + myVarItem.name + "' !!"
                                        , "system");
                    OPCVariables.Remove(OPCVariables[i]);
                }
                else
                {
                    OPCVariables[i].servHandle = myItem.ServerHandle;

                    this.myLog.LogAlert(AlertType.System, this.id.ToString(), this.GetType().ToString(), "myOPCItems_Fill()",
                                        this.OPCServerName + ", myVarItem (name; arr_idx) = "
                                        + myVarItem.name.ToString()
                                        + ", " + myVarItem.array_index.ToString()
                                        + ", myItem.ServerHandle=" + myItem.ServerHandle.ToString()
                                        + ", myItem.Quality=" + myItem.Quality.ToString()
                                        + ", myItem.ItemID=" + myItem.ItemID.ToString()
                                        // + ", myItem.Value=" + myItem.Value.ToString()
                                        , "system"
                                        );
                }
            }
        }
 public OPCGroup CreateGroup(OPCServer opcServer, OPCItems opcItems, string opcGroupName, GroupProperty groupProperty)
 {
     try
     {
         OPCGroup opcGroup = opcServer.OPCGroups.Add(opcGroupName);
         //opcServer.OPCGroups.DefaultGroupIsActive = groupProperty.DefaultGroupIsActive;
         //opcServer.OPCGroups.DefaultGroupDeadband = groupProperty.DefaultGroupDeadband;
         //opcGroup.UpdateRate = groupProperty.UpdateRate;
         //opcGroup.IsActive = groupProperty.IsActive;
         //opcGroup.IsSubscribed = groupProperty.IsSubscribed;
         opcServer.OPCGroups.DefaultGroupIsActive = true;
         opcServer.OPCGroups.DefaultGroupDeadband = 0;
         opcGroup.UpdateRate  = 1000;
         opcGroup.DataChange += OpcGroup_DataChange;
         //opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(OpcGroupDataChange);
         //opcGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
         //opcItems = opcGroup.OPCItems;
         return(opcGroup);
     }
     catch (Exception ex)
     {
         SimpleLogHelper.Instance.WriteLog(LogType.Error, ex);
         return(null);
     }
 }
Exemple #16
0
 private bool CreateGroup()
 {
     try
     {
         kepGroups = kepServer.OPCGroups;
         kepGroup  = kepGroups.Add("OPCDOTNETGROUP");
         SetGroupProperty();
         kepItems = kepGroup.OPCItems;
         kepItems.DefaultIsActive     = true;
         kepGroup.DataChange         += KepGroup_DataChange;         //数据change事件,每当项数据有变化时执行的事件
         kepGroup.AsyncWriteComplete += KepGroup_AsyncWriteComplete; //异步写 完成事件
         kepGroup.AsyncReadComplete  += GroupAsyncReadComplete;      //异步读 完成事件
         #region R 20170314 OpcItems.AddItem(ItemID,ClientHandle),其中0~15分别对应T,L,X,M四大车共计8辆车的发送(Write)和接受(Read);ItemID为从OPC服务器上约定好的Name
         int index = 0;
         //Read
         kepItem[index++] = kepItems.AddItem("T1.T1.R", 0);
         kepItem[index++] = kepItems.AddItem("T2.T2.R", 1);
         kepItem[index++] = kepItems.AddItem("L1.L1.R", 2);
         kepItem[index++] = kepItems.AddItem("L2.L2.R", 3);
         kepItem[index++] = kepItems.AddItem("X1.X1.R", 4);
         kepItem[index++] = kepItems.AddItem("X2.X2.R", 5);
         kepItem[index++] = kepItems.AddItem("M1.M1.R", 6);
         kepItem[index++] = kepItems.AddItem("M2.M2.R", 7);
         //Write
         kepItem[index++] = kepItems.AddItem("T1.T1.W", 8);
         kepItem[index++] = kepItems.AddItem("T2.T2.W", 9);
         kepItem[index++] = kepItems.AddItem("L1.L1.W", 10);
         kepItem[index++] = kepItems.AddItem("L2.L2.W", 11);
         kepItem[index++] = kepItems.AddItem("X1.X1.W", 12);
         kepItem[index++] = kepItems.AddItem("X2.X2.W", 13);
         kepItem[index++] = kepItems.AddItem("M1.M1.W", 14);
         kepItem[index++] = kepItems.AddItem("M2.M2.W", 15);
         #region 之前丰城在用
         //kepItem[1] = kepItems.AddItem("T.T2.READ", 1);
         //kepItem[2] = kepItems.AddItem("L.L1.READ", 2);
         //kepItem[3] = kepItems.AddItem("L.L2.READ", 3);
         //kepItem[4] = kepItems.AddItem("X.X1.READ", 4);
         //kepItem[5] = kepItems.AddItem("X.X2.READ", 5);
         //kepItem[6] = kepItems.AddItem("M.M1.READ", 6);
         //kepItem[7] = kepItems.AddItem("M.M2.READ", 7);
         //kepItem[8] = kepItems.AddItem("T.T1.WRITE", 8);
         //kepItem[9] = kepItems.AddItem("T.T2.WRITE", 9);
         //kepItem[10] = kepItems.AddItem("L.L1.WRITE", 10);
         //kepItem[11] = kepItems.AddItem("L.L2.WRITE", 11);
         //kepItem[12] = kepItems.AddItem("X.X1.WRITE", 12);
         //kepItem[13] = kepItems.AddItem("X.X2.WRITE", 13);
         //kepItem[14] = kepItems.AddItem("M.M1.WRITE", 14);
         //kepItem[15] = kepItems.AddItem("M.M2.WRITE", 15);
         #endregion
         #endregion
     }
     catch (Exception err)
     {
         //C.LogOpc.Info("创建组出现错误:" + err.Message);
         return(false);
     }
     return(true);
 }
Exemple #17
0
        /// <summary>
        /// 创建项
        /// </summary>
        public void AddGroupItems(string ItemID)
        {
            try
            {
                //IList<Model.PZYS_Storage.StorageTag> StorTagList = BusinessLogicLayer.PZYS_Storage.StorageTag.GetAllStorageTagList();
                //itmHandleServer;
                KepItems = KepGroup.OPCItems;
                //MyItems2 = MyGroup2.OPCItems;
                //OPCItem[] KepItem = new OPCItem[3];
                //添加item

                if (Cfg.Main.IsUseConfig)
                {
                    ItemIDArr[0]  = Cfg.Main.ItemIDComplete;
                    ItemIDArr[1]  = Cfg.Main.ItemIDSensorID;
                    ItemIDArr[2]  = Cfg.Main.ItemIDQty;
                    ItemIDArr[3]  = Cfg.Main.ItemIDClear;
                    ItemIDArr[4]  = Cfg.Main.SensorID;
                    ItemIDArr[5]  = Cfg.Main.Press;
                    KepItemArr[0] = KepItems.AddItem(Cfg.Main.ItemIDComplete, 0);
                    KepItemArr[1] = KepItems.AddItem(Cfg.Main.ItemIDSensorID, 1);
                    KepItemArr[2] = KepItems.AddItem(Cfg.Main.ItemIDQty, 2);
                    KepItemArr[3] = KepItems.AddItem(Cfg.Main.ItemIDClear, 3);
                    KepItemArr[4] = KepItems.AddItem(Cfg.Main.SensorID, 4);
                    KepItemArr[5] = KepItems.AddItem(Cfg.Main.Press, 5);
                    ClientHandle  = 6;
                }
                else
                {
                    KepItem = KepItems.AddItem(ItemID, ClientHandle);
                    ClientHandle++;
                }
                //KepItem = KepItems.AddItem("Channel_0_User_Defined.Random.Random1", 0);//byte.Sine.Sine1
                //Console.WriteLine("创建项成功");
                //KepItemArr[0] = KepItems.AddItem("Channel_0_User_Defined.Random.Random2", 1);
                //KepItemArr[1] = KepItems.AddItem("Channel_0_User_Defined.Random.Random3", 2);//byte.Sine.Sine1
                //MyItem[1] = MyItems.AddItem("BPJ.Db1.dbw10", 1);//short
                //MyItem[2] = MyItems.AddItem("BPJ.Db16.dbx0", 2);//bool
                //MyItem[3] = MyItems.AddItem("BPJ.Db11.S0", 3);//string
                //移除组内item
                //Array Errors;
                //int[] temp = new int[] { 0, MyItem[3].ServerHandle };
                //Array serverHandle = (Array)temp;
                //MyItems.Remove(1, ref serverHandle, out Errors);
                //MyItem[3] = MyItems.AddItem("BPJ.Db11.S0", 3);//string


                //MyItem2[0] = MyItems2.AddItem("BPJ.Db1.dbb96", 0);//byte
                //MyItem2[1] = MyItems2.AddItem("BPJ.Db1.dbw10", 1);//short
                //MyItem2[2] = MyItems2.AddItem("BPJ.Db16.dbx0", 2);//bool
                //MyItem2[3] = MyItems2.AddItem("BPJ.Db11.S0", 3);//string
            }
            catch (Exception err)
            {
                Console.WriteLine("创建项出错:" + err.Message);
                Log.TraceError("创建项出错:" + err.Message);
            }
        }
Exemple #18
0
        private void AddGroupItems()
        {
            opcItems = opcGroup.OPCItems;

            opcItem = new OPCItem[ItemList.Count];


            for (int i = 0; i < ItemList.Count; i++)
            {
                opcItem[i] = opcItems.AddItem(ItemList[i].name, ItemList[i].handler);
            }
        }
Exemple #19
0
 public OpcReadAndWrite(ParamClass PC)
     : base(PC)
 {
     CreateGroup();
     opcItem  = new OPCAutomation.OPCItem[PC.ParamList.Count];
     objItems = objGroup.OPCItems;
     myDc.Clear();
     for (int i = 0; i < PC.ParamList.Count; i++)
     {
         opcItem[i] = objItems.AddItem(PC.ParamList[i].KepAddress, i);
         myDc.Add(PC.ParamList[i].LabelName, opcItem[i]);
     }
 }
Exemple #20
0
 public OpcReadAndWrite(List <DSW_ParamTableModel> list)
     : base(list)
 {
     CreateGroup();
     opcItem  = new OPCAutomation.OPCItem[ModelList.Count];
     objItems = objGroup.OPCItems;
     myDcWrite.Clear();
     for (int i = 0; i < ModelList.Count; i++)
     {
         opcItem[i] = objItems.AddItem(ModelList[i].KepAddress, i);
         myDcWrite.Add(ModelList[i].KepAddress, opcItem[i]);
     }
 }
Exemple #21
0
        //添加数据项
        private void AddGroupItems()
        {
            //itmHandleServer;
            MyItems = MyGroup.OPCItems;
            MyItems2 = MyGroup2.OPCItems;

            //添加item
            MyItem[0] = MyItems.AddItem("[" + strTopic + "]int1", 0);//[opc1]int1", 0);//int
            MyItem[1] = MyItems.AddItem("[" + strTopic + "]dint1", 1);//dint
            MyItem[2] = MyItems.AddItem("[" + strTopic + "]dint2", 1);//dint

            MyItem2[0] = MyItems2.AddItem("[" + strTopic + "]int2", 0);//int
            MyItem2[1] = MyItems2.AddItem("[" + strTopic + "]sint1", 1);//byte
        }
Exemple #22
0
        /// <summary>
        /// 根据列表中选中的当前标签,定位到需要发送数据的标签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetItem_Click(object sender, EventArgs e)
        {
            try
            {
                /**
                 * 需要注意,不同的OPC服务器的标签格式也是不同的
                 * 测试时,使用的是ICONICS Simulator OPC Server,标签格式如:Textual.Memory
                 * 生产环境时,使用的是SimaticNet_V13Sp1,标签格式如:S7:[S7_Connection_1]MReal120
                 * **/

                //根据ListBox选中的标签,处理得到分组名称
                string groupName = lstItems.Text;

                //实例化组
                myGroups = myServer.OPCGroups;
                myGroup  = myGroups.Add(groupName);

                //设置缺省的组属性
                myServer.OPCGroups.DefaultGroupIsActive = true;
                myServer.OPCGroups.DefaultGroupDeadband = 0;
                myGroup.UpdateRate   = 250;
                myGroup.IsActive     = true;
                myGroup.IsSubscribed = true;

                //定位需要发送数据的目标项
                myItems = myGroup.OPCItems;

                //实例化组内标签
                myItemArray = new OPCItem[1];

                //填充项目组
                myItemArray[0] = myItems.AddItem(lstItems.Text, 1);

                //获取服务端句柄
                itmHandleServer = myItemArray[0].ServerHandle;

                //监听组内数据变化
                myGroup.DataChange         += new DIOPCGroupEvent_DataChangeEventHandler(myGroup_DataChange);
                myGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(myGroup_AsyncWriteComplete);

                //开启发送参数按钮
                btnWrite.Enabled = true;
            }
            catch (Exception err)
            {
                MessageBox.Show("创建组出现错误:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Exemple #23
0
        public ItemReadSync(ParamClass PC)
        {
            CreateGroup();
            opcItem  = new OPCAutomation.OPCItem[PC.ParamList.Count];
            objItems = objGroup.OPCItems;
            for (int i = 0; i < PC.ParamList.Count; i++)
            {
                opcItem[i] = objItems.AddItem(PC.ParamList[i].KepAddress, i);
            }

            paramClass = PC;

            tTimer          = new System.Timers.Timer(100);
            tTimer.Elapsed += SyncRead;
            tTimer.Start();
        }
Exemple #24
0
 /// <summary>
 /// 创建组
 /// </summary>
 private bool CreateGroup()
 {
     try {
         PxGroups = PxServer.OPCGroups;
         PxGroup  = PxGroups.Add("OPCDOTNETGROUP");
         SetGroupProperty();
         PxGroup.DataChange         += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
         PxGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
         PxItems = PxGroup.OPCItems;
     }
     catch (Exception err) {
         Debug.WriteLine("创建组出现错误:" + err.Message, "提示信息");
         return(false);
     }
     return(true);
 }
Exemple #25
0
        /// <summary>
        /// 第一次加载分支根据句柄读取数据
        /// </summary>
        /// <param name="handle">分支句柄</param>
        /// <param name="opcItems"></param>
        /// <param name="opcGroup"></param>
        /// <param name="bindingData"></param>
        /// <param name="serviceDic"></param>
        private void ReadOpcValue(int handle, OPCItems opcItems, OPCGroup opcGroup, List <OpcData> bindingData, Dictionary <int, string> serviceDic)
        {
            OPCItem bItem = opcItems.GetOPCItem(handle);

            int[] temp = new int[2] {
                0, bItem.ServerHandle
            };
            Array serverHandles = (Array)temp;
            Array Errors;
            int   cancelID;

            opcGroup.AsyncRead(1, ref serverHandles, out Errors, 2009, out cancelID);
            //OpcData data = bindingData.FirstOrDefault(x => x.OpcName == serviceDic[handle]);
            //data.OpcValue = bItem.Value.ToString();
            //data.OpcTime = DateTime.Now.ToString();
        }
        void CreateReadGroup()
        {
            opcGroups = opcServer.OPCGroups;
            opcGroup  = opcGroups.Add("PLCReadAddress");
            SetGroupProperty();

            opcItems = opcGroup.OPCItems;
            int i = 0;

            foreach (string address in opc_address)
            {
                opcItem[i] = opcItems.AddItem(address, i);
                i++;
            }
            //opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup_DataChange);
        }
Exemple #27
0
 //创建组
 private bool CreatGroup()
 {
     try
     {
         _kepGroups = _kepServer.OPCGroups;
         _kepGroup  = _kepGroups.Add("OPCDOTNETGROUP");
         String();
         _kepGroup.DataChange += KepGroup_DataChange;
         _kepItems             = _kepGroup.OPCItems;
     }
     catch (Exception err)
     {
         MessageBox.Show("创建组出现错误:" + err.Message, "提示信息", MessageBoxButton.OK, MessageBoxImage.Warning);
         return(false);
     }
     return(true);
 }
Exemple #28
0
        internal void queuePLCWrites(int value, string tag)
        {
            OPCItems Item = getTagItem(tag);

            if (Convert.ToInt32(Item.Value) != value)
            {
                Item.Value = value;
                if (writeTaskDic.ContainsKey(Item.Tag))
                {
                    writeTaskDic[Item.Tag] = Item;
                }
                else
                {
                    writeTaskDic.Add(Item.Tag, Item);
                }
            }
        }
 public void AddItem(string itemName)
 {
     try
     {
         KepItems = KepGroup.OPCItems;
         KepItem  = KepItems.AddItem(itemName, KepItemServerHandles.Count);
         OpcDataItems.Add(new OPCDataItem {
             ItemName = itemName, ItemHandle = KepItemServerHandles.Count
         });
         KepItemServerHandles.Add(KepItem.ServerHandle);
         KeyItemServerHandles = KepItemServerHandles.ToArray();
     }
     catch (Exception ex)
     {
         SimpleLogHelper.Instance.WriteLog(LogType.Error, ex);
     }
 }
Exemple #30
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ///

        /// <summary>
        /// 创建组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateGroup()
        {
            try
            {
                //KepServer  = new OPCServer();
                kepGroups = KepServer.OPCGroups;
                KepGroup  = kepGroups.Add("opcdontnetgroup1");
                SetGroupProperty();
                KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
                // ServerConnect.KepGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
                KepItems = KepGroup.OPCItems;
            }
            catch (Exception err)
            {
                MessageBox.Show("创建组出错:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Exemple #31
0
        //创建组
        private bool CreatGroup()
        {
            try
            {
                _kepGroups = _kepServer.OPCGroups;
                _kepGroup = _kepGroups.Add("OPCDOTNETGROUP");
                String();
                _kepGroup.DataChange += KepGroup_DataChange;
                _kepItems = _kepGroup.OPCItems;

            }
            catch (Exception err)
            {
                MessageBox.Show("创建组出现错误:" + err.Message, "提示信息", MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }
            return true;
        }
Exemple #32
0
        public void InitPt(int nNum, numInf[] nrst, int sNum, strInf[] srst)
        {
            try
            {
                MyItems = MyGroup.OPCItems;
                MyItem = new OPCItem[nNum + sNum];
                numinf = new numInf[nNum];
                strinf = new strInf[sNum];
                numNum = nNum;
                strNum = sNum;

                int i = 0;
                foreach (numInf nn in nrst)
                {
                    try
                    {
                        MyItem[i] = MyItems.AddItem(nn.srcId, i);//byte
                        i++;
                    }
                    catch
                    {
                        try
                        {
                            sw = File.AppendText(logfile);
                            sw.WriteLine("初始化点" + nn.srcId + "失败!");
                            sw.Close();
                        }
                        catch { }
                        //MyItem[i] = MyItems.AddItem(nrst[i - 1].srcId, i);
                        MyItem[i] = MyItem[i - 1];
                        i++;
                        continue;
                    }
                }
                foreach (strInf ss in srst)
                {
                    try
                    {
                        MyItem[i] = MyItems.AddItem(ss.srcId, i);//byte
                        i++;
                    }
                    catch
                    {
                        try
                        {
                            sw = File.AppendText(logfile);
                            sw.WriteLine("初始化点" + ss.srcId + "失败!");
                            sw.Close();
                        }
                        catch { }
                        MyItem[i] = MyItem[i - 1];
                        i++;
                        continue;
                    }
                }

                object ItemValuestemp; object Qualities; object TimeStamps;//同步读的临时变量:值、质量、时间戳
                for (int ii = 0; ii < nNum + sNum; ii++)
                {
                    MyItem[ii].Read(1, out ItemValuestemp, out Qualities, out TimeStamps);//同步读,第一个参数只能为1或2
                    if (ii < nNum)
                    {
                        numinf[ii].val = Convert.ToSingle(ItemValuestemp);//转换后获取item值
                        numinf[ii].dtm = Convert.ToDateTime(TimeStamps);
                    }
                    else
                    {
                        strinf[ii - numNum].val = Convert.ToString(ItemValuestemp);//转换后获取item值
                        strinf[ii - numNum].dtm = Convert.ToDateTime(TimeStamps);
                    }
                }

                MyGroup.IsSubscribed = true;//使用订阅功能,即可以异步,默认false
                try
                {
                    sw = File.AppendText(logfile);
                    sw.WriteLine("初始化点成功!");
                    sw.Close();
                }
                catch { }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Exemple #33
-1
        ///   <summary>
        ///   初始化OPC服务器
        ///   </summary>
        void InitOpc()
        {
            connected  = false;
            connecteds = false;
            isfirstuse = true;
            try
            {
                string severname = "KEPware.KEPServerEx.V4";
                string groupname = "S7-200.S7-200-1";
                string outname   = "S7-200.S7-200-1.OUT";
                string inname    = "S7-200.S7-200-1.IN";
                string chuanname = "S7-200.S7-200-1.CHUAN";
                string confname  = "S7-200.S7-200-1.CONF";

                myserver = new OPCServer();
                myserver.Connect(severname);
                mygroups             = myserver.OPCGroups;
                mygroup              = mygroups.Add(groupname);
                mygroup.DataChange  += new DIOPCGroupEvent_DataChangeEventHandler(ObjOPCGroup_DataChange);
                myitems              = mygroup.OPCItems;
                item_out             = myitems.AddItem(outname, 1);//opcitem标识,1out,2config,3in,4chuan
                item_conf            = myitems.AddItem(confname, 2);
                item_in              = myitems.AddItem(inname.ToString(), 3);
                item_chuan           = myitems.AddItem(chuanname.ToString(), 4);
                mygroup.UpdateRate   = 10;
                mygroup.IsActive     = true;
                mygroup.IsSubscribed = true;
                connected            = true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());//如果连接不上,说明运行库有问题,关了软件重新安装必要组件再运行
            }
        }