예제 #1
0
        /// <summary>
        /// 자기 자신에게 쓰이는 데이터 교환
        /// </summary>
        public void SelfDataExchange()
        {
            OPCSubscription selfRead = null;

            for (int subi = 0; subi < this.ReadSubscriptions.Count; subi++)
            {
                if (this.ReadSubscriptions[subi].Type == 2)
                {
                    selfRead = this.ReadSubscriptions[subi];
                    break;
                }
            }

            OPCSubscription selfWrite = null;

            for (int subi = 0; subi < this.WriteSubscriptions.Count; subi++)
            {
                if (this.WriteSubscriptions[subi].Type == 2)
                {
                    selfWrite = this.WriteSubscriptions[subi];
                    break;
                }
            }

            //올바른 데이터 그룹이라면.
            if (selfRead != null && selfWrite != null)
            {
                //읽기
                selfRead.ItemValues = selfRead.Subscription.Read(selfRead.Subscription.Items);

                List <Opc.Da.ItemValue> values = new List <ItemValue>();

                for (int i = 0; i < selfRead.ItemValues.Length; i++)
                {
                    Opc.Da.Item            item = selfWrite.Subscription.Items[i];
                    Opc.Da.ItemValueResult ivr  = selfRead.ItemValues[i];

                    System.Type wt = selfWrite.ItemTypes[i];
                    System.Type rt = selfRead.ItemTypes[i];

                    object value = ivr.Value;
                    if (wt != rt)
                    {
                        value = Activator.CreateInstance(wt);
                    }

                    values.Add(new ItemValue(item)
                    {
                        Value = value
                    });
                }
                selfWrite.ItemValues = selfRead.ItemValues;
                selfWrite.Subscription.Write(values.ToArray());
            }
        }
예제 #2
0
        /// <summary>
        /// 그룹추가.
        /// </summary>
        /// <param name="name">group name</param>
        /// <param name="type">read 0, write 1</param>
        /// <returns>추가 성공한 group</returns>
        public OPCSubscription AddGroup(string name, int type)
        {
            bool            duplicated = false;
            OPCSubscription result     = null;

            if (type == 0)
            {
                for (int i = 0; i < this.ReadSubscriptions.Count; i++)
                {
                    if (this.ReadSubscriptions[i].Name.Equals(name))
                    {
                        duplicated = true;
                        result     = this.ReadSubscriptions[i];
                        break;
                    }
                }
            }
            else if (type == 1)
            {
                for (int i = 0; i < this.WriteSubscriptions.Count; i++)
                {
                    if (this.WriteSubscriptions[i].Name.Equals(name))
                    {
                        duplicated = true;
                        result     = this.WriteSubscriptions[i];
                        break;
                    }
                }
            }

            //중복이 아니라면
            if (duplicated == false)
            {
                SubscriptionState subscriptionstate = new SubscriptionState()
                {
                    Active       = true,
                    Deadband     = 0,
                    KeepAlive    = 0,
                    UpdateRate   = 1000,
                    ClientHandle = Guid.NewGuid().ToString(),
                    Name         = name
                };

                //현재 선택된 서버에 Group을 생성한다.
                Opc.Da.Subscription subscription = (Opc.Da.Subscription)Server.CreateSubscription(subscriptionstate);


                OPCSubscription newgroup = new OPCSubscription()
                {
                    Subscription = subscription
                };

                if (type == 0)
                {
                    this.ReadSubscriptions.Add(newgroup);
                }
                else if (type == 1)
                {
                    this.WriteSubscriptions.Add(newgroup);
                }

                result = newgroup;
            }
            return(result);
        }