コード例 #1
0
        public void Attach(NativeOpcServer server, string serverAddress)
        {
            try
            {
                _group = server.OPCGroups.Add(Address);
            }
            catch (Exception ex)
            {
                _group = null;
                this.Error($"{Id}连接失败,错误原因{ex}");
                throw;
            }

            this.InfoSuccess($"连接");

            if (_group == null)
            {
                return;
            }

            _group.IsActive = true;
            _group.OPCItems.DefaultIsActive = true;
            _group.IsSubscribed             = true;
            _group.UpdateRate = 100;

            foreach (var item in OpcItems)
            {
                item.Attach(serverAddress, _group);
                _opcItemsLookupTable.Add(item.ClientHandle, item);
            }

            _group.DataChange += DataChange;
        }
コード例 #2
0
        public void AddOpcGroupServer(string opcgroupname, int groupupdaterate, bool groupactive, float groupdeadband)
        {
            try
            {
                OPCAutomation.OPCGroup grup = ConnectedOPCServer.OPCGroups.Add(opcgroupname);


                grup.UpdateRate   = groupupdaterate;
                grup.IsActive     = groupactive;
                grup.DeadBand     = groupdeadband;
                grup.IsSubscribed = true;
                //grup.DataChange += new OPCAutomation.DIOPCGroupEvent_DataChangeEventHandler(ConnectedGroup_DataChange);
                this.ConnectedGroup = grup;



                //grup.Name = opcgroupname;
                //ConnectedOPCServer.OPCGroups.Add(grup);
                //this.GroupName = opcgroupname;
                //ConnectedOPCServer.OPCGroups.DefaultGroupIsActive = groupactive;
                //ConnectedOPCServer.OPCGroups.DefaultGroupDeadband = groupdeadband;
                //ConnectedGroup = ConnectedOPCServer.OPCGroups.Add(opcgroupname);
                //ConnectedGroup.UpdateRate = groupupdaterate;
                //ConnectedGroup.IsSubscribed = true; ;
                //ConnectedGroup = grup;
                //ConnectedGroup.DataChange += new OPCAutomation.DIOPCGroupEvent_DataChangeEventHandler(ConnectedGroup_DataChange);
            }
            catch (Exception ex)
            {
                MessageBox.Show("OPC server add group failed with exception: " + ex.Message, "SimpleOPCInterface Exception", MessageBoxButtons.OK);
            }
        }
コード例 #3
0
 private void SetConnectedGroup(string groupname)
 {
     OPCAutomation.OPCGroup grup = ConnectedOPCServer.OPCGroups.GetOPCGroup(groupname);
     if (grup != null)
     {
         this.ConnectedGroup = grup;
     }
 }
コード例 #4
0
 public void RemoveGroupServer(string opcgroupname)
 {
     SetConnectedGroup(opcgroupname);
     //  Test to see if the OPC Group object is currently available
     if (!(ConnectedGroup == null))
     {
         try
         {
             //  Remove the group from the server
             ConnectedOPCServer.OPCGroups.Remove(opcgroupname);
         }
         catch (Exception ex)
         {
             //  Error handling
             MessageBox.Show(("OPC server remove group failed with exception: " + ex.Message), "SimpleOPCInterface Exception", MessageBoxButtons.OK);
         }
         finally
         {
             ConnectedGroup = null;
         }
     }
 }
コード例 #5
0
ファイル: OpcManager.cs プロジェクト: noseii/powerscada2011
 private void SetConnectedGroup(string groupname)
 {
     OPCAutomation.OPCGroup grup = ConnectedOPCServer.OPCGroups.GetOPCGroup(groupname);
     if (grup != null)
         this.ConnectedGroup = grup;
 }
コード例 #6
0
ファイル: OpcManager.cs プロジェクト: noseii/powerscada2011
 public void RemoveGroupServer(string opcgroupname)
 {
     SetConnectedGroup(opcgroupname);
     //  Test to see if the OPC Group object is currently available
     if (!(ConnectedGroup == null))
     {
         try
         {
             //  Remove the group from the server
             ConnectedOPCServer.OPCGroups.Remove(opcgroupname);
         }
         catch (Exception ex)
         {
             //  Error handling
             MessageBox.Show(("OPC server remove group failed with exception: " + ex.Message), "SimpleOPCInterface Exception", MessageBoxButtons.OK);
         }
         finally
         {
             ConnectedGroup = null;
         }
     }
 }
コード例 #7
0
ファイル: OpcManager.cs プロジェクト: noseii/powerscada2011
        public void AddOpcGroupServer(string opcgroupname,int groupupdaterate,bool groupactive, float groupdeadband)
        {
            try
            {

                OPCAutomation.OPCGroup grup = ConnectedOPCServer.OPCGroups.Add(opcgroupname);

                grup.UpdateRate = groupupdaterate;
                grup.IsActive = groupactive;
                grup.DeadBand = groupdeadband;
                grup.IsSubscribed=true;
                //grup.DataChange += new OPCAutomation.DIOPCGroupEvent_DataChangeEventHandler(ConnectedGroup_DataChange);
                this.ConnectedGroup = grup;

                //grup.Name = opcgroupname;
                //ConnectedOPCServer.OPCGroups.Add(grup);
                //this.GroupName = opcgroupname;
                //ConnectedOPCServer.OPCGroups.DefaultGroupIsActive = groupactive;
                //ConnectedOPCServer.OPCGroups.DefaultGroupDeadband = groupdeadband;
                //ConnectedGroup = ConnectedOPCServer.OPCGroups.Add(opcgroupname);
                //ConnectedGroup.UpdateRate = groupupdaterate;
                //ConnectedGroup.IsSubscribed = true; ;
                //ConnectedGroup = grup;
                //ConnectedGroup.DataChange += new OPCAutomation.DIOPCGroupEvent_DataChangeEventHandler(ConnectedGroup_DataChange);

            }
            catch (Exception ex)
            {
                MessageBox.Show("OPC server add group failed with exception: " + ex.Message, "SimpleOPCInterface Exception", MessageBoxButtons.OK);

            }
        }
コード例 #8
0
        protected virtual void Station_opcConnctionChangedEvent(object sender, bool e)
        {
            log.Debug("Station_opcConnctionChangedEvent");
            {
                Pack.ConnctionChanged p = new Pack.ConnctionChanged()
                {
                    connected = e
                };
                Parallel.ForEach(EventPushList, c => c.connectChanged(p));
            }

            if (e)
            {
                //OPC服务器连接成功时
                var groupName = "theopc_" + this.GetHashCode();
                // _opcServer.OPCGroups.Remove(groupName);

                _opcGroup = _opcServer.OPCGroups.Add(groupName);
                _opcGroup.AsyncReadComplete += _opcGroup_AsyncReadComplete;
                var test = _opcServer.OPCGroups;
                _opcGroup.DataChange         += _opcGroup_DataChange;
                _opcGroup.AsyncWriteComplete += _opcGroup_AsyncWriteComplete;
                Array ClientHandles = new int[ss.tags.Count + 1]; //Array.CreateInstance
                Array arr_error     = new int[ss.tags.Count + 1];
                {
                    int i = 1;  //下标要从1开始

                    foreach (TagConfigElement item in ss.tags)
                    {
                        OPCAutomation.OPCItem opcItem = _opcGroup.OPCItems.AddItem(item.name, item.ClientHandle);  //IOPCItem  OPCItem
                        item.valueType = TagConfigElement.getOPCTypeByVarType(opcItem.CanonicalDataType);
                        //28E68F99-8D75-11D1-8DC3-3C302A000000
                        item.ServerHandle = opcItem.ServerHandle;
                        opcItem.IsActive  = true;
                        ClientHandles.SetValue(item.ServerHandle, i);
                        i++;
                    }
                    {
                        Pack.OpcReference p = new Pack.OpcReference()
                        {
                        };
                        p.OpcTagList = (from TagConfigElement item in ss.tags select new Pack.OpcTag()
                        {
                            tagName = item.name, type = item.valueType
                        })
                                       .ToList().
                                       ConvertAll(c => c as IOpcTag);
                        Parallel.ForEach(EventPushList, c =>
                        {
                            try
                            {
                                c.opcReferenceReady(p);
                            }
                            catch (Exception ex1)
                            {
                                log.Warn("调用插件opcReferenceReady失败", ex1);
                            }
                        });
                        //EventPushList.ForEach(c => c.opcReferenceReady(p));
                    }
                }
                //_opcGroup.DeadBand=
                _opcGroup.UpdateRate   = 600;
                _opcGroup.ClientHandle = this.GetHashCode();
                _opcGroup.IsSubscribed = true;
                _opcGroup.IsActive     = true;
                log.Debug("_opcGroup reged");
                //AsyncRead(int NumItems, ref Array ServerHandles, out Array Errors, int TransactionID, out int CancelID);
                //int tid = DateTime.Now.Second;
                //int cid = 0;
                //_opcGroup.AsyncRead(ss.tags.Count, ref ClientHandles, out arr_error, tid, out cid);
            }
            else
            {
                //OPC服务器连接失败
                _opcGroup = null;
                System.Threading.Thread.Sleep(500);
                connectOPCAsync();  //继续连接
            }
        }