//################ Sub procedure to initialize OPC Server ######
        private void StopOPCClient()
        {
            //       ==
            //	Description     :	断开连接OPCServer,返回值False为断开失败,True为断开成功
            //	Created			:	2009/01/19
            //	Author			:	zgh
            //   Update          :
            //   Updater         :
            //       ==
            //bool value = false;
            try
            {
                ObjOPCGroup.IsActive = false;
                //ObjOPCGroup.Remove(ObjOPCGroup.ServerHandle)
                ObjOPCGroups.RemoveAll();
                ObjOPCItems  = null;
                ObjOPCGroups = null;
                ObjOPCGroup  = null;
                //ObjOPCGroup.AsyncReadComplete += ObjOPCGroup_AsyncReadComplete;
                ObjOPCServer.Disconnect();
                ObjOPCServer = null;
                //colTagName = Nothing
                //value = true;

                //MessageLog("clsDeviceOPC.StopOPCClient(): Disconnected from OPC device \'" + DeviceName + "\'");
                LOG.Info("断开OPC连接 \'" + SourceName + "\'");
            }
            catch (Exception ex)
            {
                //value = false;
                //WarnLog(ex.StackTrace + "\r\n" + ex.Message);
                //  MsgBox(ex.Message)
                LOG.Error(string.Format("OPCDataSource断开连接出错行:{0},", ex.Message));
            }
            finally
            {
                System.GC.Collect(); //强制垃圾回收
            }
        }
示例#2
0
        public bool Start()
        {
            if (!bInit)
            {
                this.lastErrInfo = "初始化未完成,先执行Init操作";
                return(false);
            }

            //try {
            string opcGroupString = groupHash["OPCGroupString"].ToString();
            string s1             = opcHash["OPCConnectString"].ToString();
            string s2             = opcHash["OPCNode"].ToString();
            object o = OPCServer.GetOPCServers();

            OPCServer.Connect(opcHash["OPCConnectString"].ToString(), opcHash["OPCNode"]);
            ////Group的默认死区,变化量超过死区后将会触发DataChange事件,合理的设置该值可以提高程序性能
            //OPCServer.OPCGroups.DefaultGroupDeadband = int.Parse(groupHash["GroupDeadband"].ToString());
            ////组(Group)的默认通信区域编号,如1024
            //OPCServer.OPCGroups.DefaultGroupLocaleID = int.Parse(groupHash["GroupLocaleID"].ToString());
            ////组(Group)的默认刷新率
            //OPCServer.OPCGroups.DefaultGroupUpdateRate = int.Parse(groupHash["GroupUpdateRate"].ToString());
            //组(Group)的默认时间偏差
            OPCBrowser opcBrowser = OPCServer.CreateBrowser();

            //展开分支

            opcBrowser.ShowBranches();

            //展开叶子

            opcBrowser.ShowLeafs(true);

            // OPCServer.OPCGroups.DefaultGroupTimeBias = int.Parse(groupHash["GroupTimeBias"].ToString());


            opcGroup = OPCServer.OPCGroups.Add("AXC3050");

            opcGroup.IsActive     = false;
            opcGroup.IsSubscribed = false;

            int clientHandle = 1;

            OPCAutomation.OPCItems opcItems = opcGroup.OPCItems;
            IDictionaryEnumerator  ide      = readHash.GetEnumerator();

            while (ide.MoveNext())
            {
                Item item = ide.Value as Item;
                if (item.IsActive)
                {
                    opcItems.AddItem(opcGroupString + item.OPCItemString, clientHandle);
                    clientHandle++;
                }
            }

            ide = writeHash.GetEnumerator();
            while (ide.MoveNext())
            {
                Item item = ide.Value as Item;
                if (item.IsActive)
                {
                    opcItems.AddItem(opcGroupString + item.OPCItemString, clientHandle);
                    clientHandle++;
                }
            }

            opcGroup.DataChange += new OPCAutomation.DIOPCGroupEvent_DataChangeEventHandler(Automation_DataChange);

            opcGroup.IsActive     = true;
            opcGroup.IsSubscribed = true;

            //}
            //catch (SystemException ex) {
            //    this.lastErrInfo = ex.Message;
            //    return false;
            //}
            bConnect = true;
            return(true);
        }
        //################ Sub procedure to initialize OPC Server ######
        private bool StartOPCClient()
        {
            int    liCounter        = 0;
            Array  gpServerHandlers = null;
            Array  gsErrors         = null;
            string GroupName        = SourceName;

            try
            {
                ObjOPCServer = new OPCServer();
                //##### Initialize OPC Server ################################
                ObjOPCServer.Connect(OPCServerName, "");
                ObjOPCGroups = ObjOPCServer.OPCGroups;
                ObjOPCGroup  = ObjOPCGroups.Add(GroupName);
                ObjOPCGroup.AsyncReadComplete += ObjOPCGroup_AsyncReadComplete;
                //Add OPCGroup
                ObjOPCGroup.UpdateRate   = UpdateInterval;
                ObjOPCGroup.IsActive     = false;
                ObjOPCGroup.IsSubscribed = ObjOPCGroup.IsActive;
                ObjOPCItems = ObjOPCGroup.OPCItems;

                //Create opc items
                Array_Address        = Array.CreateInstance(typeof(string), NoOfItems + 1);
                Array_ClientHanlers  = Array.CreateInstance(typeof(int), NoOfItems + 1);
                Array_ServerHandlers = Array.CreateInstance(typeof(int), NoOfItems + 1);



                //Build OPCItems Array
                for (liCounter = 1; liCounter <= NoOfItems; liCounter++)
                {
                    Array_Address.SetValue(List_Address[liCounter - 1], liCounter);
                    Array_ClientHanlers.SetValue(liCounter, liCounter);
                }

                ObjOPCItems.AddItems(NoOfItems, ref Array_Address, ref Array_ClientHanlers, out
                                     gpServerHandlers, out gsErrors);

                //Get the server handlers
                bool HasError = false;
                for (liCounter = 1; liCounter <= NoOfItems; liCounter++)
                {
                    if ((int)gsErrors.GetValue(liCounter) == 0)
                    {
                        Array_ServerHandlers.SetValue((int)gpServerHandlers.GetValue(liCounter), liCounter);
                    }
                    else
                    {
                        LOG.Error("Tag \'" + List_Address[liCounter - 1] + "\' has problem连接失败错误");
                        HasError = true;
                    }
                }

                if (!HasError)
                {
                    LOG.Info(" 启动OPC组 \'" + SourceName + "\'");
                }

                return(!HasError);
            }
            catch (Exception ex)
            {
                LOG.Error(string.Format("连接OPC出错:读取资源{0}{1}", GroupName, ex.Message));
                return(false);
            }
        }